Hi there,
For a new project I was toying around with two ESP8266's connected without WiFi, where an input from one ESP leads to a response on another ESP.
In this case I want to have a pushbutton input on one ESP lead to an LED turn on/off on another ESP. I followed a tutorial to connect the two ESP's where one acts as an AP and the other as a client (https://blog.zairza.in/esp8266-to-esp8266-communication-without-a-router-fe9642b93292), which worked fine, but now I want to take the next step: writing HIGH on one of the ESP's which turns on the LED on the other ESP. From there I want to add the pushbutton state to read as an input.
The problem I'm running into is that I can't for the life of me figure out what I can put into the functions that are supplied with the ESP, like these: Client Class — ESP8266 Arduino Core 3.1.1-19-gbe02af05 documentation. It seems to be relying on bytes, chars and other things that I don't really understand well.
What I'm looking for is a sample of what I need to put into the loop's so that the ESP's actually have an input/output connectiong rather than blinking in sync. Or am I better off implementing HTTP GET/POST requests, which seems to be another commonly used way of communicating between ESP's.
Any help is appreciated, thanks!
The client code:
#include <ESP8266WiFi.h>
byte ledPin = 2;
char ssid[] = "Wifiathome"; // SSID of your AP
char pass[] = "Het#Katten@Huis=0"; // password of your AP
IPAddress server(192,168,4,15); // IP address of the AP
WiFiClient client;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass); // connects to the WiFi AP
Serial.println();
Serial.println("Connection to the AP");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("Connected");
Serial.println("station_bare_01.ino");
Serial.print("LocalIP:"); Serial.println(WiFi.localIP());
Serial.println("MAC:" + WiFi.macAddress());
Serial.print("Gateway:"); Serial.println(WiFi.gatewayIP());
Serial.print("AP MAC:"); Serial.println(WiFi.BSSIDstr());
pinMode(ledPin, OUTPUT);
}
void loop() {
client.connect(server, 80);
digitalWrite(ledPin, LOW);
// Serial.println("********************************");
// Serial.print("Byte sent to the AP: ");
// Serial.println(client.print("KleinPoepje\r"));
// String answer = client.readStringUntil('\r');
// Serial.println("From the AP: " + answer);
client.flush();
digitalWrite(ledPin, HIGH);
client.stop();
delay(2000);
}
The Access Point code
//for more info go to https://blog.zairza.in/esp8266-to-esp8266-communication-without-a-router-fe9642b93292
#include <ESP8266WiFi.h>
WiFiServer server(80);
IPAddress IP(192,168,4,15);
IPAddress mask = (255, 255, 255, 0);
byte ledPin = 2;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_AP);
WiFi.softAP("Wifiathome", "Het#Katten@Huis=0");
WiFi.softAPConfig(IP, IP, mask);
server.begin();
pinMode(ledPin, OUTPUT);
Serial.println();
Serial.println("accesspoint_bare_01.ino");
Serial.println("Server started.");
Serial.print("IP: "); Serial.println(WiFi.softAPIP());
Serial.print("vMAC:"); Serial.println(WiFi.softAPmacAddress());
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
digitalWrite(ledPin, LOW);
// String request = client.readStringUntil('\r');
// Serial.println("********************************");
// Serial.println("From the station: " + request);
client.flush();
// Serial.print("Byte sent to the station: ");
// Serial.println(client.println(request + "ca" + "\r"));
digitalWrite(ledPin, HIGH);
}