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@5:8099493f2c35, 2017-04-05 (annotated)
- Committer:
- jk431j
- Date:
- Wed Apr 05 16:37:18 2017 +0000
- Revision:
- 5:8099493f2c35
- Parent:
- 4:08979e323c6e
- Child:
- 6:731f412e6571
Added sensor reading and M2X command handling.
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 | 4:08979e323c6e | 38 | K64F_Sensors_t SENSOR_DATA = |
jk431j | 4:08979e323c6e | 39 | { |
jk431j | 5:8099493f2c35 | 40 | .Temperature = 0, |
jk431j | 5:8099493f2c35 | 41 | .Humidity = 0, |
jk431j | 5:8099493f2c35 | 42 | .AccelX = 0, |
jk431j | 5:8099493f2c35 | 43 | .AccelY = 0, |
jk431j | 5:8099493f2c35 | 44 | .AccelZ = 0, |
jk431j | 5:8099493f2c35 | 45 | .MagnetometerX = 0, |
jk431j | 5:8099493f2c35 | 46 | .MagnetometerY = 0, |
jk431j | 5:8099493f2c35 | 47 | .MagnetometerZ = 0, |
jk431j | 5:8099493f2c35 | 48 | .AmbientLightVis = 0, |
jk431j | 5:8099493f2c35 | 49 | .AmbientLightIr = 0, |
jk431j | 5:8099493f2c35 | 50 | .UVindex = 0, |
jk431j | 5:8099493f2c35 | 51 | .Proximity = 0, |
jk431j | 5:8099493f2c35 | 52 | .Temperature_Si7020 = 0, |
jk431j | 5:8099493f2c35 | 53 | .Humidity_Si7020 = 0, |
jk431j | 4:08979e323c6e | 54 | }; |
jk431j | 4:08979e323c6e | 55 | |
jk431j | 4:08979e323c6e | 56 | //******************************************************************************************************************************************** |
jk431j | 4:08979e323c6e | 57 | //* Set the RGB LED's Color |
jk431j | 4:08979e323c6e | 58 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
jk431j | 4:08979e323c6e | 59 | //******************************************************************************************************************************************** |
jk431j | 4:08979e323c6e | 60 | void SetLedColor(unsigned char ucColor) |
jk431j | 4:08979e323c6e | 61 | { |
jk431j | 4:08979e323c6e | 62 | //Note that when an LED is on, you write a 0 to it: |
jk431j | 4:08979e323c6e | 63 | led_red = !(ucColor & 0x1); //bit 0 |
jk431j | 4:08979e323c6e | 64 | led_green = !(ucColor & 0x2); //bit 1 |
jk431j | 4:08979e323c6e | 65 | led_blue = !(ucColor & 0x4); //bit 2 |
jk431j | 4:08979e323c6e | 66 | } //SetLedColor() |
jk431j | 4:08979e323c6e | 67 | |
JMF | 0:62feed0f1fd9 | 68 | |
jk431j | 4:08979e323c6e | 69 | bool ExecuteCommand(const char* Command) |
jk431j | 4:08979e323c6e | 70 | { |
jk431j | 4:08979e323c6e | 71 | char cLedColor = *Command; |
jk431j | 4:08979e323c6e | 72 | switch(cLedColor) |
jk431j | 4:08979e323c6e | 73 | { |
jk431j | 4:08979e323c6e | 74 | case 'O': |
jk431j | 4:08979e323c6e | 75 | { //Off |
jk431j | 4:08979e323c6e | 76 | SetLedColor(0); |
jk431j | 4:08979e323c6e | 77 | break; |
jk431j | 4:08979e323c6e | 78 | } |
jk431j | 4:08979e323c6e | 79 | case 'R': |
jk431j | 4:08979e323c6e | 80 | { //Red |
jk431j | 4:08979e323c6e | 81 | SetLedColor(1); |
jk431j | 4:08979e323c6e | 82 | break; |
jk431j | 4:08979e323c6e | 83 | } |
jk431j | 4:08979e323c6e | 84 | case 'G': |
jk431j | 4:08979e323c6e | 85 | { //Green |
jk431j | 4:08979e323c6e | 86 | SetLedColor(2); |
jk431j | 4:08979e323c6e | 87 | break; |
jk431j | 4:08979e323c6e | 88 | } |
jk431j | 4:08979e323c6e | 89 | case 'Y': |
jk431j | 4:08979e323c6e | 90 | { //Yellow |
jk431j | 4:08979e323c6e | 91 | SetLedColor(3); |
jk431j | 4:08979e323c6e | 92 | break; |
jk431j | 4:08979e323c6e | 93 | } |
jk431j | 4:08979e323c6e | 94 | case 'B': |
jk431j | 4:08979e323c6e | 95 | { //Blue |
jk431j | 4:08979e323c6e | 96 | SetLedColor(4); |
jk431j | 4:08979e323c6e | 97 | break; |
jk431j | 4:08979e323c6e | 98 | } |
jk431j | 4:08979e323c6e | 99 | case 'M': |
jk431j | 4:08979e323c6e | 100 | { //Magenta |
jk431j | 4:08979e323c6e | 101 | SetLedColor(5); |
jk431j | 4:08979e323c6e | 102 | break; |
jk431j | 4:08979e323c6e | 103 | } |
jk431j | 4:08979e323c6e | 104 | case 'T': |
jk431j | 4:08979e323c6e | 105 | { //Turquoise |
jk431j | 4:08979e323c6e | 106 | SetLedColor(6); |
jk431j | 4:08979e323c6e | 107 | break; |
jk431j | 4:08979e323c6e | 108 | } |
jk431j | 4:08979e323c6e | 109 | case 'W': |
jk431j | 4:08979e323c6e | 110 | { //White |
jk431j | 4:08979e323c6e | 111 | SetLedColor(7); |
jk431j | 4:08979e323c6e | 112 | break; |
jk431j | 4:08979e323c6e | 113 | } |
jk431j | 4:08979e323c6e | 114 | default: |
jk431j | 4:08979e323c6e | 115 | { |
jk431j | 4:08979e323c6e | 116 | return false; |
jk431j | 4:08979e323c6e | 117 | } |
jk431j | 4:08979e323c6e | 118 | } //switch(cLedColor) |
jk431j | 4:08979e323c6e | 119 | return true; |
JMF | 0:62feed0f1fd9 | 120 | } |
JMF | 0:62feed0f1fd9 | 121 | |
JMF | 0:62feed0f1fd9 | 122 | |
jk431j | 4:08979e323c6e | 123 | void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { |
jk431j | 4:08979e323c6e | 124 | pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type); |
jk431j | 4:08979e323c6e | 125 | pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value); |
jk431j | 4:08979e323c6e | 126 | } |
jk431j | 4:08979e323c6e | 127 | |
jk431j | 4:08979e323c6e | 128 | void on_fill_data(Print *print, void *context) { |
jk431j | 4:08979e323c6e | 129 | // no data to fill |
JMF | 0:62feed0f1fd9 | 130 | } |
JMF | 0:62feed0f1fd9 | 131 | |
jk431j | 4:08979e323c6e | 132 | void on_command_found(const char* id, const char* name, int index, void *context) { |
jk431j | 4:08979e323c6e | 133 | pc.printf(">>Found a command, index: %d" CRLF, index); |
jk431j | 4:08979e323c6e | 134 | pc.printf(">>ID: %s" CRLF ">>Name: %s" CRLF, id, name); |
jk431j | 4:08979e323c6e | 135 | ExecuteCommand(name); |
jk431j | 4:08979e323c6e | 136 | m2xClient.markCommandProcessed(deviceId, id, on_fill_data, NULL); |
jk431j | 4:08979e323c6e | 137 | pc.printf(">>Command confirmed" CRLF, id, name); |
JMF | 0:62feed0f1fd9 | 138 | } |
JMF | 0:62feed0f1fd9 | 139 | |
jk431j | 4:08979e323c6e | 140 | |
JMF | 0:62feed0f1fd9 | 141 | int main() { |
jk431j | 4:08979e323c6e | 142 | char timestamp[25]; |
jk431j | 4:08979e323c6e | 143 | int length = 25; |
jk431j | 4:08979e323c6e | 144 | int response; |
jk431j | 4:08979e323c6e | 145 | |
jk431j | 4:08979e323c6e | 146 | pc.baud(115200); |
jk431j | 4:08979e323c6e | 147 | pc.printf("M2X StarterKit demo: initializng the network" CRLF); |
jk431j | 4:08979e323c6e | 148 | response = eth.init(); |
jk431j | 4:08979e323c6e | 149 | pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); |
jk431j | 4:08979e323c6e | 150 | if( !response ) { |
jk431j | 4:08979e323c6e | 151 | pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF); |
jk431j | 4:08979e323c6e | 152 | NVIC_SystemReset(); |
jk431j | 4:08979e323c6e | 153 | while(1); |
jk431j | 4:08979e323c6e | 154 | } |
JMF | 0:62feed0f1fd9 | 155 | |
jk431j | 4:08979e323c6e | 156 | response = eth.connect(); |
jk431j | 4:08979e323c6e | 157 | pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); |
jk431j | 4:08979e323c6e | 158 | |
jk431j | 4:08979e323c6e | 159 | pc.printf("Initialize the sensors" CRLF); |
jk431j | 4:08979e323c6e | 160 | sensors_init(); |
jk431j | 4:08979e323c6e | 161 | read_sensors(); |
jk431j | 4:08979e323c6e | 162 | |
jk431j | 4:08979e323c6e | 163 | pc.printf(WHT "initialize the M2X time service" CRLF); |
jk431j | 4:08979e323c6e | 164 | if (!m2x_status_is_success(timeService.init())) |
jk431j | 4:08979e323c6e | 165 | pc.printf("Cannot initialize time service!" CRLF); |
jk431j | 4:08979e323c6e | 166 | else { |
jk431j | 4:08979e323c6e | 167 | timeService.getTimestamp(timestamp, &length); |
jk431j | 4:08979e323c6e | 168 | pc.printf("Current timestamp: %s" CRLF, timestamp); |
jk431j | 4:08979e323c6e | 169 | } |
jk431j | 4:08979e323c6e | 170 | |
jk431j | 4:08979e323c6e | 171 | pc.printf("Query for possible commands using this device..." CRLF); |
jk431j | 4:08979e323c6e | 172 | response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); |
jk431j | 4:08979e323c6e | 173 | pc.printf("listCommands response code: %d" CRLF, response); |
JMF | 0:62feed0f1fd9 | 174 | |
jk431j | 4:08979e323c6e | 175 | while (true) { |
jk431j | 4:08979e323c6e | 176 | // read sensor values |
jk431j | 4:08979e323c6e | 177 | read_sensors(); |
JMF | 0:62feed0f1fd9 | 178 | |
jk431j | 4:08979e323c6e | 179 | // post the humidity value |
jk431j | 5:8099493f2c35 | 180 | pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.Humidity); |
jk431j | 5:8099493f2c35 | 181 | response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.Humidity); |
jk431j | 4:08979e323c6e | 182 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 183 | |
jk431j | 4:08979e323c6e | 184 | // post the temp value |
jk431j | 5:8099493f2c35 | 185 | pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 186 | response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 187 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 5:8099493f2c35 | 188 | |
jk431j | 5:8099493f2c35 | 189 | pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature); |
jk431j | 5:8099493f2c35 | 190 | response = m2xClient.postDeviceUpdate(deviceId, 3, accelstreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ}); |
jk431j | 4:08979e323c6e | 191 | pc.printf("Post response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 192 | |
jk431j | 4:08979e323c6e | 193 | timeService.getTimestamp(timestamp, &length); |
jk431j | 4:08979e323c6e | 194 | pc.printf("%s waiting for 60 seconds... " CRLF CRLF CRLF, timestamp); |
jk431j | 4:08979e323c6e | 195 | |
jk431j | 4:08979e323c6e | 196 | for (short idx=0; idx < 6; idx++) { |
jk431j | 4:08979e323c6e | 197 | // wait 10 seconds |
jk431j | 4:08979e323c6e | 198 | delay(10 * 1000); |
JMF | 0:62feed0f1fd9 | 199 | |
jk431j | 4:08979e323c6e | 200 | // and then query fo commands |
jk431j | 4:08979e323c6e | 201 | pc.printf("Query for possible commands using this device..." CRLF); |
jk431j | 4:08979e323c6e | 202 | response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); |
jk431j | 4:08979e323c6e | 203 | pc.printf("listCommands response code: %d" CRLF, response); |
jk431j | 4:08979e323c6e | 204 | } |
JMF | 0:62feed0f1fd9 | 205 | |
jk431j | 4:08979e323c6e | 206 | } |
JMF | 0:62feed0f1fd9 | 207 | } |
JMF | 0:62feed0f1fd9 | 208 |