port to cellular

Dependencies:   C027_Support LM75B M2XStreamClient jsonlite mbed mbed-rtos

Fork of m2x-demo-all by AT&T M2X Team

Committer:
mazgch
Date:
Tue May 20 14:34:10 2014 +0000
Revision:
3:dac7a2335ba5
Parent:
2:83dd0b8109ca
Child:
5:df776765d890
use restructured library

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 "LM75B.h" //I2C Temperature Sensor
jb8414 0:38a7a8cae773 6
mazgch 3:dac7a2335ba5 7 //------------------------------------------------------------------------------------
mazgch 3:dac7a2335ba5 8 // You need to configure these cellular modem / SIM parameters.
mazgch 3:dac7a2335ba5 9 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 3:dac7a2335ba5 10 //------------------------------------------------------------------------------------
mazgch 1:c73a98da0e7a 11 #include "MDM.h"
mazgch 3:dac7a2335ba5 12 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 1:c73a98da0e7a 13 #define SIMPIN NULL
mazgch 3:dac7a2335ba5 14 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 3:dac7a2335ba5 15 contract with the network operator. You can also try to look-up your settings in
mazgch 3:dac7a2335ba5 16 google: https://www.google.de/search?q=APN+list */
mazgch 1:c73a98da0e7a 17 #define APN "gprs.swisscom.ch"
mazgch 3:dac7a2335ba5 18 //! Set the user name for your APN, or NULL if not needed
mazgch 1:c73a98da0e7a 19 #define USERNAME NULL
mazgch 3:dac7a2335ba5 20 //! Set the password for your APN, or NULL if not needed
mazgch 1:c73a98da0e7a 21 #define PASSWORD NULL
mazgch 3:dac7a2335ba5 22 //------------------------------------------------------------------------------------
mazgch 1:c73a98da0e7a 23
jb8414 0:38a7a8cae773 24 char feedId[] = "<feed id>"; // Feed you want to post to
jb8414 0:38a7a8cae773 25 char m2xKey[] = "<m2x api key>"; // Your M2X access key
jb8414 0:38a7a8cae773 26 char streamName[] = "<stream name>"; // Stream you want to post to
jb8414 0:38a7a8cae773 27
jb8414 0:38a7a8cae773 28 char name[] = "<location name>"; // Name of current location of datasource
jb8414 0:38a7a8cae773 29 double latitude = 33.007872;
jb8414 0:38a7a8cae773 30 double longitude = -96.751614; // You can also read those values from a GPS
jb8414 0:38a7a8cae773 31 double elevation = 697.00;
jb8414 0:38a7a8cae773 32
jb8414 0:38a7a8cae773 33 Client client;
jb8414 0:38a7a8cae773 34 M2XStreamClient m2xClient(&client, m2xKey);
jb8414 0:38a7a8cae773 35
mazgch 1:c73a98da0e7a 36 LM75B tmp(SDA,SCL); // I2C Temperature Sensor
jb8414 0:38a7a8cae773 37
jb8414 0:38a7a8cae773 38 void on_data_point_found(const char* at, const char* value, int index, void* context) {
jb8414 0:38a7a8cae773 39 printf("Found a data point, index: %d\r\n", index);
jb8414 0:38a7a8cae773 40 printf("At: %s Value: %s\r\n", at, value);
jb8414 0:38a7a8cae773 41 }
jb8414 0:38a7a8cae773 42
jb8414 0:38a7a8cae773 43 void on_location_found(const char* name,
jb8414 0:38a7a8cae773 44 double latitude,
jb8414 0:38a7a8cae773 45 double longitude,
jb8414 0:38a7a8cae773 46 double elevation,
jb8414 0:38a7a8cae773 47 const char* timestamp,
jb8414 0:38a7a8cae773 48 int index,
jb8414 0:38a7a8cae773 49 void* context) {
jb8414 0:38a7a8cae773 50 printf("Found a location, index: %d\r\n", index);
jb8414 0:38a7a8cae773 51 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
jb8414 0:38a7a8cae773 52 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
jb8414 0:38a7a8cae773 53 }
jb8414 0:38a7a8cae773 54
jb8414 0:38a7a8cae773 55 int main() {
mazgch 1:c73a98da0e7a 56 MDMSerial mdm;
mazgch 3:dac7a2335ba5 57 //mdm.setDebug(4); // enable this for debugging issues
mazgch 3:dac7a2335ba5 58 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 1:c73a98da0e7a 59 return -1;
jb8414 0:38a7a8cae773 60
jb8414 0:38a7a8cae773 61 char amb_temp[6];
jb8414 0:38a7a8cae773 62
jb8414 0:38a7a8cae773 63 while (true) {
jb8414 0:38a7a8cae773 64
jb8414 0:38a7a8cae773 65 // read temp
jb8414 0:38a7a8cae773 66 sprintf(amb_temp, "%0.2f", tmp.read());
jb8414 0:38a7a8cae773 67
jb8414 0:38a7a8cae773 68 // post temperature
jb8414 0:38a7a8cae773 69 int response = m2xClient.post(feedId, streamName, amb_temp);
jb8414 0:38a7a8cae773 70 printf("Post response code: %d\r\n", response);
jb8414 0:38a7a8cae773 71 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 72
jb8414 0:38a7a8cae773 73 // read temperature
jb8414 0:38a7a8cae773 74 response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL);
jb8414 0:38a7a8cae773 75 printf("Fetch response code: %d\r\n", response);
jb8414 0:38a7a8cae773 76 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 77
jb8414 0:38a7a8cae773 78 // update location
jb8414 0:38a7a8cae773 79 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
jb8414 0:38a7a8cae773 80 printf("updateLocation response code: %d\r\n", response);
jb8414 0:38a7a8cae773 81 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 82
jb8414 0:38a7a8cae773 83 // read location
jb8414 0:38a7a8cae773 84 response = m2xClient.readLocation(feedId, on_location_found, NULL);
jb8414 0:38a7a8cae773 85 printf("readLocation response code: %d\r\n", response);
jb8414 0:38a7a8cae773 86 if (response == -1) while (true) ;
jb8414 0:38a7a8cae773 87
jb8414 0:38a7a8cae773 88 // wait 60 secs and then loop
jb8414 0:38a7a8cae773 89 delay(60000);
jb8414 0:38a7a8cae773 90 }
mazgch 1:c73a98da0e7a 91
mazgch 1:c73a98da0e7a 92 mdm.disconnect();
mazgch 1:c73a98da0e7a 93 mdm.powerOff();
jb8414 0:38a7a8cae773 94 }