Azure-Samples / iot-hub-c-m0wifi-getstartedkit

Get started with the Adafruit Feather M0 Wifi Azure IoT Starter Kit
MIT License
5 stars 8 forks source link

Get Started with Microsoft Azure IoT Starter Kit - Adafruit Feather M0 WiFi (Arduino-compatible)

Note: this article has been archived and it is not longer updated. Instructions may not be totally up to date.


Looking for something simple? This tutorial is very complete and contains a lot of detail. If you want something quick and simple, try one of these:

You can choose to start with whichever tutorial you want to. If you've never worked with Azure IoT services before, we encourage you to start with the Remote Monitoring solution tutorial, because all of the Azure services will be provisioned for you in a built-in preconfigured solution. Then you can explore how each of the services work by going through the second tutorial.

We hope you enjoy the process. Please provide feedback if there's anything that we can improve.


Don't have a kit yet?: Click here


Running a Simple Remote Monitoring Solution on Feather M0 WiFi (Arduino-compatible)

This tutorial describes the process of taking your Feather M0 WiFi kit, and using it to develop a temperature, humidity and pressure reader that can communicate with the cloud using the  Microsoft Azure IoT SDK. 

Table of Contents

1.1 Tutorial Overview

In this tutorial, you'll be doing the following:

  • Setting up your environment on Azure using the Microsoft Azure IoT Suite Remote Monitoring preconfgiured solution, getting a large portion of the set-up that would be required done in one step.
  • Setting your device and sensors up so that it can communicate with both your computer, and Azure IoT.
  • Updating the device code sample to include our connection data and send it to Azure IoT to be viewed remotely.

1.2 Before Starting

1.2.1 Required Software

1.2.2 Required Hardware

  • Adafruit Feather M0 WiFi kit
    • A microB USB cable
    • A desktop or laptop computer which can run Arduino IDE 1.6.8

1.3 Create a New Azure IoT Suite Remote Monitoring solution and Add Device


Note: For first time users, click here to get your Azure free trial which gives you 200USD of credit to get started.



Note: Make sure to copy down the names and connection strings mentioned into a text document for reference later.



Note: You may be asked to log back in. This is to ensure your solution has proper permissions associated with your account.



Warning: The Remote Monitoring solution provisions a set of Azure IoT Services in your Azure account. It is meant to reflect a real enterprise architecture and thus its Azure consumption is quite heavy. To avoid unnecessary Azure consumption, we recommend you delete the preconfigured solution in azureiotsuite.com once you are done with your work (since it is easy to recreate). Alternatively, if you want to keep it up and running you can do two things to reduce consumption:

1) Visit this guide to run the solution in demo mode and reduce the Azure consumption.

2) Disable the simulated devices created with the solution (Go to Devices>>Select the device>> on the device details menu on the right, clich on Disable Device. Repeat with all the simulated devices).

3) Stop your remote monitoring solution while you are working on the next steps. (See: Troubleshooting)


1.4 Connect the BME280 Sensor Module to your Device


Note: Column on the left corresponds to the sensor and on the right to the board. On the image, the board is place between 10 and 30, with the RST pin connected to row 29, and sensor between 1 and 9, with the CS pin connected to the row 1. With this layout, you are able to connect wires next to the corresponding pins on the breadboard. Additionally, when counting the - and + pins, start from the right and count in, as these do not align with the numbers indicated on the board.


Start End Cable Color
CS (Pin 1E) CE0 (Pin 16B) Orange cable
SDI (Pin 2E) MO (Pin 18J) Blue cable
SDO (Pin 3E) MI (Pin 17J) White cable
SCK (Pin 4E) SCK (Pin 19J) Yellow cable
GND (Pin 5E) Pin 5- Black cable
3Vo (6E) Pin 6+ Red cable
Pin 21- Pin 26J Black cable
Pin 23+ Pin 28J Red cable
Start End Connector
B7 B1 BME280

At the end of your work, your Feather M0 WiFi should be connected to the sensor. We'll test it in the next sections.

1.5 Add the Feather M0 WiFi to the Arduino IDE

You will need to install the Feather M0 WiFi board extension for the Arduino IDE. This takes two steps:

1) Follow the instructions here. There you will see how to add a URL pointing to Adafruit's repository of board extensions.

2) Then continue with Using Arduino IDE, and see how to make the Feather M0 WiFi board selectable under the Tools menu, and how to get the Blink sketch to run.

1.6 Install Library Dependencies

For this project, we'll need to install the following libraries using the Arduino IDE:

  • Adafruit BME280
  • Adafruit Unified Sensor
  • RTCZero
  • AzureIoTHub
  • AzureIoTUtility
  • AzureIoTProtocol_HTTP
  • WiFi101

To install them, click on the Sketch -> Include Library -> Manage Libraries. Search for each library using the box in the upper-right to filter your search, click on the found library, and click the "Install" button.


Note: Starting on version 1.0.17, AzureIoTHub required the AzureIoTUtility and one of the available protocols. These samples use the AzureIoTProtocol_HTTP, but it is prepared to work with AzureIoTProtocol_MQTT too.


Note: Arduino version 1.8.3 with WiFi101 does not support SSL connections on ports other than 443, so the MQTT protocol fails to connect. After this issue has been fixed MQTT support will be available again.


Note: If you have an earlier version of the IoT library, navigate to your Arduino documents directory. Inside the "Libraries" folder, there will be a number of installed libraries. Simply delete the AzureIoT folder.


1.7 Modify the Remote Monitoring sample

#define IOT_CONFIG_WIFI_SSID            "<Your WiFi network SSID or name>"
#define IOT_CONFIG_WIFI_PASSWORD        "<Your WiFi network WPA password or WEP key>"
#define IOT_CONFIG_CONNECTION_STRING    "HostName=<host_name>.azure-devices.net;DeviceId=<device_id>;SharedAccessKey=<device_key>"

1.8 Build Your Remote Monitoring Sample

  • Double-click the reset button (located by the USB input) on the Feather M0 WiFi to put it in boot-loader mode. The red LED will fade in and out to show bootload mode.
  • On windows, the COM port will disconnect, and a new one will appear. Use Tools -> Port -> COM to re-select it.
  • Build and upload the code using Sketch ->  Upload.

Note: As of 1.6.8, the Arduino IDE doesn't properly show "Upload Completed", even when it succeeds.


  • There should now be a green LED on your Feather M0 WiFi. Re-select the COM port if necessary, and then open the Serial Monitor. After 15 seconds you should see a measurements update.

Note: When first starting you will likely see a “Fetching NTP epoch time failed” error – This is normal, and it trying to sync with Azure. This can take even up to 30 seconds to find a NTP server to sync with. One it is synced, it should start transmitting from there.


1.9 View the Sensor Data from the Remote Monitoring Portal

  • Once you have the sample running, visit your dashboard by visiting azureiotsuite.com and clicking “Launch” on your solution
  • Make sure the “Device to View” in the upper right is set to your device
  • If the demo is running, you should see the graph change as your data updates in real time!

Note: Make sure you delete or stop your remote monitoring solution once you have completed this to avoid unnecessary Azure consumption! (See: Troubleshooting)


1.10 Next steps

Please visit our Azure IoT Dev Center for more samples and documentation on Azure IoT.

Using Microsoft Azure IoT Services to Identify Temperature Anomalies

This tutorial describes the process of taking your Microsoft Azure IoT Starter Kit for the Feather M0 WiFi, and using it to develop a temperature and humidity reader that can communicate with the cloud using the Microsoft Azure IoT SDK.

Table of Contents

2.1 Tutorial Overview

This tutorial has the following steps:

  • Provision an IoT Hub instance on Microsoft Azure and adding your device.
  • Prepare the device, get connected to the device, and set it up so that it can read sensor data.
  • Configure your Microsoft Azure IoT services by adding Event Hub, Storage Account, and Stream Analytics resources.
  • Prepare your local web solution for monitoring and sending commands to your device.
  • Update the sample code to respond to commands and include the data from our sensors, sending it to Microsoft Azure to be viewed remotely.

Here is a breakdown of the data flow:

  • The application running on the Adafruit Feather M0 WiFi will get temperature data from the temperature sensor and it will send them to the IoT Hub
  • A Stream Analytics job will read the data from IoT Hub and write them to an Azure Storage Table. Also, if an anomaly is detected, then this job will write data to an Event Hub
  • The Node.js application that is running on your computers will read the data from the Azure Storage Table and the Event Hub and will present them to the user

The end result will be a functional command center where you can view the history of your device's sensor data, a history of alerts, and send commands back to the device.

2.2 Before Starting

2.2.1 Required Software

2.2.2 Required Hardware

  • Adafruit Feather M0 WiFi IoT kit
    • A microB USB cable
    • A desktop or laptop computer which can run Arduino IDE 1.6.8

2.3 Connect the Sensor Module to your Device


Note: Column on the left corresponds to the sensor and on the right to the board. On the image, the board is place between 10 and 30, with the RST pin connected to row 29, and sensor between 1 and 9, with the CS pin connected to the row 1. With this layout, you are able to connect wires next to the corresponding pins on the breadboard. Additionally, when counting the - and + pins, start from the right and count in, as these do not align with the numbers indicated on the board.

Note: The resistor can change a little from one kit to another, e.g. it can be 330 Ohm (orange, orange, brown) or 560 Ohm (green, blue, brown). Both will work with success.


Start End Cable Color
CS (Pin 1E) Pin 16B Orange cable
SDI (Pin 2E) MO (Pin 18J) Blue cable
SDO (Pin 3E) MI (Pin 17J) White cable
SCK (Pin 4E) SCK (Pin 19J) Yellow cable
GND (Pin 5E) Pin 7- Black cable
GND (Pin 26J) Pin 21- Black cable
3Vo (Pin 6E) Pin 8+ Red cable
3V (Pin 28J) Pin 23+ Red cable
Start End Connector
B7 B1 BME280
3I 4I Green LED
5I 6I Red LED
Pin 6J (long LED leg) Pin 21B Red cable
Pin 4J (long LED leg) Pin 20B Green cable
Pin 3J Pin 3- 330 Ohm
Pin 5J Pin 5- 330 Ohm

At the end of your work, your Feather M0 WiFi should be connected with a working sensor. We'll test it in the next sections.

2.4 Create a New Microsoft Azure IoT Hub and Add Device


Note: Make sure to copy down the names and connection strings mentioned into a text document for reference later.


2.5 Create an Event Hub

Event Hub is an Azure IoT publish-subscribe service that can ingest millions of events per second and stream them into multiple applications, services or devices.

2.6 Create a Storage Account for Table Storage

Now we will create a service to store our data in the cloud.

2.7 Create a Stream Analytics job to Save IoT Data in Table Storage and Raise Alerts

Stream Analytics is an Azure IoT service that streams and analyzes data in the cloud. We'll use it to process data coming from your device.

SELECT
    DeviceId,
    EventTime,
    MTemperature as TemperatureReading
INTO
    TemperatureTableStorage
from TempSensors
WHERE
   DeviceId is not null
   and EventTime is not null

SELECT
    DeviceId,
    EventTime,
    MTemperature as TemperatureReading
INTO   
    TemperatureAlertToEventHub
FROM
    TempSensors
WHERE MTemperature>25

Note: You can change the 25 to 0 when you're ready to generate alerts to look at. This number represents the temperature in degrees celsius to check for when creating alerts. 25 degrees celsius is 77 degrees fahrenheit.



Note: Make sure to stop your Command Center jobs once you have when you take a break or finish to avoid unnecessary Azure consumption! (See: Troubleshooting)


2.8 Node Application Setup

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
git clone https://github.com/Azure-Samples/iot-hub-c-m0wifi-getstartedkit.git
npm install -g bower
npm install
bower install
{
    "port": "3000",
    "eventHubName": "event-hub-name",
    "ehConnString": "Endpoint=sb://name.servicebus.windows.net/;SharedAccessKeyName=readwrite;SharedAccessKey=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=",
    "deviceConnString": "HostName=name.azure-devices.net;DeviceId=device-id;SharedAccessKey=aaaaaaaaaaaaaaaaaaaaaa=="
    "iotHubConnString": "HostName=iot-hub-name.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=",
    "storageAcountName": "aaaaaaaaaaa",
    "storageAccountKey": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa==",
    "storageTable": "TemperatureRecords"
}
node server.js
app running on http://localhost:3000
client connected

To deploy this project to the cloud using Azure, you can reference Creating a Node.js web app in Azure App Service.

Next, we will update your device so that it can interact with all the things you just created.

2.9 Add the Feather M0 WiFi to the Arduino IDE

You will need to install the Feather M0 WiFi board extension for the Arduino IDE. This takes two steps:

1) Follow the instructions here. There you will see how to add a URL pointing to Adafruit's repository of board extensions.

2) Then continue with Using Arduino IDE, and see how to make the Feather M0 WiFi board selectable under the Tools menu, and how to get the Blink sketch to run.

2.10 Install Library Dependencies

For this project, we'll need to install the following libraries using the Arduino IDE:

  • Adafruit BME280
  • RTCZero
  • AzureIoTHub
  • AzureIoTUtility
  • AzureIoTProtocol_MQTT

To install them, click on the Sketch -> Include Library -> Manage Libraries. Search for each library using the box in the upper-right to filter your search, click on the found library, and click the "Install" button.

We'll also need to manually install the following libraries:

Instructions for manually installing a library can be found here.


Note: If you have an earlier version of the IoT library, navigate to your Arduino documents directory. Inside the "Libraries" folder, there will be a number of installed libraries. Simply delete the AzureIoT folder.


2.11 Modify the Command Center sample

  • Unzip the example code, and double-click the file command_center.ino to open the project in the Arduino IDE.
  • You will be prompted to creat a folder. Do this, and move the other files in the folder into the newly created child folder
  • Look for the following lines of code:
static const char ssid[] = "[Your WiFi network SSID or name]";
static const char pass[] = "[Your WiFi network WPA password or WEP key]";
static const char* connectionString = "[Device Connection String]";
  • Replace the placeholders with your WiFi name (SSID), WiFi password, and the device connection string you created at the beginning of this tutorial. 

  • Save with Control-s

  • In the same project, click on the rem_ctrl.c tab to see that file.

  • Look for the following lines of code:

static const char DeviceId[] = "[Device Name]";
  • Replace the placeholders with your Device ID you created at the beginning of this tutorial. 
  • Save with Control-s

2.12 Build Your Command Center Sample

  • Double-click the reset button on the Feather M0 WiFi to put it in boot-loader mode. The red LED will fade in and out to show bootload mode.
  • On Windows, the COM port will disconnect, and a new one will appear. Use Tools -> Port -> COM to re-select it.
  • Build and upload the code using Sketch ->  Upload.

Note: As of 1.6.8, the Arduino IDE doesn't properly show "Upload Completed", even when it succeeds.


  • There should now be a green LED on your Feather M0 WiFi. Re-select the COM port if necessary, and then open the Serial Monitor. After 15 seconds you should see a measurements update.
  • Data is now being sent off at regular intervals to Microsoft Azure. When it detects something out of range, you will see the LED you’ve set up turn from green to red!
  • You can click the green button (labeled "Turn on") and the red button (labeled "Turn off") in the application to toggle the green and red LEDs in your kit.

Head back to your Node application and you will have a fully functional command center, complete with a history of sensor data, alerts that display when the temperature got outside a certain range, and commands that you can send to your device remotely.


Note: Make sure to stop your Command Center jobs once you have when you finish to avoid unnecessary Azure consumption! (See: Troubleshooting)


2.13 Next steps

Please visit our Azure IoT Dev Center for more samples and documentation on Azure IoT.

Troubleshooting

Stopping Provisioned Services

Data is not showing up in the Node.js application

In this section we will explain how to see the data flowing from the Arduino application to the Node.js application: