
nb-iot
这篇文章介绍了如何创建一个IoT项目,该项目使用Arduino通过Temboo和Parse.com将推送消息发送到Android智能手机。 例如,我们将构建一个基于Arduino和Android的警报系统,这是一个有趣的物联网(IoT)示例,该项目的目的是使用连接到Arduino板上的红外传感器(PIR)来构建警报将消息推送到Android智能手机。 该项目融合了不同的技术和平台,我们使它们一起工作!
警报系统使用两个平台来简化项目:
- 滕博
- Parse.com
物联网项目概述
在深入研究该项目之前,描述这两个平台非常有用。
Temboo是一个平台,具有一组“连接器”,可用于与其他平台或服务提供商(例如eBay,Yahoo!Weather,Google等)交换数据。 Temboo有趣的部分是它与Arduino开发板,以便这些连接器可以在Arduino上导出。
Parse.com是我们在上一篇文章中使用的平台,用于发送android push消息。
物联网项目的主要概述如下所示:
显而易见,构建物联网项目由多个部分组成。 第一部分是带有PIR传感器的Arduino板,用于检测运动。 Arduino运行一个Temboo代理,该代理将数据发送到Parse平台。 当Arduino数字输入之一变高时,将触发此代理。 Temboo平台用于创建代理,而无需编写过多的代码行。 借助Temboo choreo ,Arduino可以将JSON数据直接发送到Parse.com,后者再将推送消息发送到我们的智能手机。
Arduino素描
第一步是设置使用PIR传感器的Arduino草图并对其进行测试。 这一步非常简单,我们必须使用三根电线将传感器连接到Arduino板:电源,接地和信号。
传感器非常简单,检测到运动时输出就很高。 对于本示例,我们可以假设它连接在数字引脚8上。
要检查我们的传感器是否正常工作,并以正确的方式将其连接到Arduino板,以便它检测到运动,请尝试将此代码加载到arduino中:
int stato = 0;void setup() {// put your setup code here, to run once:Serial.begin(9600);pinMode(8, INPUT);
}void loop() {// put your main code here, to run repeatedly:stato = digitalRead(8);Serial.println(stato);delay(500);}
现在运行代码,将手移到传感器的前面,看一眼串行监视器以检查其是否有效!
现在,Arduino组件已准备就绪!
Temboo choreo
下一步是设置将Arduino开发板连接到Parse.com的代理。 在这种情况下,我们需要一个以太网屏蔽将Arduino连接到互联网。 我用过Wiznet W5500 。 创建帐户后,就可以配置Temboo杂务了。 我们想将Arduino连接到Parse,所以我们检查Parse-> Push Notification 。 解析杂项需要一些信息,然后再使用:
- 申请编号
- RestAPI密钥
这两个参数用于将代理连接到Parse.com。 您可以在Parse.com中找到以下信息:
您必须将所需的密钥复制到Temboo中:
好,我们准备好了。 如果需要,您可以尝试从Temboo发送通知到Parse.com。
现在设置控制代理的触发器:
最后, Temboo将创建可立即使用的Arduino代码!! 最后将代码复制并粘贴到您的Arduino IDE中。
Temboo生成的代码如下所示:
#include
#include <dhcp.h>
#include <dns.h>
#include <ethernet.h>
#include <ethernetclient.h>
#include <temboo.h>
#include "TembooAccount.h" // Contains Temboo account informationbyte ethernetMACAddress[] = ETHERNET_SHIELD_MAC;
EthernetClient client;// The number of times to trigger the action if the condition is met
// We limit this so you won't use all of your Temboo calls while testing
int maxCalls = 10;// The number of times this Choreo has been run so far in this sketch
int calls = 0;int inputPin = 8;IPAddress ip(192, 168, 1, 130); // Arduino IP Addvoid setup() {Serial.begin(9600);// For debugging, wait until the serial console is connecteddelay(4000);while(!Serial);Ethernet.begin(ethernetMACAddress, ip) ;Serial.println("OK");delay(5000);// Initialize pinspinMode(inputPin, INPUT);Serial.println("Setup complete.\n");
}void loop() {int sensorValue = digitalRead(inputPin);Serial.println("Sensor: " + String(sensorValue));if (sensorValue == HIGH) {if (calls < maxCalls) {Serial.println("\nTriggered! Calling SendNotification Choreo...");runSendNotification(sensorValue);calls++;} else {Serial.println("\nTriggered! Skipping to save Temboo calls. Adjust maxCalls as required.");}}delay(250);
}void runSendNotification(int sensorValue) {TembooChoreo SendNotificationChoreo(client);// Set Temboo account credentialsSendNotificationChoreo.setAccountName(TEMBOO_ACCOUNT);SendNotificationChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);SendNotificationChoreo.setAppKey(TEMBOO_APP_KEY);// Set profile to use for executionSendNotificationChoreo.setProfile("ParseAccount");// Set Choreo inputsString NotificationValue = "{\"channel\": \"temboo\", \"type\": \"android\", \"data\": {\"message\": \"This is a test alert!\"}}";SendNotificationChoreo.addInput("Notification", NotificationValue);// Identify the Choreo to runSendNotificationChoreo.setChoreo("/Library/Parse/PushNotifications/SendNotification");// Run the Choreounsigned int returnCode = SendNotificationChoreo.run();// Read and print the error messagewhile (SendNotificationChoreo.available()) {char c = SendNotificationChoreo.read();Serial.print(c);}Serial.println();SendNotificationChoreo.close();
}
配置Parse.com频道并构建Android应用
Temboo要求我们使用“解析”通道发送通知。 然后,我们必须修改我们的Android应用程序以使用渠道来监听传入的通知。 如果您不知道如何编写处理推送消息的android应用程序,则可以阅读我以前的文章,其中介绍了如何通过Parse.com发送android推送消息。
以这种方式稍微修改ParseTutorialApplication.java
:
@Overridepublic void onCreate() {super.onCreate();System.out.println("Application");Parse.initialize(this, "sfFzOxotpsdOHWJE3nMmD0erLgFoecttKvC9CzIc", "nwbMEy7l4STpyNrABdrQxpEjdKIynSbuec56QbEz");ParseInstallation.getCurrentInstallation().saveInBackground();ParsePush.subscribeInBackground("temboo");}
其中temboo
是通道。
我们准备好了!! 运行该应用程序并将您的手移到传感器附近, Arduino会向Android智能手机发送推送消息!
最终结果如下所示:
在本文的最后,您知道如何使用Arduino和Android构建IoT项目,以及如何通过Temboo和Parse.com集成这两个生态系统。
翻译自: https://www.javacodegeeks.com/2015/09/iot-project-arduino-sends-push-notification-to-android-using-temboo-parse-com.html
nb-iot