![](/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@0:70a6082c1bf7, 2016-07-14 (annotated)
- Committer:
- ShaneKirkbride
- Date:
- Thu Jul 14 20:38:18 2016 +0000
- Revision:
- 0:70a6082c1bf7
- Child:
- 1:99c58a942425
first commit. Rest example working
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 | 0:70a6082c1bf7 | 51 | #define BUFLEN 266 |
ShaneKirkbride | 0:70a6082c1bf7 | 52 | |
ShaneKirkbride | 0:70a6082c1bf7 | 53 | int loop() { |
ShaneKirkbride | 0:70a6082c1bf7 | 54 | //debugSerial.printf("begin main loop \n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 55 | // process any callbacks coming from esp_link |
ShaneKirkbride | 0:70a6082c1bf7 | 56 | esp.Process(); |
ShaneKirkbride | 0:70a6082c1bf7 | 57 | debugSerial.printf("Wifi Connected: %i \n\r",wifiConnected); |
ShaneKirkbride | 0:70a6082c1bf7 | 58 | // if we're connected make an HTTP request |
ShaneKirkbride | 0:70a6082c1bf7 | 59 | while(wifiConnected) { |
ShaneKirkbride | 0:70a6082c1bf7 | 60 | // Request /utc/now from the previously set-up server |
ShaneKirkbride | 0:70a6082c1bf7 | 61 | rest.get("/utc/now"); |
ShaneKirkbride | 0:70a6082c1bf7 | 62 | |
ShaneKirkbride | 0:70a6082c1bf7 | 63 | char response[BUFLEN]; |
ShaneKirkbride | 0:70a6082c1bf7 | 64 | memset(response, 0, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 65 | uint16_t code = rest.waitResponse(response, BUFLEN); |
ShaneKirkbride | 0:70a6082c1bf7 | 66 | if(code == HTTP_STATUS_OK){ |
ShaneKirkbride | 0:70a6082c1bf7 | 67 | debugSerial.printf("STM: GET successful: %s\n\r", response); |
ShaneKirkbride | 0:70a6082c1bf7 | 68 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 69 | debugSerial.printf("STM: GET failed: %i\n\r",code); |
ShaneKirkbride | 0:70a6082c1bf7 | 70 | return code; |
ShaneKirkbride | 0:70a6082c1bf7 | 71 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 72 | wait(1); |
ShaneKirkbride | 0:70a6082c1bf7 | 73 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 74 | return 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 75 | |
ShaneKirkbride | 0:70a6082c1bf7 | 76 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 77 | |
ShaneKirkbride | 0:70a6082c1bf7 | 78 | int main() { |
ShaneKirkbride | 0:70a6082c1bf7 | 79 | led1=0; |
ShaneKirkbride | 0:70a6082c1bf7 | 80 | debugSerial.baud(115200); // the baud rate here needs to match the esp-link config |
ShaneKirkbride | 0:70a6082c1bf7 | 81 | espSerial.baud(115200); |
ShaneKirkbride | 0:70a6082c1bf7 | 82 | |
ShaneKirkbride | 0:70a6082c1bf7 | 83 | debugSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 84 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 85 | |
ShaneKirkbride | 0:70a6082c1bf7 | 86 | espSerial.printf("STM-Client starting!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 87 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 88 | |
ShaneKirkbride | 0:70a6082c1bf7 | 89 | // Sync-up with esp-link, this is required at the start of any sketch and initializes the |
ShaneKirkbride | 0:70a6082c1bf7 | 90 | // callbacks to the wifi status change callback. The callback gets called with the initial |
ShaneKirkbride | 0:70a6082c1bf7 | 91 | // status right after Sync() below completes. |
ShaneKirkbride | 0:70a6082c1bf7 | 92 | esp.wifiCb.attach(wifiCb); // wifi status change callback, optional (delete if not desired) |
ShaneKirkbride | 0:70a6082c1bf7 | 93 | bool ok; |
ShaneKirkbride | 0:70a6082c1bf7 | 94 | do { |
ShaneKirkbride | 0:70a6082c1bf7 | 95 | //debugSerial.printf("main syncing..\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 96 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 97 | ok = esp.Sync(); // sync up with esp-link, blocks for up to 2 seconds |
ShaneKirkbride | 0:70a6082c1bf7 | 98 | if (!ok){ |
ShaneKirkbride | 0:70a6082c1bf7 | 99 | debugSerial.printf("STM-Client sync failed!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 100 | wait(0.5); |
ShaneKirkbride | 0:70a6082c1bf7 | 101 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 102 | } while(!ok); |
ShaneKirkbride | 0:70a6082c1bf7 | 103 | |
ShaneKirkbride | 0:70a6082c1bf7 | 104 | debugSerial.printf("STM-Client synced!\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 105 | |
ShaneKirkbride | 0:70a6082c1bf7 | 106 | // Get immediate wifi status info for demo purposes. This is not normally used because the |
ShaneKirkbride | 0:70a6082c1bf7 | 107 | // wifi status callback registered above gets called immediately. |
ShaneKirkbride | 0:70a6082c1bf7 | 108 | esp.GetWifiStatus(); |
ShaneKirkbride | 0:70a6082c1bf7 | 109 | STMClientPacket *packet; |
ShaneKirkbride | 0:70a6082c1bf7 | 110 | if ((packet=esp.WaitReturn()) != NULL) |
ShaneKirkbride | 0:70a6082c1bf7 | 111 | { |
ShaneKirkbride | 0:70a6082c1bf7 | 112 | //debugSerial.printf("Wifi status: %i\n\r", packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 113 | //debugSerial.printf("waiting for wifi status...\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 114 | if(packet->value == 2) { ///ideally this would coincide with STATION_GOT_IP... |
ShaneKirkbride | 0:70a6082c1bf7 | 115 | debugSerial.printf("WIFI CONNECTED\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 116 | wifiConnected = true; |
ShaneKirkbride | 0:70a6082c1bf7 | 117 | } else { |
ShaneKirkbride | 0:70a6082c1bf7 | 118 | debugSerial.printf("WIFI NOT READY: %i\n\r",packet->value); |
ShaneKirkbride | 0:70a6082c1bf7 | 119 | //Serial.printf(status); |
ShaneKirkbride | 0:70a6082c1bf7 | 120 | wifiConnected = false; |
ShaneKirkbride | 0:70a6082c1bf7 | 121 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 122 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 123 | |
ShaneKirkbride | 0:70a6082c1bf7 | 124 | |
ShaneKirkbride | 0:70a6082c1bf7 | 125 | // Set up the REST client to talk to www.timeapi.org, this doesn't connect to that server, |
ShaneKirkbride | 0:70a6082c1bf7 | 126 | // it just sets-up stuff on the esp-link side |
ShaneKirkbride | 0:70a6082c1bf7 | 127 | int err = rest.begin("www.timeapi.org"); |
ShaneKirkbride | 0:70a6082c1bf7 | 128 | if (err != 0) { |
ShaneKirkbride | 0:70a6082c1bf7 | 129 | debugSerial.printf("REST begin failed: %i\n\r",err); |
ShaneKirkbride | 0:70a6082c1bf7 | 130 | while(1) ; |
ShaneKirkbride | 0:70a6082c1bf7 | 131 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 132 | debugSerial.printf("STM-REST ready\n\r"); |
ShaneKirkbride | 0:70a6082c1bf7 | 133 | int loopStat = 0; |
ShaneKirkbride | 0:70a6082c1bf7 | 134 | while(loopStat == 0){ |
ShaneKirkbride | 0:70a6082c1bf7 | 135 | debugSerial.printf("status: %i\n\r",loopStat); |
ShaneKirkbride | 0:70a6082c1bf7 | 136 | loopStat = loop(); |
ShaneKirkbride | 0:70a6082c1bf7 | 137 | wait(1); |
ShaneKirkbride | 0:70a6082c1bf7 | 138 | } |
ShaneKirkbride | 0:70a6082c1bf7 | 139 | } |