#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <AsyncTCP.h>
const char* wifi_ssid = "Wokwi-GUEST";
const char* wifi_pass = "";
HTTPClient http;
AsyncClient async;
const char *host = "worldtimeapi.org";
const char *path = "/api/timezone/America/Chicago.txt";
void on_http_connect( void* arg, AsyncClient* client )
{
Serial.println( ">>> connected" );
char request[128];
sprintf( request, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path, host );
Serial.print( request );
client->write( request );
}
void on_http_disconnect( void* arg, AsyncClient* client )
{
Serial.println( ">>> disconnected" );
client->stop();
}
void on_http_data( void* arg, AsyncClient* client, void *data, size_t len )
{
Serial.println( ">>> data" );
char *s = (char *) data;
Serial.println( s );
}
void setup()
{
Serial.begin( 115200 );
Serial.println();
WiFi.mode( WIFI_STA );
WiFi.setAutoReconnect( true );
WiFi.begin( wifi_ssid, wifi_pass );
while (WiFi.status() != WL_CONNECTED)
{
Serial.println("Waiting for Wifi");
delay(500);
}
async.onConnect( on_http_connect );
async.onDisconnect( on_http_disconnect );
async.onData( on_http_data );
async.connect( host, 80 );
}
void loop()
{
// Serial.println( "[HTTP] begin ..." );
// http.begin( "http://www.example.com" );
// const char *headerKeys[] = { "Date" };
// http.collectHeaders( headerKeys, 1 );
// int httpCode = http.GET();
// if (httpCode > 0)
// {
// Serial.printf( "[HTTP] Code: %d, Headers: %d\n", httpCode, http.headers() );
// for (int i = 0; i < http.headers(); i++)
// {
// Serial.printf( "%s: %s", http.headerName(i), http.header(i) );
// }
// if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
// {
// String payload = http.getString();
// Serial.println( payload );
// }
// }
// else
// {
// Serial.printf ("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str() );
// }
// http.end();
delay(5000);
}