Chris RobertsAdrian Fernandez
Published

Educational BoosterPack MKII + IBM Bluemix: Cloud interface

Interface the numerous sensors on the Educational BoosterPack MKII with the cloud using Multi Tasking, MQTT, and the JSON library!

IntermediateFull instructions provided837
Educational BoosterPack MKII + IBM Bluemix: Cloud interface

Things used in this project

Story

Read more

Code

EDUMKII_IoT.zip

C/C++
Zip file containing all of the below .ino files that run in parallel through multi tasking
No preview (download only).

EDUMKII_IoT.ino

C/C++
Main .ino sketch that initializes variables
#include <Wire.h>
#include "Adafruit_TMP006.h"
#define USE_USCI_B1 
#include "OPT3001.h"
#include <aJSON.h>
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>

// your network name also called SSID
char ssid[] = "XXXXXXXXXX"; //Replace this with your SSID
// your network password
char password[] = "XXXXXXXXXX"; //Replace this with your WiFi password
// MQTTServer to use
char server[] = "iot.eclipse.org";
int initialized = 0b000000;
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

opt3001 opt3001;
uint32_t luxRead = 0;

Adafruit_TMP006 tmp006;
float objt = 0;

int mic = 0;

int S2 = 0;
int S1 = 0;

int JOY_X = 0;
int JOY_Y = 0;
int JOY_SEL = 0;

int ACC_X = 0;
int ACC_Y = 0;
int ACC_Z = 0;

void setup(){
}

void loop(){

}

ACCEL.ino

C/C++
Code to initialize and read accelerometer values
void setupAccel(){
  initialized = initialized | 0b100000;
}

void loopAccel(){
  ACC_X = analogRead(23);
  ACC_Y = analogRead(24);
  ACC_Z = analogRead(25);
  delay(250);
}

BUTTONS.ino

C/C++
Code to initialize and read button values
void setupButtons(){
  pinMode(32, INPUT_PULLUP);
  pinMode(33, INPUT_PULLUP);
  initialized = initialized | 0b010000;
}

void loopButtons(){
 S2 = digitalRead(32); 
 S1 = digitalRead(33);
 
}

JOYSTICK.ino

C/C++
Code to initialize and read joystick values
void setupJoy(){
  pinMode(JOY_SEL, INPUT_PULLUP);
  initialized = initialized | 0b001000;
}

void loopJoy(){
  JOY_X = analogRead(2);
  JOY_Y = analogRead(26);
  //JOY_SEL = digitalRead(5); //Commented out because causes conflict with the CC3100 WiFi BoosterPack
  delay(250);
}

JSON.ino

C/C++
Code to set up a WiFi connection, package the sensor values into a json, and publish the json to the MQTT client
aJsonStream serial_stream(&Serial);

void setupJSON(){
  Serial.begin(115200);
  Serial.print("Attempting to connect to Network named: ");
  // print the network name (SSID);
  Serial.println(ssid); 
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  WiFi.begin(ssid, password);
  while ( WiFi.status() != WL_CONNECTED) {
    // print dots while we wait to connect
    Serial.print(".");
    delay(300);
  }
  
  Serial.println("\nYou're connected to the network");
  Serial.println("Waiting for an ip address");
  
  while (WiFi.localIP() == INADDR_NONE) {
    // print dots while we wait for an ip addresss
    Serial.print(".");
    delay(300);
  }

  Serial.println("\nIP Address obtained");
  // We are connected and have an IP address.
  // Print the WiFi status.
  printWifiStatus();
  client.connect("EDUMKII"); // Connect to the MQTT topic, for this example it is set to EDUMKII, can be anything just has to match on the subscriber side
  while((initialized & 0b111111) != 0b111111){
    delay(10);
  }
  
}

void loopJSON(){

    aJsonObject *msg = createMessage();
    aJson.print(msg, &serial_stream);
    Serial.println();
    
    
    // Reconnect if the connection was lost
    if (!client.connected()) {
      Serial.println("Disconnected. Reconnecting....");
  
      if(!client.connect("EDUMKII")) {
        Serial.println("Connection failed");
      } else {
        Serial.println("Connection success");
//        if(client.subscribe("inTopic")) {
//          Serial.println("Subscription successfull");
//        }
      }
    }
    char* test = aJson.print(msg);
    if(client.publish("EDUMKII", test)) {
      Serial.println("Publish success");
    } else {
      Serial.println("Publish failed");
    }
    free(test);
    aJson.deleteItem(msg);
    // Check if any message were received
    // on the topic we subsrcived to
//    client.poll();
    delay(500);
}

aJsonObject *createMessage()
{
  aJsonObject *msg = aJson.createObject();
  aJsonObject *EDU = aJson.createObject();
  aJson.addNumberToObject(EDU, "ACC_X", ACC_X);
  aJson.addNumberToObject(EDU, "ACC_Y", ACC_Y);
  aJson.addNumberToObject(EDU, "ACC_Z", ACC_Z);
  aJson.addNumberToObject(EDU, "JOY_X", JOY_X);
  aJson.addNumberToObject(EDU, "JOY_Y", JOY_Y);
  //aJson.addNumberToObject(EDU, "JOY_SEL", JOY_SEL); //Commented out because causes conflict with the CC3100 WiFi BoosterPack
  aJson.addNumberToObject(EDU, "TMP", (float)objt);
  aJson.addNumberToObject(EDU, "OPT", (float)luxRead);
  aJson.addNumberToObject(EDU, "MIC", mic);
  aJson.addNumberToObject(EDU, "S1", S1);
  aJson.addNumberToObject(EDU, "S2", S2);
  aJson.addItemToObject(msg, "EDU", EDU);

  return msg;
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received message for topic ");
  Serial.print(topic);
  Serial.print("with length ");
  Serial.println(length);
  Serial.println("Message:");
  Serial.write(payload, length);
  Serial.println();
}

MIC.ino

C/C++
Code to initialize and read microphone values
void setupMic(){
  initialized = initialized | 0b000100;
}

void loopMic(){
  mic = analogRead(6);
  delay(200);
}

OPT3001.ino

C/C++
Code to initialize and read optical sensor values
/*OPT3001_Demo Example
In this example, we will read the various registers available in the OPT3001.
The LUX readings from the OPT3001 are also shown in the Energia serial monitor.


Created by Adrian Fernandez & Dung Dang, Dec 2013
Released to public domain.
*/


void OPTsetup()
{
  opt3001.begin(); 
  initialized = initialized | 0b000010;
}

void OPTloop()
{
  // Read OPT3001
  luxRead = opt3001.readResult();  
 
  delay(800);
}

SERIAL.ino

C/C++
Debug tab used to print out all values. Not necessary to implement the code and is completely commented by default
//Used for debugging


//void setupSerial(){
//  Serial.begin(115200);
//
//}
//
//void loopSerial(){
//  Serial.print("JOY_X: ");
//  Serial.print(JOY_X);
//  Serial.print("\tJOY_Y: ");
//  Serial.print(JOY_Y);
//  Serial.print("\tJOY_SEL: ");
//  Serial.println(JOY_SEL);
//
//  Serial.print("ACC_X: ");
//  Serial.print(ACC_X);
//  Serial.print("\tACC_Y: ");
//  Serial.print(ACC_Y);
//  Serial.print("\tACC_Z: ");
//  Serial.println(ACC_Z);
//  
//  Serial.print("LUX Readings = ");
//  Serial.println(luxRead, DEC);
//  
//  Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C");
//
//  Serial.print("S1: ");
//  Serial.print(S1);
//  Serial.print("\tS2: ");
//  Serial.println(S2);
//  
//  Serial.print("Mic: ");
//  Serial.println(mic);
//  
//  delay(1000);
//}

TMP006.ino

C/C++
Code to initialize and read temperature sensor values
void setupTMP()
{
  // Initalizes the TMP006 for operation and for I2C communication
  if (! tmp006.begin(TMP006_CFG_8SAMPLE)) {
    Serial.println("No sensor found");
    while (1);
  }
  initialized = initialized | 0b000001;
}

void loopTMP()
{
  objt = tmp006.readObjTempC();
//  float diet = tmp006.readDieTempC();
//  Serial.print("Die Temperature: "); Serial.print(diet); Serial.println("*C");
//   
  delay(1000); // 4 seconds per reading for 16 samples per reading
}

Credits

Chris Roberts

Chris Roberts

8 projects • 18 followers
I am an applications engineer with Texas Instruments working with the Launchpad by trade, and a maker by passion!
Adrian Fernandez

Adrian Fernandez

10 projects • 45 followers
peek a boo my name is adrian

Comments

Add projectSign up / Login