port to cellular

Dependencies:   C027_Support LM75B M2XStreamClient jsonlite mbed mbed-rtos

Fork of m2x-demo-all by AT&T M2X Team

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 
00004 #include "mbed.h"
00005 #include "LM75B.h"  //I2C Temperature Sensor
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[] = "81b9fcc5a8585c55ae622488f50d8de0"; // Feed you want to post to
00026 char m2xKey[] = "1e1133cd475954868602c0f7503d4f22"; // 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 
00034 Client client;
00035 M2XStreamClient m2xClient(&client, m2xKey);
00036 
00037 LM75B tmp(SDA,SCL);         // I2C Temperature Sensor
00038 
00039 void on_data_point_found(const char* at, const char* value, int index, void* context) {
00040   printf("Found a data point, index: %d\r\n", index);
00041   printf("At: %s Value: %s\r\n", at, value);
00042 }
00043 
00044 void on_location_found(const char* name,
00045                        double latitude,
00046                        double longitude,
00047                        double elevation,
00048                        const char* timestamp,
00049                        int index,
00050                        void* context) {
00051   printf("Found a location, index: %d\r\n", index);
00052   printf("Name: %s  Latitude: %lf  Longitude: %lf\r\n", name, latitude, longitude);
00053   printf("Elevation: %lf  Timestamp: %s\r\n", elevation, timestamp);
00054 }
00055 
00056 int main() {
00057   MDMSerial mdm;
00058   GPSI2C gps;
00059   //mdm.setDebug(4); // enable this for debugging issues 
00060   if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
00061     return -1;
00062  
00063   char buf[256];
00064   
00065   Timer tmr;
00066   tmr.reset();
00067   tmr.start();
00068   while (true) {
00069     int ret;
00070     // extract the location information from the GPS NMEA data
00071     while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) {
00072         int len = LENGTH(ret);
00073         if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
00074             if (!strncmp("$GPGGA", buf, 6)) {
00075                 char ch;
00076                 if (gps.getNmeaAngle(2,buf,len,latitude) && 
00077                     gps.getNmeaAngle(4,buf,len,longitude) && 
00078                     gps.getNmeaItem(6,buf,len,ch) &&
00079                     gps.getNmeaItem(9,buf,len,elevation)) {
00080                    printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); 
00081                    location_valid = ch == '1' || ch == '2' || ch == '6';
00082                 }
00083             }
00084         }
00085     }
00086     
00087     if (tmr.read_ms() > 1000) {
00088         tmr.reset();
00089         tmr.start();
00090         int response;
00091         
00092         MDMParser::NetStatus status;
00093         if (mdm.checkNetStatus(&status)) {
00094             sprintf(buf, "%d", status.rssi);
00095             response = m2xClient.put(feedId, "rssi", buf);
00096             printf("Put response code: %d\r\n", response);
00097             if (response == -1) while (true) ;
00098         }
00099 //#define READING
00100 #ifdef READING
00101         // read temperature
00102         response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL);
00103         printf("Fetch response code: %d\r\n", response);
00104         if (response == -1) while (true) ;
00105 #endif    
00106         // update location
00107         if (location_valid) {
00108             response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
00109             printf("updateLocation response code: %d\r\n", response);
00110             if (response == -1) while (true) ;
00111         }
00112 #ifdef READING
00113         // read location
00114         response = m2xClient.readLocation(feedId, on_location_found, NULL);
00115         printf("readLocation response code: %d\r\n", response);
00116         if (response == -1) while (true) ;
00117 #endif
00118     }
00119     else {
00120         delay(100);
00121     }
00122   }
00123 
00124   mdm.disconnect();
00125   mdm.powerOff();
00126 }