diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2b27f19..efc6342 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `RP2040` Core Version (e.g. RP2040 core v2.6.1) +* `RP2040` Core Version (e.g. RP2040 core v2.6.3) * `RP2040` Board type (e.g. RASPBERRY_PI_PICO_W) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce @@ -28,14 +28,13 @@ Please ensure to specify the following: ``` Arduino IDE version: 1.8.19 -RP2040 core v2.6.1 +RP2040 core v2.6.3 RASPBERRY_PI_PICO_W Module 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. ... @@ -43,13 +42,36 @@ 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_RP2040W/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_RP2040W_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncHTTPRequest_RP2040W_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/AsyncHTTPRequest_RP2040W_GitHub$ bash utils/restyle.sh +``` + diff --git a/changelog.md b/changelog.md index ab0ce30..1a8e39b 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Release v1.2.2](#Release-v122) * [Release v1.2.1](#Release-v121) * [Release v1.2.0](#Release-v120) * [Release v1.1.1](#Release-v111) @@ -23,6 +24,12 @@ ## Changelog +### Release v1.2.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/issues/44) +2. Fix bug +3. Use `allman astyle` and add `utils` + ### Release v1.2.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/AsyncCustomHeader.ino b/examples/AsyncCustomHeader/AsyncCustomHeader.ino index 91e8247..b9aaf09 100644 --- a/examples/AsyncCustomHeader/AsyncCustomHeader.ino +++ b/examples/AsyncCustomHeader/AsyncCustomHeader.ino @@ -1,25 +1,28 @@ /**************************************************************************************************************************** AsyncCustomHeader.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial @@ -38,131 +41,137 @@ int status = WL_IDLE_STATUS; void sendRequest() { - 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) - { - 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("**************************************")); - } - } + (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("**************************************")); + } + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncCustomHeader on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + Serial.print("\nStart AsyncCustomHeader on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); - status = WiFi.begin(ssid, pass); + status = WiFi.begin(ssid, pass); - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } + delay(1000); - printWifiStatus(); + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); - /////////////////////////////////// + // Connect to WPA/WPA2 network + status = WiFi.status(); + } - request.setDebug(false); + printWifiStatus(); - // 5s timeout - request.setTimeout(5); + /////////////////////////////////// - request.onReadyStateChange(requestCB); + request.setDebug(false); + + // 5s timeout + request.setTimeout(5); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncCustomHeader/defines.h b/examples/AsyncCustomHeader/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncCustomHeader/defines.h +++ b/examples/AsyncCustomHeader/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/AsyncDweetGet/AsyncDweetGet.ino b/examples/AsyncDweetGet/AsyncDweetGet.ino index f78a96e..51416ca 100644 --- a/examples/AsyncDweetGet/AsyncDweetGet.ino +++ b/examples/AsyncDweetGet/AsyncDweetGet.ino @@ -1,15 +1,15 @@ /**************************************************************************************************************************** AsyncDweetGET.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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. @@ -19,22 +19,25 @@ /** 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" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial #define _ASYNC_HTTP_LOGLEVEL_ 2 -// Select a test server address +// Select a test server address const char GET_ServerAddress[] = "dweet.io"; // use your own thing name here @@ -48,167 +51,172 @@ AsyncHTTPRequest request; int status = WL_IDLE_STATUS; void sendRequest() -{ - 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) - { - AHTTP_LOGWARN(F("\n**************************************")); - AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - String responseText = request->responseText(); - - Serial.println("\n**************************************"); - Serial.println(responseText); - Serial.println("**************************************"); - - parseResponse(responseText); - } - - request->setDebug(false); - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGWARN(F("\n**************************************")); + AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + } + + request->setDebug(false); + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncDweetGET on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + Serial.print("\nStart AsyncDweetGET on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } - - printWifiStatus(); - - /////////////////////////////////// - - request.setDebug(false); - - request.onReadyStateChange(requestCB); + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + printWifiStatus(); + + /////////////////////////////////// + + request.setDebug(false); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncDweetGet/defines.h b/examples/AsyncDweetGet/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncDweetGet/defines.h +++ b/examples/AsyncDweetGet/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/AsyncDweetPost/AsyncDweetPost.ino b/examples/AsyncDweetPost/AsyncDweetPost.ino index 33c5864..f44ac24 100644 --- a/examples/AsyncDweetPost/AsyncDweetPost.ino +++ b/examples/AsyncDweetPost/AsyncDweetPost.ino @@ -1,34 +1,37 @@ /**************************************************************************************************************************** AsyncDweetPOST.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial #define _ASYNC_HTTP_LOGLEVEL_ 2 -// Select a test server address +// Select a test server address const char POST_ServerAddress[] = "dweet.io"; // use your own thing name here @@ -42,172 +45,177 @@ AsyncHTTPRequest request; int status = WL_IDLE_STATUS; void sendRequest() -{ - 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) - { - AHTTP_LOGWARN(F("\n**************************************")); - AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); - - if (request->responseHTTPcode() == 200) - { - String responseText = request->responseText(); - - Serial.println("\n**************************************"); - Serial.println(responseText); - Serial.println("**************************************"); - - parseResponse(responseText); - } - - request->setDebug(false); - } + (void) optParm; + + if (readyState == readyStateDone) + { + AHTTP_LOGWARN(F("\n**************************************")); + AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString()); + + if (request->responseHTTPcode() == 200) + { + String responseText = request->responseText(); + + Serial.println("\n**************************************"); + Serial.println(responseText); + Serial.println("**************************************"); + + parseResponse(responseText); + } + + request->setDebug(false); + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); + + while (!Serial && millis() < 5000); - Serial.print("\nStart AsyncDweetPOST on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + Serial.print("\nStart AsyncDweetPOST on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } - - printWifiStatus(); - - /////////////////////////////////// - - request.setDebug(false); - - request.onReadyStateChange(requestCB); + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + printWifiStatus(); + + /////////////////////////////////// + + request.setDebug(false); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncDweetPost/defines.h b/examples/AsyncDweetPost/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncDweetPost/defines.h +++ b/examples/AsyncDweetPost/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/AsyncHTTPRequest/AsyncHTTPRequest.ino b/examples/AsyncHTTPRequest/AsyncHTTPRequest.ino index ec7dc5e..43afdd5 100644 --- a/examples/AsyncHTTPRequest/AsyncHTTPRequest.ino +++ b/examples/AsyncHTTPRequest/AsyncHTTPRequest.ino @@ -1,19 +1,19 @@ /**************************************************************************************************************************** AsyncHTTPRequest.ino - Dead simple AsyncHTTPRequest Portenta_H7 - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ //************************************************************************************************************ // @@ -40,12 +40,15 @@ #include "defines.h" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 -#define ASYNC_HTTP_DEBUG_PORT Serial -#define _ASYNC_HTTP_LOGLEVEL_ 1 +#define ASYNC_HTTP_DEBUG_PORT Serial +#define _ASYNC_HTTP_LOGLEVEL_ 1 // To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W @@ -56,130 +59,135 @@ int status = WL_IDLE_STATUS; 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"); - //requestOpenResult = request.open("GET", "http://213.188.196.246/api/timezone/America/Toronto.txt"); - - if (requestOpenResult) - { - Serial.println("Request sent"); - - // 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"); + //requestOpenResult = request.open("GET", "http://213.188.196.246/api/timezone/America/Toronto.txt"); + + if (requestOpenResult) + { + Serial.println("Request sent"); + + // 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_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("**************************************")); - } - } + (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("**************************************")); + } + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); - - Serial.print("\nStart AsyncHTTPRequest on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.print("\nStart AsyncHTTPRequest on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } - - printWifiStatus(); - - /////////////////////////////////// - - request.setDebug(false); - - request.onReadyStateChange(requestCB); + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + printWifiStatus(); + + /////////////////////////////////// + + request.setDebug(false); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncHTTPRequest/defines.h b/examples/AsyncHTTPRequest/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncHTTPRequest/defines.h +++ b/examples/AsyncHTTPRequest/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/AsyncSimpleGET/AsyncSimpleGET.ino b/examples/AsyncSimpleGET/AsyncSimpleGET.ino index 6a78aa4..f05ab13 100644 --- a/examples/AsyncSimpleGET/AsyncSimpleGET.ino +++ b/examples/AsyncSimpleGET/AsyncSimpleGET.ino @@ -1,31 +1,34 @@ /**************************************************************************************************************************** AsyncSimpleGET.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ - + #include "defines.h" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial #define _ASYNC_HTTP_LOGLEVEL_ 2 -// 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"; @@ -38,126 +41,131 @@ int status = WL_IDLE_STATUS; 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_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("**************************************")); - } - } + (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("**************************************")); + } + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); - Serial.print("\nStart AsyncSimpleGET on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + while (!Serial && millis() < 5000); + + Serial.print("\nStart AsyncSimpleGET on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } - - printWifiStatus(); - - /////////////////////////////////// - - request.setDebug(false); - - request.onReadyStateChange(requestCB); + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + printWifiStatus(); + + /////////////////////////////////// + + request.setDebug(false); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncSimpleGET/defines.h b/examples/AsyncSimpleGET/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncSimpleGET/defines.h +++ b/examples/AsyncSimpleGET/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/AsyncWebClientRepeating/AsyncWebClientRepeating.ino b/examples/AsyncWebClientRepeating/AsyncWebClientRepeating.ino index cb3cde6..3571aca 100644 --- a/examples/AsyncWebClientRepeating/AsyncWebClientRepeating.ino +++ b/examples/AsyncWebClientRepeating/AsyncWebClientRepeating.ino @@ -1,31 +1,34 @@ /**************************************************************************************************************************** AsyncWebClientRepeating.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial #define _ASYNC_HTTP_LOGLEVEL_ 2 -// Select a test server address +// Select a test server address const char GET_ServerAddress[] = "arduino.tips"; // GET location @@ -39,127 +42,132 @@ AsyncHTTPRequest request; int status = WL_IDLE_STATUS; 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_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("**************************************")); - } - } + (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("**************************************")); + } + } } void printWifiStatus() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); - - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("Local IP Address: "); - Serial.println(ip); + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("Local IP Address: "); + Serial.println(ip); } -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); + Serial.begin(115200); - Serial.print("\nStart AsyncWebClientRepeating on "); Serial.println(BOARD_NAME); - Serial.println(ASYNCTCP_RP2040W_VERSION); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + while (!Serial && millis() < 5000); + + Serial.print("\nStart AsyncWebClientRepeating on "); + Serial.println(BOARD_NAME); + Serial.println(ASYNCTCP_RP2040W_VERSION); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); + } + #endif - /////////////////////////////////// - - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) - { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } - - Serial.print(F("Connecting to SSID: ")); - Serial.println(ssid); - - status = WiFi.begin(ssid, pass); - - delay(1000); - - // attempt to connect to WiFi network - while ( status != WL_CONNECTED) - { - delay(500); - - // Connect to WPA/WPA2 network - status = WiFi.status(); - } - - printWifiStatus(); - - /////////////////////////////////// - - request.setDebug(false); - - request.onReadyStateChange(requestCB); + /////////////////////////////////// + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) + { + Serial.println("Communication with WiFi module failed!"); + + // don't continue + while (true); + } + + Serial.print(F("Connecting to SSID: ")); + Serial.println(ssid); + + status = WiFi.begin(ssid, pass); + + delay(1000); + + // attempt to connect to WiFi network + while ( status != WL_CONNECTED) + { + delay(500); + + // Connect to WPA/WPA2 network + status = WiFi.status(); + } + + printWifiStatus(); + + /////////////////////////////////// + + request.setDebug(false); + + request.onReadyStateChange(requestCB); } void sendRequestRepeat() { - static unsigned long sendRequest_timeout = 0; + static unsigned long sendRequest_timeout = 0; #define SEND_REQUEST_INTERVAL 60000L - // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently - if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) - { - sendRequest(); - - sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; - } + // sendRequest every SEND_REQUEST_INTERVAL (60) seconds: we don't need to sendRequest frequently + if ((millis() > sendRequest_timeout) || (sendRequest_timeout == 0)) + { + sendRequest(); + + sendRequest_timeout = millis() + SEND_REQUEST_INTERVAL; + } } void loop() { - sendRequestRepeat(); + sendRequestRepeat(); } diff --git a/examples/AsyncWebClientRepeating/defines.h b/examples/AsyncWebClientRepeating/defines.h index 53be8c0..2ebd245 100644 --- a/examples/AsyncWebClientRepeating/defines.h +++ b/examples/AsyncWebClientRepeating/defines.h @@ -1,19 +1,19 @@ /**************************************************************************************************************************** defines.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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 . *****************************************************************************************************************************/ @@ -21,7 +21,7 @@ #define defines_h #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif char ssid[] = "your_ssid"; // your network SSID (name) diff --git a/examples/multiFileProject/multiFileProject.cpp b/examples/multiFileProject/multiFileProject.cpp index 7fb2844..88d5369 100644 --- a/examples/multiFileProject/multiFileProject.cpp +++ b/examples/multiFileProject/multiFileProject.cpp @@ -1,10 +1,10 @@ /**************************************************************************************************************************** multiFileProject.cpp - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W *****************************************************************************************************************************/ diff --git a/examples/multiFileProject/multiFileProject.h b/examples/multiFileProject/multiFileProject.h index 623b4ce..f0fba49 100644 --- a/examples/multiFileProject/multiFileProject.h +++ b/examples/multiFileProject/multiFileProject.h @@ -1,10 +1,10 @@ /**************************************************************************************************************************** multiFileProject.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W *****************************************************************************************************************************/ diff --git a/examples/multiFileProject/multiFileProject.ino b/examples/multiFileProject/multiFileProject.ino index f97badf..9139a2e 100644 --- a/examples/multiFileProject/multiFileProject.ino +++ b/examples/multiFileProject/multiFileProject.ino @@ -1,10 +1,10 @@ /**************************************************************************************************************************** multiFileProject.ino - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W *****************************************************************************************************************************/ @@ -12,11 +12,14 @@ // To demo how to include files in multi-file Projects #if !( defined(ARDUINO_RASPBERRY_PI_PICO_W) ) - #error For RASPBERRY_PI_PICO_W only + #error For RASPBERRY_PI_PICO_W only #endif -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.1" -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET "AsyncHTTPRequest_RP2040W v1.2.2" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN 1002002 + +// Uncomment for certain HTTP site to optimize +//#define NOT_SEND_HEADER_AFTER_CONNECTED true // Level from 0-4 #define ASYNC_HTTP_DEBUG_PORT Serial @@ -27,24 +30,27 @@ // Can be included as many times as necessary, without `Multiple Definitions` Linker Error #include // https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W -void setup() +void setup() { - Serial.begin(115200); - while (!Serial && millis() < 5000); - - Serial.println("\nStart multiFileProject"); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); + Serial.begin(115200); + + while (!Serial && millis() < 5000); + + Serial.println("\nStart multiFileProject"); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION); #if defined(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) - { - Serial.print("Warning. Must use this example on Version equal or later than : "); - Serial.println(ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN_TARGET); - } + + if (ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT < ASYNC_HTTP_REQUEST_RP2040W_VERSION_MIN) + { + Serial.print("Warning. Must use this example on Version equal or later than : "); + Serial.println(ASYNC_HTTP_REQUEST_RP2040W_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 05901f6..99e4fc0 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name": "AsyncHTTPRequest_RP2040W", - "version": "1.2.1", - "keywords": "communication, http, async, websocket, webserver, async-webserver, async-tcp, async-udp, async-websocket, async-http, ssl, tls, mbed, mbed-portenta, portenta-h7, portentah7, portenta-h7-m7, portenta-h7-m4, portentah7-m7, portentah7-m4, stm32h7", + "version": "1.2.2", + "keywords": "communication, http, async, websocket, webserver, async-webserver, async-tcp, async-udp, async-websocket, async-http, ssl, tls, rp2040, rp2040w, raspberry-pi-pico-w, cyw43439, wifi", "description": "Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of AsyncTCP_RP2040W library for RASPBERRY_PI_PICO_W with CYW43439 WiFi. This library, which relies on AsyncTCP_RP2040W, is part of a series of advanced Async libraries, such as AsyncTCP_RP2040W, AsyncUDP_RP2040W, AsyncWebSockets_RP2040W, AsyncWebServer_RP2040W, AsyncHTTPRequest_RP2040W, AsyncHTTPSRequest_RP2040W, etc.", "authors": [ diff --git a/library.properties b/library.properties index 4cb9851..1948051 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AsyncHTTPRequest_RP2040W -version=1.2.1 +version=1.2.2 author=Bob Lemaire,Khoi Hoang maintainer=Khoi Hoang sentence=Simple Async HTTP Request library, supporting GET, POST, PUT, PATCH, DELETE and HEAD, on top of AsyncTCP_RP2040W library for RASPBERRY_PI_PICO_W with CYW43439 WiFi. diff --git a/src/AsyncHTTPRequest_RP2040W.h b/src/AsyncHTTPRequest_RP2040W.h index 1292980..84fa5e0 100644 --- a/src/AsyncHTTPRequest_RP2040W.h +++ b/src/AsyncHTTPRequest_RP2040W.h @@ -1,23 +1,23 @@ /**************************************************************************************************************************** AsyncHTTPRequest_RP2040W.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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.2.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see + + Version: 1.2.2 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 14/08/2022 Initial coding for RP2040W with CYW43439 WiFi @@ -26,6 +26,7 @@ 1.1.1 K Hoang 19/10/2022 Not try to reconnect to the same host:port after connected 1.2.0 K Hoang 21/10/2022 Fix bug. Clean up 1.2.1 K Hoang 22/10/2022 Fix bug of wrong reqStates + 1.2.1 K Hoang 10/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites. Fix bug *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_RP2040W.hpp b/src/AsyncHTTPRequest_RP2040W.hpp index 418a98e..20d6a2c 100644 --- a/src/AsyncHTTPRequest_RP2040W.hpp +++ b/src/AsyncHTTPRequest_RP2040W.hpp @@ -16,8 +16,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see - Version: 1.2.1 - + Version: 1.2.2 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 14/08/2022 Initial coding for RP2040W with CYW43439 WiFi @@ -26,6 +26,7 @@ 1.1.1 K Hoang 19/10/2022 Not try to reconnect to the same host:port after connected 1.2.0 K Hoang 21/10/2022 Fix bug. Clean up 1.2.1 K Hoang 22/10/2022 Fix bug of wrong reqStates + 1.2.1 K Hoang 10/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites. Fix bug *****************************************************************************************************************************/ #pragma once @@ -73,13 +74,13 @@ //////////////////////////////////////// -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION "AsyncHTTPRequest_RP2040W v1.2.1" +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION "AsyncHTTPRequest_RP2040W v1.2.2" #define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MAJOR 1 #define ASYNC_HTTP_REQUEST_RP2040W_VERSION_MINOR 2 -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_PATCH 1 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_PATCH 2 -#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT 1002001 +#define ASYNC_HTTP_REQUEST_RP2040W_VERSION_INT 1002002 //////////////////////////////////////// @@ -89,6 +90,13 @@ //////////////////////////////////////// +#if !defined(NOT_SEND_HEADER_AFTER_CONNECTED) + // Default is false + #define NOT_SEND_HEADER_AFTER_CONNECTED false +#endif + +//////////////////////////////////////// + #define MUTEX_LOCK_NR #define MUTEX_LOCK(returnVal) #define _AHTTP_lock diff --git a/src/AsyncHTTPRequest_RP2040W_Debug.h b/src/AsyncHTTPRequest_RP2040W_Debug.h index ff58198..407528c 100644 --- a/src/AsyncHTTPRequest_RP2040W_Debug.h +++ b/src/AsyncHTTPRequest_RP2040W_Debug.h @@ -1,23 +1,23 @@ /**************************************************************************************************************************** AsyncHTTPRequest_RP2040W_Debug.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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.2.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see + + Version: 1.2.2 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 14/08/2022 Initial coding for RP2040W with CYW43439 WiFi @@ -26,6 +26,7 @@ 1.1.1 K Hoang 19/10/2022 Not try to reconnect to the same host:port after connected 1.2.0 K Hoang 21/10/2022 Fix bug. Clean up 1.2.1 K Hoang 22/10/2022 Fix bug of wrong reqStates + 1.2.1 K Hoang 10/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites. Fix bug *****************************************************************************************************************************/ #pragma once diff --git a/src/AsyncHTTPRequest_RP2040W_Impl.h b/src/AsyncHTTPRequest_RP2040W_Impl.h index 5a93eb3..f966cbc 100644 --- a/src/AsyncHTTPRequest_RP2040W_Impl.h +++ b/src/AsyncHTTPRequest_RP2040W_Impl.h @@ -1,23 +1,23 @@ /**************************************************************************************************************************** AsyncHTTPRequest_RP2040W_Impl.h - + For RP2040W with CYW43439 WiFi - + AsyncHTTPRequest_RP2040W is a library for the RP2040W with CYW43439 WiFi - + Based on and modified from asyncHTTPrequest Library (https://github.com/boblemaire/asyncHTTPrequest) Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPRequest_RP2040W - + 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.2.1 - + You should have received a copy of the GNU General Public License along with this program. + If not, see + + Version: 1.2.2 + Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 14/08/2022 Initial coding for RP2040W with CYW43439 WiFi @@ -26,8 +26,9 @@ 1.1.1 K Hoang 19/10/2022 Not try to reconnect to the same host:port after connected 1.2.0 K Hoang 21/10/2022 Fix bug. Clean up 1.2.1 K Hoang 22/10/2022 Fix bug of wrong reqStates + 1.2.1 K Hoang 10/11/2022 Default to reconnect to the same host:port after connected for new HTTP sites. Fix bug *****************************************************************************************************************************/ - + #pragma once #ifndef ASYNC_HTTP_REQUEST_RP2040W_IMPL_H @@ -155,7 +156,7 @@ size_t xbuf::read(uint8_t* buf, const size_t len) 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; @@ -438,7 +439,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) @@ -550,26 +552,39 @@ bool AsyncHTTPRequest::open(const char* method, const char* 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) @@ -616,7 +631,7 @@ bool AsyncHTTPRequest::send() if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } @@ -639,7 +654,7 @@ bool AsyncHTTPRequest::send(const String& body) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } @@ -669,7 +684,7 @@ bool AsyncHTTPRequest::send(const char* body) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } @@ -699,7 +714,7 @@ bool AsyncHTTPRequest::send(const uint8_t* body, size_t len) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } @@ -729,7 +744,7 @@ bool AsyncHTTPRequest::send(xbuf* body, size_t len) if (!_requestReadyToSend) { AHTTP_LOGERROR(CANT_SEND_BAD_REQUEST); - + return false; } @@ -791,71 +806,163 @@ String AsyncHTTPRequest::responseHTTPString() { case 0: return ("OK"); + case HTTPCODE_CONNECTION_REFUSED: return ("CONNECTION_REFUSED"); + case HTTPCODE_SEND_HEADER_FAILED: return ("SEND_HEADER_FAILED"); + case HTTPCODE_SEND_PAYLOAD_FAILED: return ("SEND_PAYLOAD_FAILED"); + case HTTPCODE_NOT_CONNECTED: return ("NOT_CONNECTED"); + case HTTPCODE_CONNECTION_LOST: return ("CONNECTION_LOST"); + case HTTPCODE_NO_STREAM: return ("NO_STREAM"); + case HTTPCODE_NO_HTTP_SERVER: return ("NO_HTTP_SERVER"); + case HTTPCODE_TOO_LESS_RAM: return ("TOO_LESS_RAM"); + case HTTPCODE_ENCODING: return ("ENCODING"); + case HTTPCODE_STREAM_WRITE: return ("STREAM_WRITE"); + case HTTPCODE_TIMEOUT: return ("TIMEOUT"); // HTTP positive code - case 100: return ("Continue"); - case 101: return ("Switching Protocols"); - case 200: return ("HTTP OK"); - case 201: return ("Created"); - case 202: return ("Accepted"); - case 203: return ("Non-Authoritative Information"); - case 204: return ("No Content"); - case 205: return ("Reset Content"); - case 206: return ("Partial Content"); - case 300: return ("Multiple Choices"); - case 301: return ("Moved Permanently"); - case 302: return ("Found"); - case 303: return ("See Other"); - case 304: return ("Not Modified"); - case 305: return ("Use Proxy"); - case 307: return ("Temporary Redirect"); - case 400: return ("Bad Request"); - case 401: return ("Unauthorized"); - case 402: return ("Payment Required"); - case 403: return ("Forbidden"); - case 404: return ("Not Found"); - case 405: return ("Method Not Allowed"); - case 406: return ("Not Acceptable"); - case 407: return ("Proxy Authentication Required"); - case 408: return ("Request Time-out"); - case 409: return ("Conflict"); - case 410: return ("Gone"); - case 411: return ("Length Required"); - case 412: return ("Precondition Failed"); - case 413: return ("Request Entity Too Large"); - case 414: return ("Request-URI Too Large"); - case 415: return ("Unsupported Media Type"); - case 416: return ("Requested range not satisfiable"); - case 417: return ("Expectation Failed"); - case 500: return ("Internal Server Error"); - case 501: return ("Not Implemented"); - case 502: return ("Bad Gateway"); - case 503: return ("Service Unavailable"); - case 504: return ("Gateway Time-out"); - case 505: return ("HTTP Version not supported"); - default: return "UNKNOWN"; + case 100: + return ("Continue"); + + case 101: + return ("Switching Protocols"); + + case 200: + return ("HTTP OK"); + + case 201: + return ("Created"); + + case 202: + return ("Accepted"); + + case 203: + return ("Non-Authoritative Information"); + + case 204: + return ("No Content"); + + case 205: + return ("Reset Content"); + + case 206: + return ("Partial Content"); + + case 300: + return ("Multiple Choices"); + + case 301: + return ("Moved Permanently"); + + case 302: + return ("Found"); + + case 303: + return ("See Other"); + + case 304: + return ("Not Modified"); + + case 305: + return ("Use Proxy"); + + case 307: + return ("Temporary Redirect"); + + case 400: + return ("Bad Request"); + + case 401: + return ("Unauthorized"); + + case 402: + return ("Payment Required"); + + case 403: + return ("Forbidden"); + + case 404: + return ("Not Found"); + + case 405: + return ("Method Not Allowed"); + + case 406: + return ("Not Acceptable"); + + case 407: + return ("Proxy Authentication Required"); + + case 408: + return ("Request Time-out"); + + case 409: + return ("Conflict"); + + case 410: + return ("Gone"); + + case 411: + return ("Length Required"); + + case 412: + return ("Precondition Failed"); + + case 413: + return ("Request Entity Too Large"); + + case 414: + return ("Request-URI Too Large"); + + case 415: + return ("Unsupported Media Type"); + + case 416: + return ("Requested range not satisfiable"); + + case 417: + return ("Expectation Failed"); + + case 500: + return ("Internal Server Error"); + + case 501: + return ("Not Implemented"); + + case 502: + return ("Bad Gateway"); + + case 503: + return ("Service Unavailable"); + + case 504: + return ("Gateway Time-out"); + + case 505: + return ("HTTP Version not supported"); + + default: + return "UNKNOWN"; } } @@ -1038,13 +1145,13 @@ bool AsyncHTTPRequest::_parseURL(const String& 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)) @@ -1187,7 +1294,7 @@ bool AsyncHTTPRequest::_buildRequest() _request->write(_HTTPmethodStringwithSpace[_HTTPmethod]); _request->write(_URL->path); _request->write(_URL->query); - + _request->write(" HTTP/1.1\r\n"); SAFE_DELETE(_URL) @@ -1221,7 +1328,7 @@ size_t AsyncHTTPRequest::_send() return 0; if ( ! _client->connected()) - { + { // KH fix bug https://github.com/khoih-prog/AsyncHTTPRequest_Generic/issues/38 _timeout = DEFAULT_RX_TIMEOUT; @@ -1240,19 +1347,19 @@ size_t AsyncHTTPRequest::_send() size_t sent = 0; - #define MAX_CHUNK_SIZE 255 +#define MAX_CHUNK_SIZE 255 uint8_t* temp = new uint8_t[MAX_CHUNK_SIZE + 1]; - + if (!temp) return 0; while (supply) { size_t chunk = supply < MAX_CHUNK_SIZE ? supply : MAX_CHUNK_SIZE; - - memset(temp, 0, MAX_CHUNK_SIZE + 1); - supply -= _request->read(temp, chunk); + + memset(temp, 0, MAX_CHUNK_SIZE + 1); + supply -= _request->read(temp, chunk); sent += _client->add((char*)temp, chunk); } @@ -1263,15 +1370,16 @@ size_t AsyncHTTPRequest::_send() SAFE_DELETE(_request) _request = nullptr; - - // KH fix crash bug - return 0; + + AHTTP_LOGDEBUG("_send: _request->available() == 0 => _request = nullptr"); } _client->send(); _lastActivity = millis(); + AHTTP_LOGDEBUG1("_send: _client->send() @ millis =", _lastActivity); + return sent; } @@ -1436,7 +1544,7 @@ void AsyncHTTPRequest::_onDisconnect(AsyncClient* client) AHTTP_LOGDEBUG("_onDisconnect: HTTPCODE_CONNECTION_LOST"); _HTTPcode = HTTPCODE_CONNECTION_LOST; } - + SAFE_DELETE(_client) _client = nullptr; diff --git a/utils/astyle_library.conf b/utils/astyle_library.conf new file mode 100644 index 0000000..8a73bc2 --- /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 0000000..bcd846f --- /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 +