farukc / AzureIoTHub-TI-CC3200

Sample Wiring example code showing how to connect and send data to Microsoft Azure IoT Hub with a TI CC3200 Launchpad

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sample Wiring example code showing how to connect and send data to Microsoft Azure IoT Hub with a TI CC3200 Launchpad

Texas Instruments is one of the [Microsoft Azure Certified IoT partners] (https://azure.microsoft.com/en-us/marketplace/certified-iot-partners/) and TI CC3200 is listed in Microsoft Azure IoT Developer Center. Documentation in Azure IoT Developer Center includes an example for C code only. This document is prepared for TI CC3200 users who wants to connect to Azure IoT Hub using their Energia IDE to enter into IoT world quickly.

Please note that this is a device-to-cloud (D2C) scenario. If you want to extend this project by adding a feature for your CC3200 to receive "command" from cloud, which is called cloud-to-device C2D as well, you may need to implement MQTT instead of HTTP(s) which is used in this example. Built-in "PubSubClient" library (and examples) in your Energia IDE should help you connecting to IoT Hub using MQTT.

Requirements:

  • TI CC3200 Launchpad
  • Following (http://www.ti.com/cc3200start) if you're just starting to write code for TI CC3200 Launchpad XL using Energia(http://www.energia.nu)
  • A Microsoft Account (if you have any @hotmail.com, @live.com etc. email you already have one)
  • Microsoft Azure Subscription (Azure free trial is OK as well)
    (P.S.: DreamSpark Azure Subscription won't work as it doesn't allow you creating Azue IoT Hub, Stream Analytics )
  • A non-free email address to create a Power BI account (.edu email is ok as well)

Summary :

By following this document (and the code in the repo) you will be able to,

  1. Connect your TI CC3200 to Azure IoT Hub
  2. Create an Azure free trial account
  3. Create an Azure IoT Hub
  4. Connect to your Azure IoT Hub using "Device Explorer" tool and create a "device"
  5. Program your CC3200 to connect your Azure IoT Hub
  6. Create your Microsoft Power BI account
  7. See how to get your telemetry data out of Azure Stream Analytics by creating an Azure Stream Analytics job, defining the input, output and query and running it
  8. Create your Power BI dashboard to show telemetry data in a realtime dashboard

Step-by-step instructions:

  1. Connect your TI CC3200 to Azure IoT Hub

  2. Create an Azure free trial account Create a Microsoft Azure free trial account from here if you haven't used Microsoft Azure IoT Hub.

  3. Create an Azure IoT Hub

    1. Go to http://portal.azure.com and login to the portal
    2. Click "New" as shown below :
    3. Select "Internet of Things" and then "Azure IoT Hub" as shown below :
    4. Give a "Name" to your IoT Hub being sure that you received check mark after typing your iot hub name, pick "Pricing and scale tier" as Free (you can have only one Free IoT Hub per each Azure subscription), keep "IoT Hub units" value as one and pick the lowes value for "Device-to-cloud partitions" as we will not put this system on production yet, give a name to your "Resource group", pick the right Azure Subscription in case if you have multiple Azure subscriptions, pick the "Location" and check the "Pin to dashboard" to be able to reach your Azure IoT Hub quickly through your portal dashboard.
    5. You will see that your IoT Hub started to be created :
    6. When your IoT Hub created, click on its tile and you should see its details similar to the shown below :
    7. Click on "Shared access policies", "iothubowner" to see the "Connection String"(s)
    8. Just copy and note either of primary/secondary connection strings somewhere which will be using in coming steps
  4. Connect to your Azure IoT Hub using "Device Explorer" tool and create a "device" :

    1. Go ahead and download free tool Azure Device Explorer and install
    2. Copy/Paste the connection string you noted down in step 1/ii/h into "IoT Hub Connection String" textarea under "Management" tab in Device Explorer and click "Update" button. You should get "Settings updated succesfully" message as shown below :
    3. Switch to "Management" tab and click "Create" button to create a new "device" in your IoT hub :
    4. In the "Create Device" window, give any name to your device (like "myCC3200-01"). You should see "Device created" dialog box as shown below :
      You will also see your device is added to your IoT hub:
    5. Right-mouse click on your device in the "Devices" list and select "Copy connection string for selected device" from the context menu. Note this connection string down
  5. Program your CC3200 to connect your Azure IoT Hub 1. Download all the .ino, .cpp and .h files from this repo to a folder in your machine 2. Run your Energia and open "TICC3200_AzureIoTHub_Client.ino" sketch and put your WiFi name and password in the lines below

    // START: WiFi settings
    char SSID[] = "{YourWifiSSID}";
    char PASSWORD[] = "{YourWifiPassword}";
    // END: WiFi settings
    
 3. Copy/paste your device connection string you noted down in Step 1/iii/e and paste in the same "TICC3200_AzureIoTHub_Client.ino" sketch : 
   
   ````Arduino
   //Azure IoT Hub connection string
   String IoTHubConnectionString = "{PUT YOUR IOT HUB CONNECTION STRING HERE}";
   //String IoTHubConnectionString = "HostName=youriothubname.azure-devices.net;DeviceId=TIcc3200-a;SharedAccessKey=8A0K1er45k5l3achAmda7UvVh6s4L7HTBdt/RnAdYuk=";
   ````
   
 4. Save your sketch, compile and upload it to your TI CC3200 XL
 5. Using Energia's "Serial Monitor", verify that your TI CC3200 XL is connected to your IoT Hub and started sending telemetry data
  1. Create your free Microsoft Power BI account :

    1. Go to http://www.powerbi.com and click "Start for free" button :

    2. We will use "Power BI" (not Power BI Desktop) :

    3. Use your non-free mail to sign up (You can use .edu email as well)

    4. Check your email :

    5. Fill in the necessary details :

    6. Power BI is going to create your account shortly :

    7. Your Power BI account is ready :

    8. Click "Get Data" to see from which data sources you can get data

  2. Get your telemetry data from IoT Hub using Azure Stream Analytics by creating an Azure Stream Analytics job, defining the "input", "output" and "query" and running it

  3. Go to http://portal.azure.com and login to the portal

  4. Click "New" and select "Internet of Things" and then "Stream Analytics job" as shown below :

  5. Give any name to your job as shown above (Note that you should select the "Location" of your job same with your IoT Hub's location. In my example it's "North Europe")

  6. When your Stream Analytics job created, click on your Stream Analytics job and scroll down to see the "Job Topology" for inputs,query and outputs

  7. Click on "Inputs":

  8. Click on "Add" in the new blade :

  9. Give a name for your "Input Alias" to use in your query later. Select the "Source" as "IoT hub" and select "Use IoT hub from current subscription" for "Subscription" and find your "IoT hub". Leave the rest as in default values :

  10. It's time to define an "Output" for "Power BI". As of this document is prepared, creating an output for Power BI in http://portal.azure.com is not ready yet and we should switch to Azure Classic Portal http://manage.windowsazure.com and login with our Azure credentials. Your services are listed on the left column in the classic portal. Find "Stream Analytics" and click to see your stream analytics jobs. Find your job you created in previous step and click to see its details as shown below :

  11. Click on "Outputs" either from the top or click "3 Add an output" as shown below :

  12. Click "ADD AN OUTPUT" and select "Power BI" radio button :

  13. Now you will "Authorize" your stream analytics job to reach your Power BI account. Click on "Authorize Now" as shown below :

  14. Login to your Power BI account using your non-free email address :

  15. Authorization is completed. Now you will define your "Output Alias" to use in your stream analtyics "Query". Give a name to your output alias, give a name for your "DataSet" which will be shown in your Power BI acount, give a name to your "Table" which will again be shown in your Power BI when you started using your "DataSet", leave "Workspace" as default and check mark at the bottom right :

  16. In the classic portal, operational messages are shown at the bottom. You will see that it will create your "output" and also will test the connection :

  17. After successfuly passing the connection for your output for Power BI, you can click "Dismiss Completed" to clear those operational messages :

  18. You will see that your "output" for Power BI is "OK" :

  19. Now click on "Query" to write your simple Stream Analytics job :

  20. As we are just trying to show the incoming telemetry in a realtime Power BI dashboard, we will not do a "filtering" in our query and it will be a simple "SELECT * INTO yourpowerbioutputalias FROM youriothubinputalias" query as shown below :

  21. After writing your query, you will see that stream analytics asks when the output should start. Leave the default option "Job Start Time" and click check mark at the bottom right and wait for job to start (which may take several minutes) :

  22. Create your Power BI dashboard to show telemetry data in a realtime dashboard

  23. Login to your Power BI account in http://www.powerbi.com and click anywhere on the left to see "hamburger button" right below 9 dot logo :

  24. Check your "Datasets" and you should see nothing or the "DataSet" name you set in step 3/xiii . If you see nothing be sure that your stream analytic job is running and you are not connected to your IoT Hub with "Device Explorer":

  25. Click on your dataset and you should see your table name you set in step 3/xiii. Expand your table to see your "fields".

  26. Under "Visualisations" select line chart icon as shown below :

  27. Maximize the line chart and under your table, drag&drop the "EventEnqueuedUTCTime" field into "Axis" field and "temperature" field into "Values" field :

  28. From "File" menu select "Save" and give any name to this "Report" and click "Save" button:

  29. You will need to "Pin" this report into a "Dashboard" so you can see your line chart showing the telemetry in real-time. Click the "Pin" button on your reports top-right :

  30. You will get "Pin to dashboard" screen. As you don't have any dashboard yet, select "New dashboard" and give any name to this dashboard as shown below :

  31. On the left pane, you should see your dashboard is created under "Dashboards" with a "*" sign right next to it :

  32. Click on your dashboard and you should see your report updates the temperature value in real-time :

About

Sample Wiring example code showing how to connect and send data to Microsoft Azure IoT Hub with a TI CC3200 Launchpad


Languages

Language:Arduino 45.7%Language:C++ 41.5%Language:C 12.8%