Public Repository for IoT demo, leveraging the FRDM-K64F, WNC Cellular Modem, and AT&T M2X platform. Expects GPIO input from a sensor to send a discrete value to M2X.

Dependencies:   ConfigFile M2XStreamClient-JMF PinDetect SDFileSystem WNCInterface jsonlite mbed-rtos mbed

Fork of SVP_IoT_M2X_Cellular by Casey Bleeker

Committer:
JMF
Date:
Wed Sep 21 16:22:43 2016 +0000
Revision:
0:62feed0f1fd9
Child:
1:1c840717deea
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew 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
JMF 0:62feed0f1fd9 15 #define CRLF "\n\r"
JMF 0:62feed0f1fd9 16
JMF 0:62feed0f1fd9 17 char deviceId[] = "e83cdd8645ab1a7c0c480156efbf78f6"; // Device you want to post to
JMF 0:62feed0f1fd9 18 char m2xKey[] = "4d7e1da7f05c3fa4d5426419891a254d"; // Your M2X API Key or Master API Key
JMF 0:62feed0f1fd9 19
JMF 0:62feed0f1fd9 20 const char *hstreamName = "humidity";
JMF 0:62feed0f1fd9 21 const char *tstreamName = "temperature";
JMF 0:62feed0f1fd9 22 const char *streamNames[] = { tstreamName, hstreamName };
JMF 0:62feed0f1fd9 23 char name[] = "Wake Forest"; // Name of current location of datasource
JMF 0:62feed0f1fd9 24
JMF 0:62feed0f1fd9 25 int counts[] = { 2, 1 };
JMF 0:62feed0f1fd9 26 const char *ats[] = { "2016-09-09T02:05:14.692Z",
JMF 0:62feed0f1fd9 27 "2016-09-09T02:05:14.700Z",
JMF 0:62feed0f1fd9 28 "2016-09-09T02:05:14.692Z" };
JMF 0:62feed0f1fd9 29 double values[] = { 10.9, 11.2, 6.1 };
JMF 0:62feed0f1fd9 30
JMF 0:62feed0f1fd9 31 char fromTime[]= "1969-12-31T19:00:01.000Z"; // yyyy-mm-ddTHH:MM:SS.SSSZ
JMF 0:62feed0f1fd9 32 char endTime[25];
JMF 0:62feed0f1fd9 33
JMF 0:62feed0f1fd9 34 double latitude = 33.007872; // You could read these values from a GPS but
JMF 0:62feed0f1fd9 35 double longitude = -96.751614; // for now, will just hardcode them
JMF 0:62feed0f1fd9 36 double elevation = 697.00;
JMF 0:62feed0f1fd9 37
JMF 0:62feed0f1fd9 38 WNCInterface eth;
JMF 0:62feed0f1fd9 39 Client client;
JMF 0:62feed0f1fd9 40 M2XStreamClient m2xClient(&client, m2xKey);
JMF 0:62feed0f1fd9 41 TimeService timeService(&m2xClient);
JMF 0:62feed0f1fd9 42
JMF 0:62feed0f1fd9 43 void on_data_point_found(const char* at, const char* value, int index, void* context, int type) {
JMF 0:62feed0f1fd9 44 printf(">>Found a data point, index: %d type: %d" CRLF, index, type);
JMF 0:62feed0f1fd9 45 printf(">>At: %s" CRLF " Value: %s" CRLF, at, value);
JMF 0:62feed0f1fd9 46 }
JMF 0:62feed0f1fd9 47
JMF 0:62feed0f1fd9 48
JMF 0:62feed0f1fd9 49 void on_command_found(const char* id, const char* name, int index, void *context) {
JMF 0:62feed0f1fd9 50 printf(">>Found a command, index: %d" CRLF, index);
JMF 0:62feed0f1fd9 51 printf(">>ID: %s\n Name: %s" CRLF, id, name);
JMF 0:62feed0f1fd9 52 }
JMF 0:62feed0f1fd9 53
JMF 0:62feed0f1fd9 54 void on_location_found(const char* name,
JMF 0:62feed0f1fd9 55 double latitude,
JMF 0:62feed0f1fd9 56 double longitude,
JMF 0:62feed0f1fd9 57 double elevation,
JMF 0:62feed0f1fd9 58 const char* timestamp,
JMF 0:62feed0f1fd9 59 int index,
JMF 0:62feed0f1fd9 60 void* context) {
JMF 0:62feed0f1fd9 61 printf(">>Found a location, index: %d" CRLF, index);
JMF 0:62feed0f1fd9 62 printf(">>Name: %s" CRLF ">>Latitude: %lf" CRLF ">>Longitude: %lf" CRLF, name, latitude, longitude);
JMF 0:62feed0f1fd9 63 printf(">>Elevation: %lf" CRLF ">>Timestamp: %s" CRLF, elevation, timestamp);
JMF 0:62feed0f1fd9 64 }
JMF 0:62feed0f1fd9 65
JMF 0:62feed0f1fd9 66 int main() {
JMF 0:62feed0f1fd9 67 char timestamp[25];
JMF 0:62feed0f1fd9 68 int length = 25;
JMF 0:62feed0f1fd9 69 char amb_temp[6];
JMF 0:62feed0f1fd9 70 char amb_humd[6];
JMF 0:62feed0f1fd9 71 int response, cnt=1;
JMF 0:62feed0f1fd9 72 double temp=0.00; //we will just increment these 0.01 each time through the loop
JMF 0:62feed0f1fd9 73 double humid=0.00; //we will just increment these 1 each time through the loop wrapping at 100
JMF 0:62feed0f1fd9 74
JMF 0:62feed0f1fd9 75 printf("Start m2x-demo-all by initializng the network" CRLF);
JMF 0:62feed0f1fd9 76 response = eth.init();
JMF 0:62feed0f1fd9 77 printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response);
JMF 0:62feed0f1fd9 78 if( !response ) {
JMF 0:62feed0f1fd9 79 printf(" - - - - - - - ALL DONE - - - - - - - " CRLF);
JMF 0:62feed0f1fd9 80 while(1);
JMF 0:62feed0f1fd9 81 }
JMF 0:62feed0f1fd9 82
JMF 0:62feed0f1fd9 83 response = eth.connect();
JMF 0:62feed0f1fd9 84 printf("IP Address: %s " CRLF CRLF, eth.getIPAddress());
JMF 0:62feed0f1fd9 85
JMF 0:62feed0f1fd9 86 printf("initialize the M2X time service" CRLF);
JMF 0:62feed0f1fd9 87 if (!m2x_status_is_success(timeService.init()))
JMF 0:62feed0f1fd9 88 printf("Cannot initialize time service!" CRLF);
JMF 0:62feed0f1fd9 89 else {
JMF 0:62feed0f1fd9 90 timeService.getTimestamp(timestamp, &length);
JMF 0:62feed0f1fd9 91 printf("Current timestamp: %s" CRLF, timestamp);
JMF 0:62feed0f1fd9 92 strcpy(endTime,timestamp);
JMF 0:62feed0f1fd9 93 }
JMF 0:62feed0f1fd9 94
JMF 0:62feed0f1fd9 95 printf("Now delete all existing values" CRLF);
JMF 0:62feed0f1fd9 96 // Delete values
JMF 0:62feed0f1fd9 97 printf("Delete humidity values..." CRLF);
JMF 0:62feed0f1fd9 98 response = m2xClient.deleteValues(deviceId,hstreamName, fromTime, endTime);
JMF 0:62feed0f1fd9 99 printf("Delete response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 100
JMF 0:62feed0f1fd9 101 printf("Delete temp values..." CRLF);
JMF 0:62feed0f1fd9 102 response = m2xClient.deleteValues(deviceId,tstreamName, fromTime, endTime);
JMF 0:62feed0f1fd9 103 printf("Delete response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 104
JMF 0:62feed0f1fd9 105 printf("Delete location values..." CRLF);
JMF 0:62feed0f1fd9 106 response = m2xClient.deleteLocations(deviceId, fromTime, endTime);
JMF 0:62feed0f1fd9 107 printf("Delete response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 108
JMF 0:62feed0f1fd9 109 printf("Query for possible commands using this device..." CRLF);
JMF 0:62feed0f1fd9 110 response = m2xClient.listCommands(deviceId, on_command_found, NULL);
JMF 0:62feed0f1fd9 111 printf("listCommands response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 112
JMF 0:62feed0f1fd9 113 while (true) {
JMF 0:62feed0f1fd9 114 // read temp -- for now, just use a fixed temp, but will need to read the HTS221
JMF 0:62feed0f1fd9 115 // and put it into a 6 byte string formatted as "%0.2f"
JMF 0:62feed0f1fd9 116 sprintf(amb_temp,"%0.2f",temp);
JMF 0:62feed0f1fd9 117 sprintf(amb_humd,"%0.2f",humid);
JMF 0:62feed0f1fd9 118 temp += .01;
JMF 0:62feed0f1fd9 119 humid += 1.0;
JMF 0:62feed0f1fd9 120 humid = fmod(humid,100.0);
JMF 0:62feed0f1fd9 121 printf("cnt=%d\r\n",cnt++);
JMF 0:62feed0f1fd9 122 // post the humidity value
JMF 0:62feed0f1fd9 123 printf("Post updateStreamValue (humidity)..." CRLF);
JMF 0:62feed0f1fd9 124 response = m2xClient.updateStreamValue(deviceId, "humidity", humid);
JMF 0:62feed0f1fd9 125 printf("Post response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 126
JMF 0:62feed0f1fd9 127 // post the temp value
JMF 0:62feed0f1fd9 128 printf("Post updateStreamValue (temp)..." CRLF);
JMF 0:62feed0f1fd9 129 response = m2xClient.updateStreamValue(deviceId, "temperature", temp);
JMF 0:62feed0f1fd9 130 printf("Post response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 131
JMF 0:62feed0f1fd9 132 // read temperature
JMF 0:62feed0f1fd9 133 printf("listStreamValues (temp)..." CRLF);
JMF 0:62feed0f1fd9 134 response = m2xClient.listStreamValues(deviceId, tstreamName, on_data_point_found, NULL);
JMF 0:62feed0f1fd9 135 printf("listStreamValues response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 136 if (response == -1) while (true) ;
JMF 0:62feed0f1fd9 137
JMF 0:62feed0f1fd9 138 // read temperature
JMF 0:62feed0f1fd9 139 printf("listStreamValues (humid)..." CRLF);
JMF 0:62feed0f1fd9 140 response = m2xClient.listStreamValues(deviceId, hstreamName, on_data_point_found, NULL);
JMF 0:62feed0f1fd9 141 printf("listStreamValues response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 142 if (response == -1) while (true) ;
JMF 0:62feed0f1fd9 143
JMF 0:62feed0f1fd9 144 // update location
JMF 0:62feed0f1fd9 145 printf("updateLocation..." CRLF);
JMF 0:62feed0f1fd9 146 response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation);
JMF 0:62feed0f1fd9 147 printf("updateLocation response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 148 if (response == -1) while (true) ;
JMF 0:62feed0f1fd9 149
JMF 0:62feed0f1fd9 150 // read location
JMF 0:62feed0f1fd9 151 printf("readLocation..." CRLF);
JMF 0:62feed0f1fd9 152 int response = m2xClient.readLocation(deviceId, on_location_found, NULL);
JMF 0:62feed0f1fd9 153 printf("readLocation response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 154
JMF 0:62feed0f1fd9 155 printf("PostDeviceUpdates..." CRLF);
JMF 0:62feed0f1fd9 156 response = m2xClient.postDeviceUpdates(deviceId, 2, streamNames, counts, ats, values);
JMF 0:62feed0f1fd9 157 printf("Post response code: %d" CRLF, response);
JMF 0:62feed0f1fd9 158
JMF 0:62feed0f1fd9 159 timeService.getTimestamp(timestamp, &length);
JMF 0:62feed0f1fd9 160 printf("Thats all folks, got to wait 60 seconds... (%s)" CRLF CRLF CRLF, timestamp);
JMF 0:62feed0f1fd9 161
JMF 0:62feed0f1fd9 162 // wait 60 secs and then loop
JMF 0:62feed0f1fd9 163 delay(6000);
JMF 0:62feed0f1fd9 164
JMF 0:62feed0f1fd9 165
JMF 0:62feed0f1fd9 166 }
JMF 0:62feed0f1fd9 167 }
JMF 0:62feed0f1fd9 168