Sample code for AT&T IoT Services DevLab with IoT StarterKit.

Dependencies:   FXOS8700CQ M2XStreamClient-JMF WNCInterface jsonlite mbed-rtos mbed

Fork of WNCInterface_M2Xdemo by Avnet

main.cpp

Committer:
jk431j
Date:
2017-04-05
Revision:
6:731f412e6571
Parent:
5:8099493f2c35
Child:
7:721eb6bb68d3

File content as of revision 6:731f412e6571:

//
// This file contains an example implementation of M2X using the HTTP interface as the underlying 
// transport.
//

#include "mbed.h"
#include "WNCInterface.h"

#define MBED_PLATFORM
#define M2X_ENABLE_READER

#include <jsonlite.h>
#include "M2XStreamClient.h"

#include "sensors.h"

#define CRLF "\n\r"

char deviceId[] = "9fc504277536c2450ad5fa2cb4e2b478"; // Device you want to post to
char m2xKey[]   = "8ec6719c0ccbd8049494b7404af762f8"; // Your M2X API Key or Master API Key

const char *hstreamName = "humidity";
const char *tstreamName = "temp";
const char *accelstreamNames[] = { "accelX", "accelY", "accelZ" };


WNCInterface eth;
Client client;
M2XStreamClient m2xClient(&client, m2xKey);
TimeService timeService(&m2xClient);

I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut led_green(LED_GREEN);
DigitalOut led_red(LED_RED);
DigitalOut led_blue(LED_BLUE);

K64F_Sensors_t  SENSOR_DATA = {};

//********************************************************************************************************************************************
//* Set the RGB LED's Color
//* LED Color 0=Off to 7=White.  3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 
//********************************************************************************************************************************************
void SetLedColor(unsigned char ucColor)
{
    //Note that when an LED is on, you write a 0 to it:
    led_red = !(ucColor & 0x1); //bit 0
    led_green = !(ucColor & 0x2); //bit 1
    led_blue = !(ucColor & 0x4); //bit 2
} //SetLedColor()


bool ExecuteCommand(const char* Command)
{
    char cLedColor = *Command;
    switch(cLedColor)
    {
        case 'O':
        { //Off
            SetLedColor(0);
            break;
        }
        case 'R':
        { //Red
            SetLedColor(1);
            break;
        }
        case 'G':
        { //Green
            SetLedColor(2);
            break;
        }
        case 'Y':
        { //Yellow
            SetLedColor(3);
            break;
        }
        case 'B':
        { //Blue
            SetLedColor(4);
            break;
        }
        case 'M':
        { //Magenta
            SetLedColor(5);
            break;
        }
        case 'T':
        { //Turquoise
            SetLedColor(6);
            break;
        }
        case 'W':
        { //White
            SetLedColor(7);
            break;
        }
        default:
        {
            return false;
        }
    } //switch(cLedColor)
    return true;
}


void on_data_point_found(const char* at, const char* value, int index, void* context, int type) {
    pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type);
    pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value);
}

void on_fill_data(Print *print, void *context) {
    // no data to fill
}

void on_command_found(const char* id, const char* name, int index, void *context) {
    pc.printf(">>Found a command, index: %d" CRLF, index);
    pc.printf(">>ID: %s" CRLF ">>Name: %s" CRLF, id, name);
    ExecuteCommand(name);
    m2xClient.markCommandProcessed(deviceId, id, on_fill_data, NULL);
    pc.printf(">>Command confirmed" CRLF, id, name);
}


int main() {
    char timestamp[25];
    int length = 25;
    int response;
    
    pc.baud(115200);
    pc.printf("M2X StarterKit demo: initializng the network" CRLF);
    response = eth.init();                     
    pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
    if( !response ) {
        pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF);
        NVIC_SystemReset();
        while(1);
    }
        
    response = eth.connect();                 
    pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress());
    
    pc.printf("Initialize the sensors" CRLF);    
    sensors_init();
    read_sensors();
        
    pc.printf(WHT "initialize the M2X time service" CRLF);
    if (!m2x_status_is_success(timeService.init())) 
        pc.printf("Cannot initialize time service!" CRLF);
    else {
        timeService.getTimestamp(timestamp, &length);
        pc.printf("Current timestamp: %s" CRLF, timestamp);
    }
    
    pc.printf("Query for possible commands using this device..." CRLF);
    response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
    pc.printf("listCommands response code: %d" CRLF, response);  

    while (true) {
        // read sensor values 
        read_sensors();

        // post the humidity value
        pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.Humidity);
        response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.Humidity);
        pc.printf("Post response code: %d" CRLF, response);
        
        // post the temp value
        pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.Temperature);
        response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.Temperature);
        pc.printf("Post response code: %d" CRLF, response);

        pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature);
        response = m2xClient.postDeviceUpdate(deviceId, 3, accelstreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ});
        pc.printf("Post response code: %d" CRLF, response);
                                   
        timeService.getTimestamp(timestamp, &length);
        pc.printf("%s waiting for 60 seconds... " CRLF CRLF CRLF, timestamp);
        
        for (short idx=0; idx < 6; idx++) {
            // wait 10 seconds
            delay(10 * 1000);

            // and then query fo commands
            pc.printf("Query for possible commands using this device..." CRLF);
            response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending");
            pc.printf("listCommands response code: %d" CRLF, response);              
        }

    }
}