![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@7:ff4efdc27514, 2016-08-16 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Tue Aug 16 16:04:33 2016 +0000
- Revision:
- 7:ff4efdc27514
- Parent:
- 6:faf57101b59c
- Child:
- 8:6a3b7c5d9ba7
added random number functionality...;
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 | 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 | 0:70a6082c1bf7 | 21 | |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | // Initialize a connection to esp-link using the normal hardware serial port both for |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | // SLIP and for debug messages. |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | STMClient esp(&espSerial, &debugSerial); |
ShaneKirkbride | 0:70a6082c1bf7 | 25 | |
ShaneKirkbride | 0:70a6082c1bf7 | 26 | // Initialize a REST client on the connection to esp-link |
ShaneKirkbride | 0:70a6082c1bf7 | 27 | STMClientRest rest(&esp); |
ShaneKirkbride | 0:70a6082c1bf7 | 28 | bool wifiConnected = false; |
ShaneKirkbride | 4:31bed73a0d08 | 29 | Ticker flipper; |
ShaneKirkbride | 4:31bed73a0d08 | 30 | Ticker post_data; |
ShaneKirkbride | 4:31bed73a0d08 | 31 | bool posted = false; |
ShaneKirkbride | 6:faf57101b59c | 32 | |
ShaneKirkbride | 5:9f3015b18b24 | 33 | //generate some random numbers |
ShaneKirkbride | 5:9f3015b18b24 | 34 | int random_number(int min_num, int max_num); |
ShaneKirkbride | 5:9f3015b18b24 | 35 | int random_number(int min_num, int max_num){ |
ShaneKirkbride | 7:ff4efdc27514 | 36 | int result=0,low_num=0,hi_num=0; |
ShaneKirkbride | 7:ff4efdc27514 | 37 | if(min_num<max_num) |
ShaneKirkbride | 7:ff4efdc27514 | 38 | { |
ShaneKirkbride | 7:ff4efdc27514 | 39 | low_num=min_num; |
ShaneKirkbride | 7:ff4efdc27514 | 40 | hi_num=max_num+1; // this is done to include max_num in output. |
ShaneKirkbride | 7:ff4efdc27514 | 41 | }else{ |
ShaneKirkbride | 7:ff4efdc27514 | 42 | low_num=max_num+1;// this is done to include max_num in output. |
ShaneKirkbride | 7:ff4efdc27514 | 43 | hi_num=min_num; |
ShaneKirkbride | 7:ff4efdc27514 | 44 | } |
ShaneKirkbride | 7:ff4efdc27514 | 45 | srand(time(NULL)); |
ShaneKirkbride | 7:ff4efdc27514 | 46 | result = (rand()%(hi_num-low_num))+low_num; |
ShaneKirkbride | 7:ff4efdc27514 | 47 | return result; |
ShaneKirkbride | 5:9f3015b18b24 | 48 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 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 | 4:31bed73a0d08 | 70 | #define BUFLEN 100 |
ShaneKirkbride | 4:31bed73a0d08 | 71 | char response[BUFLEN]; |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | |
ShaneKirkbride | 1:99c58a942425 | 73 | char *loop_get() { |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | //debugSerial.printf("begin main loop \n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | // process any callbacks coming from esp_link |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | esp.Process(); |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected); |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | // if we're connected make an HTTP request |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | while(wifiConnected) { |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | // Request /utc/now from the previously set-up server |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | rest.get("/utc/now"); |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | |
ShaneKirkbride | 1:99c58a942425 | 83 | //char response[BUFLEN]; |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | memset(response, 0, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | uint16_t code = rest.waitResponse(response, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | if(code == HTTP_STATUS_OK){ |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | debugSerial.printf("STM: GET successful: %s\n\r", response); |
ShaneKirkbride | 1:99c58a942425 | 88 | wait(60); |
ShaneKirkbride | 1:99c58a942425 | 89 | return response; |
ShaneKirkbride | 0:70a6082c1bf7 | 90 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 91 | debugSerial.printf("STM: GET failed: %i\n\r",code); |
ShaneKirkbride | 1:99c58a942425 | 92 | wait(60); |
ShaneKirkbride | 1:99c58a942425 | 93 | return (char *)code; |
ShaneKirkbride | 1:99c58a942425 | 94 | } |
ShaneKirkbride | 1:99c58a942425 | 95 | } |
ShaneKirkbride | 1:99c58a942425 | 96 | return "0"; |
ShaneKirkbride | 1:99c58a942425 | 97 | } |
ShaneKirkbride | 1:99c58a942425 | 98 | |
ShaneKirkbride | 5:9f3015b18b24 | 99 | |
ShaneKirkbride | 1:99c58a942425 | 100 | int loop_post() { |
ShaneKirkbride | 4:31bed73a0d08 | 101 | posted = false; |
ShaneKirkbride | 4:31bed73a0d08 | 102 | |
ShaneKirkbride | 1:99c58a942425 | 103 | // process any callbacks coming from esp_link |
ShaneKirkbride | 1:99c58a942425 | 104 | esp.Process(); |
ShaneKirkbride | 4:31bed73a0d08 | 105 | |
ShaneKirkbride | 1:99c58a942425 | 106 | debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected); |
ShaneKirkbride | 1:99c58a942425 | 107 | // if we're connected make an HTTP request |
ShaneKirkbride | 4:31bed73a0d08 | 108 | if(wifiConnected) { |
ShaneKirkbride | 5:9f3015b18b24 | 109 | //this is where the calls to the sensor drivers should go. |
ShaneKirkbride | 5:9f3015b18b24 | 110 | //ideally they will be blocking so they don't return a value and allow the program to move on until each |
ShaneKirkbride | 5:9f3015b18b24 | 111 | //function is complete |
ShaneKirkbride | 5:9f3015b18b24 | 112 | int Tint = random_number(65, 99); |
ShaneKirkbride | 5:9f3015b18b24 | 113 | int Lint = random_number(0, 99); |
ShaneKirkbride | 5:9f3015b18b24 | 114 | int Hint = random_number(20, 25); |
ShaneKirkbride | 5:9f3015b18b24 | 115 | |
ShaneKirkbride | 5:9f3015b18b24 | 116 | char T[2]; |
ShaneKirkbride | 5:9f3015b18b24 | 117 | sprintf(T, "%d", Tint); |
ShaneKirkbride | 5:9f3015b18b24 | 118 | char L[2]; |
ShaneKirkbride | 5:9f3015b18b24 | 119 | sprintf(L, "%d", Lint); |
ShaneKirkbride | 5:9f3015b18b24 | 120 | char H[2]; |
ShaneKirkbride | 5:9f3015b18b24 | 121 | sprintf(H, "%d", Hint); |
ShaneKirkbride | 5:9f3015b18b24 | 122 | //debugSerial.printf("H: %s \n\r",H); //check to make sure the value is the same |
ShaneKirkbride | 4:31bed73a0d08 | 123 | |
ShaneKirkbride | 4:31bed73a0d08 | 124 | /**make sure that the size of this array is consistent with the input string**/ |
ShaneKirkbride | 3:8ed85d940c4c | 125 | const char* body = ""; |
ShaneKirkbride | 4:31bed73a0d08 | 126 | char output [62]; |
ShaneKirkbride | 3:8ed85d940c4c | 127 | sprintf(output, "/update?api_key=3FF5CTKAJIU2IH0M&field1=%s&field2=%s&field3=%s", T,L,H); |
ShaneKirkbride | 3:8ed85d940c4c | 128 | //debugSerial.printf("output: %s \n\r", output); |
ShaneKirkbride | 4:31bed73a0d08 | 129 | //debugSerial.printf("size: %i \n\r", strlen(output)); |
ShaneKirkbride | 2:20ea1be14e4b | 130 | |
ShaneKirkbride | 3:8ed85d940c4c | 131 | rest.post(output, body); //basic post test |
ShaneKirkbride | 3:8ed85d940c4c | 132 | |
ShaneKirkbride | 1:99c58a942425 | 133 | char response[BUFLEN]; |
ShaneKirkbride | 1:99c58a942425 | 134 | memset(response, 0, BUFLEN); |
ShaneKirkbride | 1:99c58a942425 | 135 | uint16_t code = rest.waitResponse(response, BUFLEN); |
ShaneKirkbride | 1:99c58a942425 | 136 | if(code == HTTP_STATUS_OK){ |
ShaneKirkbride | 1:99c58a942425 | 137 | debugSerial.printf("STM: POST successful: %s\n\r", response); |
ShaneKirkbride | 2:20ea1be14e4b | 138 | }else { |
ShaneKirkbride | 1:99c58a942425 | 139 | debugSerial.printf("STM: POST failed: %i\n\r",code); |
ShaneKirkbride | 4:31bed73a0d08 | 140 | return 1; |
ShaneKirkbride | 0:70a6082c1bf7 | 141 | } |
ShaneKirkbride | 4:31bed73a0d08 | 142 | }else{ |
ShaneKirkbride | 4:31bed73a0d08 | 143 | debugSerial.printf("STM: wifi NOT connected: %i\n\r",wifiConnected); |
ShaneKirkbride | 4:31bed73a0d08 | 144 | return 1; |
ShaneKirkbride | 4:31bed73a0d08 | 145 | } |
ShaneKirkbride | 4:31bed73a0d08 | 146 | //debugSerial.printf("Exiting...\n\r"); |
ShaneKirkbride | 4:31bed73a0d08 | 147 | return 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 148 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 149 | |
ShaneKirkbride | 4:31bed73a0d08 | 150 | void flip() { |
ShaneKirkbride | 4:31bed73a0d08 | 151 | |
ShaneKirkbride | 4:31bed73a0d08 | 152 | int postVal = loop_post(); |
ShaneKirkbride | 4:31bed73a0d08 | 153 | if(!postVal) |
ShaneKirkbride | 4:31bed73a0d08 | 154 | { |
ShaneKirkbride | 4:31bed73a0d08 | 155 | posted = true; |
ShaneKirkbride | 4:31bed73a0d08 | 156 | }else{ |
ShaneKirkbride | 4:31bed73a0d08 | 157 | debugSerial.printf("error...%i\n\r",postVal); |
ShaneKirkbride | 4:31bed73a0d08 | 158 | posted = false; |
ShaneKirkbride | 4:31bed73a0d08 | 159 | } |
ShaneKirkbride | 4:31bed73a0d08 | 160 | } |
ShaneKirkbride | 1:99c58a942425 | 161 | |
ShaneKirkbride | 0:70a6082c1bf7 | 162 | int main() { |
ShaneKirkbride | 4:31bed73a0d08 | 163 | //setup |
ShaneKirkbride | 0:70a6082c1bf7 | 164 | led1=0; |
ShaneKirkbride | 0:70a6082c1bf7 | 165 | debugSerial.baud(115200); // the baud rate here needs to match the esp-link config |
ShaneKirkbride | 0:70a6082c1bf7 | 166 | espSerial.baud(115200); |
ShaneKirkbride | 0:70a6082c1bf7 | 167 | |
ShaneKirkbride | 0:70a6082c1bf7 | 168 | debugSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 169 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 170 | |
ShaneKirkbride | 0:70a6082c1bf7 | 171 | espSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 172 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 173 | |
ShaneKirkbride | 0:70a6082c1bf7 | 174 | // Sync-up with esp-link, this is required at the start of any sketch and initializes the |
ShaneKirkbride | 0:70a6082c1bf7 | 175 | // callbacks to the wifi status change callback. The callback gets called with the initial |
ShaneKirkbride | 0:70a6082c1bf7 | 176 | // status right after Sync() below completes. |
ShaneKirkbride | 0:70a6082c1bf7 | 177 | esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) |
ShaneKirkbride | 0:70a6082c1bf7 | 178 | bool ok; |
ShaneKirkbride | 0:70a6082c1bf7 | 179 | do { |
ShaneKirkbride | 0:70a6082c1bf7 | 180 | //debugSerial.printf("main syncing..\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 181 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 182 | ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds |
ShaneKirkbride | 0:70a6082c1bf7 | 183 | if (!ok){ |
ShaneKirkbride | 0:70a6082c1bf7 | 184 | debugSerial.printf("STM-Client sync failed!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 185 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 186 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 187 | } while(!ok); |
ShaneKirkbride | 0:70a6082c1bf7 | 188 | |
ShaneKirkbride | 0:70a6082c1bf7 | 189 | debugSerial.printf("STM-Client synced!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 190 | |
ShaneKirkbride | 0:70a6082c1bf7 | 191 | // Get immediate wifi status info for demo purposes. This is not normally used because the |
ShaneKirkbride | 0:70a6082c1bf7 | 192 | // wifi status callback registered above gets called immediately. |
ShaneKirkbride | 0:70a6082c1bf7 | 193 | esp.GetWifiStatus(); |
ShaneKirkbride | 0:70a6082c1bf7 | 194 | STMClientPacket *packet; |
ShaneKirkbride | 0:70a6082c1bf7 | 195 | if ((packet=esp.WaitReturn()) != NULL) |
ShaneKirkbride | 0:70a6082c1bf7 | 196 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 197 | //debugSerial.printf("Wifi status: %i\n\r", packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 198 | //debugSerial.printf("waiting for wifi status...\n\r"); |
ShaneKirkbride | 1:99c58a942425 | 199 | if(packet->value >= 1) { ///ideally this would coincide with STATION_GOT_IP... |
ShaneKirkbride | 0:70a6082c1bf7 | 200 | debugSerial.printf("WIFI CONNECTED\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 201 | wifiConnected = true; |
ShaneKirkbride | 0:70a6082c1bf7 | 202 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 203 | debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 204 | //Serial.printf(status); |
ShaneKirkbride | 0:70a6082c1bf7 | 205 | wifiConnected = false; |
ShaneKirkbride | 0:70a6082c1bf7 | 206 | } |
ShaneKirkbride | 4:31bed73a0d08 | 207 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 208 | |
ShaneKirkbride | 0:70a6082c1bf7 | 209 | // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server, |
ShaneKirkbride | 0:70a6082c1bf7 | 210 | // it just sets-up stuff on the esp-link side |
ShaneKirkbride | 2:20ea1be14e4b | 211 | //int err = rest.begin("www.timeapi.org"); //for basic example of get |
ShaneKirkbride | 3:8ed85d940c4c | 212 | const char* host = "api.thingspeak.com"; |
ShaneKirkbride | 3:8ed85d940c4c | 213 | //const char* host = "posttestserver.com"; |
ShaneKirkbride | 2:20ea1be14e4b | 214 | //const char* host = "vivaplanetbusservicedev.servicebus.windows.net"; |
ShaneKirkbride | 3:8ed85d940c4c | 215 | uint16_t port = 80; |
ShaneKirkbride | 3:8ed85d940c4c | 216 | bool security = true; |
ShaneKirkbride | 3:8ed85d940c4c | 217 | int err = rest.begin(host,port,security); |
ShaneKirkbride | 2:20ea1be14e4b | 218 | |
ShaneKirkbride | 0:70a6082c1bf7 | 219 | if (err != 0) { |
ShaneKirkbride | 0:70a6082c1bf7 | 220 | debugSerial.printf("REST begin failed: %i\n\r",err); |
ShaneKirkbride | 0:70a6082c1bf7 | 221 | while(1) ; |
ShaneKirkbride | 0:70a6082c1bf7 | 222 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 223 | debugSerial.printf("STM-REST ready\n\r"); |
ShaneKirkbride | 4:31bed73a0d08 | 224 | |
ShaneKirkbride | 4:31bed73a0d08 | 225 | //post_data.attach(&loop_post,15.0); //fire a post every minute |
ShaneKirkbride | 4:31bed73a0d08 | 226 | flipper.attach(&flip, 60.0); // the address of the function to be attached (flip) and the interval (2 seconds) |
ShaneKirkbride | 4:31bed73a0d08 | 227 | |
ShaneKirkbride | 4:31bed73a0d08 | 228 | while(true){ |
ShaneKirkbride | 4:31bed73a0d08 | 229 | |
ShaneKirkbride | 4:31bed73a0d08 | 230 | if(posted) |
ShaneKirkbride | 4:31bed73a0d08 | 231 | { |
ShaneKirkbride | 4:31bed73a0d08 | 232 | debugSerial.printf("sleeping..."); |
ShaneKirkbride | 4:31bed73a0d08 | 233 | posted = false; |
ShaneKirkbride | 4:31bed73a0d08 | 234 | sleep(); //then sleep |
ShaneKirkbride | 4:31bed73a0d08 | 235 | }else{ |
ShaneKirkbride | 4:31bed73a0d08 | 236 | led2 = !led2; |
ShaneKirkbride | 4:31bed73a0d08 | 237 | wait(0.5); // give everything a second to wake up |
ShaneKirkbride | 4:31bed73a0d08 | 238 | } |
ShaneKirkbride | 4:31bed73a0d08 | 239 | } |
ShaneKirkbride | 4:31bed73a0d08 | 240 | |
ShaneKirkbride | 4:31bed73a0d08 | 241 | } |
ShaneKirkbride | 4:31bed73a0d08 | 242 |