|
| 1 | +#if !( defined(ESP8266) || defined(ESP32) ) |
| 2 | + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. |
| 3 | +#endif |
| 4 | + |
| 5 | +// Level from 0-4 |
| 6 | +#define ASYNC_HTTP_DEBUG_PORT Serial |
| 7 | + |
| 8 | +#define _ASYNC_HTTP_LOGLEVEL_ 1 |
| 9 | + |
| 10 | +// 300s = 5 minutes to not flooding |
| 11 | +#define HTTP_REQUEST_INTERVAL 300 |
| 12 | + |
| 13 | +// 10s |
| 14 | +#define HEARTBEAT_INTERVAL 10 |
| 15 | + |
| 16 | +int status; // the Wifi radio's status |
| 17 | + |
| 18 | +const char* ssid = "your_ssid"; |
| 19 | +const char* password = "your_pass"; |
| 20 | + |
| 21 | +#if (ESP8266) |
| 22 | + #include <ESP8266WiFi.h> |
| 23 | +#elif (ESP32) |
| 24 | + #include <WiFi.h> |
| 25 | +#endif |
| 26 | + |
| 27 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 28 | +#include <AsyncHTTPRequest_Generic.h> // http://github.com/khoih-prog/AsyncHTTPRequest_Generic |
| 29 | + |
| 30 | +#include <Ticker.h> |
| 31 | + |
| 32 | +#define NUM_DIFFERENT_SITES 2 |
| 33 | + |
| 34 | +const char* addreses[][NUM_DIFFERENT_SITES] = |
| 35 | +{ |
| 36 | + {"http://worldtimeapi.org/api/timezone/America/Toronto.txt", "http://worldtimeapi.org/api/timezone/Europe/Prague.txt"}, |
| 37 | + {"http://www.myexternalip.com/raw"} |
| 38 | +}; |
| 39 | + |
| 40 | +#define NUM_ENTRIES_SITE_0 2 |
| 41 | +#define NUM_ENTRIES_SITE_1 1 |
| 42 | + |
| 43 | +byte reqCount[NUM_DIFFERENT_SITES] = { NUM_ENTRIES_SITE_0, NUM_ENTRIES_SITE_1 }; |
| 44 | +bool readySend[NUM_DIFFERENT_SITES] = { true, true }; |
| 45 | + |
| 46 | +AsyncHTTPRequest request[NUM_DIFFERENT_SITES]; |
| 47 | + |
| 48 | +void requestCB0(void* optParm, AsyncHTTPRequest* thisRequest, int readyState); |
| 49 | +void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState); |
| 50 | + |
| 51 | +void sendRequest0(); |
| 52 | +void sendRequest1(); |
| 53 | + |
| 54 | +typedef void (*requestCallback)(void* optParm, AsyncHTTPRequest* thisRequest, int readyState); |
| 55 | +typedef void (*sendCallback)(); |
| 56 | + |
| 57 | +requestCallback requestCB [NUM_DIFFERENT_SITES] = { requestCB0, requestCB1 }; |
| 58 | +sendCallback sendRequestCB [NUM_DIFFERENT_SITES] = { sendRequest0, sendRequest1 }; |
| 59 | + |
| 60 | +Ticker ticker; |
| 61 | +Ticker ticker1; |
| 62 | + |
| 63 | +void heartBeatPrint() |
| 64 | +{ |
| 65 | + static int num = 1; |
| 66 | + |
| 67 | + if (WiFi.status() == WL_CONNECTED) |
| 68 | + Serial.print(F("H")); // H means connected to WiFi |
| 69 | + else |
| 70 | + Serial.print(F("F")); // F means not connected to WiFi |
| 71 | + |
| 72 | + if (num == 80) |
| 73 | + { |
| 74 | + Serial.println(); |
| 75 | + num = 1; |
| 76 | + } |
| 77 | + else if (num++ % 10 == 0) |
| 78 | + { |
| 79 | + Serial.print(F(" ")); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void sendRequest(uint16_t index) |
| 84 | +{ |
| 85 | + static bool requestOpenResult; |
| 86 | + |
| 87 | + reqCount[index]--; |
| 88 | + readySend[index] = false; |
| 89 | + |
| 90 | + requestOpenResult = request[index].open("GET", addreses[index][reqCount[index]]); |
| 91 | + |
| 92 | + if (requestOpenResult) |
| 93 | + { |
| 94 | + // Only send() if open() returns true, or crash |
| 95 | + Serial.print("\nSending request: "); |
| 96 | + request[index].send(); |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + Serial.print("\nCan't send bad request : "); |
| 101 | + } |
| 102 | + |
| 103 | + Serial.println(addreses[index][reqCount[index]]); |
| 104 | +} |
| 105 | + |
| 106 | +void sendRequest0() |
| 107 | +{ |
| 108 | + sendRequest(0); |
| 109 | +} |
| 110 | + |
| 111 | +void sendRequest1() |
| 112 | +{ |
| 113 | + sendRequest(1); |
| 114 | +} |
| 115 | + |
| 116 | +void sendRequests() |
| 117 | +{ |
| 118 | + for (int index = 0; index < NUM_DIFFERENT_SITES; index++) |
| 119 | + { |
| 120 | + reqCount[index] = 2; |
| 121 | + } |
| 122 | + reqCount[0] = NUM_ENTRIES_SITE_0; |
| 123 | + reqCount[1] = NUM_ENTRIES_SITE_1; |
| 124 | +} |
| 125 | + |
| 126 | + |
| 127 | +void requestCB0(void* optParm, AsyncHTTPRequest* thisRequest, int readyState) |
| 128 | +{ |
| 129 | + (void) optParm; |
| 130 | + |
| 131 | + if (readyState == readyStateDone) |
| 132 | + { |
| 133 | + Serial.println("\n**************************************"); |
| 134 | + Serial.println(thisRequest->responseText()); |
| 135 | + Serial.println("**************************************"); |
| 136 | + |
| 137 | + thisRequest->setDebug(false); |
| 138 | + readySend[0] = true; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState) |
| 143 | +{ |
| 144 | + (void) optParm; |
| 145 | + |
| 146 | + if (readyState == readyStateDone) |
| 147 | + { |
| 148 | + Serial.println("\n**************************************"); |
| 149 | + Serial.println(thisRequest->responseText()); |
| 150 | + Serial.println("**************************************"); |
| 151 | + |
| 152 | + thisRequest->setDebug(false); |
| 153 | + readySend[1] = true; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +void setup() |
| 158 | +{ |
| 159 | + // put your setup code here, to run once: |
| 160 | + Serial.begin(115200); |
| 161 | + while (!Serial); |
| 162 | + |
| 163 | + Serial.println("\nStarting AsyncHTTPRequest_ESP_Multi using " + String(ARDUINO_BOARD)); |
| 164 | + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); |
| 165 | + |
| 166 | + WiFi.mode(WIFI_STA); |
| 167 | + |
| 168 | + WiFi.begin(ssid, password); |
| 169 | + |
| 170 | + Serial.println("Connecting to WiFi SSID: " + String(ssid)); |
| 171 | + |
| 172 | + while (WiFi.status() != WL_CONNECTED) |
| 173 | + { |
| 174 | + delay(500); |
| 175 | + Serial.print("."); |
| 176 | + } |
| 177 | + |
| 178 | + Serial.print(F("\nAsyncHTTPSRequest @ IP : ")); |
| 179 | + Serial.println(WiFi.localIP()); |
| 180 | + |
| 181 | + for (int index = 0; index < NUM_DIFFERENT_SITES; index++) |
| 182 | + { |
| 183 | + request[index].setDebug(false); |
| 184 | + |
| 185 | + request[index].onReadyStateChange(requestCB[index]); |
| 186 | + } |
| 187 | + |
| 188 | + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequests); |
| 189 | + |
| 190 | + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); |
| 191 | +} |
| 192 | + |
| 193 | +void loop() |
| 194 | +{ |
| 195 | + for (int index = 0; index < NUM_DIFFERENT_SITES; index++) |
| 196 | + { |
| 197 | + if ((reqCount[index] > 0) && readySend[index]) |
| 198 | + // OK but have to use delay(100) |
| 199 | + //if ( reqCount[index] > 0 ) |
| 200 | + { |
| 201 | + sendRequestCB[index](); |
| 202 | + |
| 203 | + //delay(100); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments