From 639ce917e54c296f15fdb8bbad2c34efcc6a0f61 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Wed, 9 Nov 2022 23:46:36 -0500 Subject: [PATCH] v1.10.2 default to reconnect to the same host:port ### Releases v1.10.2 1. Default to reconnect to the same `host:port` after connected for new HTTP sites. Check [Host/Headers not always sent with 1.10.1 #44](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues44) 2. Update `Packages' Patches` --- CONTRIBUTING.md | 30 +- changelog.md | 6 + .../AsyncCustomHeader_STM32.ino | 164 +-- examples/AsyncCustomHeader_STM32/defines.h | 122 +- .../AsyncDweetGet_STM32.ino | 240 ++-- examples/AsyncDweetGet_STM32/defines.h | 122 +- .../AsyncDweetPost_STM32.ino | 249 ++-- examples/AsyncDweetPost_STM32/defines.h | 122 +- .../AsyncHTTPMultiRequests_ESP.ino | 238 ++-- .../AsyncHTTPRequest_ESP.ino | 200 ++-- .../AsyncHTTPRequest_ESP8266_Ethernet.ino | 268 +++-- .../AsyncHTTPRequest_ESP_Multi.ino | 261 ++-- .../AsyncHTTPRequest_ESP_WiFiManager.ino | 1065 +++++++++-------- .../AsyncHTTPRequest_STM32.ino | 167 +-- examples/AsyncHTTPRequest_STM32/defines.h | 122 +- .../AsyncSimpleGET_STM32.ino | 156 +-- examples/AsyncSimpleGET_STM32/defines.h | 102 +- .../AsyncWebClientRepeating_STM32.ino | 165 +-- .../AsyncWebClientRepeating_STM32/defines.h | 122 +- .../AsyncHTTPMultiRequests_WT32_ETH01.ino | 232 ++-- .../AsyncHTTPRequest_WT32_ETH01.ino | 204 ++-- .../multiFileProject/multiFileProject.cpp | 6 +- examples/multiFileProject/multiFileProject.h | 6 +- .../multiFileProject/multiFileProject.ino | 41 +- library.json | 4 +- library.properties | 2 +- platformio/platformio.ini | 14 +- src/AsyncHTTPRequest_Debug_Generic.h | 23 +- src/AsyncHTTPRequest_Generic.h | 23 +- src/AsyncHTTPRequest_Generic.hpp | 17 +- src/AsyncHTTPRequest_Impl_Generic.h | 863 +++++++------ utils/astyle_library.conf | 70 ++ utils/restyle.sh | 6 + 33 files changed, 2884 insertions(+), 2548 deletions(-) create mode 100644 utils/astyle_library.conf create mode 100644 utils/restyle.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 35016224..07a0f1f5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,11 +29,10 @@ Please ensure to specify the following: Arduino IDE version: 1.8.19 ESP32 Core Version 2.0.5 OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.15.0-50-generic #56~20.04.1-Ubuntu SMP Tue Sep 27 15:51:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: -I encountered a crash when using this library - +I encountered a crash while using this library Steps to reproduce: 1. ... 2. ... @@ -41,13 +40,38 @@ Steps to reproduce: 4. ... ``` +### Additional context + +Add any other context about the problem here. + +--- + ### Sending Feature Requests Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. +--- + ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/AsyncHTTPRequest_Generic_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncHTTPRequest_Generic_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncHTTPRequest_Generic_GitHub$ bash utils/restyle.sh +``` + + + diff --git a/changelog.md b/changelog.md index 904d9e61..da789575 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.10.2](#releases-v1102) * [Releases v1.10.1](#releases-v1101) * [Releases v1.10.0](#releases-v1100) * [Releases v1.9.2](#releases-v192) @@ -44,6 +45,11 @@ ## Changelog +### Releases v1.10.2 + +1. Default to reconnect to the same `host:port` after connected for new HTTP sites. Check [Host/Headers not always sent with 1.10.1 #44](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues44) +2. Update `Packages' Patches` + ### Releases v1.10.1 1. Fix bug of wrong `reqStates`. Check [Release 1.9 breakes previously running code #39](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/39) and [Callback behaviour is buggy (ESP8266) #43](https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/43) diff --git a/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino index e880e09c..ba535ef5 100644 --- a/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino +++ b/examples/AsyncCustomHeader_STM32/AsyncCustomHeader_STM32.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncCustomHeader_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ #include "defines.h" @@ -24,12 +24,15 @@ //char GET_ServerAddress[] = "192.168.2.110/"; char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -44,97 +47,102 @@ Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest(void) { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - Serial.println("\nSending GET Request to " + String(GET_ServerAddress)); - - requestOpenResult = request.open("GET", GET_ServerAddress); - //request.setReqHeader("X-CUSTOM-HEADER", "custom_value"); - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + Serial.println("\nSending GET Request to " + String(GET_ServerAddress)); + + requestOpenResult = request.open("GET", GET_ServerAddress); + + //request.setReqHeader("X-CUSTOM-HEADER", "custom_value"); + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - Serial.println(); - AHTTP_LOGDEBUG(F("**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - else - { - AHTTP_LOGERROR(F("Response error")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + Serial.println(); + AHTTP_LOGDEBUG(F("**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + else + { + AHTTP_LOGERROR(F("Response error")); + } + } } void setup(void) { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncCustomHeader_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncCustomHeader_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(Ethernet.localIP()); - Serial.println(); + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); - request.setDebug(false); + request.setDebug(false); - // 5s timeout - request.setTimeout(5); + // 5s timeout + request.setTimeout(5); - request.onReadyStateChange(requestCB); + request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker. + sendHTTPRequest.start(); //start the ticker. - // Send first request now - delay(10000); - sendRequest(); + // Send first request now + delay(10000); + sendRequest(); } void loop(void) { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncCustomHeader_STM32/defines.h b/examples/AsyncCustomHeader_STM32/defines.h index 7e73c81a..44a93644 100644 --- a/examples/AsyncCustomHeader_STM32/defines.h +++ b/examples/AsyncCustomHeader_STM32/defines.h @@ -1,30 +1,30 @@ /**************************************************************************************************************************** defines.h - + 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /* Currently support 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : - Nucleo-144 (F429ZI, F767ZI) - Discovery (STM32F746G-DISCOVERY) - - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields @@ -36,7 +36,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define ASYNC_HTTP_DEBUG_PORT Serial @@ -46,54 +46,54 @@ #if defined(STM32F0) - #warning STM32F0 board selected - #define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) - #warning STM32F1 board selected - #define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) - #warning STM32F2 board selected - #define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) - #warning STM32F3 board selected - #define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) - #warning STM32F4 board selected - #define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) - #warning STM32F7 board selected - #define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) - #warning STM32L0 board selected - #define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) - #warning STM32L1 board selected - #define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) - #warning STM32L4 board selected - #define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) - #warning STM32H7 board selected - #define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) - #warning STM32G0 board selected - #define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) - #warning STM32G4 board selected - #define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) - #warning STM32WB board selected - #define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) - #warning STM32MP1 board selected - #define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else - #warning STM32 unknown board selected - #define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino index 1260f2fa..d00537dc 100644 --- a/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino +++ b/examples/AsyncDweetGet_STM32/AsyncDweetGet_STM32.ino @@ -1,46 +1,50 @@ /**************************************************************************************************************************** AsyncDweetGET_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /** Connects to dweet.io once every 1 minutes, sends a GET request and a request body. - Shows how to use Strings to assemble path and parse content from response. + Shows how to use Strings to assemble path and parse content from response. dweet.io expects: https://dweet.io/dweet/for/thingName - For more on dweet.io, see https://dweet.io/play/ + For more on dweet.io, see https://dweet.io/play/ * */ #include "defines.h" -// Select a test server address +// Select a test server address const char GET_ServerAddress[] = "dweet.io"; // use your own thing name here String dweetName = "/dweet/for/currentSecond?second="; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + +// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error @@ -53,132 +57,136 @@ AsyncHTTPRequest request; void sendRequest(void); // Repeat forever, millis() resolution -Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest(void) -{ - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - requestOpenResult = request.open("GET", (GET_ServerAddress + dweetName + String(millis()/1000)).c_str() ); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } +{ + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + requestOpenResult = request.open("GET", (GET_ServerAddress + dweetName + String(millis() / 1000)).c_str() ); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void parseResponse(String responseText) { - /* - Typical response is: - {"this":"succeeded", - "by":"getting", - "the":"dweets", - "with":[{"thing":"my-thing-name", - "created":"2016-02-16T05:10:36.589Z", - "content":{"sensorValue":456}}]} - - You want "content": numberValue - */ - // now parse the response looking for "content": - int labelStart = responseText.indexOf("content\":"); - // find the first { after "content": - int contentStart = responseText.indexOf("{", labelStart); - // find the following } and get what's between the braces: - int contentEnd = responseText.indexOf("}", labelStart); - String content = responseText.substring(contentStart + 1, contentEnd); - - Serial.println(content); - - // now get the value after the colon, and convert to an int: - int valueStart = content.indexOf(":"); - String valueString = content.substring(valueStart + 1); - int number = valueString.toInt(); - - Serial.print("Value string: "); - Serial.println(valueString); - Serial.print("Actual value: "); - Serial.println(number); + /* + Typical response is: + {"this":"succeeded", + "by":"getting", + "the":"dweets", + "with":[{"thing":"my-thing-name", + "created":"2016-02-16T05:10:36.589Z", + "content":{"sensorValue":456}}]} + + You want "content": numberValue + */ + // now parse the response looking for "content": + int labelStart = responseText.indexOf("content\":"); + // find the first { after "content": + int contentStart = responseText.indexOf("{", labelStart); + // find the following } and get what's between the braces: + int contentEnd = responseText.indexOf("}", labelStart); + String content = responseText.substring(contentStart + 1, contentEnd); + + Serial.println(content); + + // now get the value after the colon, and convert to an int: + int valueStart = content.indexOf(":"); + String valueString = content.substring(valueStart + 1); + int number = valueString.toInt(); + + Serial.print("Value string: "); + Serial.println(valueString); + Serial.print("Actual value: "); + Serial.println(number); } void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - Serial.println(); - AHTTP_LOGDEBUG(F("**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - String responseText = request->responseText(); - - Serial.println("\n**************************************"); - //Serial.println(request->responseText()); - Serial.println(responseText); - Serial.println("**************************************"); - - parseResponse(responseText); - - request->setDebug(false); - } - else - { - AHTTP_LOGERROR(F("Response error")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + Serial.println(); + AHTTP_LOGDEBUG(F("**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + //Serial.println(request->responseText()); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + + request->setDebug(false); + } + else + { + AHTTP_LOGERROR(F("Response error")); + } + } } -void setup(void) +void setup(void) { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncDweetGET_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncDweetGET_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(Ethernet.localIP()); - Serial.println(); + request.setDebug(false); - request.setDebug(false); - - request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker. + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. } -void loop(void) +void loop(void) { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncDweetGet_STM32/defines.h b/examples/AsyncDweetGet_STM32/defines.h index 7e73c81a..44a93644 100644 --- a/examples/AsyncDweetGet_STM32/defines.h +++ b/examples/AsyncDweetGet_STM32/defines.h @@ -1,30 +1,30 @@ /**************************************************************************************************************************** defines.h - + 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /* Currently support 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : - Nucleo-144 (F429ZI, F767ZI) - Discovery (STM32F746G-DISCOVERY) - - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields @@ -36,7 +36,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define ASYNC_HTTP_DEBUG_PORT Serial @@ -46,54 +46,54 @@ #if defined(STM32F0) - #warning STM32F0 board selected - #define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) - #warning STM32F1 board selected - #define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) - #warning STM32F2 board selected - #define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) - #warning STM32F3 board selected - #define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) - #warning STM32F4 board selected - #define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) - #warning STM32F7 board selected - #define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) - #warning STM32L0 board selected - #define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) - #warning STM32L1 board selected - #define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) - #warning STM32L4 board selected - #define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) - #warning STM32H7 board selected - #define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) - #warning STM32G0 board selected - #define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) - #warning STM32G4 board selected - #define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) - #warning STM32WB board selected - #define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) - #warning STM32MP1 board selected - #define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else - #warning STM32 unknown board selected - #define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino index deb189a7..8dc9e920 100644 --- a/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino +++ b/examples/AsyncDweetPost_STM32/AsyncDweetPost_STM32.ino @@ -1,40 +1,43 @@ /**************************************************************************************************************************** AsyncDweetPOST_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ - // Dweet.io POST client. Connects to dweet.io once every ten seconds, sends a POST request and a request body. - // Shows how to use Strings to assemble path and body +// Dweet.io POST client. Connects to dweet.io once every ten seconds, sends a POST request and a request body. +// Shows how to use Strings to assemble path and body #include "defines.h" -// Select a test server address +// Select a test server address const char POST_ServerAddress[] = "dweet.io"; // use your own thing name here String dweetName = "/dweet/for/pinA0-Read?"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -45,137 +48,141 @@ AsyncHTTPRequest request; void sendRequest(void); // Repeat forever, millis() resolution -Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest(void) -{ - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - String postData = "sensorValue="; - postData += analogRead(A0); - - Serial.println("\nMaking new POST request"); - - requestOpenResult = request.open("POST", (POST_ServerAddress + dweetName + postData).c_str() ); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } +{ + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + String postData = "sensorValue="; + postData += analogRead(A0); + + Serial.println("\nMaking new POST request"); + + requestOpenResult = request.open("POST", (POST_ServerAddress + dweetName + postData).c_str() ); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void parseResponse(String responseText) { - /* - Typical response is: - {"this":"succeeded", - "by":"getting", - "the":"dweets", - "with":[{"thing":"my-thing-name", - "created":"2016-02-16T05:10:36.589Z", - "content":{"sensorValue":456}}]} - - You want "content": numberValue - */ - // now parse the response looking for "content": - int labelStart = responseText.indexOf("content\":"); - // find the first { after "content": - int contentStart = responseText.indexOf("{", labelStart); - // find the following } and get what's between the braces: - int contentEnd = responseText.indexOf("}", labelStart); - String content = responseText.substring(contentStart + 1, contentEnd); - - Serial.println(content); - - // now get the value after the colon, and convert to an int: - int valueStart = content.indexOf(":"); - String valueString = content.substring(valueStart + 1); - int number = valueString.toInt(); - - Serial.print("Value string: "); - Serial.println(valueString); - Serial.print("Actual value: "); - Serial.println(number); + /* + Typical response is: + {"this":"succeeded", + "by":"getting", + "the":"dweets", + "with":[{"thing":"my-thing-name", + "created":"2016-02-16T05:10:36.589Z", + "content":{"sensorValue":456}}]} + + You want "content": numberValue + */ + // now parse the response looking for "content": + int labelStart = responseText.indexOf("content\":"); + // find the first { after "content": + int contentStart = responseText.indexOf("{", labelStart); + // find the following } and get what's between the braces: + int contentEnd = responseText.indexOf("}", labelStart); + String content = responseText.substring(contentStart + 1, contentEnd); + + Serial.println(content); + + // now get the value after the colon, and convert to an int: + int valueStart = content.indexOf(":"); + String valueString = content.substring(valueStart + 1); + int number = valueString.toInt(); + + Serial.print("Value string: "); + Serial.println(valueString); + Serial.print("Actual value: "); + Serial.println(number); } void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - Serial.println(); - AHTTP_LOGDEBUG(F("**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - String responseText = request->responseText(); - - Serial.println("\n**************************************"); - //Serial.println(request->responseText()); - Serial.println(responseText); - Serial.println("**************************************"); - - parseResponse(responseText); - - request->setDebug(false); - } - else - { - AHTTP_LOGERROR(F("Response error")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + Serial.println(); + AHTTP_LOGDEBUG(F("**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + //Serial.println(request->responseText()); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + + request->setDebug(false); + } + else + { + AHTTP_LOGERROR(F("Response error")); + } + } } -void setup(void) +void setup(void) { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncDweetPOST_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncDweetPOST_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(Ethernet.localIP()); - Serial.println(); + request.setDebug(false); - request.setDebug(false); - - request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker. + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. } -void loop(void) +void loop(void) { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncDweetPost_STM32/defines.h b/examples/AsyncDweetPost_STM32/defines.h index fc573fb6..b6524710 100644 --- a/examples/AsyncDweetPost_STM32/defines.h +++ b/examples/AsyncDweetPost_STM32/defines.h @@ -1,30 +1,30 @@ /**************************************************************************************************************************** defines.h - + 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /* Currently support 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : - Nucleo-144 (F429ZI, F767ZI) - Discovery (STM32F746G-DISCOVERY) - - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields @@ -36,7 +36,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define ASYNC_HTTP_DEBUG_PORT Serial @@ -46,54 +46,54 @@ #if defined(STM32F0) - #warning STM32F0 board selected - #define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) - #warning STM32F1 board selected - #define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) - #warning STM32F2 board selected - #define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) - #warning STM32F3 board selected - #define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) - #warning STM32F4 board selected - #define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) - #warning STM32F7 board selected - #define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) - #warning STM32L0 board selected - #define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) - #warning STM32L1 board selected - #define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) - #warning STM32L4 board selected - #define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) - #warning STM32H7 board selected - #define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) - #warning STM32G0 board selected - #define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) - #warning STM32G4 board selected - #define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) - #warning STM32WB board selected - #define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) - #warning STM32MP1 board selected - #define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else - #warning STM32 unknown board selected - #define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino b/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino index 3d91d822..3f3ea0db 100644 --- a/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino +++ b/examples/AsyncHTTPMultiRequests_ESP/AsyncHTTPMultiRequests_ESP.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncHTTPMultiRequests.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -41,11 +41,11 @@ //************************************************************************************************************* #if !( defined(ESP8266) || defined(ESP32) ) - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial @@ -63,13 +63,16 @@ const char* ssid = "your_ssid"; const char* password = "your_pass"; #if (ESP8266) - #include + #include #elif (ESP32) - #include + #include #endif // Seconds for timeout, default is 3s -#define DEFAULT_RX_TIMEOUT 10 +#define DEFAULT_RX_TIMEOUT 10 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -82,22 +85,22 @@ Ticker ticker1; 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(" ")); - } + 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(" ")); + } } // To replace with your real APP_API @@ -121,106 +124,115 @@ const char* requestAll[ NUM_REQUESTS ] = { requestCurrent.c_str(), requestMinute uint8_t requestIndex = 0; -void sendRequest() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - requestOpenResult = request.open("GET", requestAll[requestIndex] ); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println(F("Can't send bad request")); - } - } - else - { - Serial.println(F("Can't send request")); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + requestOpenResult = request.open("GET", requestAll[requestIndex] ); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println(F("Can't send bad request")); + } + } + else + { + Serial.println(F("Can't send request")); + } } -void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.print(F("\n***************")); Serial.print(requestName[ requestIndex ]); Serial.println(F("***************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.print(F("\n***************")); + Serial.print(requestName[ requestIndex ]); + Serial.println(F("***************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } #if 1 - // Bypass hourly - if (requestIndex == 1) - requestIndex = 3; - else - requestIndex = (requestIndex + 1) % NUM_REQUESTS; + + // Bypass hourly + if (requestIndex == 1) + requestIndex = 3; + else + requestIndex = (requestIndex + 1) % NUM_REQUESTS; + #else - // hourly too long, not display anyway. Not enough heap. - requestIndex = (requestIndex + 1) % NUM_REQUESTS; + // hourly too long, not display anyway. Not enough heap. + requestIndex = (requestIndex + 1) % NUM_REQUESTS; #endif - - request->setDebug(false); - } + + request->setDebug(false); + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); - - delay(200); - - Serial.print(F("\nStarting AsyncHTTPMultiRequests using ")); Serial.println(ARDUINO_BOARD); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + delay(200); + + Serial.print(F("\nStarting AsyncHTTPMultiRequests using ")); + Serial.println(ARDUINO_BOARD); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print(F("Warning. Must use this example on Version equal or later than : ")); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print(F("Warning. Must use this example on Version equal or later than : ")); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - WiFi.mode(WIFI_STA); - - WiFi.begin(ssid, password); - - Serial.print(F("Connecting to WiFi SSID: ")); Serial.println(ssid); - - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print(F(".")); - } - - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(WiFi.localIP()); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); - - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); - - // Send first request now - sendRequest(); + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, password); + + Serial.print(F("Connecting to WiFi SSID: ")); + Serial.println(ssid); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print(F(".")); + } + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(WiFi.localIP()); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + + // Send first request now + sendRequest(); } void loop() -{ +{ } diff --git a/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino b/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino index 4e9e2775..21d67a83 100644 --- a/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino +++ b/examples/AsyncHTTPRequest_ESP/AsyncHTTPRequest_ESP.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -41,7 +41,7 @@ //************************************************************************************************************* #if !( defined(ESP8266) || defined(ESP32) ) - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif // Level from 0-4 @@ -60,16 +60,19 @@ const char* ssid = "your_ssid"; const char* password = "your_pass"; #if (ESP8266) - #include + #include #elif (ESP32) - #include + #include #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // Seconds for timeout, default is 3s -#define DEFAULT_RX_TIMEOUT 10 +#define DEFAULT_RX_TIMEOUT 10 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -82,102 +85,105 @@ Ticker ticker1; void heartBeatPrint() { - 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(" ")); - } + 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() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); - requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println(F("Can't send bad request")); - } - } - else - { - Serial.println(F("Can't send request")); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println(F("Can't send bad request")); + } + } + else + { + Serial.println(F("Can't send request")); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); - - Serial.print(F("\nStarting AsyncHTTPRequest_ESP using ")); Serial.println(ARDUINO_BOARD); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - - WiFi.mode(WIFI_STA); - - WiFi.begin(ssid, password); - - Serial.print(F("Connecting to WiFi SSID: ")); Serial.println(ssid); - - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print(F(".")); - } - - Serial.print(F("\nAsyncHTTPRequest @ IP : ")); - Serial.println(WiFi.localIP()); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); - - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); - - // Send first request now - sendRequest(); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.print(F("\nStarting AsyncHTTPRequest_ESP using ")); + Serial.println(ARDUINO_BOARD); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + + WiFi.mode(WIFI_STA); + + WiFi.begin(ssid, password); + + Serial.print(F("Connecting to WiFi SSID: ")); + Serial.println(ssid); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print(F(".")); + } + + Serial.print(F("\nAsyncHTTPRequest @ IP : ")); + Serial.println(WiFi.localIP()); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + + // Send first request now + sendRequest(); } void loop() -{ +{ } diff --git a/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino b/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino index fca38186..db78f6f4 100644 --- a/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino +++ b/examples/AsyncHTTPRequest_ESP8266_Ethernet/AsyncHTTPRequest_ESP8266_Ethernet.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -41,12 +41,12 @@ //************************************************************************************************************* #if !( defined(ESP8266) ) - #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting. #endif // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial -#define _ASYNC_HTTP_LOGLEVEL_ 1 +#define _ASYNC_HTTP_LOGLEVEL_ 1 // 300s = 5 minutes to not flooding #define HTTP_REQUEST_INTERVAL 60 //300 @@ -65,27 +65,27 @@ #define CSPIN 16 // 5 #if USING_W5500 - #include "W5500lwIP.h" - #define SHIELD_TYPE "ESP8266_W5500 Ethernet" - - Wiznet5500lwIP eth(CSPIN); - + #include "W5500lwIP.h" + #define SHIELD_TYPE "ESP8266_W5500 Ethernet" + + Wiznet5500lwIP eth(CSPIN); + #elif USING_W5100 - #include - #define SHIELD_TYPE "ESP8266_W5100 Ethernet" - - Wiznet5100lwIP eth(CSPIN); + #include + #define SHIELD_TYPE "ESP8266_W5100 Ethernet" + + Wiznet5100lwIP eth(CSPIN); #elif USING_ENC28J60 - #include - #define SHIELD_TYPE "ESP8266_ENC28J60 Ethernet" - - ENC28J60lwIP eth(CSPIN); + #include + #define SHIELD_TYPE "ESP8266_ENC28J60 Ethernet" + + ENC28J60lwIP eth(CSPIN); #else - // default if none selected - #include "W5500lwIP.h" + // default if none selected + #include "W5500lwIP.h" - Wiznet5500lwIP eth(CSPIN); + Wiznet5500lwIP eth(CSPIN); #endif @@ -95,11 +95,14 @@ using TCPClient = WiFiClient; ////////////////////////////////////////////////////////// -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // Seconds for timeout, default is 3s -#define DEFAULT_RX_TIMEOUT 10 +#define DEFAULT_RX_TIMEOUT 10 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -112,125 +115,128 @@ Ticker ticker1; void heartBeatPrint() { - static int num = 1; - - if (eth.connected()) - Serial.print(F("H")); // H means connected to Ethernet - else - Serial.print(F("F")); // F means not connected to Ethernet - - if (num == 80) - { - Serial.println(); - num = 1; - } - else if (num++ % 10 == 0) - { - Serial.print(F(" ")); - } + static int num = 1; + + if (eth.connected()) + Serial.print(F("H")); // H means connected to Ethernet + else + Serial.print(F("F")); // F means not connected to Ethernet + + if (num == 80) + { + Serial.println(); + num = 1; + } + else if (num++ % 10 == 0) + { + Serial.print(F(" ")); + } } -void sendRequest() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); - requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println(F("Can't send bad request")); - } - } - else - { - Serial.println(F("Can't send request")); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println(F("Can't send bad request")); + } + } + else + { + Serial.println(F("Can't send request")); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } void initEthernet() { - SPI.begin(); - SPI.setClockDivider(SPI_CLOCK_DIV4); - SPI.setBitOrder(MSBFIRST); - SPI.setDataMode(SPI_MODE0); - eth.setDefault(); - - if (!eth.begin()) - { - Serial.println("No Ethernet hardware ... Stop here"); - - while (true) - { - delay(1000); - } - } - else - { - Serial.print("Connecting to network : "); - - while (!eth.connected()) - { - Serial.print("."); - delay(1000); - } - } - - Serial.println(); - Serial.print("Ethernet IP address: "); - Serial.println(eth.localIP()); + SPI.begin(); + SPI.setClockDivider(SPI_CLOCK_DIV4); + SPI.setBitOrder(MSBFIRST); + SPI.setDataMode(SPI_MODE0); + eth.setDefault(); + + if (!eth.begin()) + { + Serial.println("No Ethernet hardware ... Stop here"); + + while (true) + { + delay(1000); + } + } + else + { + Serial.print("Connecting to network : "); + + while (!eth.connected()) + { + Serial.print("."); + delay(1000); + } + } + + Serial.println(); + Serial.print("Ethernet IP address: "); + Serial.println(eth.localIP()); } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); - - delay(200); - - Serial.print("\nStart AsyncHTTPRequest_ESP8266_Ethernet on "); Serial.print(ARDUINO_BOARD); - Serial.print(" using "); Serial.println(SHIELD_TYPE); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - - initEthernet(); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); - - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); - - // Send first request now - sendRequest(); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + delay(200); + + Serial.print("\nStart AsyncHTTPRequest_ESP8266_Ethernet on "); + Serial.print(ARDUINO_BOARD); + Serial.print(" using "); + Serial.println(SHIELD_TYPE); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + + initEthernet(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + + // Send first request now + sendRequest(); } void loop() -{ +{ } diff --git a/examples/AsyncHTTPRequest_ESP_Multi/AsyncHTTPRequest_ESP_Multi.ino b/examples/AsyncHTTPRequest_ESP_Multi/AsyncHTTPRequest_ESP_Multi.ino index 9e102e6b..174916fb 100644 --- a/examples/AsyncHTTPRequest_ESP_Multi/AsyncHTTPRequest_ESP_Multi.ino +++ b/examples/AsyncHTTPRequest_ESP_Multi/AsyncHTTPRequest_ESP_Multi.ino @@ -1,25 +1,25 @@ /**************************************************************************************************************************** AsyncHTTPRequest_ESP_Multi.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ #if !( defined(ESP8266) || defined(ESP32) ) - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif // Level from 0-4 @@ -39,25 +39,28 @@ const char* ssid = "your_ssid"; const char* password = "your_pass"; #if (ESP8266) - #include + #include #elif (ESP32) - #include + #include #endif // Seconds for timeout, default is 3s -#define DEFAULT_RX_TIMEOUT 10 +#define DEFAULT_RX_TIMEOUT 10 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error -#include // http://github.com/khoih-prog/AsyncHTTPRequest_Generic +#include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic #include #define NUM_DIFFERENT_SITES 2 -const char* addreses[][NUM_DIFFERENT_SITES] = +const char* addreses[][NUM_DIFFERENT_SITES] = { - {"http://worldtimeapi.org/api/timezone/America/Toronto.txt", "http://worldtimeapi.org/api/timezone/Europe/Prague.txt"}, - {"http://www.myexternalip.com/raw"} + {"http://worldtimeapi.org/api/timezone/America/Toronto.txt", "http://worldtimeapi.org/api/timezone/Europe/Prague.txt"}, + {"http://www.myexternalip.com/raw"} }; #define NUM_ENTRIES_SITE_0 2 @@ -85,159 +88,161 @@ Ticker ticker1; void heartBeatPrint() { - 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(" ")); - } + 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(uint16_t index) { - static bool requestOpenResult; - - reqCount[index]--; - readySend[index] = false; - - requestOpenResult = request[index].open("GET", addreses[index][reqCount[index]]); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - Serial.print("\nSending request: "); - request[index].send(); - } - else - { - Serial.print("\nCan't send bad request : "); - } - - Serial.println(addreses[index][reqCount[index]]); + static bool requestOpenResult; + + reqCount[index]--; + readySend[index] = false; + + requestOpenResult = request[index].open("GET", addreses[index][reqCount[index]]); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + Serial.print("\nSending request: "); + request[index].send(); + } + else + { + Serial.print("\nCan't send bad request : "); + } + + Serial.println(addreses[index][reqCount[index]]); } void sendRequest0() { - sendRequest(0); + sendRequest(0); } void sendRequest1() { - sendRequest(1); + sendRequest(1); } void sendRequests() { - reqCount[0] = NUM_ENTRIES_SITE_0; - reqCount[1] = NUM_ENTRIES_SITE_1; + reqCount[0] = NUM_ENTRIES_SITE_0; + reqCount[1] = NUM_ENTRIES_SITE_1; } void requestCB0(void* optParm, AsyncHTTPRequest* thisRequest, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGWARN(F("\n**************************************")); - AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - else - { - AHTTP_LOGERROR(F("Response error")); - } - - thisRequest->setDebug(false); - readySend[0] = true; - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGWARN(F("\n**************************************")); + AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + else + { + AHTTP_LOGERROR(F("Response error")); + } + + thisRequest->setDebug(false); + readySend[0] = true; + } } void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGWARN(F("\n**************************************")); - AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - else - { - AHTTP_LOGERROR(F("Response error")); - } - - thisRequest->setDebug(false); - readySend[1] = true; - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGWARN(F("\n**************************************")); + AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + else + { + AHTTP_LOGERROR(F("Response error")); + } + + thisRequest->setDebug(false); + readySend[1] = true; + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncHTTPRequest_ESP_Multi on "); Serial.println(ARDUINO_BOARD); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncHTTPRequest_ESP_Multi on "); + Serial.println(ARDUINO_BOARD); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - WiFi.mode(WIFI_STA); + WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); + WiFi.begin(ssid, password); - Serial.println("Connecting to WiFi SSID: " + String(ssid)); + Serial.println("Connecting to WiFi SSID: " + String(ssid)); - while (WiFi.status() != WL_CONNECTED) - { - delay(1000); - Serial.print("."); - } + while (WiFi.status() != WL_CONNECTED) + { + delay(1000); + Serial.print("."); + } - Serial.print(F("\nAsyncHTTPSRequest @ IP : ")); - Serial.println(WiFi.localIP()); + Serial.print(F("\nAsyncHTTPSRequest @ IP : ")); + Serial.println(WiFi.localIP()); - for (int index = 0; index < NUM_DIFFERENT_SITES; index++) - { - request[index].setDebug(false); + for (int index = 0; index < NUM_DIFFERENT_SITES; index++) + { + request[index].setDebug(false); - request[index].onReadyStateChange(requestCB[index]); - } + request[index].onReadyStateChange(requestCB[index]); + } - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequests); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequests); - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); } void loop() { - for (int index = 0; index < NUM_DIFFERENT_SITES; index++) - { - if ((reqCount[index] > 0) && readySend[index]) - { - sendRequestCB[index](); - - // Don't send too fast - delay(1000); - } - } + for (int index = 0; index < NUM_DIFFERENT_SITES; index++) + { + if ((reqCount[index] > 0) && readySend[index]) + { + sendRequestCB[index](); + + // Don't send too fast + delay(1000); + } + } } diff --git a/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino b/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino index 4f7eafac..af00cc56 100644 --- a/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino +++ b/examples/AsyncHTTPRequest_ESP_WiFiManager/AsyncHTTPRequest_ESP_WiFiManager.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncHTTPRequest_ESP_WiFiManager.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -43,14 +43,17 @@ // Important note: This code is intended to run with the ESPAsync_WiFiManager library version 1.4.1+. #if !( defined(ESP8266) || defined(ESP32) ) - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET "ESPAsync_WiFiManager v1.15.0" #define ESP_ASYNC_WIFIMANAGER_VERSION_MIN 1015000 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial @@ -63,90 +66,90 @@ //Ported to ESP32 #ifdef ESP32 - #include - #include - #include - - // From v1.1.1 - #include - WiFiMulti wifiMulti; - - // LittleFS has higher priority than SPIFFS - #define USE_LITTLEFS true - #define USE_SPIFFS false - - #if USE_LITTLEFS - // Use LittleFS - #include "FS.h" - - // The library will be depreciated after being merged to future major Arduino esp32 core release 2.x - // At that time, just remove this library inclusion - #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) - #warning Using ESP32 Core 1.0.6 or 2.0.0+ and core LittleFS library - // The library has been merged into esp32 core from release 1.0.6 - #include // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS - - //#define FileFS LittleFS - //#define FS_Name "LittleFS" - FS* filesystem = &LittleFS; - #define FileFS LittleFS - #else - #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library - // The library has been merged into esp32 core from release 1.0.6 - #include // https://github.com/lorol/LITTLEFS - - //#define FileFS LITTLEFS - FS* filesystem = &LITTLEFS; - #define FileFS LITTLEFS - #endif - - #define FS_Name "LittleFS" - #elif USE_SPIFFS - #include - FS* filesystem = &SPIFFS; - #define FileFS SPIFFS - #define FS_Name "SPIFFS" - #else - // +Use FFat - #include - FS* filesystem = &FFat; - #define FileFS FFat - #define FS_Name "FFat" - #endif - ////// - - #define LED_BUILTIN 2 - #define LED_ON HIGH - #define LED_OFF LOW + #include + #include + #include + + // From v1.1.1 + #include + WiFiMulti wifiMulti; + + // LittleFS has higher priority than SPIFFS + #define USE_LITTLEFS true + #define USE_SPIFFS false + + #if USE_LITTLEFS + // Use LittleFS + #include "FS.h" + + // The library will be depreciated after being merged to future major Arduino esp32 core release 2.x + // At that time, just remove this library inclusion + #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) + #warning Using ESP32 Core 1.0.6 or 2.0.0+ and core LittleFS library + // The library has been merged into esp32 core from release 1.0.6 + #include // https://github.com/espressif/arduino-esp32/tree/master/libraries/LittleFS + + //#define FileFS LittleFS + //#define FS_Name "LittleFS" + FS* filesystem = &LittleFS; + #define FileFS LittleFS + #else + #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library + // The library has been merged into esp32 core from release 1.0.6 + #include // https://github.com/lorol/LITTLEFS + + //#define FileFS LITTLEFS + FS* filesystem = &LITTLEFS; + #define FileFS LITTLEFS + #endif + + #define FS_Name "LittleFS" + #elif USE_SPIFFS + #include + FS* filesystem = &SPIFFS; + #define FileFS SPIFFS + #define FS_Name "SPIFFS" + #else + // +Use FFat + #include + FS* filesystem = &FFat; + #define FileFS FFat + #define FS_Name "FFat" + #endif + ////// + + #define LED_BUILTIN 2 + #define LED_ON HIGH + #define LED_OFF LOW #else - #include //https://github.com/esp8266/Arduino - //needed for library - #include - - // From v1.1.1 - #include - ESP8266WiFiMulti wifiMulti; - - #define USE_LITTLEFS true - - #if USE_LITTLEFS - #include - FS* filesystem = &LittleFS; - #define FileFS LittleFS - #define FS_Name "LittleFS" - #else - FS* filesystem = &SPIFFS; - #define FileFS SPIFFS - #define FS_Name "SPIFFS" - #endif - ////// - - #define ESP_getChipId() (ESP.getChipId()) - - #define LED_ON LOW - #define LED_OFF HIGH + #include //https://github.com/esp8266/Arduino + //needed for library + #include + + // From v1.1.1 + #include + ESP8266WiFiMulti wifiMulti; + + #define USE_LITTLEFS true + + #if USE_LITTLEFS + #include + FS* filesystem = &LittleFS; + #define FileFS LittleFS + #define FS_Name "LittleFS" + #else + FS* filesystem = &SPIFFS; + #define FileFS SPIFFS + #define FS_Name "SPIFFS" + #endif + ////// + + #define ESP_getChipId() (ESP.getChipId()) + + #define LED_ON LOW + #define LED_OFF HIGH #endif // SSID and PW for your Router @@ -166,21 +169,21 @@ String Router_Pass; typedef struct { - char wifi_ssid[SSID_MAX_LEN]; - char wifi_pw [PASS_MAX_LEN]; + char wifi_ssid[SSID_MAX_LEN]; + char wifi_pw [PASS_MAX_LEN]; } WiFi_Credentials; typedef struct { - String wifi_ssid; - String wifi_pw; + String wifi_ssid; + String wifi_pw; } WiFi_Credentials_String; #define NUM_WIFI_CREDENTIALS 2 typedef struct { - WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; + WiFi_Credentials WiFi_Creds [NUM_WIFI_CREDENTIALS]; } WM_Config; WM_Config WM_config; @@ -215,35 +218,35 @@ bool initialConfig = false; // Use USE_DHCP_IP == true for dynamic DHCP IP, false to use static IP which you have to change accordingly to your network #if (defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP) -// Force DHCP to be true - #if defined(USE_DHCP_IP) - #undef USE_DHCP_IP - #endif - #define USE_DHCP_IP true + // Force DHCP to be true + #if defined(USE_DHCP_IP) + #undef USE_DHCP_IP + #endif + #define USE_DHCP_IP true #else - // You can select DHCP or Static IP here - #define USE_DHCP_IP true - //#define USE_DHCP_IP false + // You can select DHCP or Static IP here + #define USE_DHCP_IP true + //#define USE_DHCP_IP false #endif #if ( USE_DHCP_IP || ( defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP ) ) - // Use DHCP - #warning Using DHCP IP - IPAddress stationIP = IPAddress(0, 0, 0, 0); - IPAddress gatewayIP = IPAddress(192, 168, 2, 1); - IPAddress netMask = IPAddress(255, 255, 255, 0); + // Use DHCP + #warning Using DHCP IP + IPAddress stationIP = IPAddress(0, 0, 0, 0); + IPAddress gatewayIP = IPAddress(192, 168, 2, 1); + IPAddress netMask = IPAddress(255, 255, 255, 0); #else - // Use static IP - #warning Using static IP - - #ifdef ESP32 - IPAddress stationIP = IPAddress(192, 168, 2, 232); - #else - IPAddress stationIP = IPAddress(192, 168, 2, 186); - #endif - - IPAddress gatewayIP = IPAddress(192, 168, 2, 1); - IPAddress netMask = IPAddress(255, 255, 255, 0); + // Use static IP + #warning Using static IP + + #ifdef ESP32 + IPAddress stationIP = IPAddress(192, 168, 2, 232); + #else + IPAddress stationIP = IPAddress(192, 168, 2, 186); + #endif + + IPAddress gatewayIP = IPAddress(192, 168, 2, 1); + IPAddress netMask = IPAddress(255, 255, 255, 0); #endif #define USE_CONFIGURABLE_DNS true @@ -280,25 +283,25 @@ Ticker ticker; /////////////////////////////////////////// // New in v1.4.0 /****************************************** - * // Defined in ESPAsync_WiFiManager.h -typedef struct -{ + // Defined in ESPAsync_WiFiManager.h + typedef struct + { IPAddress _ap_static_ip; IPAddress _ap_static_gw; IPAddress _ap_static_sn; -} WiFi_AP_IPConfig; + } WiFi_AP_IPConfig; -typedef struct -{ + typedef struct + { IPAddress _sta_static_ip; IPAddress _sta_static_gw; IPAddress _sta_static_sn; -#if USE_CONFIGURABLE_DNS + #if USE_CONFIGURABLE_DNS IPAddress _sta_static_dns1; IPAddress _sta_static_dns2; -#endif -} WiFi_STA_IPConfig; + #endif + } WiFi_STA_IPConfig; ******************************************/ WiFi_AP_IPConfig WM_AP_IPconfig; @@ -306,40 +309,40 @@ WiFi_STA_IPConfig WM_STA_IPconfig; void initAPIPConfigStruct(WiFi_AP_IPConfig &in_WM_AP_IPconfig) { - in_WM_AP_IPconfig._ap_static_ip = APStaticIP; - in_WM_AP_IPconfig._ap_static_gw = APStaticGW; - in_WM_AP_IPconfig._ap_static_sn = APStaticSN; + in_WM_AP_IPconfig._ap_static_ip = APStaticIP; + in_WM_AP_IPconfig._ap_static_gw = APStaticGW; + in_WM_AP_IPconfig._ap_static_sn = APStaticSN; } void initSTAIPConfigStruct(WiFi_STA_IPConfig &in_WM_STA_IPconfig) { - in_WM_STA_IPconfig._sta_static_ip = stationIP; - in_WM_STA_IPconfig._sta_static_gw = gatewayIP; - in_WM_STA_IPconfig._sta_static_sn = netMask; -#if USE_CONFIGURABLE_DNS - in_WM_STA_IPconfig._sta_static_dns1 = dns1IP; - in_WM_STA_IPconfig._sta_static_dns2 = dns2IP; + in_WM_STA_IPconfig._sta_static_ip = stationIP; + in_WM_STA_IPconfig._sta_static_gw = gatewayIP; + in_WM_STA_IPconfig._sta_static_sn = netMask; +#if USE_CONFIGURABLE_DNS + in_WM_STA_IPconfig._sta_static_dns1 = dns1IP; + in_WM_STA_IPconfig._sta_static_dns2 = dns2IP; #endif } void displayIPConfigStruct(WiFi_STA_IPConfig in_WM_STA_IPconfig) { - LOGERROR3(F("stationIP ="), in_WM_STA_IPconfig._sta_static_ip, F(", gatewayIP ="), in_WM_STA_IPconfig._sta_static_gw); - LOGERROR1(F("netMask ="), in_WM_STA_IPconfig._sta_static_sn); + LOGERROR3(F("stationIP ="), in_WM_STA_IPconfig._sta_static_ip, F(", gatewayIP ="), in_WM_STA_IPconfig._sta_static_gw); + LOGERROR1(F("netMask ="), in_WM_STA_IPconfig._sta_static_sn); #if USE_CONFIGURABLE_DNS - LOGERROR3(F("dns1IP ="), in_WM_STA_IPconfig._sta_static_dns1, F(", dns2IP ="), in_WM_STA_IPconfig._sta_static_dns2); + LOGERROR3(F("dns1IP ="), in_WM_STA_IPconfig._sta_static_dns1, F(", dns2IP ="), in_WM_STA_IPconfig._sta_static_dns2); #endif } void configWiFi(WiFi_STA_IPConfig in_WM_STA_IPconfig) { - #if USE_CONFIGURABLE_DNS - // Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5 - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2); - #else - // Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2. - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn); - #endif +#if USE_CONFIGURABLE_DNS + // Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5 + WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2); +#else + // Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2. + WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn); +#endif } /////////////////////////////////////////// @@ -347,471 +350,481 @@ void configWiFi(WiFi_STA_IPConfig in_WM_STA_IPconfig) uint8_t connectMultiWiFi() { #if ESP32 - // For ESP32, this better be 0 to shorten the connect time. - // For ESP32-S2, must be > 500 - #if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_PROS2 || ARDUINO_MICROS2 ) - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 500L - #else - // For ESP32 core v1.0.6, must be >= 500 - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 800L - #endif + // For ESP32, this better be 0 to shorten the connect time. + // For ESP32-S2, must be > 500 +#if ( ARDUINO_ESP32S2_DEV || ARDUINO_FEATHERS2 || ARDUINO_PROS2 || ARDUINO_MICROS2 ) +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 500L +#else + // For ESP32 core v1.0.6, must be >= 500 +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 800L +#endif #else - // For ESP8266, this better be 2200 to enable connect the 1st time - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 2200L + // For ESP8266, this better be 2200 to enable connect the 1st time +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 2200L #endif #define WIFI_MULTI_CONNECT_WAITING_MS 100L - - uint8_t status; - LOGERROR(F("ConnectMultiWiFi with :")); - - if ( (Router_SSID != "") && (Router_Pass != "") ) - { - LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); - } + uint8_t status; - for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) - { - // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) - { - LOGERROR3(F("* Additional SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); - } - } - - LOGERROR(F("Connecting MultiWifi...")); + LOGERROR(F("ConnectMultiWiFi with :")); - WiFi.mode(WIFI_STA); + if ( (Router_SSID != "") && (Router_Pass != "") ) + { + LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); + } + + for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) + { + // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + { + LOGERROR3(F("* Additional SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); + } + } + + LOGERROR(F("Connecting MultiWifi...")); + + WiFi.mode(WIFI_STA); #if !USE_DHCP_IP - // New in v1.4.0 - configWiFi(WM_STA_IPconfig); - ////// + // New in v1.4.0 + configWiFi(WM_STA_IPconfig); + ////// #endif - int i = 0; - status = wifiMulti.run(); + int i = 0; + status = wifiMulti.run(); - delay(WIFI_MULTI_1ST_CONNECT_WAITING_MS); + delay(WIFI_MULTI_1ST_CONNECT_WAITING_MS); - while ( ( i++ < 20 ) && ( status != WL_CONNECTED ) ) - { - status = wifiMulti.run(); + while ( ( i++ < 20 ) && ( status != WL_CONNECTED ) ) + { + status = wifiMulti.run(); - if ( status == WL_CONNECTED ) - break; - else - delay(WIFI_MULTI_CONNECT_WAITING_MS); - } + if ( status == WL_CONNECTED ) + break; + else + delay(WIFI_MULTI_CONNECT_WAITING_MS); + } - if ( status == WL_CONNECTED ) - { - LOGERROR1(F("WiFi connected after time: "), i); - LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI()); - LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() ); - } - else - LOGERROR(F("WiFi not connected")); - - return status; + if ( status == WL_CONNECTED ) + { + LOGERROR1(F("WiFi connected after time: "), i); + LOGERROR3(F("SSID:"), WiFi.SSID(), F(",RSSI="), WiFi.RSSI()); + LOGERROR3(F("Channel:"), WiFi.channel(), F(",IP address:"), WiFi.localIP() ); + } + else + LOGERROR(F("WiFi not connected")); + + return status; } void heartBeatPrint() { - 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(" ")); - } + 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 check_WiFi() { - if ( (WiFi.status() != WL_CONNECTED) ) - { - Serial.println(F("\nWiFi lost. Call connectMultiWiFi in loop")); - connectMultiWiFi(); - } -} + if ( (WiFi.status() != WL_CONNECTED) ) + { + Serial.println(F("\nWiFi lost. Call connectMultiWiFi in loop")); + connectMultiWiFi(); + } +} void check_status() { - static ulong checkstatus_timeout = 0; - static ulong checkwifi_timeout = 0; + static ulong checkstatus_timeout = 0; + static ulong checkwifi_timeout = 0; - static ulong current_millis; + static ulong current_millis; #define WIFICHECK_INTERVAL 1000L #define HEARTBEAT_INTERVAL 10000L - current_millis = millis(); - - // Check WiFi every WIFICHECK_INTERVAL (1) seconds. - if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0)) - { - check_WiFi(); - checkwifi_timeout = current_millis + WIFICHECK_INTERVAL; - } - - // Print hearbeat every HEARTBEAT_INTERVAL (10) seconds. - if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) - { - heartBeatPrint(); - checkstatus_timeout = current_millis + HEARTBEAT_INTERVAL; - } + current_millis = millis(); + + // Check WiFi every WIFICHECK_INTERVAL (1) seconds. + if ((current_millis > checkwifi_timeout) || (checkwifi_timeout == 0)) + { + check_WiFi(); + checkwifi_timeout = current_millis + WIFICHECK_INTERVAL; + } + + // Print hearbeat every HEARTBEAT_INTERVAL (10) seconds. + if ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) + { + heartBeatPrint(); + checkstatus_timeout = current_millis + HEARTBEAT_INTERVAL; + } } void loadConfigData() { - File file = FileFS.open(CONFIG_FILENAME, "r"); - LOGERROR(F("LoadWiFiCfgFile ")); - - memset(&WM_config, 0, sizeof(WM_config)); - - // New in v1.4.0 - //memset(&WM_STA_IPconfig, 0, sizeof(WM_STA_IPconfig)); - // New in v1.8.2 - initSTAIPConfigStruct(WM_STA_IPconfig); - ////// - - if (file) - { - file.readBytes((char *) &WM_config, sizeof(WM_config)); - - // New in v1.4.0 - file.readBytes((char *) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); - ////// - - file.close(); - LOGERROR(F("OK")); - - // New in v1.4.0 - displayIPConfigStruct(WM_STA_IPconfig); - ////// - } - else - { - LOGERROR(F("failed")); - } + File file = FileFS.open(CONFIG_FILENAME, "r"); + LOGERROR(F("LoadWiFiCfgFile ")); + + memset(&WM_config, 0, sizeof(WM_config)); + + // New in v1.4.0 + //memset(&WM_STA_IPconfig, 0, sizeof(WM_STA_IPconfig)); + // New in v1.8.2 + initSTAIPConfigStruct(WM_STA_IPconfig); + ////// + + if (file) + { + file.readBytes((char *) &WM_config, sizeof(WM_config)); + + // New in v1.4.0 + file.readBytes((char *) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); + ////// + + file.close(); + LOGERROR(F("OK")); + + // New in v1.4.0 + displayIPConfigStruct(WM_STA_IPconfig); + ////// + } + else + { + LOGERROR(F("failed")); + } } - + void saveConfigData() { - File file = FileFS.open(CONFIG_FILENAME, "w"); - LOGERROR(F("SaveWiFiCfgFile ")); - - if (file) - { - file.write((uint8_t*) &WM_config, sizeof(WM_config)); - - // New in v1.4.0 - file.write((uint8_t*) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); - ////// - - file.close(); - LOGERROR(F("OK")); - } - else - { - LOGERROR(F("failed")); - } + File file = FileFS.open(CONFIG_FILENAME, "w"); + LOGERROR(F("SaveWiFiCfgFile ")); + + if (file) + { + file.write((uint8_t*) &WM_config, sizeof(WM_config)); + + // New in v1.4.0 + file.write((uint8_t*) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); + ////// + + file.close(); + LOGERROR(F("OK")); + } + else + { + LOGERROR(F("failed")); + } } -void sendRequest() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); - requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println(F("Can't send bad request")); - } - } - else - { - Serial.println(F("Can't send request")); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println(F("Can't send bad request")); + } + } + else + { + Serial.println(F("Can't send request")); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); - delay(200); - - Serial.print(F("\nStarting AsyncHTTPRequest_ESP_WiFiManager using ")); Serial.print(FS_Name); - Serial.print(F(" on ")); Serial.println(ARDUINO_BOARD); - Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + delay(200); + + Serial.print(F("\nStarting AsyncHTTPRequest_ESP_WiFiManager using ")); + Serial.print(FS_Name); + Serial.print(F(" on ")); + Serial.println(ARDUINO_BOARD); + Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ESP_ASYNC_WIFIMANAGER_VERSION_INT) - if (ESP_ASYNC_WIFIMANAGER_VERSION_INT < ESP_ASYNC_WIFIMANAGER_VERSION_MIN) - { - Serial.print(F("Warning. Must use this example on Version later than : ")); - Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET); - } + + if (ESP_ASYNC_WIFIMANAGER_VERSION_INT < ESP_ASYNC_WIFIMANAGER_VERSION_MIN) + { + Serial.print(F("Warning. Must use this example on Version later than : ")); + Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION_MIN_TARGET); + } + #endif #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print(F("Warning. Must use this example on Version equal or later than : ")); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print(F("Warning. Must use this example on Version equal or later than : ")); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - if (FORMAT_FILESYSTEM) - FileFS.format(); + if (FORMAT_FILESYSTEM) + FileFS.format(); - // Format FileFS if not yet + // Format FileFS if not yet #ifdef ESP32 - if (!FileFS.begin(true)) + + if (!FileFS.begin(true)) #else - if (!FileFS.begin()) -#endif - { - Serial.print(FS_Name); - Serial.println(F(" failed! AutoFormatting.")); - + if (!FileFS.begin()) +#endif + { + Serial.print(FS_Name); + Serial.println(F(" failed! AutoFormatting.")); + #ifdef ESP8266 - FileFS.format(); + FileFS.format(); #endif - } - - unsigned long startedAt = millis(); - - // New in v1.4.0 - initAPIPConfigStruct(WM_AP_IPconfig); - initSTAIPConfigStruct(WM_STA_IPconfig); - ////// - - //Local intialization. Once its business is done, there is no need to keep it around - // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX - //ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer); - // Use this to personalize DHCP hostname (RFC952 conformed) - AsyncWebServer webServer(HTTP_PORT); - - //ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "AutoConnectAP"); + } + + unsigned long startedAt = millis(); + + // New in v1.4.0 + initAPIPConfigStruct(WM_AP_IPconfig); + initSTAIPConfigStruct(WM_STA_IPconfig); + ////// + + //Local intialization. Once its business is done, there is no need to keep it around + // Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX + //ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer); + // Use this to personalize DHCP hostname (RFC952 conformed) + AsyncWebServer webServer(HTTP_PORT); + + //ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "AutoConnectAP"); #if ( USING_ESP32_S2 || USING_ESP32_C3 ) - ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, NULL, "AutoConnectAP"); + ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, NULL, "AutoConnectAP"); #else - AsyncDNSServer dnsServer; + AsyncDNSServer dnsServer; - ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "AutoConnectAP"); -#endif + ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "AutoConnectAP"); +#endif - //ESPAsync_wifiManager.setDebugOutput(true); + //ESPAsync_wifiManager.setDebugOutput(true); - //reset settings - for testing - //ESPAsync_wifiManager.resetSettings(); + //reset settings - for testing + //ESPAsync_wifiManager.resetSettings(); #if USE_CUSTOM_AP_IP - //set custom ip for portal - // New in v1.4.0 - ESPAsync_wifiManager.setAPStaticIPConfig(WM_AP_IPconfig); - ////// + //set custom ip for portal + // New in v1.4.0 + ESPAsync_wifiManager.setAPStaticIPConfig(WM_AP_IPconfig); + ////// #endif - ESPAsync_wifiManager.setMinimumSignalQuality(-1); - - // From v1.0.10 only - // Set config portal channel, default = 1. Use 0 => random channel from 1-13 - ESPAsync_wifiManager.setConfigPortalChannel(0); - ////// - -#if !USE_DHCP_IP - // Set (static IP, Gateway, Subnetmask, DNS1 and DNS2) or (IP, Gateway, Subnetmask). New in v1.0.5 - // New in v1.4.0 - ESPAsync_wifiManager.setSTAStaticIPConfig(WM_STA_IPconfig); - ////// + ESPAsync_wifiManager.setMinimumSignalQuality(-1); + + // From v1.0.10 only + // Set config portal channel, default = 1. Use 0 => random channel from 1-13 + ESPAsync_wifiManager.setConfigPortalChannel(0); + ////// + +#if !USE_DHCP_IP + // Set (static IP, Gateway, Subnetmask, DNS1 and DNS2) or (IP, Gateway, Subnetmask). New in v1.0.5 + // New in v1.4.0 + ESPAsync_wifiManager.setSTAStaticIPConfig(WM_STA_IPconfig); + ////// #endif - // New from v1.1.1 + // New from v1.1.1 #if USING_CORS_FEATURE - ESPAsync_wifiManager.setCORSHeader("Your Access-Control-Allow-Origin"); + ESPAsync_wifiManager.setCORSHeader("Your Access-Control-Allow-Origin"); #endif - // We can't use WiFi.SSID() in ESP32 as it's only valid after connected. - // SSID and Password stored in ESP32 wifi_ap_record_t and wifi_config_t are also cleared in reboot - // Have to create a new function to store in EEPROM/SPIFFS for this purpose - Router_SSID = ESPAsync_wifiManager.WiFi_SSID(); - Router_Pass = ESPAsync_wifiManager.WiFi_Pass(); + // We can't use WiFi.SSID() in ESP32 as it's only valid after connected. + // SSID and Password stored in ESP32 wifi_ap_record_t and wifi_config_t are also cleared in reboot + // Have to create a new function to store in EEPROM/SPIFFS for this purpose + Router_SSID = ESPAsync_wifiManager.WiFi_SSID(); + Router_Pass = ESPAsync_wifiManager.WiFi_Pass(); - //Remove this line if you do not want to see WiFi password printed - Serial.print(F("Stored: SSID = ")); Serial.print(Router_SSID); - Serial.print(F(", Pass = ")); Serial.println(Router_Pass); + //Remove this line if you do not want to see WiFi password printed + Serial.print(F("Stored: SSID = ")); + Serial.print(Router_SSID); + Serial.print(F(", Pass = ")); + Serial.println(Router_Pass); - if (Router_SSID != "") - { - ESPAsync_wifiManager.setConfigPortalTimeout(120); //If no access point name has been previously entered disable timeout. - Serial.println(F("Got stored Credentials. Timeout 120s")); - } - else - { - Serial.println(F("No stored Credentials. No timeout")); - } + if (Router_SSID != "") + { + ESPAsync_wifiManager.setConfigPortalTimeout(120); //If no access point name has been previously entered disable timeout. + Serial.println(F("Got stored Credentials. Timeout 120s")); + } + else + { + Serial.println(F("No stored Credentials. No timeout")); + } - String chipID = String(ESP_getChipId(), HEX); - chipID.toUpperCase(); + String chipID = String(ESP_getChipId(), HEX); + chipID.toUpperCase(); - // SSID and PW for Config Portal - String AP_SSID = "ESP_" + chipID + "_AutoConnectAP"; - String AP_PASS = "MyESP_" + chipID; + // SSID and PW for Config Portal + String AP_SSID = "ESP_" + chipID + "_AutoConnectAP"; + String AP_PASS = "MyESP_" + chipID; - // From v1.1.0, Don't permit NULL password - if ( (Router_SSID == "") || (Router_Pass == "") ) - { - Serial.println(F("We haven't got any access point credentials, so get them now")); + // From v1.1.0, Don't permit NULL password + if ( (Router_SSID == "") || (Router_Pass == "") ) + { + Serial.println(F("We haven't got any access point credentials, so get them now")); - initialConfig = true; + initialConfig = true; - Serial.print(F("Starting configuration portal @ ")); - -#if USE_CUSTOM_AP_IP - Serial.print(APStaticIP); + Serial.print(F("Starting configuration portal @ ")); + +#if USE_CUSTOM_AP_IP + Serial.print(APStaticIP); #else - Serial.print(F("192.168.4.1")); + Serial.print(F("192.168.4.1")); #endif - Serial.print(F(", SSID = ")); - Serial.print(AP_SSID); - Serial.print(F(", PASS = ")); - Serial.println(AP_PASS); - - // Starts an access point - //if (!ESPAsync_wifiManager.startConfigPortal((const char *) ssid.c_str(), password)) - if ( !ESPAsync_wifiManager.startConfigPortal(AP_SSID.c_str(), AP_PASS.c_str()) ) - Serial.println(F("Not connected to WiFi but continuing anyway.")); - else - Serial.println(F("WiFi connected...yeey :)")); - - // Stored for later usage, from v1.1.0, but clear first - memset(&WM_config, 0, sizeof(WM_config)); - - for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) - { - String tempSSID = ESPAsync_wifiManager.getSSID(i); - String tempPW = ESPAsync_wifiManager.getPW(i); - - if (strlen(tempSSID.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1) - strcpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str()); - else - strncpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1); - - if (strlen(tempPW.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1) - strcpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str()); - else - strncpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1); - - // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) - { - LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); - wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); - } - } - - // New in v1.4.0 - ESPAsync_wifiManager.getSTAStaticIPConfig(WM_STA_IPconfig); - displayIPConfigStruct(WM_STA_IPconfig); - ////// - - saveConfigData(); - } - else - { - wifiMulti.addAP(Router_SSID.c_str(), Router_Pass.c_str()); - } - - startedAt = millis(); - - if (!initialConfig) - { - // Load stored data, the addAP ready for MultiWiFi reconnection - loadConfigData(); - - for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) - { - // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) - { - LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); - wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); - } - } - - if ( WiFi.status() != WL_CONNECTED ) - { - Serial.println(F("ConnectMultiWiFi in setup")); - - connectMultiWiFi(); - } - } - - Serial.print(F("After waiting ")); - Serial.print((float) (millis() - startedAt) / 1000L); - Serial.print(F(" secs more in setup(), connection result is ")); - - if (WiFi.status() == WL_CONNECTED) - { - Serial.print(F("connected. Local IP: ")); - Serial.println(WiFi.localIP()); - } - else - Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status())); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); - - // Send first request now - sendRequest(); + Serial.print(F(", SSID = ")); + Serial.print(AP_SSID); + Serial.print(F(", PASS = ")); + Serial.println(AP_PASS); + + // Starts an access point + //if (!ESPAsync_wifiManager.startConfigPortal((const char *) ssid.c_str(), password)) + if ( !ESPAsync_wifiManager.startConfigPortal(AP_SSID.c_str(), AP_PASS.c_str()) ) + Serial.println(F("Not connected to WiFi but continuing anyway.")); + else + Serial.println(F("WiFi connected...yeey :)")); + + // Stored for later usage, from v1.1.0, but clear first + memset(&WM_config, 0, sizeof(WM_config)); + + for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) + { + String tempSSID = ESPAsync_wifiManager.getSSID(i); + String tempPW = ESPAsync_wifiManager.getPW(i); + + if (strlen(tempSSID.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1) + strcpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str()); + else + strncpy(WM_config.WiFi_Creds[i].wifi_ssid, tempSSID.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_ssid) - 1); + + if (strlen(tempPW.c_str()) < sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1) + strcpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str()); + else + strncpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1); + + // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + { + LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); + wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); + } + } + + // New in v1.4.0 + ESPAsync_wifiManager.getSTAStaticIPConfig(WM_STA_IPconfig); + displayIPConfigStruct(WM_STA_IPconfig); + ////// + + saveConfigData(); + } + else + { + wifiMulti.addAP(Router_SSID.c_str(), Router_Pass.c_str()); + } + + startedAt = millis(); + + if (!initialConfig) + { + // Load stored data, the addAP ready for MultiWiFi reconnection + loadConfigData(); + + for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) + { + // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + { + LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); + wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); + } + } + + if ( WiFi.status() != WL_CONNECTED ) + { + Serial.println(F("ConnectMultiWiFi in setup")); + + connectMultiWiFi(); + } + } + + Serial.print(F("After waiting ")); + Serial.print((float) (millis() - startedAt) / 1000L); + Serial.print(F(" secs more in setup(), connection result is ")); + + if (WiFi.status() == WL_CONNECTED) + { + Serial.print(F("connected. Local IP: ")); + Serial.println(WiFi.localIP()); + } + else + Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status())); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + // Send first request now + sendRequest(); } void loop() -{ - // put your main code here, to run repeatedly - check_status(); +{ + // put your main code here, to run repeatedly + check_status(); } diff --git a/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino b/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino index b9bf2c3e..ec4a997d 100644 --- a/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino +++ b/examples/AsyncHTTPRequest_STM32/AsyncHTTPRequest_STM32.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncHTTPRequest_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -42,12 +42,15 @@ #include "defines.h" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -58,91 +61,95 @@ AsyncHTTPRequest request; void sendRequest(); // Repeat forever, millis() resolution -Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); - requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); - Serial.print("\nStart AsyncHTTPRequest_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + while (!Serial && millis() < 5000); + + Serial.print("\nStart AsyncHTTPRequest_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } -#endif - - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; - - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); - - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(Ethernet.localIP()); - Serial.println(); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker. - - // Send first request now - //delay(60); - sendRequest(); + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + +#endif + + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. + + // Send first request now + //delay(60); + sendRequest(); } -void loop() +void loop() { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncHTTPRequest_STM32/defines.h b/examples/AsyncHTTPRequest_STM32/defines.h index 7e73c81a..44a93644 100644 --- a/examples/AsyncHTTPRequest_STM32/defines.h +++ b/examples/AsyncHTTPRequest_STM32/defines.h @@ -1,30 +1,30 @@ /**************************************************************************************************************************** defines.h - + 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /* Currently support 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : - Nucleo-144 (F429ZI, F767ZI) - Discovery (STM32F746G-DISCOVERY) - - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields @@ -36,7 +36,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define ASYNC_HTTP_DEBUG_PORT Serial @@ -46,54 +46,54 @@ #if defined(STM32F0) - #warning STM32F0 board selected - #define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) - #warning STM32F1 board selected - #define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) - #warning STM32F2 board selected - #define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) - #warning STM32F3 board selected - #define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) - #warning STM32F4 board selected - #define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) - #warning STM32F7 board selected - #define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) - #warning STM32L0 board selected - #define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) - #warning STM32L1 board selected - #define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) - #warning STM32L4 board selected - #define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) - #warning STM32H7 board selected - #define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) - #warning STM32G0 board selected - #define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) - #warning STM32G4 board selected - #define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) - #warning STM32WB board selected - #define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) - #warning STM32MP1 board selected - #define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else - #warning STM32 unknown board selected - #define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino index 90f401f3..ea22db43 100644 --- a/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino +++ b/examples/AsyncSimpleGET_STM32/AsyncSimpleGET_STM32.ino @@ -1,35 +1,38 @@ /**************************************************************************************************************************** AsyncSimpleGET_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ - + #include "defines.h" -// Select a test server address +// Select a test server address //char GET_ServerAddress[] = "ipv4bot.whatismyipaddress.com/"; char GET_ServerAddress[] = "http://worldtimeapi.org/api/timezone/America/Toronto.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -40,89 +43,94 @@ AsyncHTTPRequest request; void sendRequest(); // Repeat forever, millis() resolution -Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - requestOpenResult = request.open("GET", GET_ServerAddress); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + requestOpenResult = request.open("GET", GET_ServerAddress); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncSimpleGET_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncSimpleGET_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); - Serial.print(F("AsyncHTTPRequest @ IP : ")); Serial.println(Ethernet.localIP()); - Serial.println(); + request.setDebug(false); - request.setDebug(false); - - request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker. + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker. - // Send first request now - delay(10000); - sendRequest(); + // Send first request now + delay(10000); + sendRequest(); } -void loop() +void loop() { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncSimpleGET_STM32/defines.h b/examples/AsyncSimpleGET_STM32/defines.h index 20649fc3..44a93644 100644 --- a/examples/AsyncSimpleGET_STM32/defines.h +++ b/examples/AsyncSimpleGET_STM32/defines.h @@ -46,54 +46,54 @@ #if defined(STM32F0) -#warning STM32F0 board selected -#define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) -#warning STM32F1 board selected -#define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) -#warning STM32F2 board selected -#define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) -#warning STM32F3 board selected -#define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) -#warning STM32F4 board selected -#define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) -#warning STM32F7 board selected -#define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) -#warning STM32L0 board selected -#define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) -#warning STM32L1 board selected -#define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) -#warning STM32L4 board selected -#define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) -#warning STM32H7 board selected -#define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) -#warning STM32G0 board selected -#define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) -#warning STM32G4 board selected -#define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) -#warning STM32WB board selected -#define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) -#warning STM32MP1 board selected -#define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else -#warning STM32 unknown board selected -#define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME -#define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino b/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino index 9f88de3c..7aaad3eb 100644 --- a/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino +++ b/examples/AsyncWebClientRepeating_STM32/AsyncWebClientRepeating_STM32.ino @@ -1,37 +1,40 @@ /**************************************************************************************************************************** AsyncWebClientRepeating_STM32.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ #include "defines.h" -// Select a test server address +// Select a test server address const char GET_ServerAddress[] = "arduino.tips"; // GET location String GET_Location = "/asciilogo.txt"; -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 // 600s = 10 minutes to not flooding, 60s in testing #define HTTP_REQUEST_INTERVAL_MS 60000 //600000 +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true + // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -42,88 +45,92 @@ AsyncHTTPRequest request; void sendRequest(); // Repeat forever, millis() resolution -Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); +Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS); void sendRequest() -{ - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - requestOpenResult = request.open("GET", (GET_ServerAddress + GET_Location).c_str()); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } +{ + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + requestOpenResult = request.open("GET", (GET_ServerAddress + GET_Location).c_str()); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); - - Serial.print("\nStart AsyncWebClientRepeating_STM32 on "); Serial.println(BOARD_NAME); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.print("\nStart AsyncWebClientRepeating_STM32 on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // start the ethernet connection and the server - // Use random mac - uint16_t index = millis() % NUMBER_OF_MAC; - - // Use Static IP - //Ethernet.begin(mac[index], ip); - // Use DHCP dynamic IP and random mac - Ethernet.begin(mac[index]); - - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(Ethernet.localIP()); - Serial.println(); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - sendHTTPRequest.start(); //start the ticker - - // Send first request now - sendRequest(); + // start the ethernet connection and the server + // Use random mac + uint16_t index = millis() % NUMBER_OF_MAC; + + // Use Static IP + //Ethernet.begin(mac[index], ip); + // Use DHCP dynamic IP and random mac + Ethernet.begin(mac[index]); + + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(Ethernet.localIP()); + Serial.println(); + + request.setDebug(false); + + request.onReadyStateChange(requestCB); + sendHTTPRequest.start(); //start the ticker + + // Send first request now + sendRequest(); } -void loop() +void loop() { - sendHTTPRequest.update(); + sendHTTPRequest.update(); } diff --git a/examples/AsyncWebClientRepeating_STM32/defines.h b/examples/AsyncWebClientRepeating_STM32/defines.h index 7e73c81a..44a93644 100644 --- a/examples/AsyncWebClientRepeating_STM32/defines.h +++ b/examples/AsyncWebClientRepeating_STM32/defines.h @@ -1,30 +1,30 @@ /**************************************************************************************************************************** defines.h - + 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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ /* Currently support 1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as : - Nucleo-144 (F429ZI, F767ZI) - Discovery (STM32F746G-DISCOVERY) - - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, + - STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet, - See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1) 2) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false) 3) STM32F/L/H/G/WB/MP1 boards (with 32+K Flash) running W5x00 shields @@ -36,7 +36,7 @@ #if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) - #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. #endif #define ASYNC_HTTP_DEBUG_PORT Serial @@ -46,54 +46,54 @@ #if defined(STM32F0) - #warning STM32F0 board selected - #define BOARD_TYPE "STM32F0" + #warning STM32F0 board selected + #define BOARD_TYPE "STM32F0" #elif defined(STM32F1) - #warning STM32F1 board selected - #define BOARD_TYPE "STM32F1" + #warning STM32F1 board selected + #define BOARD_TYPE "STM32F1" #elif defined(STM32F2) - #warning STM32F2 board selected - #define BOARD_TYPE "STM32F2" + #warning STM32F2 board selected + #define BOARD_TYPE "STM32F2" #elif defined(STM32F3) - #warning STM32F3 board selected - #define BOARD_TYPE "STM32F3" + #warning STM32F3 board selected + #define BOARD_TYPE "STM32F3" #elif defined(STM32F4) - #warning STM32F4 board selected - #define BOARD_TYPE "STM32F4" + #warning STM32F4 board selected + #define BOARD_TYPE "STM32F4" #elif defined(STM32F7) - #warning STM32F7 board selected - #define BOARD_TYPE "STM32F7" + #warning STM32F7 board selected + #define BOARD_TYPE "STM32F7" #elif defined(STM32L0) - #warning STM32L0 board selected - #define BOARD_TYPE "STM32L0" + #warning STM32L0 board selected + #define BOARD_TYPE "STM32L0" #elif defined(STM32L1) - #warning STM32L1 board selected - #define BOARD_TYPE "STM32L1" + #warning STM32L1 board selected + #define BOARD_TYPE "STM32L1" #elif defined(STM32L4) - #warning STM32L4 board selected - #define BOARD_TYPE "STM32L4" + #warning STM32L4 board selected + #define BOARD_TYPE "STM32L4" #elif defined(STM32H7) - #warning STM32H7 board selected - #define BOARD_TYPE "STM32H7" + #warning STM32H7 board selected + #define BOARD_TYPE "STM32H7" #elif defined(STM32G0) - #warning STM32G0 board selected - #define BOARD_TYPE "STM32G0" + #warning STM32G0 board selected + #define BOARD_TYPE "STM32G0" #elif defined(STM32G4) - #warning STM32G4 board selected - #define BOARD_TYPE "STM32G4" + #warning STM32G4 board selected + #define BOARD_TYPE "STM32G4" #elif defined(STM32WB) - #warning STM32WB board selected - #define BOARD_TYPE "STM32WB" + #warning STM32WB board selected + #define BOARD_TYPE "STM32WB" #elif defined(STM32MP1) - #warning STM32MP1 board selected - #define BOARD_TYPE "STM32MP1" + #warning STM32MP1 board selected + #define BOARD_TYPE "STM32MP1" #else - #warning STM32 unknown board selected - #define BOARD_TYPE "STM32 Unknown" + #warning STM32 unknown board selected + #define BOARD_TYPE "STM32 Unknown" #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE + #define BOARD_NAME BOARD_TYPE #endif #include @@ -106,26 +106,26 @@ byte mac[][NUMBER_OF_MAC] = { - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, - { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, + { 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, }; // Select the static IP address according to your local network diff --git a/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino b/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino index 0f5f37cf..0d1b1374 100644 --- a/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino +++ b/examples/WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01/AsyncHTTPMultiRequests_WT32_ETH01.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncHTTPMultiRequests_WT32_ETH01.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -41,7 +41,7 @@ //************************************************************************************************************* #if !( defined(ESP32) ) - #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif // Level from 0-4 @@ -57,8 +57,11 @@ #include // https://github.com/khoih-prog/WebServer_WT32_ETH01 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -83,22 +86,22 @@ IPAddress myDNS(8, 8, 8, 8); void heartBeatPrint(void) { - static int num = 1; - - if (WT32_ETH01_isConnected()) - 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(" ")); - } + static int num = 1; + + if (WT32_ETH01_isConnected()) + 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(" ")); + } } // To replace with your real APP_API @@ -122,113 +125,122 @@ const char* requestAll[ NUM_REQUESTS ] = { requestCurrent.c_str(), requestMinute uint8_t requestIndex = 0; -void sendRequest() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - requestOpenResult = request.open("GET", requestAll[requestIndex] ); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + requestOpenResult = request.open("GET", requestAll[requestIndex] ); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } -void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) +void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.print(F("\n***************")); Serial.print(requestName[ requestIndex ]); Serial.println(F("***************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.print(F("\n***************")); + Serial.print(requestName[ requestIndex ]); + Serial.println(F("***************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } #if 1 - // Bypass hourly - if (requestIndex == 1) - requestIndex = 3; - else - requestIndex = (requestIndex + 1) % NUM_REQUESTS; + + // Bypass hourly + if (requestIndex == 1) + requestIndex = 3; + else + requestIndex = (requestIndex + 1) % NUM_REQUESTS; + #else - // hourly too long, not display anyway. Not enough heap. - requestIndex = (requestIndex + 1) % NUM_REQUESTS; + // hourly too long, not display anyway. Not enough heap. + requestIndex = (requestIndex + 1) % NUM_REQUESTS; #endif - - request->setDebug(false); - } + + request->setDebug(false); + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); - - delay(200); - - Serial.print("\nStart AsyncHTTPMultiRequests_WT32_ETH01 on "); Serial.print(ARDUINO_BOARD); - Serial.print(" with "); Serial.println(SHIELD_TYPE); - Serial.println(WEBSERVER_WT32_ETH01_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - - Serial.setDebugOutput(true); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + delay(200); + + Serial.print("\nStart AsyncHTTPMultiRequests_WT32_ETH01 on "); + Serial.print(ARDUINO_BOARD); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_WT32_ETH01_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + + Serial.setDebugOutput(true); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // To be called before ETH.begin() - WT32_ETH01_onEvent(); + // To be called before ETH.begin() + WT32_ETH01_onEvent(); - //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, - // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); - //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); - ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); + //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, + // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); + //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); + ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); - // Static IP, leave without this line to get IP via DHCP - //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); - ETH.config(myIP, myGW, mySN, myDNS); + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + ETH.config(myIP, myGW, mySN, myDNS); - WT32_ETH01_waitForConnect(); + WT32_ETH01_waitForConnect(); - Serial.print(F("AsyncHTTPRequest @ IP : ")); - Serial.println(ETH.localIP()); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + Serial.print(F("AsyncHTTPRequest @ IP : ")); + Serial.println(ETH.localIP()); - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); - - // Send first request now - sendRequest(); + request.setDebug(false); + + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + + // Send first request now + sendRequest(); } void loop() -{ +{ } diff --git a/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino b/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino index 703c0fa5..5f0d96a7 100644 --- a/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino +++ b/examples/WT32_ETH01/AsyncHTTPRequest_WT32_ETH01/AsyncHTTPRequest_WT32_ETH01.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** AsyncHTTPRequest_WT32_ETH01.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 + 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 . + You should have received a copy of the GNU General Public License along with this program. If not, see . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -41,7 +41,7 @@ //************************************************************************************************************* #if !( defined(ESP32) ) - #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif // Level from 0-4 @@ -57,8 +57,11 @@ #include // https://github.com/khoih-prog/WebServer_WT32_ETH01 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_Generic @@ -83,118 +86,123 @@ IPAddress myDNS(8, 8, 8, 8); void heartBeatPrint(void) { - static int num = 1; - - if (WT32_ETH01_isConnected()) - Serial.print(F("H")); // H means connected - else - Serial.print(F("F")); // F means not connected - - if (num == 80) - { - Serial.println(); - num = 1; - } - else if (num++ % 10 == 0) - { - Serial.print(F(" ")); - } + static int num = 1; + + if (WT32_ETH01_isConnected()) + Serial.print(F("H")); // H means connected + else + Serial.print(F("F")); // F means not connected + + if (num == 80) + { + Serial.println(); + num = 1; + } + else if (num++ % 10 == 0) + { + Serial.print(F(" ")); + } } -void sendRequest() +void sendRequest() { - static bool requestOpenResult; - - if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) - { - //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); - requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - // Only send() if open() returns true, or crash - request.send(); - } - else - { - Serial.println("Can't send bad request"); - } - } - else - { - Serial.println("Can't send request"); - } + static bool requestOpenResult; + + if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone) + { + //requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt"); + requestOpenResult = request.open("GET", "http://worldtimeapi.org/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + // Only send() if open() returns true, or crash + request.send(); + } + else + { + Serial.println("Can't send bad request"); + } + } + else + { + Serial.println("Can't send request"); + } } void requestCB(void *optParm, AsyncHTTPRequest *request, int readyState) { - (void) optParm; - - if (readyState == readyStateDone) - { - AHTTP_LOGDEBUG(F("\n**************************************")); - AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - Serial.println(F("\n**************************************")); - Serial.println(request->responseText()); - Serial.println(F("**************************************")); - } - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGDEBUG(F("\n**************************************")); + AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + Serial.println(F("\n**************************************")); + Serial.println(request->responseText()); + Serial.println(F("**************************************")); + } + } } void setup() { - // put your setup code here, to run once: - Serial.begin(115200); - while (!Serial && millis() < 5000); + // put your setup code here, to run once: + Serial.begin(115200); + + while (!Serial && millis() < 5000); - delay(200); + delay(200); - Serial.print("\nStart AsyncHTTPRequest_WT32_ETH01 on "); Serial.print(ARDUINO_BOARD); - Serial.print(" with "); Serial.println(SHIELD_TYPE); - Serial.println(WEBSERVER_WT32_ETH01_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.print("\nStart AsyncHTTPRequest_WT32_ETH01 on "); + Serial.print(ARDUINO_BOARD); + Serial.print(" with "); + Serial.println(SHIELD_TYPE); + Serial.println(WEBSERVER_WT32_ETH01_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); - Serial.setDebugOutput(true); + Serial.setDebugOutput(true); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif - // To be called before ETH.begin() - WT32_ETH01_onEvent(); + // To be called before ETH.begin() + WT32_ETH01_onEvent(); + + //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, + // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); + //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); + ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); - //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO, - // eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE); - //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); - ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER); + // Static IP, leave without this line to get IP via DHCP + //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); + ETH.config(myIP, myGW, mySN, myDNS); - // Static IP, leave without this line to get IP via DHCP - //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0); - ETH.config(myIP, myGW, mySN, myDNS); + WT32_ETH01_waitForConnect(); - WT32_ETH01_waitForConnect(); + Serial.print(F("\nHTTP WebClient is @ IP : ")); + Serial.println(ETH.localIP()); - Serial.print(F("\nHTTP WebClient is @ IP : ")); - Serial.println(ETH.localIP()); - - request.setDebug(false); - - request.onReadyStateChange(requestCB); - ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + request.setDebug(false); - ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); - - // Send first request now - sendRequest(); + request.onReadyStateChange(requestCB); + ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest); + + ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint); + + // Send first request now + sendRequest(); } void loop() -{ +{ } diff --git a/examples/multiFileProject/multiFileProject.cpp b/examples/multiFileProject/multiFileProject.cpp index cc53c7cd..8a3b00ff 100644 --- a/examples/multiFileProject/multiFileProject.cpp +++ b/examples/multiFileProject/multiFileProject.cpp @@ -1,11 +1,11 @@ /**************************************************************************************************************************** multiFileProject.cpp 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 *****************************************************************************************************************************/ diff --git a/examples/multiFileProject/multiFileProject.h b/examples/multiFileProject/multiFileProject.h index 8658a5f6..b98e7f39 100644 --- a/examples/multiFileProject/multiFileProject.h +++ b/examples/multiFileProject/multiFileProject.h @@ -1,11 +1,11 @@ /**************************************************************************************************************************** multiFileProject.h 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 *****************************************************************************************************************************/ diff --git a/examples/multiFileProject/multiFileProject.ino b/examples/multiFileProject/multiFileProject.ino index 59576b2d..47cc6dc1 100644 --- a/examples/multiFileProject/multiFileProject.ino +++ b/examples/multiFileProject/multiFileProject.ino @@ -1,11 +1,11 @@ /**************************************************************************************************************************** multiFileProject.ino 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 *****************************************************************************************************************************/ @@ -16,35 +16,38 @@ ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ defined(STM32WB) || defined(STM32MP1) ) ) - #error This code is intended to run on the ESP8266, ESP32 or STM32 platform! Please check your Tools->Board setting. +#error This code is intended to run on the ESP8266, ESP32 or STM32 platform! Please check your Tools->Board setting. #endif -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.1" -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002 #include "multiFileProject.h" // Can be included as many times as necessary, without `Multiple Definitions` Linker Error #include "AsyncHTTPRequest_Generic.h" -void setup() +void setup() { - Serial.begin(115200); - while (!Serial); - - Serial.println("\nStart multiFileProject"); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); + Serial.begin(115200); + + while (!Serial); + + Serial.println("\nStart multiFileProject"); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION); #if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET); + } + #endif } -void loop() +void loop() { - // put your main code here, to run repeatedly: + // put your main code here, to run repeatedly: } diff --git a/library.json b/library.json index d8600d58..fed96e4f 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name":"AsyncHTTPRequest_Generic", - "version": "1.10.1", + "version": "1.10.2", "description":"Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of AsyncTCP libraries, such as AsyncTCP, ESPAsyncTCP, AsyncTCP_STM32, etc.. for ESP32 (including ESP32_S2, ESP32_S3 and ESP32_C3), WT32_ETH01 (ESP32 + LAN8720), ESP8266 (WiFi, W5x00 or ENC28J60) and currently STM32 with built-in LAN8742A Ethernet.", "keywords":"communication, async, tcp, http, ESP8266, ESP32, ESP32-S2, ESP32-S3, ESP32-C3, wt32-eth01, stm32, ethernet, wifi, lan8742a, nucleo-144, stm32f7, W5x00, ENC28J60", "authors": [ @@ -45,7 +45,7 @@ { "owner": "khoih-prog", "name": "ESPAsync_WiFiManager", - "version": ">=1.15.0", + "version": ">=1.15.1", "platforms": ["espressif8266", "espressif32"] }, { diff --git a/library.properties b/library.properties index 36de5695..285aa126 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AsyncHTTPRequest_Generic -version=1.10.1 +version=1.10.2 author=Bob Lemaire,Khoi Hoang maintainer=Khoi Hoang license=GPLv3 diff --git a/platformio/platformio.ini b/platformio/platformio.ini index c4d355d7..e534a5da 100644 --- a/platformio/platformio.ini +++ b/platformio/platformio.ini @@ -45,7 +45,7 @@ lib_deps = ; https://github.com/khoih-prog/STM32AsyncTCP.git ; STM32duino LwIP@>=2.1.2 ; STM32duino STM32Ethernet@>=1.3.0 -; ESPAsync_WiFiManager@>=1.15.0 +; ESPAsync_WiFiManager@>=1.15.1 ; LittleFS_esp32@>=1.0.6 ; WebServer_WT32_ETH01@>=1.5.1 ; @@ -56,7 +56,7 @@ lib_deps = ; https://github.com/khoih-prog/STM32AsyncTCP.git ; stm32duino/STM32duino LwIP@>=2.1.2 ; stm32duino/STM32duino STM32Ethernet@>=1.3.0 -; khoih-prog/ESPAsync_WiFiManager@>=1.15.0 +; khoih-prog/ESPAsync_WiFiManager@>=1.15.1 ; lorol/LittleFS_esp32@>=1.0.6 ; khoih-prog/WebServer_WT32_ETH01@>=1.5.1 @@ -73,11 +73,11 @@ framework = arduino lib_deps = ; PlatformIO 4.x ; ESPAsyncTCP@>=1.2.2 -; ESPAsync_WiFiManager@>=1.15.0 +; ESPAsync_WiFiManager@>=1.15.1 ; ; PlatformIO 5.x me-no-dev/ESPAsyncTCP@>=1.2.2 - khoih-prog/ESPAsync_WiFiManager@>=1.15.0 + khoih-prog/ESPAsync_WiFiManager@>=1.15.1 ; ============================================================ ; Board configuration @@ -128,13 +128,13 @@ framework = arduino lib_deps = ; PlatformIO 4.x ; AsyncTCP@>=1.1.1 -; ESPAsync_WiFiManager@>=1.15.0 +; ESPAsync_WiFiManager@>=1.15.1 ; LittleFS_esp32@>=1.0.6 ; WebServer_WT32_ETH01@>=1.5.1 ; ; PlatformIO 5.x me-no-dev/AsyncTCP@>=1.1.1 - khoih-prog/ESPAsync_WiFiManager@>=1.15.0 + khoih-prog/ESPAsync_WiFiManager@>=1.15.1 ; lorol/LittleFS_esp32@>=1.0.6 khoih-prog/WebServer_WT32_ETH01@>=1.5.1 @@ -281,7 +281,7 @@ lib_deps = ; STM32duino STM32Ethernet@>=1.3.0 ; ; PlatformIO 5.x - philbowles/STM32AsyncTCP + philbowles/STM32AsyncTCP https://github.com/khoih-prog/STM32AsyncTCP.git stm32duino/STM32duino LwIP@>=2.1.2 stm32duino/STM32duino STM32Ethernet@>=1.3.0 diff --git a/src/AsyncHTTPRequest_Debug_Generic.h b/src/AsyncHTTPRequest_Debug_Generic.h index ae6ddd7a..0820b25e 100644 --- a/src/AsyncHTTPRequest_Debug_Generic.h +++ b/src/AsyncHTTPRequest_Debug_Generic.h @@ -1,25 +1,25 @@ /**************************************************************************************************************************** AsyncHTTPRequest_Debug_Generic.h - 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_STM32 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 + 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.10.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see . + + Version: 1.10.2 + 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). @@ -34,6 +34,7 @@ 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up 1.10.1 K Hoang 21/10/2022 Fix bug of wrong reqStates + 1.10.2 K Hoang 09/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_Generic.h b/src/AsyncHTTPRequest_Generic.h index 4d5286c9..578a8af8 100644 --- a/src/AsyncHTTPRequest_Generic.h +++ b/src/AsyncHTTPRequest_Generic.h @@ -1,25 +1,25 @@ /**************************************************************************************************************************** AsyncHTTPRequest_Generic.h - 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_STM32 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 + 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.10.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see . + + Version: 1.10.2 + 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). @@ -34,6 +34,7 @@ 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up 1.10.1 K Hoang 21/10/2022 Fix bug of wrong reqStates + 1.10.2 K Hoang 09/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_Generic.hpp b/src/AsyncHTTPRequest_Generic.hpp index 78d73809..1bdc9007 100644 --- a/src/AsyncHTTPRequest_Generic.hpp +++ b/src/AsyncHTTPRequest_Generic.hpp @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - Version: 1.10.1 + Version: 1.10.2 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -34,6 +34,7 @@ 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up 1.10.1 K Hoang 21/10/2022 Fix bug of wrong reqStates + 1.10.2 K Hoang 09/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites *****************************************************************************************************************************/ #pragma once @@ -43,13 +44,13 @@ //////////////////////////////////////// -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.10.1" +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION "AsyncHTTPRequest_Generic v1.10.2" #define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MAJOR 1 #define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MINOR 10 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_PATCH 1 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_PATCH 2 -#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT 1010001 +#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT 1010002 //////////////////////////////////////// @@ -57,6 +58,14 @@ #include "AsyncHTTPRequest_Debug_Generic.h" + +//////////////////////////////////////// + +#if !defined(NOT_SEND_HEADER_AFTER_CONNECTED) + // Default is false + #define NOT_SEND_HEADER_AFTER_CONNECTED false +#endif + //////////////////////////////////////// #ifndef DEBUG_IOTA_PORT diff --git a/src/AsyncHTTPRequest_Impl_Generic.h b/src/AsyncHTTPRequest_Impl_Generic.h index 9500305f..66a9f258 100644 --- a/src/AsyncHTTPRequest_Impl_Generic.h +++ b/src/AsyncHTTPRequest_Impl_Generic.h @@ -1,25 +1,25 @@ /**************************************************************************************************************************** AsyncHTTPRequest_Impl_Generic.h - 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_STM32 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 + 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.10.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see . + + Version: 1.10.2 + 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). @@ -34,8 +34,9 @@ 1.9.2 K Hoang 18/10/2022 Not try to reconnect to the same host:port after connected 1.10.0 K Hoang 20/10/2022 Fix bug. Clean up 1.10.1 K Hoang 21/10/2022 Fix bug of wrong reqStates + 1.10.2 K Hoang 09/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites *****************************************************************************************************************************/ - + #pragma once #ifndef ASYNC_HTTP_REQUEST_GENERIC_IMPL_H @@ -46,347 +47,347 @@ // Merge xbuf //////////////////////////////////////// -xbuf::xbuf(const uint16_t segSize) : _head(nullptr), _tail(nullptr), _used(0), _free(0), _offset(0) +xbuf::xbuf(const uint16_t segSize) : _head(nullptr), _tail(nullptr), _used(0), _free(0), _offset(0) { _segSize = (segSize + 3) & -4;//((segSize + 3) >> 2) << 2; } //////////////////////////////////////// -xbuf::~xbuf() +xbuf::~xbuf() { flush(); } //////////////////////////////////////// -size_t xbuf::write(const uint8_t byte) +size_t xbuf::write(const uint8_t byte) { return write((uint8_t*) &byte, 1); } //////////////////////////////////////// -size_t xbuf::write(const char* buf) +size_t xbuf::write(const char* buf) { return write((uint8_t*)buf, strlen(buf)); } //////////////////////////////////////// -size_t xbuf::write(const String& string) +size_t xbuf::write(const String& string) { return write((uint8_t*)string.c_str(), string.length()); } //////////////////////////////////////// -size_t xbuf::write(const uint8_t* buf, const size_t len) +size_t xbuf::write(const uint8_t* buf, const size_t len) { size_t supply = len; - - while (supply) + + while (supply) { - if (!_free) + if (!_free) { addSeg(); } - + size_t demand = _free < supply ? _free : supply; memcpy(_tail->data + ((_offset + _used) % _segSize), buf + (len - supply), demand); _free -= demand; _used += demand; supply -= demand; } - + return len; } //////////////////////////////////////// -size_t xbuf::write(xbuf* buf, const size_t len) +size_t xbuf::write(xbuf* buf, const size_t len) { size_t supply = len; - - if (supply > buf->available()) + + if (supply > buf->available()) { supply = buf->available(); } - + size_t read = 0; - - while (supply) + + while (supply) { - if (!_free) + if (!_free) { addSeg(); } - + size_t demand = _free < supply ? _free : supply; read += buf->read(_tail->data + ((_offset + _used) % _segSize), demand); _free -= demand; _used += demand; supply -= demand; } - + return read; } //////////////////////////////////////// -uint8_t xbuf::read() +uint8_t xbuf::read() { uint8_t byte = 0; read((uint8_t*) &byte, 1); - + return byte; } //////////////////////////////////////// -uint8_t xbuf::peek() +uint8_t xbuf::peek() { uint8_t byte = 0; peek((uint8_t*) &byte, 1); - + return byte; } //////////////////////////////////////// -size_t xbuf::read(uint8_t* buf, const size_t len) +size_t xbuf::read(uint8_t* buf, const size_t len) { size_t read = 0; - - while (read < len && _used) + + while (read < len && _used) { size_t supply = (_offset + _used) > _segSize ? _segSize - _offset : _used; size_t demand = len - read; size_t chunk = supply < demand ? supply : demand; - + memcpy(buf + read, _head->data + _offset, chunk); _offset += chunk; _used -= chunk; read += chunk; - - if (_offset == _segSize) + + if (_offset == _segSize) { remSeg(); _offset = 0; } } - - if ( ! _used) + + if ( ! _used) { flush(); } - + return read; } //////////////////////////////////////// -size_t xbuf::peek(uint8_t* buf, const size_t len) +size_t xbuf::peek(uint8_t* buf, const size_t len) { size_t read = 0; xseg* seg = _head; size_t offset = _offset; size_t used = _used; - - while (read < len && used) + + while (read < len && used) { size_t supply = (offset + used) > _segSize ? _segSize - offset : used; size_t demand = len - read; size_t chunk = supply < demand ? supply : demand; - + memcpy(buf + read, seg->data + offset, chunk); - + offset += chunk; used -= chunk; read += chunk; - - if (offset == _segSize) + + if (offset == _segSize) { seg = seg->next; offset = 0; } } - + return read; } //////////////////////////////////////// -size_t xbuf::available() +size_t xbuf::available() { return _used; } //////////////////////////////////////// -int xbuf::indexOf(const char target, const size_t begin) +int xbuf::indexOf(const char target, const size_t begin) { char targetstr[2] = " "; targetstr[0] = target; - + return indexOf(targetstr, begin); } //////////////////////////////////////// -int xbuf::indexOf(const char* target, const size_t begin) +int xbuf::indexOf(const char* target, const size_t begin) { size_t targetLen = strlen(target); - - if (targetLen > _segSize || targetLen > _used) + + if (targetLen > _segSize || targetLen > _used) return -1; - + size_t searchPos = _offset + begin; size_t searchEnd = _offset + _used - targetLen; - - if (searchPos > searchEnd) + + if (searchPos > searchEnd) return -1; - + size_t searchSeg = searchPos / _segSize; xseg* seg = _head; - - while (searchSeg) + + while (searchSeg) { seg = seg->next; searchSeg --; } - + size_t segPos = searchPos % _segSize; - - while (searchPos <= searchEnd) + + while (searchPos <= searchEnd) { size_t compLen = targetLen; - - if (compLen <= (_segSize - segPos)) + + if (compLen <= (_segSize - segPos)) { - if (memcmp(target, seg->data + segPos, compLen) == 0) + if (memcmp(target, seg->data + segPos, compLen) == 0) { return searchPos - _offset; } } - else + else { size_t compLen = _segSize - segPos; - - if (memcmp(target, seg->data + segPos, compLen) == 0) + + if (memcmp(target, seg->data + segPos, compLen) == 0) { compLen = targetLen - compLen; - - if (memcmp(target + targetLen - compLen, seg->next->data, compLen) == 0) + + if (memcmp(target + targetLen - compLen, seg->next->data, compLen) == 0) { return searchPos - _offset; } } } - + searchPos++; segPos++; - - if (segPos == _segSize) + + if (segPos == _segSize) { seg = seg->next; segPos = 0; } } - + return -1; } //////////////////////////////////////// -String xbuf::readStringUntil(const char target) +String xbuf::readStringUntil(const char target) { return readString(indexOf(target) + 1); } //////////////////////////////////////// -String xbuf::readStringUntil(const char* target) +String xbuf::readStringUntil(const char* target) { int index = indexOf(target); - - if (index < 0) + + if (index < 0) return String(); - + return readString(index + strlen(target)); } //////////////////////////////////////// -String xbuf::readString(int endPos) +String xbuf::readString(int endPos) { String result; - - if ( ! result.reserve(endPos + 1)) - { + + if ( ! result.reserve(endPos + 1)) + { return result; } - - if (endPos > _used) + + if (endPos > _used) { endPos = _used; } - - if (endPos > 0 && result.reserve(endPos + 1)) + + if (endPos > 0 && result.reserve(endPos + 1)) { - while (endPos--) + while (endPos--) { result += (char)_head->data[_offset++]; _used--; - - if (_offset >= _segSize) + + if (_offset >= _segSize) { remSeg(); } } } - + return result; } //////////////////////////////////////// -String xbuf::peekString(int endPos) +String xbuf::peekString(int endPos) { String result; - + xseg* seg = _head; size_t offset = _offset; - - if (endPos > _used) + + if (endPos > _used) { endPos = _used; } - - if (endPos > 0 && result.reserve(endPos + 1)) + + if (endPos > 0 && result.reserve(endPos + 1)) { - while (endPos--) + while (endPos--) { result += (char)seg->data[offset++]; - - if ( offset >= _segSize) + + if ( offset >= _segSize) { seg = seg->next; offset = 0; } } } - + return result; } //////////////////////////////////////// -void xbuf::flush() +void xbuf::flush() { - while (_head) + while (_head) remSeg(); - + _tail = nullptr; _offset = 0; _used = 0; @@ -395,51 +396,51 @@ void xbuf::flush() //////////////////////////////////////// -void xbuf::addSeg() +void xbuf::addSeg() { - if (_tail) + if (_tail) { _tail->next = (xseg*) new uint32_t[_segSize / 4 + 1]; - + if (_tail->next == NULL) { AHTTP_LOGERROR(F("xbuf::addSeg: error new 1")); - } - else + } + else { _tail = _tail->next; } } - else + else { _tail = _head = (xseg*) new uint32_t[_segSize / 4 + 1]; - + if (_tail == NULL) AHTTP_LOGERROR(F("xbuf::addSeg: error new 2")); } - + if (_tail) _tail->next = nullptr; - + _free += _segSize; } //////////////////////////////////////// -void xbuf::remSeg() +void xbuf::remSeg() { - if (_head) + if (_head) { xseg *next = _head->next; delete[] (uint32_t*) _head; _head = next; - - if ( ! _head) + + if ( ! _head) { _tail = nullptr; } } - + _offset = 0; } @@ -447,7 +448,8 @@ void xbuf::remSeg() //////////////////////////////////////// //************************************************************************************************************** -AsyncHTTPRequest::AsyncHTTPRequest(): _readyState(readyStateUnsent), _HTTPcode(0), _chunked(false), _debug(DEBUG_IOTA_HTTP_SET) +AsyncHTTPRequest::AsyncHTTPRequest(): _readyState(readyStateUnsent), _HTTPcode(0), _chunked(false), + _debug(DEBUG_IOTA_HTTP_SET) , _timeout(DEFAULT_RX_TIMEOUT), _lastActivity(0), _requestStartTime(0), _requestEndTime(0), _URL(nullptr) , _connectedHost(nullptr), _connectedPort(-1), _client(nullptr), _contentLength(0), _contentRead(0) , _readyStateChangeCB(nullptr), _readyStateChangeCBarg(nullptr), _onDataCB(nullptr), _onDataCBarg(nullptr) @@ -474,11 +476,13 @@ AsyncHTTPRequest::~AsyncHTTPRequest() SAFE_DELETE_ARRAY(_connectedHost) #ifdef ESP32 + // KH add if (threadLock) { - vSemaphoreDelete(threadLock); + vSemaphoreDelete(threadLock); } + #endif } @@ -490,7 +494,7 @@ void AsyncHTTPRequest::setDebug(bool debug) { _debug = true; } - + _debug = debug; } @@ -510,18 +514,18 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) if (_readyState != readyStateUnsent && _readyState != readyStateDone) { AHTTP_LOGERROR(F("open: not ready")); - + return false; } _requestStartTime = millis(); - + SAFE_DELETE(_URL) SAFE_DELETE(_headers) SAFE_DELETE(_request) SAFE_DELETE(_response) SAFE_DELETE(_chunks) - + _URL = nullptr; _headers = nullptr; _response = nullptr; @@ -530,7 +534,7 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) _chunked = false; _contentRead = 0; _readyState = readyStateUnsent; - + _requestReadyToSend = false; if (strcmp(method, "GET") == 0) @@ -560,65 +564,78 @@ bool AsyncHTTPRequest::open(const char* method, const char* URL) else { AHTTP_LOGERROR(F("open: Bad method")); - + return false; } if (!_parseURL(URL)) { AHTTP_LOGERROR(F("open: error parsing URL")); - + return false; } +#if NOT_SEND_HEADER_AFTER_CONNECTED + if ( _client && _client->connected() ) { if ( (strcmp(_URL->host, _connectedHost) == 0) && (_URL->port == _connectedPort) ) { AHTTP_LOGINFO(F("open: already connected")); - + _lastActivity = millis(); - + _requestReadyToSend = true; - + return _connect(); } else - { + { AHTTP_LOGINFO(F("open: not connected: different host or port")); - + return false; } } +#else + + if ( _client && _client->connected() && (strcmp(_URL->host, _connectedHost) != 0 || _URL->port != _connectedPort)) + { + AHTTP_LOGERROR(F("open: not connected")); + + return false; + } + +#endif + char* hostName = new char[strlen(_URL->host) + 10]; - + if (hostName) { sprintf(hostName, "%s:%d", _URL->host, _URL->port); _addHeader("host", hostName); - + AHTTP_LOGINFO1(F("open: connecting to hostname ="), hostName); - + SAFE_DELETE_ARRAY(hostName) - + _lastActivity = millis(); - + _requestReadyToSend = true; - + return _connect(); } else { AHTTP_LOGERROR(F("open: error alloc")); - + return false; } } //////////////////////////////////////// -void AsyncHTTPRequest::onReadyStateChange(readyStateChangeCB cb, void* arg) +void AsyncHTTPRequest::onReadyStateChange(readyStateChangeCB cb, void* arg) { _readyStateChangeCB = cb; _readyStateChangeCBarg = arg; @@ -626,31 +643,31 @@ void AsyncHTTPRequest::onReadyStateChange(readyStateChangeCB cb, void* arg) //////////////////////////////////////// -void AsyncHTTPRequest::setTimeout(int seconds) +void AsyncHTTPRequest::setTimeout(int seconds) { _timeout = seconds; } //////////////////////////////////////// -bool AsyncHTTPRequest::send() -{ +bool AsyncHTTPRequest::send() +{ if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } MUTEX_LOCK(false) - - if ( ! _buildRequest()) + + if ( ! _buildRequest()) return false; - + _send(); - + _AHTTP_unlock; - + return true; } @@ -661,56 +678,56 @@ bool AsyncHTTPRequest::send(const String& body) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } - + MUTEX_LOCK(false) - + _addHeader("Content-Length", String(body.length()).c_str()); - - if ( ! _buildRequest()) + + if ( ! _buildRequest()) { _AHTTP_unlock; - + return false; } - + _request->write(body); _send(); - + _AHTTP_unlock; - + return true; } //////////////////////////////////////// -bool AsyncHTTPRequest::send(const char* body) +bool AsyncHTTPRequest::send(const char* body) { if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } MUTEX_LOCK(false) - + _addHeader("Content-Length", String(strlen(body)).c_str()); - - if ( ! _buildRequest()) + + if ( ! _buildRequest()) { _AHTTP_unlock; - + return false; } - + _request->write(body); _send(); - + _AHTTP_unlock; - + return true; } @@ -721,26 +738,26 @@ bool AsyncHTTPRequest::send(const uint8_t* body, size_t len) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } MUTEX_LOCK(false) - + _addHeader("Content-Length", String(len).c_str()); - - if ( ! _buildRequest()) + + if ( ! _buildRequest()) { _AHTTP_unlock; - + return false; } - + _request->write(body, len); _send(); - + _AHTTP_unlock; - + return true; } @@ -751,26 +768,26 @@ bool AsyncHTTPRequest::send(xbuf* body, size_t len) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } MUTEX_LOCK(false) - + _addHeader("Content-Length", String(len).c_str()); - - if ( ! _buildRequest()) + + if ( ! _buildRequest()) { _AHTTP_unlock; - + return false; } - + _request->write(body, len); _send(); - + _AHTTP_unlock; - + return true; } @@ -784,11 +801,11 @@ void AsyncHTTPRequest::abort() { return; } - + MUTEX_LOCK_NR - + _client->abort(); - + _AHTTP_unlock; } @@ -810,75 +827,167 @@ int AsyncHTTPRequest::responseHTTPcode() String AsyncHTTPRequest::responseHTTPString() { - switch(_HTTPcode) + switch (_HTTPcode) { - case 0: + case 0: return F("OK"); - case HTTPCODE_CONNECTION_REFUSED: + + case HTTPCODE_CONNECTION_REFUSED: return F("CONNECTION_REFUSED"); - case HTTPCODE_SEND_HEADER_FAILED: + + case HTTPCODE_SEND_HEADER_FAILED: return F("SEND_HEADER_FAILED"); - case HTTPCODE_SEND_PAYLOAD_FAILED: + + case HTTPCODE_SEND_PAYLOAD_FAILED: return F("SEND_PAYLOAD_FAILED"); - case HTTPCODE_NOT_CONNECTED: + + case HTTPCODE_NOT_CONNECTED: return F("NOT_CONNECTED"); - case HTTPCODE_CONNECTION_LOST: + + case HTTPCODE_CONNECTION_LOST: return F("CONNECTION_LOST"); - case HTTPCODE_NO_STREAM: + + case HTTPCODE_NO_STREAM: return F("NO_STREAM"); - case HTTPCODE_NO_HTTP_SERVER: + + case HTTPCODE_NO_HTTP_SERVER: return F("NO_HTTP_SERVER"); - case HTTPCODE_TOO_LESS_RAM: + + case HTTPCODE_TOO_LESS_RAM: return F("TOO_LESS_RAM"); - case HTTPCODE_ENCODING: + + case HTTPCODE_ENCODING: return F("ENCODING"); - case HTTPCODE_STREAM_WRITE: + + case HTTPCODE_STREAM_WRITE: return F("STREAM_WRITE"); - case HTTPCODE_TIMEOUT: + + case HTTPCODE_TIMEOUT: return F("TIMEOUT"); - - // HTTP positive code - case 100: return F("Continue"); - case 101: return F("Switching Protocols"); - case 200: return F("HTTP OK"); - case 201: return F("Created"); - case 202: return F("Accepted"); - case 203: return F("Non-Authoritative Information"); - case 204: return F("No Content"); - case 205: return F("Reset Content"); - case 206: return F("Partial Content"); - case 300: return F("Multiple Choices"); - case 301: return F("Moved Permanently"); - case 302: return F("Found"); - case 303: return F("See Other"); - case 304: return F("Not Modified"); - case 305: return F("Use Proxy"); - case 307: return F("Temporary Redirect"); - case 400: return F("Bad Request"); - case 401: return F("Unauthorized"); - case 402: return F("Payment Required"); - case 403: return F("Forbidden"); - case 404: return F("Not Found"); - case 405: return F("Method Not Allowed"); - case 406: return F("Not Acceptable"); - case 407: return F("Proxy Authentication Required"); - case 408: return F("Request Time-out"); - case 409: return F("Conflict"); - case 410: return F("Gone"); - case 411: return F("Length Required"); - case 412: return F("Precondition Failed"); - case 413: return F("Request Entity Too Large"); - case 414: return F("Request-URI Too Large"); - case 415: return F("Unsupported Media Type"); - case 416: return F("Requested range not satisfiable"); - case 417: return F("Expectation Failed"); - case 500: return F("Internal Server Error"); - case 501: return F("Not Implemented"); - case 502: return F("Bad Gateway"); - case 503: return F("Service Unavailable"); - case 504: return F("Gateway Time-out"); - case 505: return F("HTTP Version not supported"); - default: return "UNKNOWN"; + + // HTTP positive code + case 100: + return F("Continue"); + + case 101: + return F("Switching Protocols"); + + case 200: + return F("HTTP OK"); + + case 201: + return F("Created"); + + case 202: + return F("Accepted"); + + case 203: + return F("Non-Authoritative Information"); + + case 204: + return F("No Content"); + + case 205: + return F("Reset Content"); + + case 206: + return F("Partial Content"); + + case 300: + return F("Multiple Choices"); + + case 301: + return F("Moved Permanently"); + + case 302: + return F("Found"); + + case 303: + return F("See Other"); + + case 304: + return F("Not Modified"); + + case 305: + return F("Use Proxy"); + + case 307: + return F("Temporary Redirect"); + + case 400: + return F("Bad Request"); + + case 401: + return F("Unauthorized"); + + case 402: + return F("Payment Required"); + + case 403: + return F("Forbidden"); + + case 404: + return F("Not Found"); + + case 405: + return F("Method Not Allowed"); + + case 406: + return F("Not Acceptable"); + + case 407: + return F("Proxy Authentication Required"); + + case 408: + return F("Request Time-out"); + + case 409: + return F("Conflict"); + + case 410: + return F("Gone"); + + case 411: + return F("Length Required"); + + case 412: + return F("Precondition Failed"); + + case 413: + return F("Request Entity Too Large"); + + case 414: + return F("Request-URI Too Large"); + + case 415: + return F("Unsupported Media Type"); + + case 416: + return F("Requested range not satisfiable"); + + case 417: + return F("Expectation Failed"); + + case 500: + return F("Internal Server Error"); + + case 501: + return F("Not Implemented"); + + case 502: + return F("Bad Gateway"); + + case 503: + return F("Service Unavailable"); + + case 504: + return F("Gateway Time-out"); + + case 505: + return F("HTTP Version not supported"); + + default: + return "UNKNOWN"; } } @@ -887,36 +996,36 @@ String AsyncHTTPRequest::responseHTTPString() String AsyncHTTPRequest::responseText() { MUTEX_LOCK(String()) - + if ( ! _response || _readyState < readyStateLoading || ! available()) { AHTTP_LOGWARN(F("responseText() no data")); _AHTTP_unlock; - + return String(); } - size_t avail = available(); - - String localString = _response->readString(avail); - - if (localString.length() < avail) - { - AHTTP_LOGWARN(F("!responseText() no buffer")) - - _HTTPcode = HTTPCODE_TOO_LESS_RAM; - _client->abort(); - _AHTTP_unlock; - - return String(); - } - - _contentRead += localString.length(); - + size_t avail = available(); + + String localString = _response->readString(avail); + + if (localString.length() < avail) + { + AHTTP_LOGWARN(F("!responseText() no buffer")) + + _HTTPcode = HTTPCODE_TOO_LESS_RAM; + _client->abort(); _AHTTP_unlock; - - return localString; + + return String(); + } + + _contentRead += localString.length(); + + _AHTTP_unlock; + + return localString; } //////////////////////////////////////// @@ -924,7 +1033,7 @@ String AsyncHTTPRequest::responseText() #if (ESP32) #define GLOBAL_STR_LEN (32 * 1024) #elif (ESP8266) - #define GLOBAL_STR_LEN (16 * 1024) + #define GLOBAL_STR_LEN (16 * 1024) #else #define GLOBAL_STR_LEN (4 * 1024) #endif @@ -938,13 +1047,13 @@ char globalLongString[GLOBAL_STR_LEN + 1]; char* AsyncHTTPRequest::responseLongText() { MUTEX_LOCK(NULL) - + if ( ! _response || _readyState < readyStateLoading || ! available()) { AHTTP_LOGWARN(F("responseText() no data")); _AHTTP_unlock; - + return NULL; } @@ -953,11 +1062,11 @@ char* AsyncHTTPRequest::responseLongText() strncpy(globalLongString, _response->readString(avail).c_str(), lenToCopy ); globalLongString[ lenToCopy + 1 ] = 0; - + _contentRead += _response->readString(avail).length(); - + _AHTTP_unlock; - + return globalLongString; } @@ -973,12 +1082,12 @@ size_t AsyncHTTPRequest::responseRead(uint8_t* buf, size_t len) } MUTEX_LOCK(0) - + size_t avail = available() > len ? len : available(); _response->read(buf, avail); _contentRead += avail; - + _AHTTP_unlock; return avail; @@ -1060,21 +1169,21 @@ bool AsyncHTTPRequest::_parseURL(const char* url) bool AsyncHTTPRequest::_parseURL(const String& url) { SAFE_DELETE(_URL) - + int hostBeg = 0; - + _URL = new URL; - + if (_URL) { _URL->scheme = new char[strlen(ASYNC_HTTP_PREFIX) + 1]; - + if (! (_URL->scheme) ) return false; } else return false; - + strcpy(_URL->scheme, ASYNC_HTTP_PREFIX); if (url.substring(0, strlen(ASYNC_HTTP_PREFIX)).equalsIgnoreCase(ASYNC_HTTP_PREFIX)) @@ -1101,32 +1210,32 @@ bool AsyncHTTPRequest::_parseURL(const String& url) } _URL->host = new char[hostEnd - hostBeg + 1]; - + if (_URL->host == nullptr) return false; - + strcpy(_URL->host, url.substring(hostBeg, hostEnd).c_str()); int queryBeg = url.indexOf('?'); - - if (queryBeg < 0) + + if (queryBeg < 0) queryBeg = url.length(); - + _URL->path = new char[queryBeg - pathBeg + 1]; - + if (_URL->path == nullptr) return false; - + strcpy(_URL->path, url.substring(pathBeg, queryBeg).c_str()); - + _URL->query = new char[url.length() - queryBeg + 1]; - + if (_URL->query == nullptr) return false; - + strcpy(_URL->query, url.substring(queryBeg).c_str()); - return true; + return true; } //////////////////////////////////////// @@ -1136,7 +1245,7 @@ bool AsyncHTTPRequest::_connect() if ( ! _client) { _client = new AsyncClient(); - + if (! _client) return false; } @@ -1144,29 +1253,29 @@ bool AsyncHTTPRequest::_connect() SAFE_DELETE_ARRAY(_connectedHost) _connectedHost = new char[strlen(_URL->host) + 1]; - + if (_connectedHost == nullptr) return false; - + strcpy(_connectedHost, _URL->host); _connectedPort = _URL->port; - - _client->onConnect([](void *obj, AsyncClient * client) + + _client->onConnect([](void *obj, AsyncClient * client) { ((AsyncHTTPRequest*)(obj))->_onConnect(client); }, this); - - _client->onDisconnect([](void *obj, AsyncClient * client) + + _client->onDisconnect([](void *obj, AsyncClient * client) { ((AsyncHTTPRequest*)(obj))->_onDisconnect(client); }, this); - - _client->onPoll([](void *obj, AsyncClient * client) + + _client->onPoll([](void *obj, AsyncClient * client) { ((AsyncHTTPRequest*)(obj))->_onPoll(client); }, this); - - _client->onError([](void *obj, AsyncClient * client, uint32_t error) + + _client->onError([](void *obj, AsyncClient * client, uint32_t error) { ((AsyncHTTPRequest*)(obj))->_onError(client, error); }, this); @@ -1195,7 +1304,7 @@ bool AsyncHTTPRequest::_connect() } _lastActivity = millis(); - + return true; } @@ -1207,17 +1316,17 @@ bool AsyncHTTPRequest::_buildRequest() if ( ! _request) { _request = new xbuf; - + if ( ! _request) return false; } - _request->write(_HTTPmethodStringwithSpace[_HTTPmethod]); + _request->write(_HTTPmethodStringwithSpace[_HTTPmethod]); _request->write(_URL->path); _request->write(_URL->query); - + _request->write(" HTTP/1.1\r\n"); - + SAFE_DELETE(_URL) _URL = nullptr; @@ -1229,12 +1338,12 @@ bool AsyncHTTPRequest::_buildRequest() _request->write(':'); _request->write(hdr->value); _request->write("\r\n"); - + hdr = hdr->next; } SAFE_DELETE(_headers) - + _headers = nullptr; _request->write("\r\n"); @@ -1248,8 +1357,8 @@ size_t AsyncHTTPRequest::_send() if ( ! _request) return 0; - if ( ! _client->connected()) - { + if ( ! _client->connected()) + { // KH fix bug https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/38 _timeout = DEFAULT_RX_TIMEOUT; @@ -1259,7 +1368,7 @@ size_t AsyncHTTPRequest::_send() { return 0; } - + size_t supply = _request->available(); size_t demand = _client->space(); @@ -1267,20 +1376,20 @@ size_t AsyncHTTPRequest::_send() supply = demand; size_t sent = 0; - - #define TEMP_SIZE 255 + +#define TEMP_SIZE 255 uint8_t* temp = new uint8_t[TEMP_SIZE + 1]; - + if (!temp) return 0; while (supply) { size_t chunk = supply < TEMP_SIZE ? supply : TEMP_SIZE; - - memset(temp, 0, TEMP_SIZE + 1); - supply -= _request->read(temp, chunk); + + memset(temp, 0, TEMP_SIZE + 1); + supply -= _request->read(temp, chunk); sent += _client->add((char*)temp, chunk); } @@ -1290,9 +1399,9 @@ size_t AsyncHTTPRequest::_send() if (_request->available() == 0) { SAFE_DELETE(_request) - + _request = nullptr; - + // KH fix crash bug return 0; } @@ -1306,7 +1415,7 @@ size_t AsyncHTTPRequest::_send() //////////////////////////////////////// -void AsyncHTTPRequest::_setReadyState(reqStates newState) +void AsyncHTTPRequest::_setReadyState(reqStates newState) { if (_readyState != newState) { @@ -1371,36 +1480,36 @@ void AsyncHTTPRequest::_processChunks() void AsyncHTTPRequest::_onConnect(AsyncClient* client) { MUTEX_LOCK_NR - + _client = client; _setReadyState(readyStateOpened); - + _response = new xbuf; - + if (!_response) { _AHTTP_unlock; - + return; } - + _contentLength = 0; _contentRead = 0; _chunked = false; - - _client->onAck([](void* obj, AsyncClient * client, size_t len, uint32_t time) + + _client->onAck([](void* obj, AsyncClient * client, size_t len, uint32_t time) { (void) client; (void) len; (void) time; - + ((AsyncHTTPRequest*)(obj))->_send(); }, this); - - _client->onData([](void* obj, AsyncClient * client, void* data, size_t len) + + _client->onData([](void* obj, AsyncClient * client, void* data, size_t len) { (void) client; - + ((AsyncHTTPRequest*)(obj))->_onData(data, len); }, this); @@ -1410,7 +1519,7 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client) } _lastActivity = millis(); - + _AHTTP_unlock; } @@ -1419,7 +1528,7 @@ void AsyncHTTPRequest::_onConnect(AsyncClient* client) void AsyncHTTPRequest::_onPoll(AsyncClient* client) { (void) client; - + MUTEX_LOCK_NR if (_timeout && (millis() - _lastActivity) > (_timeout * 1000)) @@ -1441,7 +1550,7 @@ void AsyncHTTPRequest::_onPoll(AsyncClient* client) void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error) { (void) client; - + AHTTP_LOGERROR1(F("_onError handler error ="), client->errorToString(error)); _HTTPcode = error; @@ -1452,35 +1561,35 @@ void AsyncHTTPRequest::_onError(AsyncClient* client, int8_t error) void AsyncHTTPRequest::_onDisconnect(AsyncClient* client) { (void) client; - + MUTEX_LOCK_NR - + if (_readyState < readyStateOpened) { _HTTPcode = HTTPCODE_NOT_CONNECTED; } else if (_HTTPcode > 0 && - (_readyState < readyStateHdrsRecvd || (_contentRead + _response->available()) < _contentLength)) + (_readyState < readyStateHdrsRecvd || (_contentRead + _response->available()) < _contentLength)) { AHTTP_LOGDEBUG(F("_onDisconnect: HTTPCODE_CONNECTION_LOST")); _HTTPcode = HTTPCODE_CONNECTION_LOST; } - + // KH, New test in AsyncHTTPRequest, v1.10.0 SAFE_DELETE(_client) - + _client = nullptr; ////// - + SAFE_DELETE_ARRAY(_connectedHost) - + _connectedHost = nullptr; - + _connectedPort = -1; _requestEndTime = millis(); _lastActivity = 0; _setReadyState(readyStateDone); - + _AHTTP_unlock; } @@ -1489,14 +1598,14 @@ void AsyncHTTPRequest::_onDisconnect(AsyncClient* client) void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) { MUTEX_LOCK_NR - + _lastActivity = millis(); // Transfer data to xbuf if (_chunks) { _chunks->write((uint8_t*)Vbuf, len); - + _processChunks(); } else @@ -1510,7 +1619,7 @@ void AsyncHTTPRequest::_onData(void* Vbuf, size_t len) if ( ! _collectHeaders()) { _AHTTP_unlock; - + return; } } @@ -1654,7 +1763,7 @@ void AsyncHTTPRequest::setReqHeader(const char* name, const __FlashStringHelper* { char* _value = _charstar(value); _addHeader(name, _value); - + SAFE_DELETE_ARRAY(_value) } } @@ -1667,7 +1776,7 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const char* { char* _name = _charstar(name); _addHeader(_name, value); - + SAFE_DELETE_ARRAY(_name) } } @@ -1681,7 +1790,7 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, const __Fla char* _name = _charstar(name); char* _value = _charstar(value); _addHeader(_name, _value); - + SAFE_DELETE_ARRAY(_name) SAFE_DELETE_ARRAY(_value) } @@ -1695,7 +1804,7 @@ void AsyncHTTPRequest::setReqHeader(const __FlashStringHelper *name, int32_t val { char* _name = _charstar(name); setReqHeader(_name, String(value).c_str()); - + SAFE_DELETE_ARRAY(_name) } } @@ -1724,16 +1833,16 @@ int AsyncHTTPRequest::respHeaderCount() //////////////////////////////////////// -char* AsyncHTTPRequest::respHeaderName(int ndx) +char* AsyncHTTPRequest::respHeaderName(int ndx) { - if (_readyState < readyStateHdrsRecvd) + if (_readyState < readyStateHdrsRecvd) return nullptr; - + header* hdr = _getHeader(ndx); - - if ( ! hdr) + + if ( ! hdr) return nullptr; - + return hdr->name; } @@ -1795,7 +1904,7 @@ char* AsyncHTTPRequest::respHeaderValue(const __FlashStringHelper *name) char* _name = _charstar(name); header* hdr = _getHeader(_name); - + SAFE_DELETE_ARRAY(_name) if ( ! hdr) @@ -1813,7 +1922,7 @@ bool AsyncHTTPRequest::respHeaderExists(const __FlashStringHelper *name) char* _name = _charstar(name); header* hdr = _getHeader(_name); - + SAFE_DELETE_ARRAY(_name) if ( ! hdr) @@ -1830,7 +1939,7 @@ bool AsyncHTTPRequest::respHeaderExists(const __FlashStringHelper *name) String AsyncHTTPRequest::headers() { MUTEX_LOCK(String()) - + String _response = ""; header* hdr = _headers; @@ -1844,7 +1953,7 @@ String AsyncHTTPRequest::headers() } _response += "\r\n"; - + _AHTTP_unlock; return _response; @@ -1855,7 +1964,7 @@ String AsyncHTTPRequest::headers() AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const char* value) { MUTEX_LOCK(nullptr) - + header* hdr = (header*) &_headers; while (hdr->next) @@ -1865,7 +1974,7 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const header* oldHdr = hdr->next; hdr->next = hdr->next->next; oldHdr->next = nullptr; - + SAFE_DELETE(oldHdr) } else @@ -1875,29 +1984,29 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const } hdr->next = new header; - + if (hdr->next) { hdr->next->name = new char[strlen(name) + 1]; - + if (hdr->next->name) strcpy(hdr->next->name, name); else { SAFE_DELETE(hdr->next) - + return nullptr; } - + hdr->next->value = new char[strlen(value) + 1]; - + if (hdr->next->value) - strcpy(hdr->next->value, value); + strcpy(hdr->next->value, value); else { SAFE_DELETE_ARRAY(hdr->next->name) SAFE_DELETE(hdr->next) - + return nullptr; } } @@ -1905,7 +2014,7 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const { return nullptr; } - + _AHTTP_unlock; return hdr->next; @@ -1916,7 +2025,7 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_addHeader(const char* name, const AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(const char* name) { MUTEX_LOCK(nullptr) - + header* hdr = _headers; while (hdr) @@ -1937,7 +2046,7 @@ AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(const char* name) AsyncHTTPRequest::header* AsyncHTTPRequest::_getHeader(int ndx) { MUTEX_LOCK(nullptr) - + header* hdr = _headers; while (hdr) @@ -1964,12 +2073,12 @@ char* AsyncHTTPRequest::_charstar(const __FlashStringHelper * str) return nullptr; char* ptr = new char[strlen_P((PGM_P)str) + 1]; - + if (ptr) { strcpy_P(ptr, (PGM_P)str); } - + // Return good ptr or nullptr return ptr; } diff --git a/utils/astyle_library.conf b/utils/astyle_library.conf new file mode 100644 index 00000000..8a73bc27 --- /dev/null +++ b/utils/astyle_library.conf @@ -0,0 +1,70 @@ +# Code formatting rules for Arduino libraries, modified from for KH libraries: +# +# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf +# + +# astyle --style=allman -s2 -t2 -C -S -xW -Y -M120 -f -p -xg -H -xb -c --xC120 -xL *.h *.cpp *.ino + +--mode=c +--lineend=linux +--style=allman + +# -r or -R +#--recursive + +# -c => Converts tabs into spaces +convert-tabs + +# -s2 => 2 spaces indentation +--indent=spaces=2 + +# -t2 => tab =2 spaces +#--indent=tab=2 + +# -C +--indent-classes + +# -S +--indent-switches + +# -xW +--indent-preproc-block + +# -Y => indent classes, switches (and cases), comments starting at column 1 +--indent-col1-comments + +# -M120 => maximum of 120 spaces to indent a continuation line +--max-continuation-indent=120 + +# -xC120 => max‑code‑length will break a line if the code exceeds # characters +--max-code-length=120 + +# -f => +--break-blocks + +# -p => put a space around operators +--pad-oper + +# -xg => Insert space padding after commas +--pad-comma + +# -H => put a space after if/for/while +pad-header + +# -xb => Break one line headers (e.g. if/for/while) +--break-one-line-headers + +# -c => Converts tabs into spaces +#--convert-tabs + +# if you like one-liners, keep them +#keep-one-line-statements + +# -xV +--attach-closing-while + +#unpad-paren + +# -xp +remove-comment-prefix + diff --git a/utils/restyle.sh b/utils/restyle.sh new file mode 100644 index 00000000..bcd846f7 --- /dev/null +++ b/utils/restyle.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for dir in . ; do + find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.ino" \) -exec astyle --suffix=none --options=./utils/astyle_library.conf \{\} \; +done +