This is a server-client arrangement. It's based on Rui Santos's examples in R.N.T. He used a temperature/humidity thingy, but I just wanted a number. There's a lot of stuff commented out.
Anyway, in the following the Client requests the Server's millis (its "time") 'once a second' and knocks that out on its LCD.
Some of the LCD stuff may appear strange (it's for a curious device, LCDBug, that I picked up several years ago), it boils down to -- Serial.print (string received);
Sorry, but the libraries used are ESP8266. I suspect there are ESP32 equivalents.
Server
// ESP8266_ServerClient_Server_03
//
// Import required libraries
#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"
// Set your access point network credentials
const char* ssid = "ESP8266-Access-Point";
const char* password = "123456789";
AsyncWebServer server(80);
float fTime;
char bufr [15];
void setup()
{
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();
// Setting the ESP as an access point
Serial.print("Setting AP (Access Point) ");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP addr: ");
Serial.println(IP);
server.on("/bigTime", HTTP_GET, [](AsyncWebServerRequest *request)
{
timeData();
request->send_P(200, "text/plain", bufr);
});
bool status;
// Start server
server.begin();
}
void loop()
{
}
void timeData ()
{
fTime = millis();
fTime = fTime / 1000;
dtostrf(fTime, 1, 1, bufr);
}
Client
// N.B. - If the Server biffs, the Client
// cannot / does not recover on its own.
// manual intervention (reset) is required
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
const char* ssid = "ESP8266-Access-Point";
const char* password = "123456789";
//Your IP address or domain name with URL path
const char* serverNameTemp = "http://192.168.4.1/temperature"; // not used
const char* serverNameHumi = "http://192.168.4.1/humidity"; // not used
const char* serverNamePres = "http://192.168.4.1/pressure"; // not used
const char* serverNameMsecs = "http://192.168.4.1/bigTime"; // it gets used
const byte hsEnable = D6; // highside switch (LCDBug)
//#include <Wire.h>
String temperature; // not used
String humidity; // not used
String pressure; // not used
String bigTime;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
void setup()
{
LCDBugprep(); // Ready my LCDmodule
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Conn'd to WiFi");
Serial1.begin(9600); // LCDBug
delay(2000);
eraseAll(); // →→ Serial1.write(0x0c); Clear Disp, Home
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
// Check WiFi connection status
if ((WiFiMulti.run() == WL_CONNECTED))
{
//temperature = httpGETRequest(serverNameTemp);
//humidity = httpGETRequest(serverNameHumi);
//pressure = httpGETRequest(serverNamePres);
bigTime = httpGETRequest(serverNameMsecs);
//Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " % - Pressure: " + pressure + " hPa");
// hereabouts, print info on LCD
eraseAll();
cursorPosition(0,1);
Serial1.print(bigTime); // in 02t this should be millis / 1000
// save the last HTTP GET Request
previousMillis = currentMillis;
}
else
{
Serial.println("WiFi Disconnected");
}
}
}
String httpGETRequest(const char* serverName)
{
WiFiClient client;
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "--";
if (httpResponseCode>0)
{
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else
{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
void cursorPosition (byte colPos, byte rowPos)
{
// -- LCDBug control --
Serial1.write(0x18); // COL control
Serial1.write(colPos); // COL position
Serial1.write(0x19); // ROW control
Serial1.write(rowPos); // ROW position
}
void eraseAll ()
{
// -- LCDBug control --
Serial1.write(0x0c); // Clear Disp, Home
}
void LCDBugPrep ()
{
// -- LCDBug preliminaries --
pinMode(hsEnable, OUTPUT);
digitalWrite(hsEnable, LOW);
delay(2000);
digitalWrite(hsEnable, HIGH);
delay(1500);
// -- End LCDBug --
}