server running on an access point
// ESP32 Access point - open server on port 10000 to receive data structure
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "ESP32_Access_point";
const char* password = "123456789";
// test structure
struct __attribute__((packed)) Data {
int16_t seq; // sequence number
int32_t distance;
float voltage;
char text[50];
} data;
WiFiServer server(10000); // server port to listen on
void setup() {
Serial.begin(115200);
// setup Wi-Fi network with SSID and password
Serial.printf("Setting AP (Access Point)… %s\n", ssid);
// 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 address: ");
Serial.print(IP);
Serial.println(" listening on port 10000");
server.begin();
}
boolean alreadyConnected = false; // whether or not the client was connected previously
void loop() {
static WiFiClient client;
static int16_t seqExpected=0;
if (!client)
client = server.available(); // Listen for incoming clients
if (client) { // if client connected
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
Serial.println("We have a new client");
alreadyConnected = true;
}
// if data available from client read and display it
int length;
if ((length = client.available()) > 0) {
//str = client.readStringUntil('\n'); // read entire response
Serial.printf("Received length %d - ", length);
// if data is correct length read and display it
if (length == sizeof(data)) {
client.readBytes((char*)&data, sizeof(data));
Serial.printf("seq %d distaance %ld voltage %f text '%s'\n",
(int)data.seq, (long)data.distance, data.voltage, data.text);
if (data.seq != seqExpected) // check sequence number received
Serial.printf("Error! seq expected %d received %d\n", seqExpected, data.seq);
seqExpected = data.seq; // set sequence number ready for next data
seqExpected++;
} else
while (client.available()) client.read(); // discard corrupt packet
}
}
}
client transmitting data to server
// ESP32 client - connect to Access point server on port 10000 - transmit data structure
#include <WiFi.h>
// access point SSID and password
char ssid[] = "ESP32_Access_point"; // AP network SSID (name)
char password[] = "123456789"; // AP network password
IPAddress apserver(192, 168, 4, 1); // AP IP address
// test structure
struct __attribute__((packed)) Data {
int16_t seq; // sequence number
int32_t distance;
float voltage;
char text[50];
} data = { 0, 56, 3.14159, "hello test" }; // sample data
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(115200);
Serial.printf("\nattempting to connect to WiFi network SSID '%s' password '%s' \n", ssid, password);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.print("connected to SSID: ");
Serial.println(ssid);
}
boolean alreadyConnected = false; // whether or not the client was connected previously
void loop() {
static WiFiClient client;
if (!client) { // connect to server on AP
if (client.connect(apserver, 10000))
Serial.println("Connected to AP server");
}
// if clength connected send data
if (client) {
if (!alreadyConnected) {
client.flush();
Serial.println("client connected to server OK");
alreadyConnected = true;
}
// transmit data structure to server
Serial.printf("seq %d distaance %ld voltage %f text '%s'\n",
(int)data.seq, (long)data.distance, data.voltage, data.text);
client.write((char*)&data, sizeof(data));
delay(1000);
data.seq++; // update data ready for next transmission
data.distance += 10;
data.voltage += 2.5;
data.text[9]++;
}
}
AP/server serial monitor output
Setting AP (Access Point)… ESP32_Access_point
AP IP address: 192.168.4.1 listening on port 10000
We have a new client
Received length 60 - seq 28 distaance 336 voltage 73.141586 text 'hello tes�'
Error! seq expected 0 received 28
Received length 60 - seq 29 distaance 346 voltage 75.641586 text 'hello tes�'
Received length 60 - seq 30 distaance 356 voltage 78.141586 text 'hello tes�'
Received length 60 - seq 31 distaance 366 voltage 80.641586 text 'hello tes�'
Received length 60 - seq 32 distaance 376 voltage 83.141586 text 'hello tes�'
Received length 60 - seq 33 distaance 386 voltage 85.641586 text 'hello tes�'
Received length 60 - seq 34 distaance 396 voltage 88.141586 text 'hello tes�'
Received length 60 - seq 35 distaance 406 voltage 90.641586 text 'hello tes�'
client serial monitor output
attempting to connect to WiFi network SSID 'ESP32_Access_point' password '123456789'
............connected to SSID: ESP32_Access_point
Connected to AP server
client connected to server OK
seq 0 distaance 56 voltage 3.141590 text 'hello test'
seq 1 distaance 66 voltage 5.641590 text 'hello tesu'
seq 2 distaance 76 voltage 8.141590 text 'hello tesv'
seq 3 distaance 86 voltage 10.641590 text 'hello tesw'
seq 4 distaance 96 voltage 13.141590 text 'hello tesx'
seq 5 distaance 106 voltage 15.641590 text 'hello tesy'
seq 6 distaance 116 voltage 18.141590 text 'hello tesz'
seq 7 distaance 126 voltage 20.641590 text 'hello tes{'
seq 8 distaance 136 voltage 23.141590 text 'hello tes|'
seq 9 distaance 146 voltage 25.641590 text 'hello tes}'
seq 10 distaance 156 voltage 28.141590 text 'hello tes~'
seq 11 distaance 166 voltage 30.641590 text 'hello tes'
seq 12 distaance 176 voltage 33.141590 text 'hello tes�'
seq 13 distaance 186 voltage 35.641590 text 'hello tes�'
the structure carries a sequence number so the receiver can check for lost or duplicate packets (could request retransmission of lost packets)
the server indicated a sequence number error as it synchronised with the client
Edit: similar code could be implemented using ESP-NOW