Sample project to connect to AT&T M2X from the STM32 Nucleo + MTS Cellular SocketModem shield

Dependencies:   M2XStreamClient jsonlite mbed

Fork of MTSAS_Cellular_Connect_Example_F411 by ST Americas mbed Team

Revision:
17:d9fb4ea14d2b
Parent:
16:3458e36115a9
--- a/main.cpp	Wed Sep 24 18:53:25 2014 +0000
+++ b/main.cpp	Thu Sep 25 19:10:38 2014 +0000
@@ -1,34 +1,37 @@
+#include <jsonlite.h>
+
 #include "mbed.h"
 #include "mtsas.h"
 #include "M2XStreamClient.h"
 
-#define DEBUG
+const char m2xKey[] = "06ce9a9febbfc50ffceb0d8214427767";   // Replace with your M2X API key
+const char feedId[] = "48c77c779f0eed1243e623190e588501";   // Replace with your blueprint Feed ID
+const char streamName[] = "temperature"; // Replace with your stream name  
 
-const char key[] = "06ce9a9febbfc50ffceb0d8214427767";    // Replace with your M2X API key
-const char feed[] = "48c77c779f0eed1243e623190e588501";   // Replace with your blueprint Feed ID
-const char stream[] = "temperature"; // Replace with your stream name  
 char name[] = "austin"; // Name of current location of datasource
-
+double latitude = 30.3748076;
+double longitude = -97.7386896; // You can also read those values from a GPS
+double elevation = 400.00;
 
-DigitalOut myled(D7);   
-AnalogIn tempSensor(A0);    
+// Note: D8, D2, D3, D6, D4, D7 are being used by MTSAS Cellular Modem
+
+PwmOut mypwm(D13);   // This is the Nucleo board LED2 
+AnalogIn temp_sensor(A1);    // There's also a conflict with A0, so connect temp_sensor A1 
 
 int main()
 { 
     int ping_status = 0;
     char amb_temp[6]; 
-    char water_sense[6];     
     int response;
-    int a;
-    int adc_scale = 65535; 
+    int adc_scale = 65536; //4096; 
     int B = 3975;
-    int water;
     float resistance; 
     float temperature;
     float temperature_f;   
                
-                        
-                
+    mypwm.period_ms(500);
+    mypwm.pulsewidth_ms(250);                        
+              
     //Modify to match your apn if you are using an HSPA radio with a SIM card
     const char APN[] = "epc.tmobile.com";
     printf("Starting....\n");
@@ -100,10 +103,10 @@
     //Ping google.com
     for (int i = 0; i < 10; i++) {
         if (i >= 10) {
-            logError("Failed to ping www.google.com");
+            logError("Failed to ping api-m2x.att.com");
         }
-        if (radio->ping("www.google.com") == true) {
-            logInfo("Successfully pinged www.google.com");
+        if (radio->ping("api-m2x.att.com") == true) {
+            logInfo("Successfully pinged api-m2x.att.com");
             ping_status = 1;
             break;
         } else {
@@ -113,42 +116,58 @@
         
     // Initialize the M2X client
     Client client;    
-    M2XStreamClient m2xClient(&client, key);    
-
+    M2XStreamClient m2xClient(&client, m2xKey);    
+     
     if (ping_status)
-    {    
-        while(1)    
-        {      
-            myled = 1; // LED is ON    
-            //a = tempSensor.read_u16();
+    { 
+        mypwm.pulsewidth_ms(500);
+     
+        /* Main loop */
+        while (true) 
+        {
+     
+            /* Wait 5 secs and then loop */
+            delay(2500);
+            mypwm.pulsewidth_ms(0);      
+                  
+            /* Read ADC value from analog sensor */
+            uint16_t a = temp_sensor.read_u16();
                    
-            //resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;              
-            //temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;  //convert to temperature via datasheet        
-            //temperature_f = (1.8 * temperature) + 32.0;
-            //sprintf(amb_temp, "%0.2f", temperature_f);  
+            /* Calculate the temperature in Fareheight and Celsius */
+            resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;              
+            temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;  //convert to temperature via datasheet ;        
+            temperature_f =(1.8 * temperature) +  32.0;
+            sprintf(amb_temp, "%0.2f", temperature_f);  
             
-            //printf("Temp Sensor Analog Reading is 0x%X = %d   ", a, a);         
-            //printf("Current Temperature: %f C  %f F \n\r", temperature, temperature_f); 
-            
-            temperature++;
-            printf("temperature: %f \n", temperature);
-            response = m2xClient.put(feed, stream, temperature);
+            printf("Temp Sensor Analog Reading is 0x%X = %d  \r\n", a, a);         
+            printf("Current Temperature: %f C  %f F \r\n", temperature, temperature_f); 
+    
+            delay(2500);
+            mypwm.pulsewidth_ms(500);   
+         
+            /* Post temperature to M2X site */
+            int response = m2xClient.put(feedId, streamName, amb_temp);
             printf("Post response code: %d\r\n", response);
-            //if (response == -1) 
-            //{
-            //    break; 
-            //}                   
-            
-            myled = 0; // LED is OFF        
-    
-            delay(5000);      
-                    
+            if (response == -1) 
+            {
+                mypwm.pulsewidth_ms(250);                
+                printf("Temperature data transmit post error\n");
+            }
+            /* Update location data */
+            response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
+            printf("updateLocation response code: %d\r\n", response);
+            if (response == -1) 
+            {
+                mypwm.pulsewidth_ms(250);        
+                printf("Location data transmit post error\n");
+            }    
+            printf("\r\n");     
         }
     }
-            
+
     //Disconnect ppp link
     radio->disconnect();
-    
+   
     logInfo("End of example code");
     return 0;
 } 
\ No newline at end of file