/**************************************************************************************************************************** AsyncHTTPRequest_ESP.ino - Dead simple AsyncHTTPRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc) AsyncHTTPRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_Generic Licensed under MIT license Copyright (C) <2018> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: 1.0.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 14/09/2020 Initial coding to add support to STM32 using built-in Ethernet (Nucleo-144, DISCOVERY, etc). *****************************************************************************************************************************/ //************************************************************************************************************ // // There are scores of ways to use AsyncHTTPRequest. The important thing to keep in mind is that // it is asynchronous and just like in JavaScript, everything is event driven. You will have some // reason to initiate an asynchronous HTTP request in your program, but then sending the request // headers and payload, gathering the response headers and any payload, and processing // of that response, can (and probably should) all be done asynchronously. // // In this example, a Ticker function is setup to fire every 300 seconds to initiate a request. // Everything is handled in AsyncHTTPRequest without blocking. // The callback onReadyStateChange is made progressively and like most JS scripts, we look for // readyState == 4 (complete) here. At that time the response is retrieved and printed. // // Note that there is no code in loop(). A code entered into loop would run oblivious to // the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest() // could be made in loop(), resulting in the same asynchronous handling. // // For demo purposes, debug is turned on for handling of the first request. These are the // events that are being handled in AsyncHTTPRequest. They all begin with Debug(nnn) where // nnn is the elapsed time in milliseconds since the transaction was started. // //************************************************************************************************************* #if !( defined(ESP8266) || defined(ESP32) ) #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial #define _ASYNC_HTTP_LOGLEVEL_ 1 // 300s = 5 minutes to not flooding #define HTTP_REQUEST_INTERVAL 300 // 10s #define HEARTBEAT_INTERVAL 10 int status; // the Wifi radio's status const char* ssid = "your_ssid"; const char* password = "your_pass"; #if (ESP8266) #include #elif (ESP32) #include #endif #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic #include AsyncHTTPRequest request; Ticker ticker; void heartBeatPrint(void) { static int num = 1; if (WiFi.status() == WL_CONNECTED) Serial.print(F("H")); // H means connected to WiFi else Serial.print(F("F")); // F means not connected to WiFi if (num == 80) { Serial.println(); num = 1; } else if (num++ % 10 == 0) { Serial.print(F(" ")); } } void sendRequest() { if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) { //request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); request.send(); } } void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) { if (readyState == readyStateDone) { Serial.println("\n**************************************"); Serial.println(request->responseText()); Serial.println("**************************************"); request->setDebug(false); } } void setup() { // put your setup code here, to run once: Serial.begin(115200); while (!Serial); Serial.println("\nStarting AsyncHTTPRequest_ESP using " + String(ARDUINO_BOARD)); WiFi.mode(WIFI_STA); if (WiFi.status() == WL_NO_SHIELD) { Serial.println(F("WiFi shield not present")); // don't continue while (true); } WiFi.begin(ssid, password); Serial.println("Connecting to WiFi SSID: " + String(ssid)); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.print(F("\nHTTP WebServer is @ IP : ")); Serial.println(WiFi.localIP()); request.setDebug(false); request.onReadyStateChange(requestCB); ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); ticker.attach(HEARTBEAT_INTERVAL, heartBeatPrint); // Send first request now sendRequest(); } void loop() { }