mirror of
https://github.com/khoih-prog/AsyncHTTPRequest_Generic.git
synced 2025-07-29 18:07:15 +02:00
Update README.md
and use allman
astyle
This commit is contained in:
@ -19,7 +19,7 @@
|
||||
*****************************************************************************************************************************/
|
||||
|
||||
#if !( defined(ESP8266) || defined(ESP32) )
|
||||
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
|
||||
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
|
||||
#endif
|
||||
|
||||
// Level from 0-4
|
||||
@ -39,9 +39,9 @@ const char* ssid = "your_ssid";
|
||||
const char* password = "your_pass";
|
||||
|
||||
#if (ESP8266)
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif (ESP32)
|
||||
#include <WiFi.h>
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
// Seconds for timeout, default is 3s
|
||||
@ -59,8 +59,8 @@ const char* password = "your_pass";
|
||||
|
||||
const char* addreses[][NUM_DIFFERENT_SITES] =
|
||||
{
|
||||
{"http://worldtimeapi.org/api/timezone/America/Toronto.txt", "http://worldtimeapi.org/api/timezone/Europe/Prague.txt"},
|
||||
{"http://www.myexternalip.com/raw"}
|
||||
{"http://worldtimeapi.org/api/timezone/America/Toronto.txt", "http://worldtimeapi.org/api/timezone/Europe/Prague.txt"},
|
||||
{"http://www.myexternalip.com/raw"}
|
||||
};
|
||||
|
||||
#define NUM_ENTRIES_SITE_0 2
|
||||
@ -88,161 +88,161 @@ Ticker ticker1;
|
||||
|
||||
void heartBeatPrint()
|
||||
{
|
||||
static int num = 1;
|
||||
static int num = 1;
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
Serial.print(F("H")); // H means connected to WiFi
|
||||
else
|
||||
Serial.print(F("F")); // F means not connected to WiFi
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
Serial.print(F("H")); // H means connected to WiFi
|
||||
else
|
||||
Serial.print(F("F")); // F means not connected to WiFi
|
||||
|
||||
if (num == 80)
|
||||
{
|
||||
Serial.println();
|
||||
num = 1;
|
||||
}
|
||||
else if (num++ % 10 == 0)
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
if (num == 80)
|
||||
{
|
||||
Serial.println();
|
||||
num = 1;
|
||||
}
|
||||
else if (num++ % 10 == 0)
|
||||
{
|
||||
Serial.print(F(" "));
|
||||
}
|
||||
}
|
||||
|
||||
void sendRequest(uint16_t index)
|
||||
{
|
||||
static bool requestOpenResult;
|
||||
static bool requestOpenResult;
|
||||
|
||||
reqCount[index]--;
|
||||
readySend[index] = false;
|
||||
reqCount[index]--;
|
||||
readySend[index] = false;
|
||||
|
||||
requestOpenResult = request[index].open("GET", addreses[index][reqCount[index]]);
|
||||
requestOpenResult = request[index].open("GET", addreses[index][reqCount[index]]);
|
||||
|
||||
if (requestOpenResult)
|
||||
{
|
||||
// Only send() if open() returns true, or crash
|
||||
Serial.print("\nSending request: ");
|
||||
request[index].send();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("\nCan't send bad request : ");
|
||||
}
|
||||
if (requestOpenResult)
|
||||
{
|
||||
// Only send() if open() returns true, or crash
|
||||
Serial.print("\nSending request: ");
|
||||
request[index].send();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("\nCan't send bad request : ");
|
||||
}
|
||||
|
||||
Serial.println(addreses[index][reqCount[index]]);
|
||||
Serial.println(addreses[index][reqCount[index]]);
|
||||
}
|
||||
|
||||
void sendRequest0()
|
||||
{
|
||||
sendRequest(0);
|
||||
sendRequest(0);
|
||||
}
|
||||
|
||||
void sendRequest1()
|
||||
{
|
||||
sendRequest(1);
|
||||
sendRequest(1);
|
||||
}
|
||||
|
||||
void sendRequests()
|
||||
{
|
||||
reqCount[0] = NUM_ENTRIES_SITE_0;
|
||||
reqCount[1] = NUM_ENTRIES_SITE_1;
|
||||
reqCount[0] = NUM_ENTRIES_SITE_0;
|
||||
reqCount[1] = NUM_ENTRIES_SITE_1;
|
||||
}
|
||||
|
||||
void requestCB0(void* optParm, AsyncHTTPRequest* thisRequest, int readyState)
|
||||
{
|
||||
(void) optParm;
|
||||
(void) optParm;
|
||||
|
||||
if (readyState == readyStateDone)
|
||||
{
|
||||
AHTTP_LOGWARN(F("\n**************************************"));
|
||||
AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString());
|
||||
if (readyState == readyStateDone)
|
||||
{
|
||||
AHTTP_LOGWARN(F("\n**************************************"));
|
||||
AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString());
|
||||
|
||||
if (request->responseHTTPcode() == 200)
|
||||
{
|
||||
Serial.println(F("\n**************************************"));
|
||||
Serial.println(request->responseText());
|
||||
Serial.println(F("**************************************"));
|
||||
}
|
||||
else
|
||||
{
|
||||
AHTTP_LOGERROR(F("Response error"));
|
||||
}
|
||||
if (request->responseHTTPcode() == 200)
|
||||
{
|
||||
Serial.println(F("\n**************************************"));
|
||||
Serial.println(request->responseText());
|
||||
Serial.println(F("**************************************"));
|
||||
}
|
||||
else
|
||||
{
|
||||
AHTTP_LOGERROR(F("Response error"));
|
||||
}
|
||||
|
||||
thisRequest->setDebug(false);
|
||||
readySend[0] = true;
|
||||
}
|
||||
thisRequest->setDebug(false);
|
||||
readySend[0] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState)
|
||||
{
|
||||
(void) optParm;
|
||||
(void) optParm;
|
||||
|
||||
if (readyState == readyStateDone)
|
||||
{
|
||||
AHTTP_LOGWARN(F("\n**************************************"));
|
||||
AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString());
|
||||
if (readyState == readyStateDone)
|
||||
{
|
||||
AHTTP_LOGWARN(F("\n**************************************"));
|
||||
AHTTP_LOGWARN1(F("Response Code = "), request->responseHTTPString());
|
||||
|
||||
if (request->responseHTTPcode() == 200)
|
||||
{
|
||||
Serial.println(F("\n**************************************"));
|
||||
Serial.println(request->responseText());
|
||||
Serial.println(F("**************************************"));
|
||||
}
|
||||
else
|
||||
{
|
||||
AHTTP_LOGERROR(F("Response error"));
|
||||
}
|
||||
if (request->responseHTTPcode() == 200)
|
||||
{
|
||||
Serial.println(F("\n**************************************"));
|
||||
Serial.println(request->responseText());
|
||||
Serial.println(F("**************************************"));
|
||||
}
|
||||
else
|
||||
{
|
||||
AHTTP_LOGERROR(F("Response error"));
|
||||
}
|
||||
|
||||
thisRequest->setDebug(false);
|
||||
readySend[1] = true;
|
||||
}
|
||||
thisRequest->setDebug(false);
|
||||
readySend[1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
// put your setup code here, to run once:
|
||||
Serial.begin(115200);
|
||||
|
||||
while (!Serial && millis() < 5000);
|
||||
while (!Serial && millis() < 5000);
|
||||
|
||||
Serial.print("\nStart AsyncHTTPRequest_ESP_Multi on ");
|
||||
Serial.println(ARDUINO_BOARD);
|
||||
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
|
||||
Serial.print("\nStart AsyncHTTPRequest_ESP_Multi on ");
|
||||
Serial.println(ARDUINO_BOARD);
|
||||
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
WiFi.begin(ssid, password);
|
||||
|
||||
Serial.println("Connecting to WiFi SSID: " + String(ssid));
|
||||
Serial.println("Connecting to WiFi SSID: " + String(ssid));
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.print(F("\nAsyncHTTPSRequest @ IP : "));
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.print(F("\nAsyncHTTPSRequest @ IP : "));
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
|
||||
{
|
||||
request[index].setDebug(false);
|
||||
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
|
||||
{
|
||||
request[index].setDebug(false);
|
||||
|
||||
request[index].onReadyStateChange(requestCB[index]);
|
||||
}
|
||||
request[index].onReadyStateChange(requestCB[index]);
|
||||
}
|
||||
|
||||
ticker.attach(HTTP_REQUEST_INTERVAL, sendRequests);
|
||||
ticker.attach(HTTP_REQUEST_INTERVAL, sendRequests);
|
||||
|
||||
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
|
||||
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
|
||||
{
|
||||
if ((reqCount[index] > 0) && readySend[index])
|
||||
{
|
||||
sendRequestCB[index]();
|
||||
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
|
||||
{
|
||||
if ((reqCount[index] > 0) && readySend[index])
|
||||
{
|
||||
sendRequestCB[index]();
|
||||
|
||||
// Don't send too fast
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
// Don't send too fast
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user