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:
joe_tijerina
Date:
Thu Dec 11 22:33:16 2014 +0000
Revision:
14:b756e26ac6bf
Parent:
13:b04452198625
Child:
16:fab4ed40e6ea
Added MEMS sensor reading and postings to M2X

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"
joe_tijerina 14:b756e26ac6bf 3 #include "x_cube_mems.h"
jb8414 0:38a7a8cae773 4
jb8414 0:38a7a8cae773 5 #include "mbed.h"
mazgch 5:df776765d890 6 #include "GPS.h" //GPS
jb8414 0:38a7a8cae773 7
mazgch 3:dac7a2335ba5 8 //------------------------------------------------------------------------------------
mazgch 3:dac7a2335ba5 9 // You need to configure these cellular modem / SIM parameters.
mazgch 3:dac7a2335ba5 10 // These parameters are ignored for LISA-C200 variants and can be left NULL.
mazgch 3:dac7a2335ba5 11 //------------------------------------------------------------------------------------
mazgch 1:c73a98da0e7a 12 #include "MDM.h"
mazgch 3:dac7a2335ba5 13 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
mazgch 1:c73a98da0e7a 14 #define SIMPIN NULL
mazgch 3:dac7a2335ba5 15 /*! The APN of your network operator SIM, sometimes it is "internet" check your
mazgch 3:dac7a2335ba5 16 contract with the network operator. You can also try to look-up your settings in
mazgch 3:dac7a2335ba5 17 google: https://www.google.de/search?q=APN+list */
mazgch 7:10d3cc37fe4c 18 #define APN NULL
mazgch 3:dac7a2335ba5 19 //! Set the user name for your APN, or NULL if not needed
mazgch 1:c73a98da0e7a 20 #define USERNAME NULL
mazgch 3:dac7a2335ba5 21 //! Set the password for your APN, or NULL if not needed
mazgch 1:c73a98da0e7a 22 #define PASSWORD NULL
mazgch 3:dac7a2335ba5 23 //------------------------------------------------------------------------------------
mazgch 1:c73a98da0e7a 24
joe_tijerina 14:b756e26ac6bf 25 char feedId[] = "0c76bf26149c9c01d8eec11553d1a6f2"; // Feed you want to post to
joe_tijerina 14:b756e26ac6bf 26 char m2xKey[] = "76ee6b8414970d7ade8c75829d0cf879"; // Your M2X access key
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;
mazgch 5:df776765d890 32 bool location_valid = false;
joe_tijerina 14:b756e26ac6bf 33 volatile float TEMPERATURE_Value, TEMPERATURE_Value_f;
joe_tijerina 14:b756e26ac6bf 34 volatile float HUMIDITY_Value;
joe_tijerina 14:b756e26ac6bf 35 volatile float PRESSURE_Value;
joe_tijerina 14:b756e26ac6bf 36 volatile AxesRaw_TypeDef MAG_Value;
joe_tijerina 14:b756e26ac6bf 37 volatile AxesRaw_TypeDef ACC_Value;
joe_tijerina 14:b756e26ac6bf 38 volatile AxesRaw_TypeDef GYR_Value;
jb8414 0:38a7a8cae773 39 Client client;
jb8414 0:38a7a8cae773 40 M2XStreamClient m2xClient(&client, m2xKey);
jb8414 0:38a7a8cae773 41
jb8414 0:38a7a8cae773 42 void on_data_point_found(const char* at, const char* value, int index, void* context) {
jb8414 0:38a7a8cae773 43 printf("Found a data point, index: %d\r\n", index);
jb8414 0:38a7a8cae773 44 printf("At: %s Value: %s\r\n", at, value);
jb8414 0:38a7a8cae773 45 }
jb8414 0:38a7a8cae773 46
jb8414 0:38a7a8cae773 47 void on_location_found(const char* name,
jb8414 0:38a7a8cae773 48 double latitude,
jb8414 0:38a7a8cae773 49 double longitude,
jb8414 0:38a7a8cae773 50 double elevation,
jb8414 0:38a7a8cae773 51 const char* timestamp,
jb8414 0:38a7a8cae773 52 int index,
jb8414 0:38a7a8cae773 53 void* context) {
jb8414 0:38a7a8cae773 54 printf("Found a location, index: %d\r\n", index);
jb8414 0:38a7a8cae773 55 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
jb8414 0:38a7a8cae773 56 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
jb8414 0:38a7a8cae773 57 }
jb8414 0:38a7a8cae773 58
jb8414 0:38a7a8cae773 59 int main() {
joe_tijerina 14:b756e26ac6bf 60 uint8_t hts221_id;
joe_tijerina 14:b756e26ac6bf 61 MDMSerial mdm(D8,D2);
mazgch 5:df776765d890 62 GPSI2C gps;
mazgch 9:08fdd1036e93 63 //mdm.setDebug(4); // enable this for debugging issues
mazgch 3:dac7a2335ba5 64 if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
mazgch 1:c73a98da0e7a 65 return -1;
mazgch 5:df776765d890 66
mazgch 5:df776765d890 67 char buf[256];
jb8414 0:38a7a8cae773 68
joe_tijerina 14:b756e26ac6bf 69 printf("M2X MEMS U-Blox Cellular Demo \r\n");
joe_tijerina 14:b756e26ac6bf 70
joe_tijerina 14:b756e26ac6bf 71 static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
joe_tijerina 14:b756e26ac6bf 72
joe_tijerina 14:b756e26ac6bf 73 hts221_id = mems_expansion_board->hts221.ReadID();
joe_tijerina 14:b756e26ac6bf 74 printf("HTS221_ID = 0x%x\n\t\r", hts221_id);
joe_tijerina 14:b756e26ac6bf 75
mazgch 5:df776765d890 76 Timer tmr;
mazgch 5:df776765d890 77 tmr.reset();
mazgch 5:df776765d890 78 tmr.start();
mazgch 5:df776765d890 79 while (true) {
mazgch 5:df776765d890 80 int ret;
mazgch 5:df776765d890 81 // extract the location information from the GPS NMEA data
mazgch 5:df776765d890 82 while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) {
mazgch 5:df776765d890 83 int len = LENGTH(ret);
mazgch 5:df776765d890 84 if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
mazgch 5:df776765d890 85 if (!strncmp("$GPGGA", buf, 6)) {
mazgch 5:df776765d890 86 char ch;
mazgch 5:df776765d890 87 if (gps.getNmeaAngle(2,buf,len,latitude) &&
mazgch 5:df776765d890 88 gps.getNmeaAngle(4,buf,len,longitude) &&
mazgch 5:df776765d890 89 gps.getNmeaItem(6,buf,len,ch) &&
mazgch 5:df776765d890 90 gps.getNmeaItem(9,buf,len,elevation)) {
mazgch 5:df776765d890 91 printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch);
mazgch 5:df776765d890 92 location_valid = ch == '1' || ch == '2' || ch == '6';
mazgch 5:df776765d890 93 }
mazgch 5:df776765d890 94 }
mazgch 5:df776765d890 95 }
mazgch 5:df776765d890 96 }
jb8414 0:38a7a8cae773 97
mazgch 10:ba926a8f1fe6 98 if (tmr.read_ms() > 1000) {
mazgch 5:df776765d890 99 tmr.reset();
mazgch 5:df776765d890 100 tmr.start();
mazgch 8:7ca1b7a712b7 101 int response;
joe_tijerina 14:b756e26ac6bf 102
joe_tijerina 14:b756e26ac6bf 103 /* Read sensors */
joe_tijerina 14:b756e26ac6bf 104 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value);
joe_tijerina 14:b756e26ac6bf 105 mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
joe_tijerina 14:b756e26ac6bf 106 //mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
joe_tijerina 14:b756e26ac6bf 107 //mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
joe_tijerina 14:b756e26ac6bf 108 //mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
joe_tijerina 14:b756e26ac6bf 109 //mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
mazgch 5:df776765d890 110
mazgch 8:7ca1b7a712b7 111 MDMParser::NetStatus status;
joe_tijerina 14:b756e26ac6bf 112 if (mdm.checkNetStatus(&status)) {
joe_tijerina 14:b756e26ac6bf 113 /* Post Temperature to M2X */
joe_tijerina 14:b756e26ac6bf 114 TEMPERATURE_Value_f =(1.8 * TEMPERATURE_Value) + 32.0;
joe_tijerina 14:b756e26ac6bf 115 printf("TEMP: %f C %f F \r\n", TEMPERATURE_Value, TEMPERATURE_Value_f);
joe_tijerina 14:b756e26ac6bf 116 response = m2xClient.put(feedId, "temperature", TEMPERATURE_Value_f);
mazgch 10:ba926a8f1fe6 117 printf("Put response code: %d\r\n", response);
mazgch 8:7ca1b7a712b7 118 if (response == -1) while (true) ;
joe_tijerina 14:b756e26ac6bf 119
joe_tijerina 14:b756e26ac6bf 120 /* Post Humidity to M2X */
joe_tijerina 14:b756e26ac6bf 121 printf("HUMIDITY: %f \r\n", HUMIDITY_Value);
joe_tijerina 14:b756e26ac6bf 122 response = m2xClient.put(feedId, "humidity", HUMIDITY_Value);
joe_tijerina 14:b756e26ac6bf 123 printf("Put response code: %d\r\n", response);
joe_tijerina 14:b756e26ac6bf 124 if (response == -1) while (true) ;
mazgch 8:7ca1b7a712b7 125 }
joe_tijerina 14:b756e26ac6bf 126
mazgch 6:7a1e717a0d1e 127 //#define READING
mazgch 6:7a1e717a0d1e 128 #ifdef READING
mazgch 13:b04452198625 129 // read signal strength
mazgch 8:7ca1b7a712b7 130 response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL);
mazgch 5:df776765d890 131 printf("Fetch response code: %d\r\n", response);
mazgch 5:df776765d890 132 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 133 #endif
mazgch 5:df776765d890 134 // update location
mazgch 5:df776765d890 135 if (location_valid) {
mazgch 5:df776765d890 136 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
mazgch 5:df776765d890 137 printf("updateLocation response code: %d\r\n", response);
mazgch 5:df776765d890 138 if (response == -1) while (true) ;
mazgch 5:df776765d890 139 }
mazgch 6:7a1e717a0d1e 140 #ifdef READING
mazgch 5:df776765d890 141 // read location
mazgch 5:df776765d890 142 response = m2xClient.readLocation(feedId, on_location_found, NULL);
mazgch 5:df776765d890 143 printf("readLocation response code: %d\r\n", response);
mazgch 5:df776765d890 144 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 145 #endif
joe_tijerina 14:b756e26ac6bf 146 printf("\r\n");
mazgch 5:df776765d890 147 }
mazgch 5:df776765d890 148 else {
mazgch 5:df776765d890 149 delay(100);
mazgch 5:df776765d890 150 }
jb8414 0:38a7a8cae773 151 }
mazgch 1:c73a98da0e7a 152
mazgch 1:c73a98da0e7a 153 mdm.disconnect();
mazgch 1:c73a98da0e7a 154 mdm.powerOff();
jb8414 0:38a7a8cae773 155 }