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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <jsonlite.h>
00002 #include "M2XStreamClient.h"
00003 #include "x_cube_mems.h"
00004 
00005 #include "mbed.h"
00006 #include "GPS.h"    //GPS
00007 
00008 //------------------------------------------------------------------------------------
00009 // You need to configure these cellular modem / SIM parameters.
00010 // These parameters are ignored for LISA-C200 variants and can be left NULL.
00011 //------------------------------------------------------------------------------------
00012 #include "MDM.h"
00013 //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual.
00014 #define SIMPIN      NULL
00015 /*! The APN of your network operator SIM, sometimes it is "internet" check your 
00016     contract with the network operator. You can also try to look-up your settings in 
00017     google: https://www.google.de/search?q=APN+list */
00018 #define APN         NULL
00019 //! Set the user name for your APN, or NULL if not needed
00020 #define USERNAME    NULL
00021 //! Set the password for your APN, or NULL if not needed
00022 #define PASSWORD    NULL 
00023 //------------------------------------------------------------------------------------
00024 
00025 char feedId[] = "0c76bf26149c9c01d8eec11553d1a6f2"; // Feed you want to post to
00026 char m2xKey[] = "76ee6b8414970d7ade8c75829d0cf879"; // Your M2X access key
00027 
00028 char name[] = "<location name>"; // Name of current location of datasource
00029 double latitude = 33.007872;
00030 double longitude = -96.751614; // You can also read those values from a GPS
00031 double elevation = 697.00;
00032 bool location_valid = false;
00033 volatile float TEMPERATURE_Value_C, TEMPERATURE_Value_F;
00034 volatile float HUMIDITY_Value;
00035 volatile float PRESSURE_Value;
00036 volatile AxesRaw_TypeDef MAG_Value;
00037 volatile AxesRaw_TypeDef ACC_Value;
00038 volatile AxesRaw_TypeDef GYR_Value;
00039 Client client;
00040 M2XStreamClient m2xClient(&client, m2xKey);
00041 
00042 void on_data_point_found(const char* at, const char* value, int index, void* context) {
00043   printf("Found a data point, index: %d\r\n", index);
00044   printf("At: %s Value: %s\r\n", at, value);
00045 }
00046 
00047 void on_location_found(const char* name,
00048                        double latitude,
00049                        double longitude,
00050                        double elevation,
00051                        const char* timestamp,
00052                        int index,
00053                        void* context) {
00054   printf("Found a location, index: %d\r\n", index);
00055   printf("Name: %s  Latitude: %lf  Longitude: %lf\r\n", name, latitude, longitude);
00056   printf("Elevation: %lf  Timestamp: %s\r\n", elevation, timestamp);
00057 }
00058 
00059 int main() {
00060   uint8_t hts221_id;
00061   MDMSerial mdm(D8,D2); 
00062   GPSI2C gps;
00063   //mdm.setDebug(4); // enable this for debugging issues 
00064   if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
00065     return -1;
00066  
00067   char buf[256];
00068   
00069   printf("M2X MEMS U-Blox Cellular Demo \r\n");
00070 
00071   static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
00072     
00073   hts221_id = mems_expansion_board->hts221.ReadID();
00074   printf("HTS221_ID = 0x%x\n\t\r", hts221_id);
00075     
00076   Timer tmr;
00077   tmr.reset();
00078   tmr.start();
00079   while (true) {
00080     int ret;
00081     // extract the location information from the GPS NMEA data
00082     while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) {
00083         int len = LENGTH(ret);
00084         if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
00085             if (!strncmp("$GPGGA", buf, 6)) {
00086                 char ch;
00087                 if (gps.getNmeaAngle(2,buf,len,latitude) && 
00088                     gps.getNmeaAngle(4,buf,len,longitude) && 
00089                     gps.getNmeaItem(6,buf,len,ch) &&
00090                     gps.getNmeaItem(9,buf,len,elevation)) {
00091                    printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); 
00092                    location_valid = ch == '1' || ch == '2' || ch == '6';
00093                 }
00094             }
00095         }
00096     }
00097     
00098     if (tmr.read_ms() > 1000) {
00099         tmr.reset();
00100         tmr.start();
00101         int response;
00102 
00103         /* Read sensors */        
00104         mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);
00105         mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
00106         mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
00107         mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
00108         mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
00109         mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
00110         
00111         /* Convert temperature to degrees Farhenheit. */
00112         TEMPERATURE_Value_F = (1.8f * TEMPERATURE_Value_C) + 32.0f;        
00113         
00114         printf("Temperature:\t\t %f C / %f F\r\n", TEMPERATURE_Value_C, TEMPERATURE_Value_F);
00115         printf("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
00116         printf("Pressure:\t\t %f hPa\r\n", PRESSURE_Value); 
00117         printf("Magnetometer (mGauss):\t X: %d, Y: %d, Z: %d\r\n", MAG_Value.AXIS_X, MAG_Value.AXIS_Y, MAG_Value.AXIS_Z);
00118         printf("Accelerometer (mg):\t X: %d, Y: %d, Z: %d\r\n", ACC_Value.AXIS_X, ACC_Value.AXIS_Y, ACC_Value.AXIS_Z);
00119         printf("Gyroscope (mdps):\t X: %d, Y: %d, Z: %d\r\n", GYR_Value.AXIS_X, GYR_Value.AXIS_Y, GYR_Value.AXIS_Z);
00120         printf("\r\n");        
00121         
00122         
00123         MDMParser::NetStatus status;
00124         if (mdm.checkNetStatus(&status)) {            
00125             /* Post Temperature to M2X */                            
00126             response = m2xClient.updateStreamValue(feedId, "temperature", TEMPERATURE_Value_F);
00127             printf("Temperature updateStreamValue response code: %d\r\n", response);
00128             if (response == -1) 
00129             {
00130                 printf("Temperature data update error\n");
00131             }        
00132     
00133             /* Post Humidity to M2X */                 
00134             response = m2xClient.updateStreamValue(feedId, "humidity", HUMIDITY_Value);
00135             printf("Humidity updateStreamValue response code: %d\r\n", response);
00136             if (response == -1) 
00137             {
00138                 printf("Humidity data update error\n");
00139             }        
00140             
00141             /* Post acceleration (x-axis) to the m2x stream. */
00142             response = m2xClient.updateStreamValue(feedId, "acceleration", ACC_Value.AXIS_X);
00143             printf("Acceleration updateStreamValue response code: %d\r\n", response);            
00144             if (response == -1) 
00145             {
00146                 printf("Acceleration data update error\n");
00147             }
00148                            
00149         }
00150         
00151 //#define READING
00152 #ifdef READING
00153         // read signal strength
00154         response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL);
00155         printf("Fetch response code: %d\r\n", response);
00156         if (response == -1) while (true) ;
00157 #endif    
00158         // update location
00159         if (location_valid) {
00160             response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
00161             printf("updateLocation response code: %d\r\n", response);
00162             if (response == -1) while (true) ;
00163         }
00164 #ifdef READING
00165         // read location
00166         response = m2xClient.readLocation(feedId, on_location_found, NULL);
00167         printf("readLocation response code: %d\r\n", response);
00168         if (response == -1) while (true) ;
00169 #endif
00170         wait(30); // 30 s
00171         printf("\r\n");
00172     }
00173     else {
00174         delay(100);
00175     }
00176   }
00177 
00178   mdm.disconnect();
00179   mdm.powerOff();
00180 }