IOT ESP8266 Tutorial – Sending ESP8266 Data to the IBM Bluemix IOT

IOT ESP8266 Tutorial – Sending ESP8266 Data to the IBM Bluemix IOTInitialPageBlueMix

This is the sixth of a multi-part posting on the ESP8266.   In this posting we are showing how to send data to the IBM Bluemix Internet of Things Service.  In the previous ESP8266 posting we showed how to get set up on the IBM Bluemix IOT backend system.  We will be understanding the formats of the data and how to establish the credentials for authenticating our ESP8266 to the IBM Bluemix system.

One item of help we can offer.   We have had a difficult time with the IBM Bluemix on the Safari browsers.   Changing to Chrome made things work much better.

For this series, we are using a IOT device designed by SwitchDoc Labs based on the ESP8266 WiFi/processor in two variants.  One device connected to a TSC3574 I2C Color Sensor and the other device a portable IoT connected heart rate pulse detector.  We are using the Color Sensor for this posting.

Part 1  IOT ESP8266 Tutorial – Using nodeMCU/LUA

Part 2 IOT ESP8266 Timer Tutorial – Arduino IDE

Part 3 IOT ESP8266 Tutorial – Using the Arduino IDE

Part 4 IOT ESP8266 Tutorial – AT Command Set Firmware

Part 5 IOT ESP8266 Tutorial – Connect to the IBM Bluemix Internet of Things

Part 6 IOT ESP8266 Tutorial – Sending ESP8266 Data to the IBM Bluemix IOT

Part 7 IOT ESP8266 Tutorial – The IOT Application and Source Code

Part 8 IOT ESP8266 Tutorial – Displaying the data on the IBM Bluemix IOT

We are using the Adafruit ESP8266 Huzzah breakout board for these postings.

The ESP8266

The ESP8266 is made by a privately held company in China called Espressif.   They are a fabless semiconductor company that just came out of nowhere and shook up the whole industry.   Now all the major players are working on inexpensive versions of an IOT chip with WiFi connectivity.  And they are all struggling to make it as inexpensive as the ESP8266

Figure3

The Adafruit ESP8266 Huzzah

The Adafruit ESP8266 Huzzah board is a great  breakout for the ESP8266.  It makes it much easier to use than the really cheap modules.

Most of the low cost modules are not breadboard friendly, don’t have an onboard 3.3V regulator or level shifting for signals.  The Huzzah has all of those features.  For more on the ESP8266 Huzzah board see this posting.

The ESP8266 Software

We are using the Arduino IDE for this project.  See how to use the Arduino IDE with the ESP8266 in this posting.  We are also assuming that you successfully set up an account on the IBM Bluemix IoT platform as shown in the previous tutorial, Part 5.

First of all, what do we send up to Bluemix from the ESP8266?  It turns out the data protocols are pretty straight forward.  There are two major areas that require description.

MQTT

AAEAAQAAAAAAAAI5AAAAJGZjYzNjOGEwLWE4YmYtNDY4ZC04OGI2LTdmMzc1NDIwZmE1Yg-2MQTT is a publish-subscribe based “light weight” messaging protocol for use on top of the TCP/IP protocol, such as the WiFi packets that we are using in this project. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited.   Both of these conditions are met with an ESP8266 IOT design, so it makes sense to use.  There is also an excellent library available for MQTT for the Arduino IDE (see below).  The publish-subscribe messaging pattern requires a message broker. The broker is responsible for distributing messages to interested clients based on the topic of a message.

Publish–subscribe is a pattern where senders of messages, called publishers (in this case our ESP8266 is the publisher), don’t program the messages to be sent directly to  subscribers, but instead characterize message payloads into classes without the specific knowledge of which subscribers the messages are sent to.   Similarly, subscribers (the IBM Bluemix IOT in this case) will only receive messages that are of interest without specific knowledge of which publishers there are.  The IBM Bluemix operates as the broker in this system and routes the published data to the appropriate subscribers inside of Bluemix.

JSON Data Payload

JSON  is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is the primary data format used for asynchronous browser/server communication, largely replacing XML.  XML is a “heavier” protocol that is also hierarchical in nature, but with a great deal more redundancy that JSON.  Yes, there are class wars going on for people that advocate JSON over XML, but in todays world of higher speed communication, it rarely matters.  You can make the argument that the higher data density of JSON is a better choice for IOT applications.

Here is an example of the data packet we are using in the ESP8266 Bluemix code in JSON for the LightSwarm data payload:

{"d":
      {
         "LightSwarm IOT":"LS1",
         "sampleCount":2118,
         "lightValue":383
      }
}

Authentication

We need to have a secure method of authenticating that our ESP8266 devices is allowed to put data into the right slots inside the IBM Bluemix.  This is done in our case by exchanging a cryptographic token that was generated by Bluemix and included in our ESP8266 code.   To do this we need to go back to the Bluemix account that you set up in the previous posting and complete the following steps:

– Click on the Internet of Things Foundation icon on the Bluemix dashboard

 

Bluemix Dashboarcd

 

– Next, click on the Launch dashboard button under “Connect your devices”\

ConnectDevicesBluemix

 

Then to the Devices Tab and finally “Add Device” down at the bottom of the page, then click on “Create Device Type”.  That will give you the following screen:

Createdevicetype

Step through the rest of the steps.  There are many configuration options that nearly all can be left blank.  You do need a device type and a device ID

When the device has been created you’ll see the Device Credentials page and you need to save those details (We have not found any way of recovering these credentials from the Bluemix system) which are used to authenticate your ESP8266 Arduino IDE sketch in the in the next step.

Credentials

Obviously, don’t use the credentials in the screen above.  They will not work.  You need to create your own.

Next Up?

The code for our example IoT Application in Part 7.