2020-09-13 23:05:46 -04:00
|
|
|
/****************************************************************************************************************************
|
|
|
|
|
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> <Bob Lemaire, IoTaWatt, Inc.>
|
|
|
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2021-04-12 00:32:52 -04:00
|
|
|
Version: 1.2.0
|
2020-09-13 23:05:46 -04:00
|
|
|
|
|
|
|
|
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).
|
2020-10-09 16:06:03 -04:00
|
|
|
1.0.1 K Hoang 09/10/2020 Restore cpp code besides Impl.h code.
|
2020-11-09 03:59:44 -05:00
|
|
|
1.0.2 K Hoang 09/11/2020 Make Mutex Lock and delete more reliable and error-proof
|
2020-12-23 17:35:01 -05:00
|
|
|
1.1.0 K Hoang 23/12/2020 Add HTTP PUT, PATCH, DELETE and HEAD methods
|
2020-12-23 23:35:07 -05:00
|
|
|
1.1.1 K Hoang 24/12/2020 Prevent crash if request and/or method not correct.
|
2021-02-11 14:48:08 -05:00
|
|
|
1.1.2 K Hoang 11/02/2021 Rename _lock and _unlock to avoid conflict with AsyncWebServer library
|
2021-02-25 16:51:32 -05:00
|
|
|
1.1.3 K Hoang 25/02/2021 Fix non-persistent Connection header bug
|
2021-03-22 20:22:33 -04:00
|
|
|
1.1.4 K Hoang 21/03/2021 Fix `library.properties` dependency
|
|
|
|
|
1.1.5 K Hoang 22/03/2021 Fix dependency on STM32AsyncTCP Library
|
2021-04-12 00:32:52 -04:00
|
|
|
1.2.0 K Hoang 11/04/2021 Add support to LAN8720 using STM32F4 or STM32F7
|
2020-09-13 23:05:46 -04:00
|
|
|
*****************************************************************************************************************************/
|
|
|
|
|
//************************************************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// There are scores of ways to use AsyncHTTPRequest. The important thing to keep in mind is that
|
|
|
|
|
// it is asynchronous and just like in JavaScript, everything is event driven. You will have some
|
|
|
|
|
// reason to initiate an asynchronous HTTP request in your program, but then sending the request
|
|
|
|
|
// headers and payload, gathering the response headers and any payload, and processing
|
|
|
|
|
// of that response, can (and probably should) all be done asynchronously.
|
|
|
|
|
//
|
|
|
|
|
// In this example, a Ticker function is setup to fire every 5 seconds to initiate a request.
|
|
|
|
|
// Everything is handled in AsyncHTTPRequest without blocking.
|
|
|
|
|
// The callback onReadyStateChange is made progressively and like most JS scripts, we look for
|
|
|
|
|
// readyState == 4 (complete) here. At that time the response is retrieved and printed.
|
|
|
|
|
//
|
|
|
|
|
// Note that there is no code in loop(). A code entered into loop would run oblivious to
|
|
|
|
|
// the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest()
|
|
|
|
|
// could be made in loop(), resulting in the same asynchronous handling.
|
|
|
|
|
//
|
|
|
|
|
// For demo purposes, debug is turned on for handling of the first request. These are the
|
|
|
|
|
// events that are being handled in AsyncHTTPRequest. They all begin with Debug(nnn) where
|
|
|
|
|
// nnn is the elapsed time in milliseconds since the transaction was started.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************************************************
|
|
|
|
|
|
|
|
|
|
#include "defines.h"
|
|
|
|
|
|
2021-02-25 16:51:32 -05:00
|
|
|
// 600s = 10 minutes to not flooding, 60s in testing
|
|
|
|
|
#define HTTP_REQUEST_INTERVAL_MS 60000 //600000
|
2020-09-13 23:05:46 -04:00
|
|
|
|
|
|
|
|
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
|
|
|
|
|
|
|
|
|
|
#include <Ticker.h> // https://github.com/sstaub/Ticker
|
|
|
|
|
|
|
|
|
|
AsyncHTTPRequest request;
|
|
|
|
|
|
|
|
|
|
void sendRequest(void);
|
|
|
|
|
|
|
|
|
|
// Repeat forever, millis() resolution
|
|
|
|
|
Ticker sendHTTPRequest(sendRequest, HTTP_REQUEST_INTERVAL_MS, 0, MILLIS);
|
|
|
|
|
|
|
|
|
|
void sendRequest(void)
|
|
|
|
|
{
|
2020-12-23 23:35:07 -05:00
|
|
|
static bool requestOpenResult;
|
|
|
|
|
|
2020-09-13 23:05:46 -04:00
|
|
|
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
|
|
|
|
|
{
|
2020-12-23 23:35:07 -05:00
|
|
|
//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");
|
2020-09-13 23:05:46 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState)
|
|
|
|
|
{
|
2021-02-11 14:48:08 -05:00
|
|
|
(void) optParm;
|
|
|
|
|
|
2020-09-13 23:05:46 -04:00
|
|
|
if (readyState == readyStateDone)
|
|
|
|
|
{
|
|
|
|
|
Serial.println("\n**************************************");
|
|
|
|
|
Serial.println(request->responseText());
|
|
|
|
|
Serial.println("**************************************");
|
|
|
|
|
|
|
|
|
|
request->setDebug(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup(void)
|
|
|
|
|
{
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
while (!Serial);
|
|
|
|
|
|
|
|
|
|
Serial.println("\nStart AsyncHTTPRequest_STM32 on " + String(BOARD_NAME));
|
2020-12-23 17:35:01 -05:00
|
|
|
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
|
2020-09-13 23:05:46 -04:00
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
sendHTTPRequest.update();
|
|
|
|
|
}
|