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