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 18 20:42:33 2014 +0000
Revision:
16:fab4ed40e6ea
Parent:
14:b756e26ac6bf
Updated M2XStreamClient library to latest Rev 13. Reformatted the read sensor portion of the application a little.

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 16:fab4ed40e6ea 33 volatile float TEMPERATURE_Value_C, 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 16:fab4ed40e6ea 104 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);
joe_tijerina 14:b756e26ac6bf 105 mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
joe_tijerina 16:fab4ed40e6ea 106 mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
joe_tijerina 16:fab4ed40e6ea 107 mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
joe_tijerina 16:fab4ed40e6ea 108 mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
joe_tijerina 16:fab4ed40e6ea 109 mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
joe_tijerina 16:fab4ed40e6ea 110
joe_tijerina 16:fab4ed40e6ea 111 /* Convert temperature to degrees Farhenheit. */
joe_tijerina 16:fab4ed40e6ea 112 TEMPERATURE_Value_F = (1.8f * TEMPERATURE_Value_C) + 32.0f;
joe_tijerina 16:fab4ed40e6ea 113
joe_tijerina 16:fab4ed40e6ea 114 printf("Temperature:\t\t %f C / %f F\r\n", TEMPERATURE_Value_C, TEMPERATURE_Value_F);
joe_tijerina 16:fab4ed40e6ea 115 printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
joe_tijerina 16:fab4ed40e6ea 116 printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value);
joe_tijerina 16:fab4ed40e6ea 117 printf("Magnetometer (mGauss):\t X: %d, Y: %d, Z: %d\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z);
joe_tijerina 16:fab4ed40e6ea 118 printf("Accelerometer (mg):\t X: %d, Y: %d, Z: %d\r\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z);
joe_tijerina 16:fab4ed40e6ea 119 printf("Gyroscope (mdps):\t X: %d, Y: %d, Z: %d\r\n", GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z);
joe_tijerina 16:fab4ed40e6ea 120 printf("\r\n");
joe_tijerina 16:fab4ed40e6ea 121
mazgch 5:df776765d890 122
mazgch 8:7ca1b7a712b7 123 MDMParser::NetStatus status;
joe_tijerina 14:b756e26ac6bf 124 if (mdm.checkNetStatus(&status)) {
joe_tijerina 16:fab4ed40e6ea 125 /* Post Temperature to M2X */
joe_tijerina 16:fab4ed40e6ea 126 response = m2xClient.updateStreamValue(feedId, "temperature", TEMPERATURE_Value_F);
joe_tijerina 16:fab4ed40e6ea 127 printf("Temperature updateStreamValue response code: %d\r\n", response);
joe_tijerina 16:fab4ed40e6ea 128 if (response == -1)
joe_tijerina 16:fab4ed40e6ea 129 {
joe_tijerina 16:fab4ed40e6ea 130 printf("Temperature data update error\n");
joe_tijerina 16:fab4ed40e6ea 131 }
joe_tijerina 14:b756e26ac6bf 132
joe_tijerina 14:b756e26ac6bf 133 /* Post Humidity to M2X */
joe_tijerina 16:fab4ed40e6ea 134 response = m2xClient.updateStreamValue(feedId, "humidity", HUMIDITY_Value);
joe_tijerina 16:fab4ed40e6ea 135 printf("Humidity updateStreamValue response code: %d\r\n", response);
joe_tijerina 16:fab4ed40e6ea 136 if (response == -1)
joe_tijerina 16:fab4ed40e6ea 137 {
joe_tijerina 16:fab4ed40e6ea 138 printf("Humidity data update error\n");
joe_tijerina 16:fab4ed40e6ea 139 }
joe_tijerina 16:fab4ed40e6ea 140
joe_tijerina 16:fab4ed40e6ea 141 /* Post acceleration (x-axis) to the m2x stream. */
joe_tijerina 16:fab4ed40e6ea 142 response = m2xClient.updateStreamValue(feedId, "acceleration", ACC_Value.AXIS_X);
joe_tijerina 16:fab4ed40e6ea 143 printf("Acceleration updateStreamValue response code: %d\r\n", response);
joe_tijerina 16:fab4ed40e6ea 144 if (response == -1)
joe_tijerina 16:fab4ed40e6ea 145 {
joe_tijerina 16:fab4ed40e6ea 146 printf("Acceleration data update error\n");
joe_tijerina 16:fab4ed40e6ea 147 }
joe_tijerina 16:fab4ed40e6ea 148
mazgch 8:7ca1b7a712b7 149 }
joe_tijerina 14:b756e26ac6bf 150
mazgch 6:7a1e717a0d1e 151 //#define READING
mazgch 6:7a1e717a0d1e 152 #ifdef READING
mazgch 13:b04452198625 153 // read signal strength
mazgch 8:7ca1b7a712b7 154 response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL);
mazgch 5:df776765d890 155 printf("Fetch response code: %d\r\n", response);
mazgch 5:df776765d890 156 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 157 #endif
mazgch 5:df776765d890 158 // update location
mazgch 5:df776765d890 159 if (location_valid) {
mazgch 5:df776765d890 160 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
mazgch 5:df776765d890 161 printf("updateLocation response code: %d\r\n", response);
mazgch 5:df776765d890 162 if (response == -1) while (true) ;
mazgch 5:df776765d890 163 }
mazgch 6:7a1e717a0d1e 164 #ifdef READING
mazgch 5:df776765d890 165 // read location
mazgch 5:df776765d890 166 response = m2xClient.readLocation(feedId, on_location_found, NULL);
mazgch 5:df776765d890 167 printf("readLocation response code: %d\r\n", response);
mazgch 5:df776765d890 168 if (response == -1) while (true) ;
mazgch 6:7a1e717a0d1e 169 #endif
joe_tijerina 16:fab4ed40e6ea 170 wait(30); // 30 s
joe_tijerina 14:b756e26ac6bf 171 printf("\r\n");
mazgch 5:df776765d890 172 }
mazgch 5:df776765d890 173 else {
mazgch 5:df776765d890 174 delay(100);
mazgch 5:df776765d890 175 }
jb8414 0:38a7a8cae773 176 }
mazgch 1:c73a98da0e7a 177
mazgch 1:c73a98da0e7a 178 mdm.disconnect();
mazgch 1:c73a98da0e7a 179 mdm.powerOff();
jb8414 0:38a7a8cae773 180 }