|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncHTTPMultiRequests.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet |
| 3 | + |
| 4 | + For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) |
| 5 | + |
| 6 | + AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer |
| 7 | + |
| 8 | + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) |
| 9 | + |
| 10 | + Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic |
| 11 | + Licensed under MIT license |
| 12 | + |
| 13 | + Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.> |
| 14 | + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 15 | + as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 18 | + You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | + Version: 1.1.1 |
| 21 | + |
| 22 | + Version Modified By Date Comments |
| 23 | + ------- ----------- ---------- ----------- |
| 24 | + 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). |
| 25 | + 1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code. |
| 26 | + 1.0.2 K Hoang 09/11/2020 Make Mutex Lock and delete more reliable and error-proof |
| 27 | + 1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods |
| 28 | + 1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct. |
| 29 | + *****************************************************************************************************************************/ |
| 30 | +//************************************************************************************************************ |
| 31 | +// |
| 32 | +// There are scores of ways to use AsyncHTTPRequest. The important thing to keep in mind is that |
| 33 | +// it is asynchronous and just like in JavaScript, everything is event driven. You will have some |
| 34 | +// reason to initiate an asynchronous HTTP request in your program, but then sending the request |
| 35 | +// headers and payload, gathering the response headers and any payload, and processing |
| 36 | +// of that response, can (and probably should) all be done asynchronously. |
| 37 | +// |
| 38 | +// In this example, a Ticker function is setup to fire every 300 seconds to initiate a request. |
| 39 | +// Everything is handled in AsyncHTTPRequest without blocking. |
| 40 | +// The callback onReadyStateChange is made progressively and like most JS scripts, we look for |
| 41 | +// readyState == 4 (complete) here. At that time the response is retrieved and printed. |
| 42 | +// |
| 43 | +// Note that there is no code in loop(). A code entered into loop would run oblivious to |
| 44 | +// the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest() |
| 45 | +// could be made in loop(), resulting in the same asynchronous handling. |
| 46 | +// |
| 47 | +// For demo purposes, debug is turned on for handling of the first request. These are the |
| 48 | +// events that are being handled in AsyncHTTPRequest. They all begin with Debug(nnn) where |
| 49 | +// nnn is the elapsed time in milliseconds since the transaction was started. |
| 50 | +// |
| 51 | +//************************************************************************************************************* |
| 52 | + |
| 53 | +#if !( defined(ESP8266) || defined(ESP32) ) |
| 54 | + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. |
| 55 | +#endif |
| 56 | + |
| 57 | +// Level from 0-4 |
| 58 | +#define ASYNC_HTTP_DEBUG_PORT Serial |
| 59 | +#define _ASYNC_HTTP_LOGLEVEL_ 4 |
| 60 | + |
| 61 | +// 300s = 5 minutes to not flooding |
| 62 | +#define HTTP_REQUEST_INTERVAL 30 //300 |
| 63 | + |
| 64 | +// 10s |
| 65 | +#define HEARTBEAT_INTERVAL 10 |
| 66 | + |
| 67 | +int status; // the Wifi radio's status |
| 68 | + |
| 69 | +const char* ssid = "your_ssid"; |
| 70 | +const char* password = "your_pass"; |
| 71 | + |
| 72 | +#if (ESP8266) |
| 73 | + #include <ESP8266WiFi.h> |
| 74 | +#elif (ESP32) |
| 75 | + #include <WiFi.h> |
| 76 | +#endif |
| 77 | + |
| 78 | +#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic |
| 79 | +#include <Ticker.h> |
| 80 | + |
| 81 | +AsyncHTTPRequest request; |
| 82 | +Ticker ticker; |
| 83 | +Ticker ticker1; |
| 84 | + |
| 85 | +void heartBeatPrint(void) |
| 86 | +{ |
| 87 | + static int num = 1; |
| 88 | + |
| 89 | + if (WiFi.status() == WL_CONNECTED) |
| 90 | + Serial.print(F("H")); // H means connected to WiFi |
| 91 | + else |
| 92 | + Serial.print(F("F")); // F means not connected to WiFi |
| 93 | + |
| 94 | + if (num == 80) |
| 95 | + { |
| 96 | + Serial.println(); |
| 97 | + num = 1; |
| 98 | + } |
| 99 | + else if (num++ % 10 == 0) |
| 100 | + { |
| 101 | + Serial.print(F(" ")); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// To replace with your real APP_API |
| 106 | +#define APP_API "SECRECT_APP_API" |
| 107 | + |
| 108 | +String requestPart1 = "http://api.openweathermap.org/data/2.5/onecall?lat=-24.32&lon=-46.9983"; |
| 109 | +String requestAPPID = "&appid=" + String(APP_API); |
| 110 | + |
| 111 | +// exclude fields: current,minutely,hourly,daily,alerts |
| 112 | +String requestCurrent = requestPart1 + "&exclude=minutely,hourly,daily,alerts" + requestAPPID; |
| 113 | +String requestMinutely = requestPart1 + "&exclude=current,hourly,daily,alerts" + requestAPPID; |
| 114 | +String requestHourly = requestPart1 + "&exclude=current,minutely,daily,alerts" + requestAPPID; |
| 115 | +String requestDaily = requestPart1 + "&exclude=current,minutely,hourly,alerts" + requestAPPID; |
| 116 | +String requestAlert = requestPart1 + "&exclude=current,minutely,hourly,daily" + requestAPPID; |
| 117 | + |
| 118 | +#define NUM_REQUESTS 5 |
| 119 | + |
| 120 | +const char* requestName[ NUM_REQUESTS ] = { "Current", "Minutely", "Hourly", "Daily", "Alert" }; |
| 121 | + |
| 122 | +const char* requestAll[ NUM_REQUESTS ] = { requestCurrent.c_str(), requestMinutely.c_str(), requestHourly.c_str(), requestDaily.c_str(), requestAlert.c_str() }; |
| 123 | + |
| 124 | +uint8_t requestIndex = 0; |
| 125 | + |
| 126 | +void sendRequest() |
| 127 | +{ |
| 128 | + static bool requestOpenResult; |
| 129 | + |
| 130 | + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) |
| 131 | + { |
| 132 | + requestOpenResult = request.open("GET", requestAll[requestIndex] ); |
| 133 | + |
| 134 | + if (requestOpenResult) |
| 135 | + { |
| 136 | + // Only send() if open() returns true, or crash |
| 137 | + request.send(); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + Serial.println("Can't send bad request"); |
| 142 | + } |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + Serial.println("Can't send request"); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) |
| 151 | +{ |
| 152 | + if (readyState == readyStateDone) |
| 153 | + { |
| 154 | + Serial.print("\n***************"); Serial.print(requestName[ requestIndex ]); Serial.println("***************"); |
| 155 | + Serial.println(request->responseText()); |
| 156 | + Serial.println("**************************************"); |
| 157 | + |
| 158 | +#if 1 |
| 159 | + // Bypass hourly |
| 160 | + if (requestIndex == 1) |
| 161 | + requestIndex = 3; |
| 162 | + else |
| 163 | + requestIndex = (requestIndex + 1) % NUM_REQUESTS; |
| 164 | +#else |
| 165 | + // hourly too long, not display anyway. Not enough heap. |
| 166 | + requestIndex = (requestIndex + 1) % NUM_REQUESTS; |
| 167 | + #endif |
| 168 | + |
| 169 | + request->setDebug(false); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void setup() |
| 174 | +{ |
| 175 | + // put your setup code here, to run once: |
| 176 | + Serial.begin(115200); |
| 177 | + while (!Serial); |
| 178 | + |
| 179 | + delay(200); |
| 180 | + |
| 181 | + Serial.println("\nStarting AsyncHTTPMultiRequests using " + String(ARDUINO_BOARD)); |
| 182 | + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); |
| 183 | + |
| 184 | + WiFi.mode(WIFI_STA); |
| 185 | + |
| 186 | + WiFi.begin(ssid, password); |
| 187 | + |
| 188 | + Serial.println("Connecting to WiFi SSID: " + String(ssid)); |
| 189 | + |
| 190 | + while (WiFi.status() != WL_CONNECTED) |
| 191 | + { |
| 192 | + delay(500); |
| 193 | + Serial.print("."); |
| 194 | + } |
| 195 | + |
| 196 | + Serial.print(F("\nHTTP WebServer is @ IP : ")); |
| 197 | + Serial.println(WiFi.localIP()); |
| 198 | + |
| 199 | + request.setDebug(false); |
| 200 | + |
| 201 | + request.onReadyStateChange(requestCB); |
| 202 | + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); |
| 203 | + |
| 204 | + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); |
| 205 | + |
| 206 | + // Send first request now |
| 207 | + sendRequest(); |
| 208 | +} |
| 209 | + |
| 210 | +void loop() |
| 211 | +{ |
| 212 | +} |
0 commit comments