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

Committer:
ShaneKirkbride
Date:
Wed Aug 17 20:57:35 2016 +0000
Revision:
8:6a3b7c5d9ba7
Parent:
7:ff4efdc27514
Child:
9:bd7f083d55ad
added low power mode. Also cleaned up the code a little more....slowly but surely.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaneKirkbride 0:70a6082c1bf7 1 /**
ShaneKirkbride 0:70a6082c1bf7 2 * Simple example to demo the STM-Client REST calls
ShaneKirkbride 0:70a6082c1bf7 3 */
ShaneKirkbride 0:70a6082c1bf7 4 #include "mbed.h"
ShaneKirkbride 4:31bed73a0d08 5
ShaneKirkbride 0:70a6082c1bf7 6 #include <STMClient.h>
ShaneKirkbride 0:70a6082c1bf7 7 #include <STMClientRest.h>
ShaneKirkbride 0:70a6082c1bf7 8
ShaneKirkbride 0:70a6082c1bf7 9 /*-------- Check if platform compatible ----------*/
ShaneKirkbride 0:70a6082c1bf7 10 #if DEVICE_SERIAL_ASYNCH
ShaneKirkbride 4:31bed73a0d08 11 Serial debugSerial(SERIAL_TX, SERIAL_RX);
ShaneKirkbride 4:31bed73a0d08 12 Serial espSerial(PA_0, PA_1);
ShaneKirkbride 0:70a6082c1bf7 13 #else
ShaneKirkbride 0:70a6082c1bf7 14 #warning "Platform not compatible with Low Power APIs for Serial"
ShaneKirkbride 0:70a6082c1bf7 15 Serial debugSerial(SERIAL_TX, SERIAL_RX);
ShaneKirkbride 0:70a6082c1bf7 16 Serial espSerial(PA_0, PA_1);
ShaneKirkbride 0:70a6082c1bf7 17 #endif
ShaneKirkbride 0:70a6082c1bf7 18
ShaneKirkbride 4:31bed73a0d08 19 DigitalOut led1(LED1);
ShaneKirkbride 4:31bed73a0d08 20 DigitalOut led2(LED2);
ShaneKirkbride 8:6a3b7c5d9ba7 21 DigitalOut LCD_D7(D7);
ShaneKirkbride 0:70a6082c1bf7 22
ShaneKirkbride 0:70a6082c1bf7 23 // Initialize a connection to esp-link using the normal hardware serial port both for
ShaneKirkbride 0:70a6082c1bf7 24 // SLIP and for debug messages.
ShaneKirkbride 0:70a6082c1bf7 25 STMClient esp(&espSerial, &debugSerial);
ShaneKirkbride 0:70a6082c1bf7 26
ShaneKirkbride 0:70a6082c1bf7 27 // Initialize a REST client on the connection to esp-link
ShaneKirkbride 0:70a6082c1bf7 28 STMClientRest rest(&esp);
ShaneKirkbride 0:70a6082c1bf7 29 bool wifiConnected = false;
ShaneKirkbride 4:31bed73a0d08 30 Ticker flipper;
ShaneKirkbride 4:31bed73a0d08 31 Ticker post_data;
ShaneKirkbride 4:31bed73a0d08 32 bool posted = false;
ShaneKirkbride 8:6a3b7c5d9ba7 33 bool ESPisAsleep = false;
ShaneKirkbride 0:70a6082c1bf7 34 // Callback made from esp-link to notify of wifi status changes
ShaneKirkbride 0:70a6082c1bf7 35 // Here we print something out and set a global flag
ShaneKirkbride 0:70a6082c1bf7 36 void wifiCb(void *response) {
ShaneKirkbride 0:70a6082c1bf7 37 debugSerial.printf("waiting for wifi status...\n\r"); //debug
ShaneKirkbride 0:70a6082c1bf7 38 STMClientResponse *res = (STMClientResponse*)response;
ShaneKirkbride 0:70a6082c1bf7 39 if (res->argc() == 1) {
ShaneKirkbride 0:70a6082c1bf7 40 uint8_t status;
ShaneKirkbride 0:70a6082c1bf7 41 res->popArg(&status, 1);
ShaneKirkbride 0:70a6082c1bf7 42 debugSerial.printf("waiting for wifi status...\n\r");
ShaneKirkbride 0:70a6082c1bf7 43 if(status == STATION_GOT_IP) {
ShaneKirkbride 0:70a6082c1bf7 44 debugSerial.printf("WIFI CONNECTED");
ShaneKirkbride 0:70a6082c1bf7 45 wifiConnected = true;
ShaneKirkbride 0:70a6082c1bf7 46 } else {
ShaneKirkbride 0:70a6082c1bf7 47 debugSerial.printf("WIFI NOT READY: %i",status);
ShaneKirkbride 0:70a6082c1bf7 48 //Serial.printf(status);
ShaneKirkbride 0:70a6082c1bf7 49 wifiConnected = false;
ShaneKirkbride 0:70a6082c1bf7 50 }
ShaneKirkbride 0:70a6082c1bf7 51 }
ShaneKirkbride 0:70a6082c1bf7 52 }
ShaneKirkbride 0:70a6082c1bf7 53
ShaneKirkbride 8:6a3b7c5d9ba7 54 //resync after sleep mode
ShaneKirkbride 8:6a3b7c5d9ba7 55 void syncESP(){
ShaneKirkbride 8:6a3b7c5d9ba7 56
ShaneKirkbride 8:6a3b7c5d9ba7 57 // Sync-up with esp-link, this is required at the start of any sketch and initializes the
ShaneKirkbride 8:6a3b7c5d9ba7 58 // callbacks to the wifi status change callback. The callback gets called with the initial
ShaneKirkbride 8:6a3b7c5d9ba7 59 // status right after Sync() below completes.
ShaneKirkbride 8:6a3b7c5d9ba7 60 esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired)
ShaneKirkbride 8:6a3b7c5d9ba7 61 bool ok;
ShaneKirkbride 8:6a3b7c5d9ba7 62 do {
ShaneKirkbride 8:6a3b7c5d9ba7 63 //debugSerial.printf("main syncing..\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 64 wait(0.5);
ShaneKirkbride 8:6a3b7c5d9ba7 65 ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds
ShaneKirkbride 8:6a3b7c5d9ba7 66 if (!ok){
ShaneKirkbride 8:6a3b7c5d9ba7 67 debugSerial.printf("STM-Client sync failed!\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 68 wait(0.5);
ShaneKirkbride 8:6a3b7c5d9ba7 69 }
ShaneKirkbride 8:6a3b7c5d9ba7 70 } while(!ok);
ShaneKirkbride 8:6a3b7c5d9ba7 71
ShaneKirkbride 8:6a3b7c5d9ba7 72 debugSerial.printf("STM-Client synced!\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 73
ShaneKirkbride 8:6a3b7c5d9ba7 74 // Get immediate wifi status info for demo purposes. This is not normally used because the
ShaneKirkbride 8:6a3b7c5d9ba7 75 // wifi status callback registered above gets called immediately.
ShaneKirkbride 8:6a3b7c5d9ba7 76 esp.GetWifiStatus();
ShaneKirkbride 8:6a3b7c5d9ba7 77
ShaneKirkbride 8:6a3b7c5d9ba7 78 STMClientPacket *packet;
ShaneKirkbride 8:6a3b7c5d9ba7 79 if ((packet=esp.WaitReturn()) != NULL)
ShaneKirkbride 8:6a3b7c5d9ba7 80 {
ShaneKirkbride 8:6a3b7c5d9ba7 81 //debugSerial.printf("Wifi status: %i\n\r", packet->value);
ShaneKirkbride 8:6a3b7c5d9ba7 82 //debugSerial.printf("waiting for wifi status...\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 83 if(packet->value >= 1) { ///ideally this would coincide with STATION_GOT_IP...
ShaneKirkbride 8:6a3b7c5d9ba7 84 debugSerial.printf("WIFI CONNECTED\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 85 wifiConnected = true;
ShaneKirkbride 8:6a3b7c5d9ba7 86 } else {
ShaneKirkbride 8:6a3b7c5d9ba7 87 debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value);
ShaneKirkbride 8:6a3b7c5d9ba7 88 //Serial.printf(status);
ShaneKirkbride 8:6a3b7c5d9ba7 89 wifiConnected = false;
ShaneKirkbride 8:6a3b7c5d9ba7 90 }
ShaneKirkbride 8:6a3b7c5d9ba7 91 }
ShaneKirkbride 8:6a3b7c5d9ba7 92 }
ShaneKirkbride 8:6a3b7c5d9ba7 93 //Start/Restart REST...can pass some fancy arguments to this in the future.
ShaneKirkbride 8:6a3b7c5d9ba7 94 void beginREST(){
ShaneKirkbride 8:6a3b7c5d9ba7 95
ShaneKirkbride 8:6a3b7c5d9ba7 96 // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server,
ShaneKirkbride 8:6a3b7c5d9ba7 97 // it just sets-up stuff on the esp-link side
ShaneKirkbride 8:6a3b7c5d9ba7 98 //int err = rest.begin("www.timeapi.org"); //for basic example of get
ShaneKirkbride 8:6a3b7c5d9ba7 99 const char* host = "api.thingspeak.com";
ShaneKirkbride 8:6a3b7c5d9ba7 100 //const char* host = "posttestserver.com";
ShaneKirkbride 8:6a3b7c5d9ba7 101 //const char* host = "vivaplanetbusservicedev.servicebus.windows.net";
ShaneKirkbride 8:6a3b7c5d9ba7 102 uint16_t port = 80;
ShaneKirkbride 8:6a3b7c5d9ba7 103 bool security = true;
ShaneKirkbride 8:6a3b7c5d9ba7 104 int err = rest.begin(host,port,security);
ShaneKirkbride 8:6a3b7c5d9ba7 105 if (err != 0) {
ShaneKirkbride 8:6a3b7c5d9ba7 106 debugSerial.printf("REST begin failed: %i\n\r",err);
ShaneKirkbride 8:6a3b7c5d9ba7 107 while(1) ;
ShaneKirkbride 8:6a3b7c5d9ba7 108 }
ShaneKirkbride 8:6a3b7c5d9ba7 109 debugSerial.printf("STM-REST ready\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 110 }
ShaneKirkbride 8:6a3b7c5d9ba7 111
ShaneKirkbride 8:6a3b7c5d9ba7 112 //generate some random numbers
ShaneKirkbride 8:6a3b7c5d9ba7 113 int random_number(int min_num, int max_num){
ShaneKirkbride 8:6a3b7c5d9ba7 114 int result=0,low_num=0,hi_num=0;
ShaneKirkbride 8:6a3b7c5d9ba7 115 if(min_num<max_num)
ShaneKirkbride 8:6a3b7c5d9ba7 116 {
ShaneKirkbride 8:6a3b7c5d9ba7 117 low_num=min_num;
ShaneKirkbride 8:6a3b7c5d9ba7 118 hi_num=max_num+1; // this is done to include max_num in output.
ShaneKirkbride 8:6a3b7c5d9ba7 119 }else{
ShaneKirkbride 8:6a3b7c5d9ba7 120 low_num=max_num+1;// this is done to include max_num in output.
ShaneKirkbride 8:6a3b7c5d9ba7 121 hi_num=min_num;
ShaneKirkbride 8:6a3b7c5d9ba7 122 }
ShaneKirkbride 8:6a3b7c5d9ba7 123 srand(time(NULL));
ShaneKirkbride 8:6a3b7c5d9ba7 124 result = (rand()%(hi_num-low_num))+low_num;
ShaneKirkbride 8:6a3b7c5d9ba7 125 return result;
ShaneKirkbride 8:6a3b7c5d9ba7 126 }
ShaneKirkbride 8:6a3b7c5d9ba7 127
ShaneKirkbride 8:6a3b7c5d9ba7 128
ShaneKirkbride 4:31bed73a0d08 129 #define BUFLEN 100
ShaneKirkbride 4:31bed73a0d08 130 char response[BUFLEN];
ShaneKirkbride 0:70a6082c1bf7 131
ShaneKirkbride 1:99c58a942425 132 char *loop_get() {
ShaneKirkbride 0:70a6082c1bf7 133 //debugSerial.printf("begin main loop \n\r");
ShaneKirkbride 0:70a6082c1bf7 134 // process any callbacks coming from esp_link
ShaneKirkbride 0:70a6082c1bf7 135 esp.Process();
ShaneKirkbride 0:70a6082c1bf7 136 debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected);
ShaneKirkbride 0:70a6082c1bf7 137 // if we're connected make an HTTP request
ShaneKirkbride 0:70a6082c1bf7 138 while(wifiConnected) {
ShaneKirkbride 0:70a6082c1bf7 139 // Request /utc/now from the previously set-up server
ShaneKirkbride 0:70a6082c1bf7 140 rest.get("/utc/now");
ShaneKirkbride 0:70a6082c1bf7 141
ShaneKirkbride 1:99c58a942425 142 //char response[BUFLEN];
ShaneKirkbride 0:70a6082c1bf7 143 memset(response, 0, BUFLEN);
ShaneKirkbride 0:70a6082c1bf7 144 uint16_t code = rest.waitResponse(response, BUFLEN);
ShaneKirkbride 0:70a6082c1bf7 145 if(code == HTTP_STATUS_OK){
ShaneKirkbride 0:70a6082c1bf7 146 debugSerial.printf("STM: GET successful: %s\n\r", response);
ShaneKirkbride 1:99c58a942425 147 wait(60);
ShaneKirkbride 1:99c58a942425 148 return response;
ShaneKirkbride 0:70a6082c1bf7 149 } else {
ShaneKirkbride 0:70a6082c1bf7 150 debugSerial.printf("STM: GET failed: %i\n\r",code);
ShaneKirkbride 1:99c58a942425 151 wait(60);
ShaneKirkbride 1:99c58a942425 152 return (char *)code;
ShaneKirkbride 1:99c58a942425 153 }
ShaneKirkbride 1:99c58a942425 154 }
ShaneKirkbride 1:99c58a942425 155 return "0";
ShaneKirkbride 1:99c58a942425 156 }
ShaneKirkbride 1:99c58a942425 157
ShaneKirkbride 5:9f3015b18b24 158
ShaneKirkbride 1:99c58a942425 159 int loop_post() {
ShaneKirkbride 4:31bed73a0d08 160 posted = false;
ShaneKirkbride 4:31bed73a0d08 161
ShaneKirkbride 1:99c58a942425 162 // process any callbacks coming from esp_link
ShaneKirkbride 1:99c58a942425 163 esp.Process();
ShaneKirkbride 4:31bed73a0d08 164
ShaneKirkbride 1:99c58a942425 165 debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected);
ShaneKirkbride 1:99c58a942425 166 // if we're connected make an HTTP request
ShaneKirkbride 4:31bed73a0d08 167 if(wifiConnected) {
ShaneKirkbride 5:9f3015b18b24 168 //this is where the calls to the sensor drivers should go.
ShaneKirkbride 5:9f3015b18b24 169 //ideally they will be blocking so they don't return a value and allow the program to move on until each
ShaneKirkbride 5:9f3015b18b24 170 //function is complete
ShaneKirkbride 8:6a3b7c5d9ba7 171 debugSerial.printf("geting measurements...\n\r");
ShaneKirkbride 5:9f3015b18b24 172 int Tint = random_number(65, 99);
ShaneKirkbride 5:9f3015b18b24 173 int Lint = random_number(0, 99);
ShaneKirkbride 5:9f3015b18b24 174 int Hint = random_number(20, 25);
ShaneKirkbride 5:9f3015b18b24 175
ShaneKirkbride 5:9f3015b18b24 176 char T[2];
ShaneKirkbride 5:9f3015b18b24 177 sprintf(T, "%d", Tint);
ShaneKirkbride 5:9f3015b18b24 178 char L[2];
ShaneKirkbride 5:9f3015b18b24 179 sprintf(L, "%d", Lint);
ShaneKirkbride 5:9f3015b18b24 180 char H[2];
ShaneKirkbride 5:9f3015b18b24 181 sprintf(H, "%d", Hint);
ShaneKirkbride 5:9f3015b18b24 182 //debugSerial.printf("H: %s \n\r",H); //check to make sure the value is the same
ShaneKirkbride 4:31bed73a0d08 183
ShaneKirkbride 4:31bed73a0d08 184 /**make sure that the size of this array is consistent with the input string**/
ShaneKirkbride 3:8ed85d940c4c 185 const char* body = "";
ShaneKirkbride 4:31bed73a0d08 186 char output [62];
ShaneKirkbride 3:8ed85d940c4c 187 sprintf(output, "/update?api_key=3FF5CTKAJIU2IH0M&field1=%s&field2=%s&field3=%s", T,L,H);
ShaneKirkbride 3:8ed85d940c4c 188 //debugSerial.printf("output: %s \n\r", output);
ShaneKirkbride 4:31bed73a0d08 189 //debugSerial.printf("size: %i \n\r", strlen(output));
ShaneKirkbride 8:6a3b7c5d9ba7 190 debugSerial.printf("sending data...\n\r");
ShaneKirkbride 3:8ed85d940c4c 191 rest.post(output, body); //basic post test
ShaneKirkbride 3:8ed85d940c4c 192
ShaneKirkbride 1:99c58a942425 193 char response[BUFLEN];
ShaneKirkbride 1:99c58a942425 194 memset(response, 0, BUFLEN);
ShaneKirkbride 1:99c58a942425 195 uint16_t code = rest.waitResponse(response, BUFLEN);
ShaneKirkbride 1:99c58a942425 196 if(code == HTTP_STATUS_OK){
ShaneKirkbride 1:99c58a942425 197 debugSerial.printf("STM: POST successful: %s\n\r", response);
ShaneKirkbride 2:20ea1be14e4b 198 }else {
ShaneKirkbride 1:99c58a942425 199 debugSerial.printf("STM: POST failed: %i\n\r",code);
ShaneKirkbride 4:31bed73a0d08 200 return 1;
ShaneKirkbride 0:70a6082c1bf7 201 }
ShaneKirkbride 4:31bed73a0d08 202 }else{
ShaneKirkbride 4:31bed73a0d08 203 debugSerial.printf("STM: wifi NOT connected: %i\n\r",wifiConnected);
ShaneKirkbride 4:31bed73a0d08 204 return 1;
ShaneKirkbride 4:31bed73a0d08 205 }
ShaneKirkbride 4:31bed73a0d08 206 //debugSerial.printf("Exiting...\n\r");
ShaneKirkbride 4:31bed73a0d08 207 return 0;
ShaneKirkbride 0:70a6082c1bf7 208 }
ShaneKirkbride 0:70a6082c1bf7 209
ShaneKirkbride 4:31bed73a0d08 210 void flip() {
ShaneKirkbride 8:6a3b7c5d9ba7 211 if(ESPisAsleep){
ShaneKirkbride 8:6a3b7c5d9ba7 212 debugSerial.printf("syncing...\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 213 LCD_D7 = 0;
ShaneKirkbride 8:6a3b7c5d9ba7 214 wait(0.5);
ShaneKirkbride 8:6a3b7c5d9ba7 215 LCD_D7 = 1;
ShaneKirkbride 8:6a3b7c5d9ba7 216 wait(20);
ShaneKirkbride 8:6a3b7c5d9ba7 217 syncESP();
ShaneKirkbride 8:6a3b7c5d9ba7 218 ESPisAsleep = false;
ShaneKirkbride 8:6a3b7c5d9ba7 219 debugSerial.printf("restarting REST...\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 220 beginREST();
ShaneKirkbride 8:6a3b7c5d9ba7 221 }
ShaneKirkbride 8:6a3b7c5d9ba7 222 debugSerial.printf("posting...\n\r");
ShaneKirkbride 4:31bed73a0d08 223 int postVal = loop_post();
ShaneKirkbride 4:31bed73a0d08 224 if(!postVal)
ShaneKirkbride 4:31bed73a0d08 225 {
ShaneKirkbride 8:6a3b7c5d9ba7 226 led2 = 1;
ShaneKirkbride 8:6a3b7c5d9ba7 227 wait(0.5);
ShaneKirkbride 4:31bed73a0d08 228 posted = true;
ShaneKirkbride 4:31bed73a0d08 229 }else{
ShaneKirkbride 4:31bed73a0d08 230 debugSerial.printf("error...%i\n\r",postVal);
ShaneKirkbride 8:6a3b7c5d9ba7 231 led2 = 0;
ShaneKirkbride 4:31bed73a0d08 232 posted = false;
ShaneKirkbride 4:31bed73a0d08 233 }
ShaneKirkbride 4:31bed73a0d08 234 }
ShaneKirkbride 1:99c58a942425 235
ShaneKirkbride 0:70a6082c1bf7 236 int main() {
ShaneKirkbride 4:31bed73a0d08 237 //setup
ShaneKirkbride 0:70a6082c1bf7 238 led1=0;
ShaneKirkbride 8:6a3b7c5d9ba7 239 LCD_D7 = 1;
ShaneKirkbride 0:70a6082c1bf7 240 debugSerial.baud(115200); // the baud rate here needs to match the esp-link config
ShaneKirkbride 0:70a6082c1bf7 241 espSerial.baud(115200);
ShaneKirkbride 8:6a3b7c5d9ba7 242 espSerial.printf("*********SunLeaf starting!**************\n\r");
ShaneKirkbride 8:6a3b7c5d9ba7 243 debugSerial.printf("*********SunLeaf starting!**************\n\r");
ShaneKirkbride 0:70a6082c1bf7 244
ShaneKirkbride 8:6a3b7c5d9ba7 245 syncESP(); //sync the ESP
ShaneKirkbride 8:6a3b7c5d9ba7 246 beginREST(); //resync the REST
ShaneKirkbride 8:6a3b7c5d9ba7 247
ShaneKirkbride 4:31bed73a0d08 248 flipper.attach(&flip, 60.0); // the address of the function to be attached (flip) and the interval (2 seconds)
ShaneKirkbride 4:31bed73a0d08 249
ShaneKirkbride 8:6a3b7c5d9ba7 250 while(true){
ShaneKirkbride 4:31bed73a0d08 251 if(posted)
ShaneKirkbride 4:31bed73a0d08 252 {
ShaneKirkbride 8:6a3b7c5d9ba7 253 debugSerial.printf("sleeping...\n\r");
ShaneKirkbride 4:31bed73a0d08 254 posted = false;
ShaneKirkbride 8:6a3b7c5d9ba7 255 esp.Sleep(); //put the esp to sleep
ShaneKirkbride 8:6a3b7c5d9ba7 256 ESPisAsleep = true;
ShaneKirkbride 8:6a3b7c5d9ba7 257 led2 = 0; //turn off all other power and sensors
ShaneKirkbride 8:6a3b7c5d9ba7 258
ShaneKirkbride 4:31bed73a0d08 259 sleep(); //then sleep
ShaneKirkbride 4:31bed73a0d08 260 }else{
ShaneKirkbride 8:6a3b7c5d9ba7 261 LCD_D7 = 1;
ShaneKirkbride 4:31bed73a0d08 262 led2 = !led2;
ShaneKirkbride 4:31bed73a0d08 263 wait(0.5); // give everything a second to wake up
ShaneKirkbride 4:31bed73a0d08 264 }
ShaneKirkbride 4:31bed73a0d08 265 }
ShaneKirkbride 4:31bed73a0d08 266
ShaneKirkbride 4:31bed73a0d08 267 }
ShaneKirkbride 4:31bed73a0d08 268