Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: M2XStreamClient-JMF WNCInterface jsonlite mbed-rtos mbed
main.cpp
00001 // 00002 // This file contains an example implementation of M2X using the HTTP interface as the underlying 00003 // transport. 00004 // 00005 00006 #include "mbed.h" 00007 #include "WNCInterface.h" 00008 00009 #define MBED_PLATFORM 00010 #define M2X_ENABLE_READER 00011 00012 #include <jsonlite.h> 00013 #include "M2XStreamClient.h" 00014 00015 #define CRLF "\n\r" 00016 00017 char deviceId[] = "e83cdd8645ab1a7c0c480156efbf78f6"; // Device you want to post to 00018 char m2xKey[] = "4d7e1da7f05c3fa4d5426419891a254d"; // Your M2X API Key or Master API Key 00019 00020 const char *hstreamName = "humidity"; 00021 const char *tstreamName = "temperature"; 00022 const char *streamNames[] = { tstreamName, hstreamName }; 00023 char name[] = "Wake Forest"; // Name of current location of datasource 00024 00025 int counts[] = { 2, 1 }; 00026 const char *ats[] = { "2016-09-09T02:05:14.692Z", 00027 "2016-09-09T02:05:14.700Z", 00028 "2016-09-09T02:05:14.692Z" }; 00029 double values[] = { 10.9, 11.2, 6.1 }; 00030 00031 char fromTime[]= "1969-12-31T19:00:01.000Z"; // yyyy-mm-ddTHH:MM:SS.SSSZ 00032 char endTime[25]; 00033 00034 double latitude = 33.007872; // You could read these values from a GPS but 00035 double longitude = -96.751614; // for now, will just hardcode them 00036 double elevation = 697.00; 00037 00038 WNCInterface eth; 00039 Client client; 00040 M2XStreamClient m2xClient(&client, m2xKey); 00041 TimeService timeService(&m2xClient); 00042 MODSERIAL pc(USBTX,USBRX,256,256); 00043 00044 void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { 00045 pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type); 00046 pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value); 00047 } 00048 00049 00050 void on_command_found(const char* id, const char* name, int index, void *context) { 00051 pc.printf(">>Found a command, index: %d" CRLF, index); 00052 pc.printf(">>ID: %s\n Name: %s" CRLF, id, name); 00053 } 00054 00055 void on_location_found(const char* name, 00056 double latitude, 00057 double longitude, 00058 double elevation, 00059 const char* timestamp, 00060 int index, 00061 void* context) { 00062 pc.printf(">>Found a location, index: %d" CRLF, index); 00063 pc.printf(">>Name: %s" CRLF ">>Latitude: %lf" CRLF ">>Longitude: %lf" CRLF, name, latitude, longitude); 00064 pc.printf(">>Elevation: %lf" CRLF ">>Timestamp: %s" CRLF, elevation, timestamp); 00065 } 00066 00067 int main() { 00068 char timestamp[25]; 00069 int length = 25; 00070 char amb_temp[6]; 00071 char amb_humd[6]; 00072 int response, cnt=1; 00073 double temp=0.00; //we will just increment these 0.01 each time through the loop 00074 double humid=0.00; //we will just increment these 1 each time through the loop wrapping at 100 00075 00076 pc.baud(115200); 00077 pc.printf("Start m2x-demo-all by initializng the network" CRLF); 00078 response = eth.init(); 00079 pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); 00080 if( !response ) { 00081 pc.printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); 00082 while(1); 00083 } 00084 00085 response = eth.connect(); 00086 pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); 00087 00088 pc.printf("initialize the M2X time service" CRLF); 00089 if (!m2x_status_is_success(timeService.init())) 00090 pc.printf("Cannot initialize time service!" CRLF); 00091 else { 00092 timeService.getTimestamp(timestamp, &length); 00093 pc.printf("Current timestamp: %s" CRLF, timestamp); 00094 strcpy(endTime,timestamp); 00095 } 00096 00097 pc.printf("Now delete all existing values" CRLF); 00098 // Delete values 00099 pc.printf("Delete humidity values..." CRLF); 00100 response = m2xClient.deleteValues(deviceId,hstreamName, fromTime, endTime); 00101 pc.printf("Delete response code: %d" CRLF, response); 00102 00103 pc.printf("Delete temp values..." CRLF); 00104 response = m2xClient.deleteValues(deviceId,tstreamName, fromTime, endTime); 00105 pc.printf("Delete response code: %d" CRLF, response); 00106 00107 pc.printf("Delete location values..." CRLF); 00108 response = m2xClient.deleteLocations(deviceId, fromTime, endTime); 00109 pc.printf("Delete response code: %d" CRLF, response); 00110 00111 pc.printf("Query for possible commands using this device..." CRLF); 00112 response = m2xClient.listCommands(deviceId, on_command_found, NULL); 00113 pc.printf("listCommands response code: %d" CRLF, response); 00114 00115 while (true) { 00116 // read temp -- for now, just use a fixed temp, but will need to read the HTS221 00117 // and put it into a 6 byte string formatted as "%0.2f" 00118 sprintf(amb_temp,"%0.2f",temp); 00119 sprintf(amb_humd,"%0.2f",humid); 00120 temp += .01; 00121 humid += 1.0; 00122 humid = fmod(humid,100.0); 00123 pc.printf("cnt=%d\r\n",cnt++); 00124 // post the humidity value 00125 pc.printf("Post updateStreamValue (humidity)..." CRLF); 00126 response = m2xClient.updateStreamValue(deviceId, "humidity", humid); 00127 pc.printf("Post response code: %d" CRLF, response); 00128 00129 // post the temp value 00130 pc.printf("Post updateStreamValue (temp)..." CRLF); 00131 response = m2xClient.updateStreamValue(deviceId, "temperature", temp); 00132 pc.printf("Post response code: %d" CRLF, response); 00133 00134 // read temperature 00135 pc.printf("listStreamValues (temp)..." CRLF); 00136 response = m2xClient.listStreamValues(deviceId, tstreamName, on_data_point_found, NULL); 00137 pc.printf("listStreamValues response code: %d" CRLF, response); 00138 if (response == -1) while (true) ; 00139 00140 // read temperature 00141 pc.printf("listStreamValues (humid)..." CRLF); 00142 response = m2xClient.listStreamValues(deviceId, hstreamName, on_data_point_found, NULL); 00143 pc.printf("listStreamValues response code: %d" CRLF, response); 00144 if (response == -1) while (true) ; 00145 00146 // update location 00147 pc.printf("updateLocation..." CRLF); 00148 response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation); 00149 pc.printf("updateLocation response code: %d" CRLF, response); 00150 if (response == -1) while (true) ; 00151 00152 // read location 00153 pc.printf("readLocation..." CRLF); 00154 int response = m2xClient.readLocation(deviceId, on_location_found, NULL); 00155 pc.printf("readLocation response code: %d" CRLF, response); 00156 00157 pc.printf("PostDeviceUpdates..." CRLF); 00158 response = m2xClient.postDeviceUpdates(deviceId, 2, streamNames, counts, ats, values); 00159 pc.printf("Post response code: %d" CRLF, response); 00160 00161 timeService.getTimestamp(timestamp, &length); 00162 pc.printf("Thats all folks, got to wait 60 seconds... (%s)" CRLF CRLF CRLF, timestamp); 00163 00164 // wait 60 secs and then loop 00165 delay(6000); 00166 00167 00168 } 00169 } 00170
Generated on Tue Jul 12 2022 15:50:50 by
1.7.2