![](/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@2:20ea1be14e4b, 2016-08-04 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Thu Aug 04 19:44:11 2016 +0000
- Revision:
- 2:20ea1be14e4b
- Parent:
- 1:99c58a942425
- Child:
- 3:8ed85d940c4c
now just posting to posttestserver.com
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 | 0:70a6082c1bf7 | 5 | #include <STMClient.h> |
ShaneKirkbride | 0:70a6082c1bf7 | 6 | #include <STMClientRest.h> |
ShaneKirkbride | 0:70a6082c1bf7 | 7 | |
ShaneKirkbride | 0:70a6082c1bf7 | 8 | /*-------- Check if platform compatible ----------*/ |
ShaneKirkbride | 0:70a6082c1bf7 | 9 | #if DEVICE_SERIAL_ASYNCH |
ShaneKirkbride | 0:70a6082c1bf7 | 10 | Serial debugSerial(SERIAL_TX, SERIAL_RX); |
ShaneKirkbride | 0:70a6082c1bf7 | 11 | Serial espSerial(PA_0, PA_1); |
ShaneKirkbride | 0:70a6082c1bf7 | 12 | #else |
ShaneKirkbride | 0:70a6082c1bf7 | 13 | #warning "Platform not compatible with Low Power APIs for Serial" |
ShaneKirkbride | 0:70a6082c1bf7 | 14 | Serial debugSerial(SERIAL_TX, SERIAL_RX); |
ShaneKirkbride | 0:70a6082c1bf7 | 15 | Serial espSerial(PA_0, PA_1); |
ShaneKirkbride | 0:70a6082c1bf7 | 16 | #endif |
ShaneKirkbride | 0:70a6082c1bf7 | 17 | |
ShaneKirkbride | 0:70a6082c1bf7 | 18 | |
ShaneKirkbride | 0:70a6082c1bf7 | 19 | DigitalOut led1(LED1); |
ShaneKirkbride | 0:70a6082c1bf7 | 20 | |
ShaneKirkbride | 0:70a6082c1bf7 | 21 | // Initialize a connection to esp-link using the normal hardware serial port both for |
ShaneKirkbride | 0:70a6082c1bf7 | 22 | // SLIP and for debug messages. |
ShaneKirkbride | 0:70a6082c1bf7 | 23 | STMClient esp(&espSerial, &debugSerial); |
ShaneKirkbride | 0:70a6082c1bf7 | 24 | |
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 | |
ShaneKirkbride | 0:70a6082c1bf7 | 29 | bool wifiConnected = false; |
ShaneKirkbride | 0:70a6082c1bf7 | 30 | |
ShaneKirkbride | 0:70a6082c1bf7 | 31 | // Callback made from esp-link to notify of wifi status changes |
ShaneKirkbride | 0:70a6082c1bf7 | 32 | // Here we print something out and set a global flag |
ShaneKirkbride | 0:70a6082c1bf7 | 33 | void wifiCb(void *response) { |
ShaneKirkbride | 0:70a6082c1bf7 | 34 | debugSerial.printf("waiting for wifi status...\n\r"); //debug |
ShaneKirkbride | 0:70a6082c1bf7 | 35 | STMClientResponse *res = (STMClientResponse*)response; |
ShaneKirkbride | 0:70a6082c1bf7 | 36 | if (res->argc() == 1) { |
ShaneKirkbride | 0:70a6082c1bf7 | 37 | uint8_t status; |
ShaneKirkbride | 0:70a6082c1bf7 | 38 | res->popArg(&status, 1); |
ShaneKirkbride | 0:70a6082c1bf7 | 39 | debugSerial.printf("waiting for wifi status...\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 40 | if(status == STATION_GOT_IP) { |
ShaneKirkbride | 0:70a6082c1bf7 | 41 | debugSerial.printf("WIFI CONNECTED"); |
ShaneKirkbride | 0:70a6082c1bf7 | 42 | wifiConnected = true; |
ShaneKirkbride | 0:70a6082c1bf7 | 43 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 44 | debugSerial.printf("WIFI NOT READY: %i",status); |
ShaneKirkbride | 0:70a6082c1bf7 | 45 | //Serial.printf(status); |
ShaneKirkbride | 0:70a6082c1bf7 | 46 | wifiConnected = false; |
ShaneKirkbride | 0:70a6082c1bf7 | 47 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 48 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 49 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 50 | |
ShaneKirkbride | 1:99c58a942425 | 51 | #define BUFLEN 512 |
ShaneKirkbride | 1:99c58a942425 | 52 | char response[BUFLEN]; |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | |
ShaneKirkbride | 1:99c58a942425 | 54 | char *loop_get() { |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | //debugSerial.printf("begin main loop \n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | // process any callbacks coming from esp_link |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | esp.Process(); |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected); |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | // if we're connected make an HTTP request |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | while(wifiConnected) { |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | // Request /utc/now from the previously set-up server |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | rest.get("/utc/now"); |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | |
ShaneKirkbride | 1:99c58a942425 | 64 | //char response[BUFLEN]; |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | memset(response, 0, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | uint16_t code = rest.waitResponse(response, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | if(code == HTTP_STATUS_OK){ |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | debugSerial.printf("STM: GET successful: %s\n\r", response); |
ShaneKirkbride | 1:99c58a942425 | 69 | wait(60); |
ShaneKirkbride | 1:99c58a942425 | 70 | return response; |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | debugSerial.printf("STM: GET failed: %i\n\r",code); |
ShaneKirkbride | 1:99c58a942425 | 73 | wait(60); |
ShaneKirkbride | 1:99c58a942425 | 74 | return (char *)code; |
ShaneKirkbride | 1:99c58a942425 | 75 | } |
ShaneKirkbride | 1:99c58a942425 | 76 | } |
ShaneKirkbride | 1:99c58a942425 | 77 | return "0"; |
ShaneKirkbride | 1:99c58a942425 | 78 | } |
ShaneKirkbride | 1:99c58a942425 | 79 | |
ShaneKirkbride | 1:99c58a942425 | 80 | int loop_post() { |
ShaneKirkbride | 1:99c58a942425 | 81 | //debugSerial.printf("begin main loop \n\r"); |
ShaneKirkbride | 1:99c58a942425 | 82 | // process any callbacks coming from esp_link |
ShaneKirkbride | 2:20ea1be14e4b | 83 | int postCount = 0; |
ShaneKirkbride | 1:99c58a942425 | 84 | esp.Process(); |
ShaneKirkbride | 1:99c58a942425 | 85 | debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected); |
ShaneKirkbride | 1:99c58a942425 | 86 | // if we're connected make an HTTP request |
ShaneKirkbride | 1:99c58a942425 | 87 | while(wifiConnected) { |
ShaneKirkbride | 2:20ea1be14e4b | 88 | postCount++; //don't post more than 3 times... |
ShaneKirkbride | 1:99c58a942425 | 89 | // Post to cloud |
ShaneKirkbride | 2:20ea1be14e4b | 90 | |
ShaneKirkbride | 2:20ea1be14e4b | 91 | //const char* header = "{Host: vivaplanetbusservicedev.servicebus.windows.net Authorization: SharedAccessSignature sr=https%3a%2f%2fvivaplanetbusservicedev.servicebus.windows.net%2fhummingbirdqueue%2fmessages&sig=QIGJfPSsortI9tZpoJ6ZUzfMdtvEaaIHA2pnmKZxNVg%3d&se=1470210743&skn=DevicePolicy Content-Length: 440 Expect: 100-continue Connection: Keep-Alive}"; |
ShaneKirkbride | 2:20ea1be14e4b | 92 | //rest.setHeader(header); |
ShaneKirkbride | 1:99c58a942425 | 93 | |
ShaneKirkbride | 2:20ea1be14e4b | 94 | //const char* body = "{\"id\":\"17564321\",\"Address\":\"$28591\",\"PhotoUrl\":\"http://vivaplanetstoragedev.blob.core.windows.net:80/devicephotoscontainer/1457a68157e446c2bc4d0af58a0ccc63.jpg?st=2016-08-02T21%3A50%3A31Z&se=2016-08-02T22%3A05%3A31Z&sr=b&sp=w&sig=rf6ovBuFXKC937b3TMY3qVSf%2F3QOYXjqJBYcwj2MUv4%3D\",\"HasPhoto\":true,\"PhotoId\":\"1457a68157e446c2bc4d0af58a0ccc63.jpg\",\"DeviceSensors\":[{\"SensorType\":\"0\",\"Reading\":\"00:03:52.7656140\",\"Value\":\"921\"},null,null,null]}"; |
ShaneKirkbride | 2:20ea1be14e4b | 95 | //rest.post("hummingbirdqueue/messages", body); //cloud post test |
ShaneKirkbride | 2:20ea1be14e4b | 96 | |
ShaneKirkbride | 2:20ea1be14e4b | 97 | const char* body = "{sunleaf: test1}"; |
ShaneKirkbride | 2:20ea1be14e4b | 98 | rest.post("/post.php", body); //basic post test |
ShaneKirkbride | 2:20ea1be14e4b | 99 | |
ShaneKirkbride | 1:99c58a942425 | 100 | char response[BUFLEN]; |
ShaneKirkbride | 1:99c58a942425 | 101 | memset(response, 0, BUFLEN); |
ShaneKirkbride | 1:99c58a942425 | 102 | uint16_t code = rest.waitResponse(response, BUFLEN); |
ShaneKirkbride | 1:99c58a942425 | 103 | if(code == HTTP_STATUS_OK){ |
ShaneKirkbride | 1:99c58a942425 | 104 | debugSerial.printf("STM: POST successful: %s\n\r", response); |
ShaneKirkbride | 2:20ea1be14e4b | 105 | }else if(postCount > 3){ |
ShaneKirkbride | 2:20ea1be14e4b | 106 | debugSerial.printf("STM: POST 3 times exiting...\n\r"); |
ShaneKirkbride | 2:20ea1be14e4b | 107 | return code; |
ShaneKirkbride | 2:20ea1be14e4b | 108 | }else { |
ShaneKirkbride | 1:99c58a942425 | 109 | debugSerial.printf("STM: POST failed: %i\n\r",code); |
ShaneKirkbride | 0:70a6082c1bf7 | 110 | return code; |
ShaneKirkbride | 0:70a6082c1bf7 | 111 | } |
ShaneKirkbride | 2:20ea1be14e4b | 112 | |
ShaneKirkbride | 1:99c58a942425 | 113 | wait(60); |
ShaneKirkbride | 0:70a6082c1bf7 | 114 | } |
ShaneKirkbride | 1:99c58a942425 | 115 | return 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 116 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 117 | |
ShaneKirkbride | 1:99c58a942425 | 118 | |
ShaneKirkbride | 0:70a6082c1bf7 | 119 | int main() { |
ShaneKirkbride | 0:70a6082c1bf7 | 120 | led1=0; |
ShaneKirkbride | 0:70a6082c1bf7 | 121 | debugSerial.baud(115200); // the baud rate here needs to match the esp-link config |
ShaneKirkbride | 0:70a6082c1bf7 | 122 | espSerial.baud(115200); |
ShaneKirkbride | 0:70a6082c1bf7 | 123 | |
ShaneKirkbride | 0:70a6082c1bf7 | 124 | debugSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 125 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 126 | |
ShaneKirkbride | 0:70a6082c1bf7 | 127 | espSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 128 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 129 | |
ShaneKirkbride | 0:70a6082c1bf7 | 130 | // Sync-up with esp-link, this is required at the start of any sketch and initializes the |
ShaneKirkbride | 0:70a6082c1bf7 | 131 | // callbacks to the wifi status change callback. The callback gets called with the initial |
ShaneKirkbride | 0:70a6082c1bf7 | 132 | // status right after Sync() below completes. |
ShaneKirkbride | 0:70a6082c1bf7 | 133 | esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) |
ShaneKirkbride | 0:70a6082c1bf7 | 134 | bool ok; |
ShaneKirkbride | 0:70a6082c1bf7 | 135 | do { |
ShaneKirkbride | 0:70a6082c1bf7 | 136 | //debugSerial.printf("main syncing..\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 137 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 138 | ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds |
ShaneKirkbride | 0:70a6082c1bf7 | 139 | if (!ok){ |
ShaneKirkbride | 0:70a6082c1bf7 | 140 | debugSerial.printf("STM-Client sync failed!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 141 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 142 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 143 | } while(!ok); |
ShaneKirkbride | 0:70a6082c1bf7 | 144 | |
ShaneKirkbride | 0:70a6082c1bf7 | 145 | debugSerial.printf("STM-Client synced!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 146 | |
ShaneKirkbride | 0:70a6082c1bf7 | 147 | // Get immediate wifi status info for demo purposes. This is not normally used because the |
ShaneKirkbride | 0:70a6082c1bf7 | 148 | // wifi status callback registered above gets called immediately. |
ShaneKirkbride | 0:70a6082c1bf7 | 149 | esp.GetWifiStatus(); |
ShaneKirkbride | 0:70a6082c1bf7 | 150 | STMClientPacket *packet; |
ShaneKirkbride | 0:70a6082c1bf7 | 151 | if ((packet=esp.WaitReturn()) != NULL) |
ShaneKirkbride | 0:70a6082c1bf7 | 152 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 153 | //debugSerial.printf("Wifi status: %i\n\r", packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 154 | //debugSerial.printf("waiting for wifi status...\n\r"); |
ShaneKirkbride | 1:99c58a942425 | 155 | if(packet->value >= 1) { ///ideally this would coincide with STATION_GOT_IP... |
ShaneKirkbride | 0:70a6082c1bf7 | 156 | debugSerial.printf("WIFI CONNECTED\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 157 | wifiConnected = true; |
ShaneKirkbride | 0:70a6082c1bf7 | 158 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 159 | debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 160 | //Serial.printf(status); |
ShaneKirkbride | 0:70a6082c1bf7 | 161 | wifiConnected = false; |
ShaneKirkbride | 0:70a6082c1bf7 | 162 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 163 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 164 | |
ShaneKirkbride | 0:70a6082c1bf7 | 165 | |
ShaneKirkbride | 0:70a6082c1bf7 | 166 | // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server, |
ShaneKirkbride | 0:70a6082c1bf7 | 167 | // it just sets-up stuff on the esp-link side |
ShaneKirkbride | 2:20ea1be14e4b | 168 | //int err = rest.begin("www.timeapi.org"); //for basic example of get |
ShaneKirkbride | 2:20ea1be14e4b | 169 | int err = rest.begin("posttestserver.com"); //for basic example of post |
ShaneKirkbride | 1:99c58a942425 | 170 | |
ShaneKirkbride | 2:20ea1be14e4b | 171 | //const char* host = "vivaplanetbusservicedev.servicebus.windows.net"; |
ShaneKirkbride | 2:20ea1be14e4b | 172 | //uint16_t port = 443; |
ShaneKirkbride | 2:20ea1be14e4b | 173 | //bool security = true; |
ShaneKirkbride | 2:20ea1be14e4b | 174 | //int err = rest.begin(host,port,security); |
ShaneKirkbride | 2:20ea1be14e4b | 175 | |
ShaneKirkbride | 0:70a6082c1bf7 | 176 | if (err != 0) { |
ShaneKirkbride | 0:70a6082c1bf7 | 177 | debugSerial.printf("REST begin failed: %i\n\r",err); |
ShaneKirkbride | 0:70a6082c1bf7 | 178 | while(1) ; |
ShaneKirkbride | 0:70a6082c1bf7 | 179 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 180 | debugSerial.printf("STM-REST ready\n\r"); |
ShaneKirkbride | 1:99c58a942425 | 181 | while(response != "0"){ |
ShaneKirkbride | 2:20ea1be14e4b | 182 | debugSerial.printf("status: %x\n\r",response); |
ShaneKirkbride | 1:99c58a942425 | 183 | //loop_get(); |
ShaneKirkbride | 1:99c58a942425 | 184 | loop_post(); |
ShaneKirkbride | 1:99c58a942425 | 185 | wait(60); |
ShaneKirkbride | 0:70a6082c1bf7 | 186 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 187 | } |