使用 Twilio 和 Arduino 在 Intel® Edison 上发送 SMS 消息
本文将介绍如何通过 Twilio 电话号码向您的手机发送短信。
获取新的 Intel® IoT Developer Kit,这是一个完整的硬件和软件解决方案,可让开发人员使用 Intel® Galileo 和 Intel® Edison 板开发令人兴奋的新解决方案。请访问 Intel® Developer Zone for IoT。
引言
Intel® Edison 是世界上最小、功耗最低的计算平台,专为物联网等可穿戴技术而设计。它是一款 Arduino* 认证的开发和原型设计板。Intel® Edison 上的 Arduino 草图是一个 C++ 程序。传感器技术可提供高度精确和一致的生物识别数据实时连续测量。您可以使用 Intel® Edison 读取传感器并控制家内外的许多设备。本文展示了一个读取温度传感器并通过 Twilio* 帐户向您的手机发送短信的示例。
Twilio 允许软件开发人员通过编程方式接收发送到您的 Twilio 电话号码的短信和彩信,并用短信和彩信回复。您可以使用 Twilio 的 REST API 发送短信彩信,跟踪短信对话,并在通话过程中发送短信。本文将介绍如何通过 Twilio 电话号码向您的手机发送短信。
目录
- 硬件组件
- 开发板下载
- 下载和安装 Twilio 库
- 从 Edison 访问 USB 大容量存储分区
- 从计算机访问 USB 大容量存储设备
- 安装 Curl 和 Crypto 库
- 安装 Curl 头文件
- 修改编译模式
- 创建 Twilio 帐户
- POST 请求到 Twilio
- 温度传感器
- 示例草图
硬件组件
此短信项目使用的硬件组件列在下面
- Intel® Edison 计算模块
- Arduino 扩展板
- Grove - 入门套件 Plus
- 两个 Micro B 转 Type A USB 线
- 直流电源。
有关组装和为 Intel® Edison 板供电的详细信息,请访问 https://software.intel.com/en-us/assembling-intel-edison-board-with-arduino-expansion-board。
开发板下载
前往 Intel® Edison Board Software Downloads 下载最新的驱动程序、安装程序、固件映像和 Arduino IDE:https://software.intel.com/en-us/iot/hardware/edison/downloads
下载和安装 Twilio 库
从 https://github.com/laurentluce/twilio-cplusplus 下载 Twilio C++ 并解压缩到 Arduino IDE 库 C:\Arduino\arduino-1.5.3-Intel.1.0.4\libraries。Arduino 不允许库名中使用“-”,请务必将 twilio-cplusplus 重命名为 Twilio。
进入 Twilio 目录并将 Example.cpp 重命名为 Example.cpp.org,这样每次使用 Twilio 库时就不会调用 Example.cpp。按如下方式编辑 Utils.h
#include <string.h>
#include <vector>
using namespace std;
从 Edison 访问 USB 大容量存储分区
Multifunction Composite Gadget (g_multi) 是一个复合设备,它广泛利用复合框架来提供多功能设备。它提供了具有 USB 大容量存储功能、以太网(RNDIS 和/或 CDC 以太网)以及 ACM 串行链路接口的配置。要手动加载或删除内核模块,我们使用 module 命令 modprobe。我们删除 g_multi 模块以禁用 Edison 上的 USB 大容量存储设备
创建挂载目录
使用 losetup 将分区标签“update”磁盘映像文件映射到具有偏移量 8192 的回环设备 (/dev/loop0)。示例如下将回环设备挂载为 /mnt/transfer。
从计算机访问 USB 大容量存储设备
按下复位按钮,USB 大容量存储将在 Windows Explorer 中显示,如下所示
卸载设备并将 g_multi 模块重新添加回来
安装 Curl 和 Crypto 库
请参阅 **从 Edison 访问 USB 大容量存储分区**,将 libcurl 及其依赖项从 Edison /usr/lib 复制到 Arduino IDE 安装根目录 hardware\tools\edison\sysroots\core2-32-poky-linux\usr\lib
- libcurl.so
- libtasn1.so.6
- libgcrypt.so.11
- libgnutls.so.26
- libgpg-error.so.0
类似地,使用 **从 Edison 访问 USB 大容量存储分区** 将下面的库从 Edison /lib 复制到 Arduino IDE 安装根目录 hardware\tools\edison\sysroots\core2-32-poky-linux\lib
- libz.so.1
- libcap.so.2
- libcrypto.so
安装 Curl 头文件
Curl 头文件应安装到 Arduino IDE 安装根目录 hardware\tools\edison\sysroots\core2-32-poky-linux\usr\include,如下所示
OpenSSL 头文件应按如下方式安装到 Arduino IDE 安装根目录 hardware\tools\edison\sysroots\core2-32-poky-linux\usr\include
修改编译模式
在 Arduino IDE 安装根目录的 hardware\arduino\edison 文件路径中存在 platform.*.txt 文件。编辑与您的构建平台匹配的平台名称文件。如果您在 Microsoft* Windows* 系统上运行 Edison 的 Arduino IDE,请编辑 platform.win.txt。
为了告诉链接器在链接可执行文件时将 libcurl 和 libcrypto 添加到库列表中,请在“recipe.c.combine.pattern
”行的末尾添加 –lcurl
和 –lcrypto
。确保不要添加任何换行符。
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -march={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm -lpthread -lcurl –lcrypto
创建 Twilio 帐户
要接收短信和彩信,您需要一个启用了短信功能的 Twilio 电话号码。要获取您自己的专用电话号码,请注册一个 Twilio 试用帐户。成功获得启用短信功能的 Twilio 访问权限后,转到“Hide API Credentials”查找您的 Account SID 和 Auth Token
// Twilio REST API version
const char API_VERSION[] = "2010-04-01";
// SMS-enabled Twilio phone number
const char CALLER_ID[] = "1480405xxxx";
// Twilio Account SID and Auth token
const char ACCOUNT_SID[] = "AC9c55339a5a070ae81e782117xxxxxxxx";
const char ACCOUNT_TOKEN[] = "59e8819f3f5b530b97b84baexxxxxxxx";
POST 请求到 Twilio
要发送短信,我们会向 Twilio 发起一个 POST 请求,URL 为“/SMS/Messages”,并填写“To”、“From”和“Body”。“To”是您的手机号码,“From”是已启用短信功能的 Twilio 电话号码,“body”是要发送的短信内容。
// URL path = /API_VERSION/Accounts/ACCOUNT_SID/SMS/Messages
char postReqStr[150];
strcpy(postReqStr, twilioAcc);
strcat(postReqStr, "/SMS/Messages");
Serial.println(postReqStr);
// Send SMS
vars.clear();
vars.push_back(Var("To", "480-xxx-xxxx"));
vars.push_back(Var("From", "480-405-xxxx"));
vars.push_back(Var("Body", smsStr));
string response = twilioRest.request(postReqStr, "POST", vars);
温度传感器
现在您已经能够访问和接收和发送短信了,您还可以创建一个温度传感器读取函数。温度传感器用于通过模拟输入测量当前温度。有关温度传感器功能的更多信息,请参阅数据手册 http://www.seeedstudio.com/wiki/images/a/a1/NCP18WF104F03RC.pdf。下面是如何从传感器读取温度并返回数据的示例
// Connect temperature sensor to Analog pin 1
const int tempSensorPin = 1;
// Temperatuere in Fahrenheit
float degF = 0;
// This function reads the analog pin value, converts to Fahrenheit, then
// returns current temperature in Fahrenheit.
float getTemperature()
{
int analogValue = analogRead(tempSensorPin);
//Get the resistance of the sensor;
float resistance = (float)(1023 - analogValue) * 10000/analogValue;
//Convert to temperature via datasheet;
float degKelvin = 1/(log(resistance/10000)/B_THERMISTOR + 1/298.15);
// Convert Kelvin to Celcius
float degC = degKelvin - 273.15;
// Celcius to Fahrenheit
degF = (degC * 9.0) / 5.0 + 32.0;
return degF;
}
示例草图
以下是一个完整的示例,当温度达到 100 华氏度 (o Fahrenheit) 的阈值时,向您的手机发送短信。输出将显示在 Galileo 的 Arduino IDE 串行控制台中。
#include "WString.h"
#include <Utils.h>
#include <Rest.h>
#include <TwiML.h>
#include <vector>
#include <math.h>
using namespace twilio;
vector<Var> vars;
// Twilio REST API version
const char API_VERSION[] = "2010-04-01";
// SMS-enabled Twilio phone number
const char CALLER_ID[] = "1480405xxxx";
// Twilio Account SID and Auth token
const char ACCOUNT_SID[] = " AC9c55339a5a070ae81e782117xxxxxxxx ";
const char ACCOUNT_TOKEN[] = "59e8819f3f5b530b97b84baexxxxxxxx ";
// Response from Twilio
const char* responseStr;
// Temperature sensor variables in Fahrenheit
const int THRESHOLD_TEMP = 100;
const int WAIT = 1000000;
// B value of the thermistor
const int B_THERMISTOR = 3975;
int analogValue = 0;
float degKelvin = 0;
float degC = 0;
float degF = 0;
float resistance = 0;
// Connect temperature sensor to Analog pin 1
const int tempSensorPin = 1;
// This function initializes the serial debug interface.
void setup() {
Serial.begin(9600);
}
// This function builds:
// - The SMS message being sent
// - URL path
// - Send SMS message to your cell phone from SMS-enabled phone
// Input: float tempF - current temperature in Fahrenheit
void sendSMS( float tempF ) {
// Convert tempF to string
char degFstr[20];
sprintf(degFstr, "%2f", degF);
// Convert THRESHOLD_TEMP to string
char thresholdStr[20];
sprintf(thresholdStr, "%d", THRESHOLD_TEMP);
// Build the SMS message
char smsStr[100] = "Current temperature is ";
strcat(smsStr, degFstr);
strcat(smsStr, " F greater than threshold temp ");
strcat(smsStr, thresholdStr);
strcat(smsStr, " F.");
// Twilio Account = /API_VERSION/Accounts/ACCOUNT_SID
char twilioAcc[70] = "/";
strcat(twilioAcc, API_VERSION);
strcat(twilioAcc, "/Accounts/");
strcat(twilioAcc, ACCOUNT_SID);
// URL path = /API_VERSION/Accounts/ACCOUNT_SID//SMS/Messages
char postReqStr[150];
strcpy(postReqStr, twilioAcc);
strcat(postReqStr, "/SMS/Messages");
Serial.println(postReqStr);
// Twilio REST
Rest rest(ACCOUNT_SID, ACCOUNT_TOKEN);
// Send SMS
vars.clear();
vars.push_back(Var("To", "480-xxx-xxxx"));
vars.push_back(Var("From", "480-405-xxxx"));
vars.push_back(Var("Body", smsStr));
string response = twilioRest.request(postReqStr, "POST", vars);
}
// This function read the analog pin value, converts to Fahrenheit, then
// returns current temperature in Fahrenheit.
float getTemperature()
{
analogValue = analogRead(tempSensorPin);
//Get the resistance of the sensor;
resistance = (float)(1023 - analogValue) * 10000/analogValue;
//Convert to temperature via datasheet;
degKelvin = 1/(log(resistance/10000)/B_THERMISTOR + 1/298.15);
// Convert Kelvin to Celcius
degC = degKelvin - 273.15;
// Celcius to Fahrenheit
degF = (degC * 9.0) / 5.0 + 32.0;
return degF;
}
// The main loop read the current temperature using the temperature sensor. If the current temperature is greater than the threshold value, send a SMS message.
void loop() {
degF = getTemperature();
if(degF > THRESHOLD_TEMP)
{
sendSMS(degF);
delay(WAIT);
}
}
摘要
在本文中,我们*测试*了温度传感器,并使用 Twilio 帐户发送了短信。您可以尝试 Grove 入门套件 Plus 中的不同传感器,并挑战自己在 https://software.intel.com/en-us/iot/hardware/sensors 中使用更多传感器。希望本文能激发您在 Intel® Edison 平台上进行有趣的实验。
参考文献
https://software.intel.com/en-us/iot/hardware/edison/downloads
https://www-ssl.intel.com/content/www/us/en/do-it-yourself/edison.html
https://software.intel.com/en-us/iot/hardware/edison
https://software.intel.com/zh-cn/iot/library/edison-getting-started
https://software.intel.com/en-us/iot/hardware/devkit
http://www.seeedstudio.com/wiki/images/a/a1/NCP18WF104F03RC.pdf.
关于作者
Nancy Le 是 Intel Corporation 软件和服务部门的一名软件工程师,致力于 Intel® AtomTM 处理器规模的赋能项目。
立即开始创新!Intel® 物联网开发者计划提供知识、工具、套件以及专家社区,助您快速轻松地将您的创新想法转化为物联网解决方案。
用 Intel® IoT Developer Kit for Intel® Edison and Intel® Galileo 平台,尽情畅想,动手创造。这些套件功能强大,性能优化,是完全集成的端到端物联网解决方案,支持多种编程环境、工具、安全、云连接和硬件。
如需更多资源并了解新的 Intel® 物联网开发者套件 v1.0 如何帮助您简化物联网项目
- 下载 Intel® 物联网开发者套件
- 访问 Intel® 物联网开发者中心
- 参加我们的 Roadshows,获取创建您自己的物联网项目的实践培训