This application enables the Adafruit Feather Huzzah or any ESP8266 chip to receive messages from an IoT-Hub that will light up a Neopixel ring or jewel when a tweet is made.
This project uses a Logic App to watch for a new tweet based on dynamic search terms. Upon triggering, a message containing the tweet content is sent to a Function app function which then send a message to the device through an IoT Hub. The device will then send a reply back to the IoT Hub to, usually to inform the resource that the message was received and can be removed from the queue.
The great thing about using a Logic App is that is has so many different connectors. So, if desired, the Logic App can be triggered when a new Facebook post occurs, when a new OneDrive document is uploaded, when a Wunderlist task reminder occurs, on a reoccuring timer, or when one of the manyother options for triggers happens.
- Install Arduino.
- Install Device Explorer (Select SetupDeviceExplorer.msi under Downloads).
There are two options.
- Option 1: If you are good at soldering: some soldering wire and hookup wire.
- Option 2: If you would rather not solder: breadboard and breadboard wiring. (Warning, you may need to fidget with the pins to keep them touching the ports on the board.)
- Adafruit Feather Huzzah or other ESP8266 chip with wifi
- Micro USB cable to USB cable
Steps:
- Create Azure IoT Hub.
- Setup with Device Explorer.
- Create Azure Functions App.
- Create Logic App.
- Setup Device.
- Deploy Code
Create Azure IoT Hub. Sign in to the Azure portal. In the Jumpbar, click New > Internet of Things > IoT Hub.
In the IoT hub blade, choose the configuration for your IoT hub.
- In the Name box, enter a name for your IoT hub. If the Name is valid and available, a green check mark appears in the Name box.
- Select a pricing and scale tier. This tutorial does not require a specific tier. For this tutorial, use the free F1 tier.
- In Resource group, either create a resource group, or select an existing one. For more information, see Using resource groups to manage your Azure resources.
- In Location, select the location to host your IoT hub. For this tutorial, choose your nearest location.
When you have chosen your IoT hub configuration options, click Create. It can take a few minutes for Azure to create your IoT hub. To check the status, you can monitor the progress on the Startboard or in the Notifications panel.
When the IoT hub has been created successfully, click the new tile for your IoT hub in the Azure portal to open the blade for the new IoT hub. Make a note of the Hostname, and then click Shared access policies.
In the Shared access policies blade, click the iothubowner policy, and then copy and make note of the IoT Hub connection string in the iothubowner blade.
Setup with Device Explorer .
Create Device with Device ExplorerOpen Device Explorer. Paste in the copied iothubowner policy from the previous step in the IoT Hub Connection String box.
Click update and you should receive a success message. Click OK to dismiss the message.
Click the Management tab and click the Create button under the Actions menu.
Enter a Device name in the Device Id box and click the Create button when finished.
In Device Explorer, open the Management tab and click SAS Token...
Be sure to select the correct Device ID and enter a number of days until this Tokenshould expire. Then click Create.
Copy the generated token and save it somewhere temportarily.
Create Azure Functions App
Enter a name for your logic app, select a location, resource group, and select Create. If you select Pin to Dashboard the logic app will automatically open once deployed.
- On the Azure portal dashboard, select New.
- In the search bar, search for 'Function App', and then select Function App.
Create Logic App
On the Azure portal dashboard, select New.
In the search bar, search for 'logic app', and then select Logic App. You can also select New, Web + Mobile, and select Logic App.
Enter a name for your logic app, select a location, resource group, and select Create. If you select Pin to Dashboard the logic app will automatically open once deployed.
After opening your logic app for the first time you can select from a template to start. For now click Blank Logic App to build this from scratch.
The first item you need to create is the trigger. This is the event that will start your logic app. Search for twitter in the trigger search box, and select "When a new tweet is posted".
You may need to create a new connection and grant Azure permission to access your account.
Now you'll type in a search term to trigger on. The Frequency and Interval will determine how often your logic app will check for new tweets (and return all tweets during that time span).
Select the New step button, and then choose Add an action.
Select the Azure Functions action from the options if available. Otherwise, search for it and select it.
Select "Azure Functions - Choose an Azure function".
Select the Azure Function App you created in the previous section.
Select "Azure Functions-Create New Function" option.
Update the input payload object box with {"tweetText":"<Tweet text dynamic content>"}
, replacing <Tweet text dynamic content>
with the Tweet text option on the dynamic content menu. Click Next.
Provide your function with a name and click Create.
Save your Logic App! Your logic app will run immediately after saving.
Open the Function App created in the Function App section and open the Function created in the Logic App section.
Copy the following code and paste it into your function.
'use strict';
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = 'your-iot-hub-connection-string';
var targetDevice = 'your-device-name';
var client = Client.fromConnectionString(connectionString);
var console, context;
var open = false;
module.exports = function(cntxt, data) {
console = cntxt;
context = cntxt;
if (open) {
var message = new Message(data.message);
message.ack = 'full';
console.log('Sending message: ' + message.getData());
client.send(targetDevice, message, printResultFor('send'));
}
else
checkClientOpen(open);
console.done();
};
function checkClientOpen(checkMe) {
if (checkMe)
return;
else
openClient();
}
function openClient() {
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
open = false;
} else {
console.log('Client connected');
//client.getFeedbackReceiver(receiveFeedback);
open = true;
}
});
}
function printResultFor(op) {
return function printResult(err, res) {
if (err) context.log(op + ' error: ' + err.toString());
if (res) context.log(op + ' status: ' + res.constructor.name);
};
}
function receiveFeedback(err, receiver){
receiver.on('message', function (msg) {
context.log('Feedback message:')
context.log(msg.getData().toString('utf-8'));
});
}
Update the connectionString
and targetDevice
with your information.
At the bottom left, click "Function app settings".
Select "Go to App Service Settings".
Scroll down in the menu and select Console to open it.
Type npm install azure-iothub
and hit enter to run the command. Be patient.
Type npm install azure-iot-common
and hit enter to run the command. Be patient.
Return to the Function app. You can view the messages your function app is displaying by selecting the Logs button.
Setup Device
There are two options to device setup.
- Option 1: If you are not soldering your parts or want to test first, connect your parts as in the following diagram.
- Option 2: If you are done testing and ready to solder, connect your parts as shown below.
Deploy Code to Device
Open the Arduino IDE and go to File -> Preferences.
Go to the field titled "Additional Boards Manager URLs:" and type http://arduino.esp8266.com/stable/package_esp8266com_index.json
.
Click on Tools -> Board -> Boards Manager.
Search for esp8266, left click on the result titled esp8266 by ESP8266 Community
and click on install. Close the window.
After the board is installed select Tools -> Board -> Adafruit HUZZAH ESP8266.
Click on the Sketch -> Include Library -> Manage Libraries. Search for and install the following libraries.
- Adafruit NeoPixel
- AzureIoTHub
- AzureIoTProtocol_HTTP
- AzureIoTProtocol_MQTT
- AzureIoTUtility
Copy the code found here and paste it into the file opened in the Arduino IDE replacing all other text.
Find where the code says the following and update the ssid
, pass
, host
, authSAS
, and deviceName
variables.
///********************** UPDATE THESE **********************///
static char ssid[] = "YourSSID"; // your network SSID (name)
static char pass[] = "YourPassword"; // your network password (use for WPA, or use as key for WEP)
char host[] = ""; // host name address for your Azure IoT Hub (Ex. "my-iot-hub.azure-devices.net")
char authSAS[] = ""; //Shared Access Signature created in Device Explorer (Ex. "SharedAccessSignature sr=your-iot-hub.azure-devices.net%2Fdevices%2FYourDeviceName&sig=X2JBAEK7gEXheusCWKEfKtDnXKpxesE%2BXT9NqEpQ4Y4%3D&se=1487716471")
String deviceName = "YourDeviceName"; //Update *YourDeviceName*
///*********************************************************///
Click the arrow on the top of the Arduino IDE to Upload the code to the board.
Comments