port to cellular

Dependencies:   C027_Support M2XStreamClient jsonlite mbed-rtos mbed

Fork of Cellular_m2x-demo-all by Michael Ammann

Committer:
mazgch
Date:
Fri Dec 12 08:19:22 2014 +0000
Revision:
16:5f9a8ea108bb
Parent:
14:fa2f402e4ce8
Child:
17:cad6c0ab9811
update M2X dependency

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"
mazgch 5:df776765d890 5 #include "GPS.h" //GPS
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 7:10d3cc37fe4c 17 #define APN NULL
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
mazgch 5:df776765d890 24 char feedId[] = "81b9fcc5a8585c55ae622488f50d8de0"; // Feed you want to post to
mazgch 5:df776765d890 25 char m2xKey[] = "1e1133cd475954868602c0f7503d4f22"; // Your M2X access key
jb8414 0:38a7a8cae773 26
jb8414 0:38a7a8cae773 27 char name[] = "<location name>"; // Name of current location of datasource
jb8414 0:38a7a8cae773 28 double latitude = 33.007872;
jb8414 0:38a7a8cae773 29 double longitude = -96.751614; // You can also read those values from a GPS
jb8414 0:38a7a8cae773 30 double elevation = 697.00;
mazgch 5:df776765d890 31 bool location_valid = false;
jb8414 0:38a7a8cae773 32
jb8414 0:38a7a8cae773 33 Client client;
jb8414 0:38a7a8cae773 34 M2XStreamClient m2xClient(&client, m2xKey);
jb8414 0:38a7a8cae773 35
mazgch 16:5f9a8ea108bb 36 void on_data_point_found(const char* at, const char* value, int index, void* context, int type) {
jb8414 0:38a7a8cae773 37 printf("Found a data point, index: %d\r\n", index);
jb8414 0:38a7a8cae773 38 printf("At: %s Value: %s\r\n", at, value);
jb8414 0:38a7a8cae773 39 }
jb8414 0:38a7a8cae773 40
jb8414 0:38a7a8cae773 41 void on_location_found(const char* name,
jb8414 0:38a7a8cae773 42 double latitude,
jb8414 0:38a7a8cae773 43 double longitude,
jb8414 0:38a7a8cae773 44 double elevation,
jb8414 0:38a7a8cae773 45 const char* timestamp,
jb8414 0:38a7a8cae773 46 int index,
jb8414 0:38a7a8cae773 47 void* context) {
jb8414 0:38a7a8cae773 48 printf("Found a location, index: %d\r\n", index);
jb8414 0:38a7a8cae773 49 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
jb8414 0:38a7a8cae773 50 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
jb8414 0:38a7a8cae773 51 }
jb8414 0:38a7a8cae773 52
jb8414 0:38a7a8cae773 53 int main() {
mazgch 1:c73a98da0e7a 54 MDMSerial mdm;
mazgch 5:df776765d890 55 GPSI2C gps;
mazgch 9:08fdd1036e93 56 //mdm.setDebug(4); // enable this for debugging issues
mazgch 3:dac7a2335ba5 57 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 1:c73a98da0e7a 58 return -1;
mazgch 5:df776765d890 59
mazgch 5:df776765d890 60 char buf[256];
jb8414 0:38a7a8cae773 61
mazgch 5:df776765d890 62 Timer tmr;
mazgch 5:df776765d890 63 tmr.reset();
mazgch 5:df776765d890 64 tmr.start();
mazgch 5:df776765d890 65 while (true) {
mazgch 5:df776765d890 66 int ret;
mazgch 5:df776765d890 67 // extract the location information from the GPS NMEA data
mazgch 5:df776765d890 68 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) {
mazgch 5:df776765d890 69 int len = LENGTH(ret);
mazgch 5:df776765d890 70 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
mazgch 14:fa2f402e4ce8 71 // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
mazgch 14:fa2f402e4ce8 72 if ((buf[0] == '$') || buf[1] == 'G') {
mazgch 14:fa2f402e4ce8 73 #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
mazgch 14:fa2f402e4ce8 74 if (_CHECK_TALKER("GLL")) {
mazgch 14:fa2f402e4ce8 75 char ch;
mazgch 14:fa2f402e4ce8 76 if (gps.getNmeaAngle(2,buf,len,latitude) &&
mazgch 14:fa2f402e4ce8 77 gps.getNmeaAngle(4,buf,len,longitude) &&
mazgch 14:fa2f402e4ce8 78 gps.getNmeaItem(6,buf,len,ch) &&
mazgch 14:fa2f402e4ce8 79 gps.getNmeaItem(9,buf,len,elevation)) {
mazgch 14:fa2f402e4ce8 80 printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch);
mazgch 14:fa2f402e4ce8 81 location_valid = ch == '1' || ch == '2' || ch == '6';
mazgch 14:fa2f402e4ce8 82 }
mazgch 5:df776765d890 83 }
mazgch 5:df776765d890 84 }
mazgch 5:df776765d890 85 }
mazgch 5:df776765d890 86 }
jb8414 0:38a7a8cae773 87
mazgch 10:ba926a8f1fe6 88 if (tmr.read_ms() > 1000) {
mazgch 5:df776765d890 89 tmr.reset();
mazgch 5:df776765d890 90 tmr.start();
mazgch 8:7ca1b7a712b7 91 int response;
mazgch 5:df776765d890 92
mazgch 8:7ca1b7a712b7 93 MDMParser::NetStatus status;
mazgch 8:7ca1b7a712b7 94 if (mdm.checkNetStatus(&status)) {
mazgch 8:7ca1b7a712b7 95 sprintf(buf, "%d", status.rssi);
mazgch 16:5f9a8ea108bb 96 response = m2xClient.updateStreamValue(feedId, "rssi", buf);
mazgch 10:ba926a8f1fe6 97 printf("Put response code: %d\r\n", response);
mazgch 8:7ca1b7a712b7 98 if (response == -1) while (true) ;
mazgch 8:7ca1b7a712b7 99 }
mazgch 16:5f9a8ea108bb 100 #define READING
mazgch 6:7a1e717a0d1e 101 #ifdef READING
mazgch 13:b04452198625 102 // read signal strength
mazgch 16:5f9a8ea108bb 103 response = m2xClient.listStreamValues(feedId, "rssi", on_data_point_found, NULL);
mazgch 5:df776765d890 104 printf("Fetch response code: %d\r\n", response);
mazgch 5:df776765d890 105 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 106 #endif
mazgch 5:df776765d890 107 // update location
mazgch 5:df776765d890 108 if (location_valid) {
mazgch 5:df776765d890 109 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
mazgch 5:df776765d890 110 printf("updateLocation response code: %d\r\n", response);
mazgch 5:df776765d890 111 if (response == -1) while (true) ;
mazgch 5:df776765d890 112 }
mazgch 6:7a1e717a0d1e 113 #ifdef READING
mazgch 5:df776765d890 114 // read location
mazgch 5:df776765d890 115 response = m2xClient.readLocation(feedId, on_location_found, NULL);
mazgch 5:df776765d890 116 printf("readLocation response code: %d\r\n", response);
mazgch 5:df776765d890 117 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 118 #endif
mazgch 5:df776765d890 119 }
mazgch 5:df776765d890 120 else {
mazgch 5:df776765d890 121 delay(100);
mazgch 5:df776765d890 122 }
jb8414 0:38a7a8cae773 123 }
mazgch 1:c73a98da0e7a 124
mazgch 1:c73a98da0e7a 125 mdm.disconnect();
mazgch 1:c73a98da0e7a 126 mdm.powerOff();
jb8414 0:38a7a8cae773 127 }