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
main.cpp@6:731f412e6571, 2017-04-05 (annotated)
- Committer:
- jk431j
- Date:
- Wed Apr 05 17:46:42 2017 +0000
- Revision:
- 6:731f412e6571
- Parent:
- 5:8099493f2c35
- Child:
- 7:721eb6bb68d3
Improved variable initialization
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:62feed0f1fd9 | 1 | // |
JMF | 0:62feed0f1fd9 | 2 | // This file contains an example implementation of M2X using the HTTP interface as the underlying |
JMF | 0:62feed0f1fd9 | 3 | // transport. |
JMF | 0:62feed0f1fd9 | 4 | // |
JMF | 0:62feed0f1fd9 | 5 | |
JMF | 0:62feed0f1fd9 | 6 | #include "mbed.h" |
JMF | 0:62feed0f1fd9 | 7 | #include "WNCInterface.h" |
JMF | 0:62feed0f1fd9 | 8 | |
JMF | 0:62feed0f1fd9 | 9 | #define MBED_PLATFORM |
JMF | 0:62feed0f1fd9 | 10 | #define M2X_ENABLE_READER |
JMF | 0:62feed0f1fd9 | 11 | |
JMF | 0:62feed0f1fd9 | 12 | #include <jsonlite.h> |
JMF | 0:62feed0f1fd9 | 13 | #include "M2XStreamClient.h" |
JMF | 0:62feed0f1fd9 | 14 | |
jk431j | 4:08979e323c6e | 15 | #include "sensors.h" |
jk431j | 4:08979e323c6e | 16 | |
JMF | 0:62feed0f1fd9 | 17 | #define CRLF "\n\r" |
JMF | 0:62feed0f1fd9 | 18 | |
jk431j | 4:08979e323c6e | 19 | char deviceId[] = "9fc504277536c2450ad5fa2cb4e2b478"; // Device you want to post to |
jk431j | 4:08979e323c6e | 20 | char m2xKey[] = "8ec6719c0ccbd8049494b7404af762f8"; // Your M2X API Key or Master API Key |
JMF | 0:62feed0f1fd9 | 21 | |
JMF | 0:62feed0f1fd9 | 22 | const char *hstreamName = "humidity"; |
jk431j | 4:08979e323c6e | 23 | const char *tstreamName = "temp"; |
jk431j | 5:8099493f2c35 | 24 | const char *accelstreamNames[] = { "accelX", "accelY", "accelZ" }; |
jk431j | 5:8099493f2c35 | 25 | |
JMF | 0:62feed0f1fd9 | 26 | |
JMF | 0:62feed0f1fd9 | 27 | WNCInterface eth; |
JMF | 0:62feed0f1fd9 | 28 | Client client; |
JMF | 0:62feed0f1fd9 | 29 | M2XStreamClient m2xClient(&client, m2xKey); |
JMF | 0:62feed0f1fd9 | 30 | TimeService timeService(&m2xClient); |
jk431j | 4:08979e323c6e | 31 | |
jk431j | 4:08979e323c6e | 32 | I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used |
jk431j | 4:08979e323c6e | 33 | Serial pc(USBTX, USBRX); // tx, rx |
jk431j | 4:08979e323c6e | 34 | DigitalOut led_green(LED_GREEN); |
jk431j | 4:08979e323c6e | 35 | DigitalOut led_red(LED_RED); |
jk431j | 4:08979e323c6e | 36 | DigitalOut led_blue(LED_BLUE); |
jk431j | 4:08979e323c6e | 37 | |
jk431j | 6:731f412e6571 | 38 | K64F_Sensors_t SENSOR_DATA = {}; |
jk431j | 4:08979e323c6e | 39 | |
jk431j | 4:08979e323c6e | 40 | //******************************************************************************************************************************************** |
jk431j | 4:08979e323c6e | 41 | //* Set the RGB LED's Color |
jk431j | 4:08979e323c6e | 42 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
jk431j | 4:08979e323c6e | 43 | //******************************************************************************************************************************************** |
jk431j | 4:08979e323c6e | 44 | void SetLedColor(unsigned char ucColor) |
jk431j | 4:08979e323c6e | 45 | { |
jk431j | 4:08979e323c6e | 46 | //Note that when an LED is on, you write a 0 to it: |
jk431j | 4:08979e323c6e | 47 | led_red = !(ucColor & 0x1); //bit 0 |
jk431j | 4:08979e323c6e | 48 | led_green = !(ucColor & 0x2); //bit 1 |
jk431j | 4:08979e323c6e | 49 | led_blue = !(ucColor & 0x4); //bit 2 |
jk431j | 4:08979e323c6e | 50 | } //SetLedColor() |
jk431j | 4:08979e323c6e | 51 | |
JMF | 0:62feed0f1fd9 | 52 | |
jk431j | 4:08979e323c6e | 53 | bool ExecuteCommand(const char* Command) |
jk431j | 4:08979e323c6e | 54 | { |
jk431j | 4:08979e323c6e | 55 | char cLedColor = *Command; |
jk431j | 4:08979e323c6e | 56 | switch(cLedColor) |
jk431j | 4:08979e323c6e | 57 | { |
jk431j | 4:08979e323c6e | 58 | case 'O': |
jk431j | 4:08979e323c6e | 59 | { //Off |
jk431j | 4:08979e323c6e | 60 | SetLedColor(0); |
jk431j | 4:08979e323c6e | 61 | break; |
jk431j | 4:08979e323c6e | 62 | } |
jk431j | 4:08979e323c6e | 63 | case 'R': |
jk431j | 4:08979e323c6e | 64 | { //Red |
jk431j | 4:08979e323c6e | 65 | SetLedColor(1); |
jk431j | 4:08979e323c6e | 66 | break; |
jk431j | 4:08979e323c6e | 67 | } |
jk431j | 4:08979e323c6e | 68 | case 'G': |
jk431j | 4:08979e323c6e | 69 | { //Green |
jk431j | 4:08979e323c6e | 70 | SetLedColor(2); |
jk431j | 4:08979e323c6e | 71 | break; |
jk431j | 4:08979e323c6e | 72 | } |
jk431j | 4:08979e323c6e | 73 | case 'Y': |
jk431j | 4:08979e323c6e | 74 | { //Yellow |
jk431j | 4:08979e323c6e | 75 | SetLedColor(3); |
jk431j | 4:08979e323c6e | 76 | break; |
jk431j | 4:08979e323c6e | 77 | } |
jk431j | 4:08979e323c6e | 78 | case 'B': |
jk431j | 4:08979e323c6e | 79 | { //Blue |
jk431j | 4:08979e323c6e | 80 | SetLedColor(4); |
jk431j | 4:08979e323c6e | 81 | break; |
jk431j | 4:08979e323c6e | 82 | } |
jk431j | 4:08979e323c6e | 83 | case 'M': |
jk431j | 4:08979e323c6e | 84 | { //Magenta |
jk431j | 4:08979e323c6e | 85 | SetLedColor(5); |
jk431j | 4:08979e323c6e | 86 | break; |
jk431j | 4:08979e323c6e | 87 | } |
jk431j | 4:08979e323c6e | 88 | case 'T': |
jk431j | 4:08979e323c6e | 89 | { //Turquoise |
jk431j | 4:08979e323c6e | 90 | SetLedColor(6); |
jk431j | 4:08979e323c6e | 91 | break; |
jk431j | 4:08979e323c6e | 92 | } |
jk431j | 4:08979e323c6e | 93 | case 'W': |
jk431j | 4:08979e323c6e | 94 | { //White |
jk431j | 4:08979e323c6e | 95 | SetLedColor(7); |
jk431j | 4:08979e323c6e | 96 | break; |
jk431j | 4:08979e323c6e | 97 | } |
jk431j | 4:08979e323c6e | 98 | default: |
jk431j | 4:08979e323c6e | 99 | { |
jk431j | 4:08979e323c6e | 100 | return false; |
jk431j | 4:08979e323c6e | 101 | } |
jk431j | 4:08979e323c6e | 102 | } //switch(cLedColor) |
jk431j | 4:08979e323c6e | 103 | return true; |
JMF | 0:62feed0f1fd9 | 104 | } |
JMF | 0:62feed0f1fd9 | 105 | |
JMF | 0:62feed0f1fd9 | 106 | |
jk431j | 4:08979e323c6e | 107 | void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { |
jk431j | 4:08979e323c6e | 108 | pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type); |
jk431j | 4:08979e323c6e | 109 | pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value); |
jk431j | 4:08979e323c6e | 110 | } |
jk431j | 4:08979e323c6e | 111 | |
jk431j | 4:08979e323c6e | 112 | void on_fill_data(Print *print, void *context) { |
jk431j | 4:08979e323c6e | 113 | // no data to fill |
JMF | 0:62feed0f1fd9 | 114 | } |
JMF | 0:62feed0f1fd9 | 115 | |
jk431j | 4:08979e323c6e | 116 | void on_command_found(const char* id, const char* name, int index, void *context) { |
jk431j | 4:08979e323c6e | 117 | pc.printf(">>Found a command, index: %d" CRLF, index); |
jk431j | 4:08979e323c6e | 118 | pc.printf(">>ID: %s" CRLF ">>Name: %s" CRLF, id, name); |
jk431j | 4:08979e323c6e | 119 | ExecuteCommand(name); |
jk431j | 4:08979e323c6e | 120 | m2xClient.markCommandProcessed(deviceId, id, on_fill_data, NULL); |
jk431j | 4:08979e323c6e | 121 | pc.printf(">>Command confirmed" CRLF, id, name); |
JMF | 0:62feed0f1fd9 | 122 | } |
JMF | 0:62feed0f1fd9 | 123 | |
jk431j | 4:08979e323c6e | 124 | |
JMF | 0:62feed0f1fd9 | 125 | int main() { |
jk431j | 4:08979e323c6e | 126 | char timestamp[25]; |
jk431j | 4:08979e323c6e | 127 | int length = 25; |
jk431j | 4:08979e323c6e | 128 | int response; |
jk431j | 4:08979e323c6e | 129 | |
jk431j | 4:08979e323c6e | 130 | pc.baud(115200); |
jk431j | 4:08979e323c6e | 131 | pc.printf("M2X StarterKit demo: initializng the network" CRLF); |
jk431j | 4:08979e323c6e | 132 | response = eth.init(); |
jk431j | 4:08979e323c6e | 133 | pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); |
jk431j | 4:08979e323c6e | 134 | if( !response ) { |
jk431j | 4:08979e323c6e | 135 | pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF); |
jk431j | 4:08979e323c6e | 136 | NVIC_SystemReset(); |
jk431j | 4:08979e323c6e | 137 | while(1); |
jk431j | 4:08979e323c6e | 138 | } |
JMF | 0:62feed0f1fd9 | 139 | |
jk431j | 4:08979e323c6e | 140 | response = eth.connect(); |
jk431j | 4:08979e323c6e | 141 | pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); |
jk431j | 4:08979e323c6e | 142 | |
jk431j | 4:08979e323c6e | 143 | pc.printf("Initialize the sensors" CRLF); |
jk431j | 4:08979e323c6e | 144 | sensors_init(); |
jk431j | 4:08979e323c6e | 145 | read_sensors(); |
jk431j | 4:08979e323c6e | 146 | |
jk431j | 4:08979e323c6e | 147 | pc.printf(WHT "initialize the M2X time service" CRLF); |
jk431j | 4:08979e323c6e | 148 | if (!m2x_status_is_success(timeService.init())) |
jk431j | 4:08979e323c6e | 149 | pc.printf("Cannot initialize time service!" CRLF); |
jk431j | 4:08979e323c6e | 150 | else { |
jk431j | 4:08979e323c6e | 151 | timeService.getTimestamp(timestamp, &length); |
jk431j | 4:08979e323c6e | 152 | pc.printf("Current timestamp: %s" CRLF, timestamp); |
jk431j | 4:08979e323c6e | 153 | } |
jk431j | 4:08979e323c6e | 154 | |
jk431j | 4:08979e323c6e | 155 | pc.printf("Query for possible commands using this device..." CRLF); |
jk431j | 4:08979e323c6e | 156 | response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); |
jk431j | 4:08979e323c6e | 157 | pc.printf("listCommands response code: %d" CRLF, response); |
JMF | 0:62feed0f1fd9 | 158 | |
jk431j | 4:08979e323c6e | 159 | while (true) { |
jk431j | 4:08979e323c6e | 160 | // read sensor values |
jk431j | 4:08979e323c6e | 161 | read_sensors(); |
JMF | 0:62feed0f1fd9 | 162 | |
jk431j | 4:08979e323c6e | 163 | // post the humidity value |
jk431j | 5:8099493f2c35 | 164 | pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.Humidity); |
jk431j | 5:8099493f2c35 | 165 | response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.Humidity); |
jk431j | 4:08979e323c6e | 166 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 167 | |
jk431j | 4:08979e323c6e | 168 | // post the temp value |
jk431j | 5:8099493f2c35 | 169 | pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 170 | response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 171 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 5:8099493f2c35 | 172 | |
jk431j | 5:8099493f2c35 | 173 | pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 174 | response = m2xClient.postDeviceUpdate(deviceId, 3, accelstreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ}); |
jk431j | 4:08979e323c6e | 175 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 176 | |
jk431j | 4:08979e323c6e | 177 | timeService.getTimestamp(timestamp, &length); |
jk431j | 4:08979e323c6e | 178 | pc.printf("%s waiting for 60 seconds... " CRLF CRLF CRLF, timestamp); |
jk431j | 4:08979e323c6e | 179 | |
jk431j | 4:08979e323c6e | 180 | for (short idx=0; idx < 6; idx++) { |
jk431j | 4:08979e323c6e | 181 | // wait 10 seconds |
jk431j | 4:08979e323c6e | 182 | delay(10 * 1000); |
JMF | 0:62feed0f1fd9 | 183 | |
jk431j | 4:08979e323c6e | 184 | // and then query fo commands |
jk431j | 4:08979e323c6e | 185 | pc.printf("Query for possible commands using this device..." CRLF); |
jk431j | 4:08979e323c6e | 186 | response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); |
jk431j | 4:08979e323c6e | 187 | pc.printf("listCommands response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 188 | } |
JMF | 0:62feed0f1fd9 | 189 | |
jk431j | 4:08979e323c6e | 190 | } |
JMF | 0:62feed0f1fd9 | 191 | } |
JMF | 0:62feed0f1fd9 | 192 |