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 24 16:29:18 2016 +0000
Revision:
10:ae1d07ffddea
Parent:
9:bd7f083d55ad
Child:
11:0b6425f59e17
added some sensors;

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