Example program to connect to the internet via Ublox Cellular Shield and post MEMS sensor readings to M2X.

Dependencies:   C027_Support M2XStreamClient Nucleo_Sensor_Shield jsonlite mbed-rtos mbed

Fork of Cellular_m2x-demo-all by u-blox

Committer:
mazgch
Date:
Wed Oct 01 09:23:57 2014 +0000
Revision:
13:b04452198625
Parent:
10:ba926a8f1fe6
Child:
14:b756e26ac6bf
update 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
jb8414 0:38a7a8cae773 36 void on_data_point_found(const char* at, const char* value, int index, void* context) {
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 5:df776765d890 71 if (!strncmp("$GPGGA", buf, 6)) {
mazgch 5:df776765d890 72 char ch;
mazgch 5:df776765d890 73 if (gps.getNmeaAngle(2,buf,len,latitude) &&
mazgch 5:df776765d890 74 gps.getNmeaAngle(4,buf,len,longitude) &&
mazgch 5:df776765d890 75 gps.getNmeaItem(6,buf,len,ch) &&
mazgch 5:df776765d890 76 gps.getNmeaItem(9,buf,len,elevation)) {
mazgch 5:df776765d890 77 printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch);
mazgch 5:df776765d890 78 location_valid = ch == '1' || ch == '2' || ch == '6';
mazgch 5:df776765d890 79 }
mazgch 5:df776765d890 80 }
mazgch 5:df776765d890 81 }
mazgch 5:df776765d890 82 }
jb8414 0:38a7a8cae773 83
mazgch 10:ba926a8f1fe6 84 if (tmr.read_ms() > 1000) {
mazgch 5:df776765d890 85 tmr.reset();
mazgch 5:df776765d890 86 tmr.start();
mazgch 8:7ca1b7a712b7 87 int response;
mazgch 5:df776765d890 88
mazgch 8:7ca1b7a712b7 89 MDMParser::NetStatus status;
mazgch 8:7ca1b7a712b7 90 if (mdm.checkNetStatus(&status)) {
mazgch 8:7ca1b7a712b7 91 sprintf(buf, "%d", status.rssi);
mazgch 10:ba926a8f1fe6 92 response = m2xClient.put(feedId, "rssi", buf);
mazgch 10:ba926a8f1fe6 93 printf("Put response code: %d\r\n", response);
mazgch 8:7ca1b7a712b7 94 if (response == -1) while (true) ;
mazgch 8:7ca1b7a712b7 95 }
mazgch 6:7a1e717a0d1e 96 //#define READING
mazgch 6:7a1e717a0d1e 97 #ifdef READING
mazgch 13:b04452198625 98 // read signal strength
mazgch 8:7ca1b7a712b7 99 response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL);
mazgch 5:df776765d890 100 printf("Fetch response code: %d\r\n", response);
mazgch 5:df776765d890 101 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 102 #endif
mazgch 5:df776765d890 103 // update location
mazgch 5:df776765d890 104 if (location_valid) {
mazgch 5:df776765d890 105 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
mazgch 5:df776765d890 106 printf("updateLocation response code: %d\r\n", response);
mazgch 5:df776765d890 107 if (response == -1) while (true) ;
mazgch 5:df776765d890 108 }
mazgch 6:7a1e717a0d1e 109 #ifdef READING
mazgch 5:df776765d890 110 // read location
mazgch 5:df776765d890 111 response = m2xClient.readLocation(feedId, on_location_found, NULL);
mazgch 5:df776765d890 112 printf("readLocation response code: %d\r\n", response);
mazgch 5:df776765d890 113 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 114 #endif
mazgch 5:df776765d890 115 }
mazgch 5:df776765d890 116 else {
mazgch 5:df776765d890 117 delay(100);
mazgch 5:df776765d890 118 }
jb8414 0:38a7a8cae773 119 }
mazgch 1:c73a98da0e7a 120
mazgch 1:c73a98da0e7a 121 mdm.disconnect();
mazgch 1:c73a98da0e7a 122 mdm.powerOff();
jb8414 0:38a7a8cae773 123 }