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.
Diff: main.cpp
- Revision:
- 8:6a3b7c5d9ba7
- Parent:
- 7:ff4efdc27514
- Child:
- 9:bd7f083d55ad
diff -r ff4efdc27514 -r 6a3b7c5d9ba7 main.cpp --- a/main.cpp Tue Aug 16 16:04:33 2016 +0000 +++ b/main.cpp Wed Aug 17 20:57:35 2016 +0000 @@ -18,6 +18,7 @@ DigitalOut led1(LED1); DigitalOut led2(LED2); +DigitalOut LCD_D7(D7); // Initialize a connection to esp-link using the normal hardware serial port both for // SLIP and for debug messages. @@ -29,24 +30,7 @@ Ticker flipper; Ticker post_data; bool posted = false; - -//generate some random numbers -int random_number(int min_num, int max_num); -int random_number(int min_num, int max_num){ - int result=0,low_num=0,hi_num=0; - if(min_num<max_num) - { - low_num=min_num; - hi_num=max_num+1; // this is done to include max_num in output. - }else{ - low_num=max_num+1;// this is done to include max_num in output. - hi_num=min_num; - } - srand(time(NULL)); - result = (rand()%(hi_num-low_num))+low_num; - return result; - } - +bool ESPisAsleep = false; // Callback made from esp-link to notify of wifi status changes // Here we print something out and set a global flag void wifiCb(void *response) { @@ -67,6 +51,81 @@ } } +//resync after sleep mode +void syncESP(){ + + // Sync-up with esp-link, this is required at the start of any sketch and initializes the + // callbacks to the wifi status change callback. The callback gets called with the initial + // status right after Sync() below completes. + esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) + bool ok; + do { + //debugSerial.printf("main syncing..\n\r"); + wait(0.5); + ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds + if (!ok){ + debugSerial.printf("STM-Client sync failed!\n\r"); + wait(0.5); + } + } while(!ok); + + debugSerial.printf("STM-Client synced!\n\r"); + + // Get immediate wifi status info for demo purposes. This is not normally used because the + // wifi status callback registered above gets called immediately. + esp.GetWifiStatus(); + + STMClientPacket *packet; + if ((packet=esp.WaitReturn()) != NULL) + { + //debugSerial.printf("Wifi status: %i\n\r", packet->value); + //debugSerial.printf("waiting for wifi status...\n\r"); + if(packet->value >= 1) { ///ideally this would coincide with STATION_GOT_IP... + debugSerial.printf("WIFI CONNECTED\n\r"); + wifiConnected = true; + } else { + debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value); + //Serial.printf(status); + wifiConnected = false; + } + } +} +//Start/Restart REST...can pass some fancy arguments to this in the future. +void beginREST(){ + + // 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 + //int err = rest.begin("www.timeapi.org"); //for basic example of get + const char* host = "api.thingspeak.com"; + //const char* host = "posttestserver.com"; + //const char* host = "vivaplanetbusservicedev.servicebus.windows.net"; + uint16_t port = 80; + bool security = true; + int err = rest.begin(host,port,security); + if (err != 0) { + debugSerial.printf("REST begin failed: %i\n\r",err); + while(1) ; + } + debugSerial.printf("STM-REST ready\n\r"); +} + +//generate some random numbers +int random_number(int min_num, int max_num){ + int result=0,low_num=0,hi_num=0; + if(min_num<max_num) + { + low_num=min_num; + hi_num=max_num+1; // this is done to include max_num in output. + }else{ + low_num=max_num+1;// this is done to include max_num in output. + hi_num=min_num; + } + srand(time(NULL)); + result = (rand()%(hi_num-low_num))+low_num; + return result; + } + + #define BUFLEN 100 char response[BUFLEN]; @@ -109,6 +168,7 @@ //this is where the calls to the sensor drivers should go. //ideally they will be blocking so they don't return a value and allow the program to move on until each //function is complete + debugSerial.printf("geting measurements...\n\r"); int Tint = random_number(65, 99); int Lint = random_number(0, 99); int Hint = random_number(20, 25); @@ -127,7 +187,7 @@ 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)); - + debugSerial.printf("sending data...\n\r"); rest.post(output, body); //basic post test char response[BUFLEN]; @@ -148,13 +208,27 @@ } void flip() { - + if(ESPisAsleep){ + debugSerial.printf("syncing...\n\r"); + LCD_D7 = 0; + wait(0.5); + LCD_D7 = 1; + wait(20); + syncESP(); + ESPisAsleep = false; + debugSerial.printf("restarting REST...\n\r"); + beginREST(); + } + debugSerial.printf("posting...\n\r"); int postVal = loop_post(); if(!postVal) { + led2 = 1; + wait(0.5); posted = true; }else{ debugSerial.printf("error...%i\n\r",postVal); + led2 = 0; posted = false; } } @@ -162,77 +236,29 @@ int main() { //setup led1=0; + LCD_D7 = 1; debugSerial.baud(115200); // the baud rate here needs to match the esp-link config espSerial.baud(115200); - - debugSerial.printf("STM-Client starting!\n\r"); - wait(0.5); - - espSerial.printf("STM-Client starting!\n\r"); - wait(0.5); - - // Sync-up with esp-link, this is required at the start of any sketch and initializes the - // callbacks to the wifi status change callback. The callback gets called with the initial - // status right after Sync() below completes. - esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) - bool ok; - do { - //debugSerial.printf("main syncing..\n\r"); - wait(0.5); - ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds - if (!ok){ - debugSerial.printf("STM-Client sync failed!\n\r"); - wait(0.5); - } - } while(!ok); + espSerial.printf("*********SunLeaf starting!**************\n\r"); + debugSerial.printf("*********SunLeaf starting!**************\n\r"); - debugSerial.printf("STM-Client synced!\n\r"); - - // Get immediate wifi status info for demo purposes. This is not normally used because the - // wifi status callback registered above gets called immediately. - esp.GetWifiStatus(); - STMClientPacket *packet; - if ((packet=esp.WaitReturn()) != NULL) - { - //debugSerial.printf("Wifi status: %i\n\r", packet->value); - //debugSerial.printf("waiting for wifi status...\n\r"); - if(packet->value >= 1) { ///ideally this would coincide with STATION_GOT_IP... - debugSerial.printf("WIFI CONNECTED\n\r"); - wifiConnected = true; - } else { - debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value); - //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 - //int err = rest.begin("www.timeapi.org"); //for basic example of get - const char* host = "api.thingspeak.com"; - //const char* host = "posttestserver.com"; - //const char* host = "vivaplanetbusservicedev.servicebus.windows.net"; - uint16_t port = 80; - bool security = true; - int err = rest.begin(host,port,security); - - if (err != 0) { - debugSerial.printf("REST begin failed: %i\n\r",err); - while(1) ; - } - debugSerial.printf("STM-REST ready\n\r"); - - //post_data.attach(&loop_post,15.0); //fire a post every minute + syncESP(); //sync the ESP + beginREST(); //resync the REST + flipper.attach(&flip, 60.0); // the address of the function to be attached (flip) and the interval (2 seconds) - while(true){ - + while(true){ if(posted) { - debugSerial.printf("sleeping..."); + debugSerial.printf("sleeping...\n\r"); posted = false; + esp.Sleep(); //put the esp to sleep + ESPisAsleep = true; + led2 = 0; //turn off all other power and sensors + sleep(); //then sleep }else{ + LCD_D7 = 1; led2 = !led2; wait(0.5); // give everything a second to wake up }