m2x-demo-all will read the temperature from the Application Board, then SEND the value to the specified M2X feed/stream, RETRIEVE the value back, UPDATE the location of the feed, and then RETRIEVE the location. The script will wait 30 seconds and then loop. Make sure to update the feedId and m2xKey values with values for your account.

Dependencies:   EthernetInterface LM75B M2XStreamClient jsonlite mbed-rtos mbed

Committer:
jb8414
Date:
Fri Feb 21 21:12:15 2014 +0000
Revision:
0:38a7a8cae773
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jb8414 0:38a7a8cae773 1 #include <jsonlite.h>
jb8414 0:38a7a8cae773 2 #include "M2XStreamClient.h"
jb8414 0:38a7a8cae773 3
jb8414 0:38a7a8cae773 4 #include "mbed.h"
jb8414 0:38a7a8cae773 5 #include "EthernetInterface.h"
jb8414 0:38a7a8cae773 6 #include "LM75B.h" //I2C Temperature Sensor
jb8414 0:38a7a8cae773 7
jb8414 0:38a7a8cae773 8 char feedId[] = "<feed id>"; // Feed you want to post to
jb8414 0:38a7a8cae773 9 char m2xKey[] = "<m2x api key>"; // Your M2X access key
jb8414 0:38a7a8cae773 10 char streamName[] = "<stream name>"; // Stream you want to post to
jb8414 0:38a7a8cae773 11
jb8414 0:38a7a8cae773 12 char name[] = "<location name>"; // Name of current location of datasource
jb8414 0:38a7a8cae773 13 double latitude = 33.007872;
jb8414 0:38a7a8cae773 14 double longitude = -96.751614; // You can also read those values from a GPS
jb8414 0:38a7a8cae773 15 double elevation = 697.00;
jb8414 0:38a7a8cae773 16
jb8414 0:38a7a8cae773 17 Client client;
jb8414 0:38a7a8cae773 18 M2XStreamClient m2xClient(&client, m2xKey);
jb8414 0:38a7a8cae773 19
jb8414 0:38a7a8cae773 20 EthernetInterface eth;
jb8414 0:38a7a8cae773 21 LM75B tmp(p28,p27); // I2C Temperature Sensor
jb8414 0:38a7a8cae773 22
jb8414 0:38a7a8cae773 23 void on_data_point_found(const char* at, const char* value, int index, void* context) {
jb8414 0:38a7a8cae773 24 printf("Found a data point, index: %d\r\n", index);
jb8414 0:38a7a8cae773 25 printf("At: %s Value: %s\r\n", at, value);
jb8414 0:38a7a8cae773 26 }
jb8414 0:38a7a8cae773 27
jb8414 0:38a7a8cae773 28 void on_location_found(const char* name,
jb8414 0:38a7a8cae773 29 double latitude,
jb8414 0:38a7a8cae773 30 double longitude,
jb8414 0:38a7a8cae773 31 double elevation,
jb8414 0:38a7a8cae773 32 const char* timestamp,
jb8414 0:38a7a8cae773 33 int index,
jb8414 0:38a7a8cae773 34 void* context) {
jb8414 0:38a7a8cae773 35 printf("Found a location, index: %d\r\n", index);
jb8414 0:38a7a8cae773 36 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
jb8414 0:38a7a8cae773 37 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
jb8414 0:38a7a8cae773 38 }
jb8414 0:38a7a8cae773 39
jb8414 0:38a7a8cae773 40 int main() {
jb8414 0:38a7a8cae773 41 eth.init();
jb8414 0:38a7a8cae773 42 eth.connect();
jb8414 0:38a7a8cae773 43 printf("IP Address: %s\r\n", eth.getIPAddress());
jb8414 0:38a7a8cae773 44
jb8414 0:38a7a8cae773 45 char amb_temp[6];
jb8414 0:38a7a8cae773 46
jb8414 0:38a7a8cae773 47 while (true) {
jb8414 0:38a7a8cae773 48
jb8414 0:38a7a8cae773 49 // read temp
jb8414 0:38a7a8cae773 50 sprintf(amb_temp, "%0.2f", tmp.read());
jb8414 0:38a7a8cae773 51
jb8414 0:38a7a8cae773 52 // post temperature
jb8414 0:38a7a8cae773 53 int response = m2xClient.post(feedId, streamName, amb_temp);
jb8414 0:38a7a8cae773 54 printf("Post response code: %d\r\n", response);
jb8414 0:38a7a8cae773 55 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 56
jb8414 0:38a7a8cae773 57 // read temperature
jb8414 0:38a7a8cae773 58 response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL);
jb8414 0:38a7a8cae773 59 printf("Fetch response code: %d\r\n", response);
jb8414 0:38a7a8cae773 60 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 61
jb8414 0:38a7a8cae773 62 // update location
jb8414 0:38a7a8cae773 63 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
jb8414 0:38a7a8cae773 64 printf("updateLocation response code: %d\r\n", response);
jb8414 0:38a7a8cae773 65 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 66
jb8414 0:38a7a8cae773 67 // read location
jb8414 0:38a7a8cae773 68 response = m2xClient.readLocation(feedId, on_location_found, NULL);
jb8414 0:38a7a8cae773 69 printf("readLocation response code: %d\r\n", response);
jb8414 0:38a7a8cae773 70 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 71
jb8414 0:38a7a8cae773 72 // wait 60 secs and then loop
jb8414 0:38a7a8cae773 73 delay(60000);
jb8414 0:38a7a8cae773 74 }
jb8414 0:38a7a8cae773 75 }