working

Dependencies:   C027_Support IBMIoTClientCellularExample MQTT mbed

Fork of MQTT-GSM-GPS-CAN-SMS by Babu Alikapati

Revision:
15:7c3279ac3c52
Parent:
14:6f36d75b3917
Child:
16:a931c0a6d987
--- a/main.cpp	Fri Mar 13 07:11:56 2015 +0000
+++ b/main.cpp	Thu Jul 02 08:19:04 2015 +0000
@@ -61,6 +61,8 @@
 //! Set the password for your APN, or NULL if not needed
 # define PASSWORD    NULL 
 //------------------------------------------------------------------------------------
+# include "GPS.h"
+//------------------------------------------------------------------------------------
 
 C12832 lcd(D11, D13, D12, D7, D10);
 PwmOut r(D5);
@@ -85,10 +87,11 @@
 bool quickstartMode = true;
 char org[11] = ORG;  
 char type[30] = TYPE_NAME;
-char id[30] = ID;                 // mac without colons
+char id[30] = ID;                 // device ID
 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
 
 bool connected = false;
+bool shieldConnected = false;
 char* joystickPos = "CENTRE";
 int blink_interval = 0;
 
@@ -303,9 +306,16 @@
     char* pubTopic = "iot-2/evt/status/fmt/json";
             
     char buf[250];
-    sprintf(buf,
-     "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
-            MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
+    int l = 0;
+    l += sprintf(buf+l,"{\"d\":{\"myName\":\"IoT mbed\"");
+    if (shieldConnected) {
+        l += sprintf(buf+l,",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f", MMA.x(), MMA.y(), MMA.z());
+        l += sprintf(buf+l,",\"temp\":%0.4f", sensor.temp());
+        l += sprintf(buf+l,",\"joystick\":\"%s\"", joystickPos);
+        l += sprintf(buf+l,",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f", ain1.read(), ain2.read());
+    }
+    l += sprintf(buf+l,"}}");
+
     message.qos = MQTT::QOS0;
     message.retained = false;
     message.dup = false;
@@ -316,6 +326,21 @@
     return client->publish(pubTopic, message);
 }
 
+int publishGps(MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTT_CLIENT_TYPE* ipstack, double lat, double lon)
+{
+    MQTT::Message message;
+    char* pubTopic = "iot-2/evt/gps/fmt/json";
+    char buf[250];
+    sprintf(buf,"{\"d\":{\"lat\":\"%.7f\",\"long\":\"%.7f\"}}", lat, lon);
+    message.qos = MQTT::QOS0;
+    message.retained = false;
+    message.dup = false;
+    message.payload = (void*)buf;
+    message.payloadlen = strlen(buf);
+    LOG("Publishing GPS %s\n", buf);
+    return client->publish(pubTopic, message);
+}
+
 void messageArrived(MQTT::MessageData& md)
 {
     MQTT::Message &message = md.message;
@@ -349,10 +374,10 @@
         WARN("Unsupported command: %.*s\n", len, start);
 }
 
-
 int main()
 {    
     quickstartMode = (strcmp(org, "quickstart") == 0);
+    shieldConnected = MMA.testConnection() && sensor.open();
 
     lcd.set_font((unsigned char*) Arial12x12);  // Set a nice font for the LCD screen
     
@@ -370,16 +395,16 @@
     if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
         return -1;
 
+    GPSI2C gps;
+    
     MQTT_CLIENT_TYPE ipstack;
     MQTT::Client<MQTT_CLIENT_TYPE, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
     if (quickstartMode)
     {
-        MDMParser::NetStatus status;
-        if (mdm.checkNetStatus(&status) && *status.num) {
-            int l = strlen(status.num);
-            strcpy(id, "000000000000");
-            strcpy(id+12-l, status.num);
-        }
+        MDMParser::DevStatus dev;
+        mdm.getDevStatus(&dev);
+        if      (*dev.imei) strncpy(id, dev.imei, sizeof(id)-1);
+        else if (*dev.meid) strncpy(id, dev.meid, sizeof(id)-1);
     }
     
     attemptConnect(&client, &ipstack);
@@ -393,6 +418,7 @@
     
     blink_interval = 0;
     int count = 0;
+    int count2 = 0;
     while (true)
     {
         if (++count == 100)
@@ -401,6 +427,39 @@
                 attemptConnect(&client, &ipstack);   // if we have lost the connection
             count = 0;
         }
+        if (++count2 == 10)
+        {               // Publish a message every second
+            count2 = 0;
+            while (1) {
+                char buf[256];
+                int ret = gps.getMessage(buf, sizeof(buf));
+                if (ret <= 0) break;
+                int len = LENGTH(ret);
+                if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) {
+                    // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS
+                    if ((buf[0] == '$') || buf[1] == 'G') {
+                        #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2]))
+                        if (_CHECK_TALKER("GGA")) {
+                            //printf("%.*s\n", len, buf); 
+                            char ch;
+                            double latitude, longitude, elevation;
+                            static double lastLat = 0, lastLon = 0;
+                            if (gps.getNmeaAngle(2,buf,len,latitude) && 
+                                gps.getNmeaAngle(4,buf,len,longitude) && 
+                                gps.getNmeaItem(6,buf,len,ch) &&
+                                gps.getNmeaItem(9,buf,len,elevation)) {
+                                //printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); 
+                                if ((ch == '1' || ch == '2' || ch == '6') && (latitude != lastLat) && (lastLon != longitude)) {
+                                    publishGps(&client, &ipstack, latitude, longitude);
+                                    lastLat = latitude;
+                                    lastLon = longitude;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
         
         if (blink_interval == 0)
             led2 = LED2_OFF;