Hey guys!
For a project, I want to use an ESP to set up a hotspot that broadcasts some data when another ESP connects to the hotspot.
I have made some code for the hotspot. And when I connect with my laptop and send a TCP packet I get the correct response. But I would like to send the data when another ESP connects.
I can't really find any guides or codes that show this use case. All guides just connect both ESP to a wifi point.
Code on the Hotspot:
#include <Arduino.h>
#include "wifiHandler.h"
#include <eepromHandler.h>
WiFiUDP UDP;
byte macRecieved[6];
uint8_t sizeMac = 17;
uint8_t sizeResponse = 2;
char lastTwoChars[2];
const char* SSID = "netwerkNaam";
const char* PSW = "netwerkWachtwoord";
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
const int port = 23;
#define MAX_SRV_CLIENTS 25
WiFiServer server(port);
WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
wifiHandler hotspot;
hotspot.setupSoftAp();
eepromHandler eeprom;
eeprom.saveData(SSID, PSW, PARAMETER_SIZE_IN_BYTES);
eeprom.dumbData();
server.begin();
server.setNoDelay(true);
}
void loop() {
// wait for a new client:
WiFiClient client = server.available();
if (client) {
Serial.printf("Client connected, IP: %s,port: %s", client.remoteIP().toString().c_str(), client.remotePort());
// clead out the input buffer:
while (client.connected()){
if (client.available()){
//String line = client.readStringUntil('\r');
// Read incoming message
char inChar = client.read();
// Echo input on Serial monitor
Serial.write(inChar);
}
client.printf("%s / %s", SSID, PSW);
client.stop();
Serial.println("Client disconnected.");
}
}
}