IOT ESP8266 Tutorial – The IOT Application and Source Code

IOT ESP8266 Tutorial – The IOT Application and Source CodeFigure2-11

This is the seventh 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 using the ESP8266 source code done in the Arduino IDE.  In the previous ESP8266 posting we showed how to get the necessary IBM Bluemix credentials.  We will be understanding the formats of the data and how to establish the credentials for authenticating our ESP8266 to the IBM Bluemix system, showing the source code.

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. as well as setting up your Bluemix IOT device credentials in Part 6.

The IBM Bluemix Internet of Things SolutionBluemix

Bluemix is an implementation of IBM’s Open Cloud Architecture that enables you to rapidly create, deploy, and manage  cloud applications for the Internet of Things. There are a significant  growing ecosystem of runtime frameworks and services.  Blue mix provides a dashboard for you to create, view, and manage your applications, devices and services.  The Bluemix dashboard also provides the ability to manage organizations, spaces, and user access.

We did find that the “dashboard” paradigm in the Bluemix system somewhat of a misnomer and a bit confusing.  Instead of having one dashboard, you have dashboards for every service that you attach.  It’s easy to get lost in the sequence.  But with some perseverance we got through the learning curve.

Bluemix provides access to a wide variety of services that can be incorporated into an application both delivered from IBM and third party vendors.

 

What is the LightSwarm Hardware?

LightSwarm is an IOT system built by SwitchDoc Labs for an upcoming book on building IOT devices.  LightSwarm uses 5 ESP8266 boards arrayed in a cooperative distributed swarm.For this ESP8266 Tutorial we are reprogramming the Swarm device to act as a Bluemix IOT node.  The parts list and the wiring diagram is below:

 

Key:

ESP8266 Huzzah Board:   Huzzah ESP8266

TCS34725 Sensor:  TCS34725 Breakout Board

9V Battery: 9VBat

ESP8266 Huzzah Board (ESP8266)
From To Description
ESP8266 / GND TCS34725 /GND Ground for I2C Light Sensor
ESP8266 / 3V TCS34725 / 3V3 3.3V Power for I2C Light Sensor
ESP8266 / #4 TCS34725 /SDA SDA for I2C Light Sensor
ESP8266 / #5 TCS34725 /SCL SCL for I2C Light Sensor
ESP8266 / GND 9VBat / “-“ terminal (minus  terminal) Ground for battery
ESP8266 / VBat 9VBat / “+” terminal (plus 9V) 9V from battery
TCS34725 / LED TCS34725 / INT Connecting these two pins together allow for software control of bright LED on TCS34725 board

An FTDI cable is plugged into the end of the Adafruit Huzzah ESP8266.  Make sure you align the black wire with the GND pin on the ESP8266 breakout board as shown below.  SwitchDoc Labs has an inexpensive one available here.

Figure2-9

 

 

 

 

 

Here is the fully wired LightSwarm node.

Figure2-11

 

 

 

 

 

 

 

 

The Arduino IDE code for the  IOT LightSwarm Device

Below is the complete code for the ESP8266 version of the LightSwarm IBM Bluemix IOT project.

/**
 *Connect LightSwarm hardware and software an ESP8266 to the IBM IoT Foundation
 * 
 * SwitchDoc Labs
 * November 2015
 */
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/releases/tag/v2.3
#include "Adafruit_TCS34725.h"

//Local WiFi Variables

const char* ssid = "gracie";
const char* password = "XXXXXXXX";

// IBM BlueMix IOT Foundation Data

#define ORG "4183lj"
#define DEVICE_TYPE "LightSwarm"
#define DEVICE_ID "LS1"
#define TOKEN "XXXXXXXXXXXXX"


// setup for IOT IBM

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;


void callback(char* topic, byte* payload, unsigned int length) {
 Serial.println("callback invoked from IOT BlueMix");
}


// variables for light sensor

int clearColor;
int redColor;
int blueColor;
int greenColor;


Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void setup() {
 Serial.begin(115200);
 Serial.println();

 Serial.println("----------------");
 Serial.println("LightSwarm IBM BlueMix IOT");
 Serial.println("----------------");
 
 Serial.print("Connecting to ");
 Serial.print(ssid);
 if (strcmp (WiFi.SSID(), ssid) != 0) {
 WiFi.begin(ssid, password);
 }
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 } 
 Serial.println("");

 Serial.print("Local WiFi connected, IP address: ");
 Serial.println(WiFi.localIP());

 
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");

  }

  // turn off the light
  tcs.setInterrupt(true);  // true means off, false means on

}

int sampleCount = 0;
int lightValue = 0;

void loop() {

 if (!!!client.connected()) {
 Serial.print("Reconnecting client to ");
 Serial.println(server);
 while (!!!client.connect(clientId, authMethod, token)) {
 Serial.print(".");
 delay(500);
 }
 Serial.println();
 }

 

  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);

  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");

  clearColor = c;
  redColor = r;
  blueColor = b;
  greenColor = g;

  lightValue = clearColor;
  sampleCount++;
  

 String payload = "{\"d\":{\"LightSwarm IOT\":\"LS1\",\"sampleCount\":";
 payload += sampleCount;
 payload += ",\"lightValue\":";
 payload += lightValue;
 payload += "}}";

 
 
 Serial.print("Sending LightSwarm payload: ");
 Serial.println(payload);
 
 if (client.publish(topic, (char*) payload.c_str())) {
 Serial.println("BlueMix IOT Publish ok");
 } else {
 Serial.println("BlueMix IOT Publish failed");
 }

 delay(10000);

}

Results

Running this in the Arduino IDE gives the following results on the Serial output:

 

----------------
LightSwarm IBM BlueMix IOT
----------------
Connecting to gracie........
Local WiFi connected, IP address: 192.168.1.123
44
Found sensor
Reconnecting client to 4183lj.messaging.internetofthings.ibmcloud.com
.
Color Temp: 4051 K - Lux: 75 - R: 74 G: 87 B: 52 C: 217  
Sending LightSwarm payload: {"d":{"LightSwarm IOT":"LS1","sampleCount":1,"lightValue":217}}
BlueMix IOT Publish ok
Color Temp: 4275 K - Lux: 81 - R: 78 G: 95 B: 59 C: 238  
Sending LightSwarm payload: {"d":{"LightSwarm IOT":"LS1","sampleCount":2,"lightValue":238}}
BlueMix IOT Publish ok
Color Temp: 4127 K - Lux: 80 - R: 76 G: 92 B: 55 C: 228  
Sending LightSwarm payload: {"d":{"LightSwarm IOT":"LS1","sampleCount":3,"lightValue":228}}
BlueMix IOT Publish ok
Color Temp: 4365 K - Lux: 85 - R: 81 G: 100 B: 63 C: 250  
Sending LightSwarm payload: {"d":{"LightSwarm IOT":"LS1","sampleCount":4,"lightValue":250}}
BlueMix IOT Publish ok
Color Temp: 3958 K - Lux: 73 - R: 72 G: 84 B: 49 C: 209  
Sending LightSwarm payload: {"d":{"LightSwarm IOT":"LS1","sampleCount":5,"lightValue":209}}
BlueMix IOT Publish ok

And the resulting data from the IBM Bluemix IOT platform:
BlueMixResults

 

Next Up?

Next we will look at the IBM Bluemix Real-Time IOT display service and look at the incoming data from LightSwarm.