|
| 1 | +/**************************************************************************************************************************** |
| 2 | + AsyncHTTPRequest_ESP.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 | +//************************************************************************************************************ |
| 21 | +// |
| 22 | +// There are scores of ways to use AsyncHTTPRequest. The important thing to keep in mind is that |
| 23 | +// it is asynchronous and just like in JavaScript, everything is event driven. You will have some |
| 24 | +// reason to initiate an asynchronous HTTP request in your program, but then sending the request |
| 25 | +// headers and payload, gathering the response headers and any payload, and processing |
| 26 | +// of that response, can (and probably should) all be done asynchronously. |
| 27 | +// |
| 28 | +// In this example, a Ticker function is setup to fire every 300 seconds to initiate a request. |
| 29 | +// Everything is handled in AsyncHTTPRequest without blocking. |
| 30 | +// The callback onReadyStateChange is made progressively and like most JS scripts, we look for |
| 31 | +// readyState == 4 (complete) here. At that time the response is retrieved and printed. |
| 32 | +// |
| 33 | +// Note that there is no code in loop(). A code entered into loop would run oblivious to |
| 34 | +// the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest() |
| 35 | +// could be made in loop(), resulting in the same asynchronous handling. |
| 36 | +// |
| 37 | +// For demo purposes, debug is turned on for handling of the first request. These are the |
| 38 | +// events that are being handled in AsyncHTTPRequest. They all begin with Debug(nnn) where |
| 39 | +// nnn is the elapsed time in milliseconds since the transaction was started. |
| 40 | +// |
| 41 | +//************************************************************************************************************* |
| 42 | + |
| 43 | +#if !( defined(ESP8266) ) |
| 44 | + #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. |
| 45 | +#endif |
| 46 | + |
| 47 | +// Level from 0-4 |
| 48 | +#define ASYNC_HTTP_DEBUG_PORT Serial |
| 49 | +#define _ASYNC_HTTP_LOGLEVEL_ 1 |
| 50 | + |
| 51 | +// 300s = 5 minutes to not flooding |
| 52 | +#define HTTP_REQUEST_INTERVAL 60 //300 |
| 53 | + |
| 54 | +// 10s |
| 55 | +#define HEARTBEAT_INTERVAL 10 |
| 56 | + |
| 57 | +////////////////////////////////////////////////////////// |
| 58 | + |
| 59 | +#define USING_W5500 true |
| 60 | +#define USING_W5100 false |
| 61 | +#define USING_ENC28J60 false |
| 62 | + |
| 63 | +#include <SPI.h> |
| 64 | + |
| 65 | +#define CSPIN 16 // 5 |
| 66 | + |
| 67 | +#if USING_W5500 |
| 68 | + #include "W5500lwIP.h" |
| 69 | + #define SHIELD_TYPE "ESP8266_W5500 Ethernet" |
| 70 | + |
| 71 | + Wiznet5500lwIP eth(CSPIN); |
| 72 | + |
| 73 | +#elif USING_W5100 |
| 74 | + #include <W5100lwIP.h> |
| 75 | + #define SHIELD_TYPE "ESP8266_W5100 Ethernet" |
| 76 | + |
| 77 | + Wiznet5100lwIP eth(CSPIN); |
| 78 | + |
| 79 | +#elif USING_ENC28J60 |
| 80 | + #include <ENC28J60lwIP.h> |
| 81 | + #define SHIELD_TYPE "ESP8266_ENC28J60 Ethernet" |
| 82 | + |
| 83 | + ENC28J60lwIP eth(CSPIN); |
| 84 | +#else |
| 85 | + // default if none selected |
| 86 | + #include "W5500lwIP.h" |
| 87 | + |
| 88 | + Wiznet5500lwIP eth(CSPIN); |
| 89 | +#endif |
| 90 | + |
| 91 | + |
| 92 | +#include <WiFiClient.h> // WiFiClient (-> TCPClient) |
| 93 | + |
| 94 | +using TCPClient = WiFiClient; |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +////////////////////////////////////////////////////////// |
| 101 | + |
| 102 | +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.7.1" |
| 103 | +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1007001 |
| 104 | + |
| 105 | +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error |
| 106 | +#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic |
| 107 | + |
| 108 | +#include <Ticker.h> |
| 109 | + |
| 110 | +AsyncHTTPRequest request; |
| 111 | +Ticker ticker; |
| 112 | +Ticker ticker1; |
| 113 | + |
| 114 | +void heartBeatPrint() |
| 115 | +{ |
| 116 | + static int num = 1; |
| 117 | + |
| 118 | + if (eth.connected()) |
| 119 | + Serial.print(F("H")); // H means connected to Ethernet |
| 120 | + else |
| 121 | + Serial.print(F("F")); // F means not connected to Ethernet |
| 122 | + |
| 123 | + if (num == 80) |
| 124 | + { |
| 125 | + Serial.println(); |
| 126 | + num = 1; |
| 127 | + } |
| 128 | + else if (num++ % 10 == 0) |
| 129 | + { |
| 130 | + Serial.print(F(" ")); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +void sendRequest() |
| 135 | +{ |
| 136 | + static bool requestOpenResult; |
| 137 | + |
| 138 | + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) |
| 139 | + { |
| 140 | + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); |
| 141 | + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); |
| 142 | + |
| 143 | + if (requestOpenResult) |
| 144 | + { |
| 145 | + // Only send() if open() returns true, or crash |
| 146 | + request.send(); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + Serial.println(F("Can't send bad request")); |
| 151 | + } |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + Serial.println(F("Can't send request")); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) |
| 160 | +{ |
| 161 | + (void) optParm; |
| 162 | + |
| 163 | + if (readyState == readyStateDone) |
| 164 | + { |
| 165 | + Serial.println(F("\n**************************************")); |
| 166 | + Serial.println(request->responseText()); |
| 167 | + Serial.println(F("**************************************")); |
| 168 | + |
| 169 | + request->setDebug(false); |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +void initEthernet() |
| 174 | +{ |
| 175 | + SPI.begin(); |
| 176 | + SPI.setClockDivider(SPI_CLOCK_DIV4); |
| 177 | + SPI.setBitOrder(MSBFIRST); |
| 178 | + SPI.setDataMode(SPI_MODE0); |
| 179 | + eth.setDefault(); |
| 180 | + |
| 181 | + if (!eth.begin()) |
| 182 | + { |
| 183 | + Serial.println("No Ethernet hardware ... Stop here"); |
| 184 | + |
| 185 | + while (true) |
| 186 | + { |
| 187 | + delay(1000); |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + Serial.print("Connecting to network : "); |
| 193 | + |
| 194 | + while (!eth.connected()) |
| 195 | + { |
| 196 | + Serial.print("."); |
| 197 | + delay(1000); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + Serial.println(); |
| 202 | + Serial.print("Ethernet IP address: "); |
| 203 | + Serial.println(eth.localIP()); |
| 204 | +} |
| 205 | + |
| 206 | +void setup() |
| 207 | +{ |
| 208 | + // put your setup code here, to run once: |
| 209 | + Serial.begin(115200); |
| 210 | + while (!Serial && millis() < 5000); |
| 211 | + |
| 212 | + delay(200); |
| 213 | + |
| 214 | + Serial.print(F("\nStarting AsyncHTTPRequest_ESP8266_Ethernet on ")); Serial.print(ARDUINO_BOARD); |
| 215 | + Serial.print(F(" using ")); Serial.println(SHIELD_TYPE); |
| 216 | + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); |
| 217 | + |
| 218 | + initEthernet(); |
| 219 | + |
| 220 | + request.setDebug(false); |
| 221 | + |
| 222 | + request.onReadyStateChange(requestCB); |
| 223 | + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); |
| 224 | + |
| 225 | + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); |
| 226 | + |
| 227 | + // Send first request now |
| 228 | + sendRequest(); |
| 229 | +} |
| 230 | + |
| 231 | +void loop() |
| 232 | +{ |
| 233 | +} |
0 commit comments