This is a SLIP interface for the STM32F446RE Nucleo Board. It is designed to work specifically with the esp-link software for the ESP8266. The program is an example of a rest command.

Dependencies:   mbed DHT Matrix

Revision:
4:31bed73a0d08
Parent:
3:8ed85d940c4c
Child:
5:9f3015b18b24
--- a/main.cpp	Fri Aug 05 16:04:04 2016 +0000
+++ b/main.cpp	Tue Aug 09 22:42:31 2016 +0000
@@ -2,31 +2,33 @@
  * Simple example to demo the STM-Client REST calls
  */
 #include "mbed.h"
+
 #include <STMClient.h>
 #include <STMClientRest.h>
 
 /*-------- Check if platform compatible ----------*/
 #if DEVICE_SERIAL_ASYNCH
-Serial debugSerial(SERIAL_TX, SERIAL_RX);
-Serial espSerial(PA_0, PA_1);
+    Serial debugSerial(SERIAL_TX, SERIAL_RX);
+    Serial espSerial(PA_0, PA_1);
 #else
     #warning "Platform not compatible with Low Power APIs for Serial"
     Serial debugSerial(SERIAL_TX, SERIAL_RX);
     Serial espSerial(PA_0, PA_1);
 #endif
 
-
-DigitalOut led1(LED1);             
+DigitalOut led1(LED1);      
+DigitalOut led2(LED2);       
 
 // Initialize a connection to esp-link using the normal hardware serial port both for
 // SLIP and for debug messages.
 STMClient esp(&espSerial, &debugSerial);
 
-
 // Initialize a REST client on the connection to esp-link
 STMClientRest rest(&esp);
-
 bool wifiConnected = false;
+Ticker flipper;
+Ticker post_data;
+bool posted = false;
 
 // Callback made from esp-link to notify of wifi status changes
 // Here we print something out and set a global flag
@@ -48,8 +50,8 @@
   }
 }
 
-#define BUFLEN 512
-    char response[BUFLEN];
+#define BUFLEN 100
+char response[BUFLEN];
 
 char *loop_get() {
   //debugSerial.printf("begin main loop \n\r");
@@ -78,34 +80,25 @@
 }
 
 int loop_post() {
-  //debugSerial.printf("begin main loop \n\r");
+  posted = false;
+
   // process any callbacks coming from esp_link
-  int postCount = 0;
   esp.Process();
+  
   debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected);
   // if we're connected make an HTTP request
-  while(wifiConnected) {
-    postCount++; //don't post more than 3 times...
-    //***Uncomment to post to cloud***//
-    /*
-    const char* header = "{Host: vivaplanetbusservicedev.servicebus.windows.net Authorization: SharedAccessSignature sr=https%3a%2f%2fvivaplanetbusservicedev.servicebus.windows.net%2fhummingbirdqueue%2fmessages&sig=QIGJfPSsortI9tZpoJ6ZUzfMdtvEaaIHA2pnmKZxNVg%3d&se=1470210743&skn=DevicePolicy Content-Length: 440 Expect: 100-continue Connection: Keep-Alive}";
-    rest.setHeader(header);
-    
-    const char* body = "{\"id\":\"17564321\",\"Address\":\"$28591\",\"PhotoUrl\":\"http://vivaplanetstoragedev.blob.core.windows.net:80/devicephotoscontainer/1457a68157e446c2bc4d0af58a0ccc63.jpg?st=2016-08-02T21%3A50%3A31Z&se=2016-08-02T22%3A05%3A31Z&sr=b&sp=w&sig=rf6ovBuFXKC937b3TMY3qVSf%2F3QOYXjqJBYcwj2MUv4%3D\",\"HasPhoto\":true,\"PhotoId\":\"1457a68157e446c2bc4d0af58a0ccc63.jpg\",\"DeviceSensors\":[{\"SensorType\":\"0\",\"Reading\":\"00:03:52.7656140\",\"Value\":\"921\"},null,null,null]}";
-    rest.post("hummingbirdqueue/messages", body); //cloud post test
-    */
-    //***Uncomment to post to test server***//
-    //const char* header = "{Host: posttestserver.com\n\rAuthorization: My_SSL_Authorization\n\rContent-Length: 27\n\rHTTP_EXPECT: 100-continue\n\rConnection: Keep-Alive}";
-    //rest.setHeader(header);
-    
+  if(wifiConnected) {
+        
     const char* T = "25";
     const char* L = "95";
     const char* H = "25";
     
+    /**make sure that the size of this array is consistent with the input string**/ 
     const char* body = "";    
-    char output [50];
+    char output [62];
     sprintf(output, "/update?api_key=3FF5CTKAJIU2IH0M&field1=%s&field2=%s&field3=%s", T,L,H);
     //debugSerial.printf("output: %s \n\r", output);
+    //debugSerial.printf("size: %i \n\r", strlen(output));    
     
     rest.post(output, body); //basic post test
       
@@ -114,21 +107,32 @@
     uint16_t code = rest.waitResponse(response, BUFLEN);
     if(code == HTTP_STATUS_OK){
       debugSerial.printf("STM: POST successful: %s\n\r", response);
-    }else if(postCount > 3){
-      debugSerial.printf("STM: POST 3 times exiting...\n\r");
-      return code;   
     }else {
       debugSerial.printf("STM: POST failed: %i\n\r",code);
-      return code;
+      return 1;
     }
-    
-    wait(60);
-  }
-  return 0;   
+  }else{
+    debugSerial.printf("STM: wifi NOT connected: %i\n\r",wifiConnected);
+    return 1;
+  } 
+  //debugSerial.printf("Exiting...\n\r");
+  return 0;
 }
 
+void flip() {
+
+    int postVal = loop_post();
+    if(!postVal)
+    {
+        posted = true;
+    }else{
+        debugSerial.printf("error...%i\n\r",postVal);
+        posted = false;
+    }
+}
 
 int main() {
+  //setup
   led1=0;
   debugSerial.baud(115200);   // the baud rate here needs to match the esp-link config
   espSerial.baud(115200);
@@ -172,8 +176,7 @@
       //Serial.printf(status);
       wifiConnected = false;
     }
-  }
-  
+  }  
 
   // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server,
   // it just sets-up stuff on the esp-link side
@@ -190,10 +193,22 @@
     while(1) ;
   }
   debugSerial.printf("STM-REST ready\n\r");
-  while(response != "0"){
-      debugSerial.printf("status: %x\n\r",response);
-      //loop_get();
-      loop_post();      
-      wait(60);
-    } 
-}
\ No newline at end of file
+  
+  //post_data.attach(&loop_post,15.0); //fire a post every minute
+  flipper.attach(&flip, 60.0); // the address of the function to be attached (flip) and the interval (2 seconds)
+ 
+  while(true){
+      
+      if(posted)
+      {
+        debugSerial.printf("sleeping...");
+        posted = false;
+        sleep();       //then sleep
+      }else{
+        led2 = !led2;     
+        wait(0.5); // give everything a second to wake up
+      }
+  } 
+
+}
+