hi
The two ESP32s I have.
AP mode.
Client mode.
Number 1 communicates with number 2 when digital read(from no.1's PIN 21).
Number 2 will turn on the built-in LED when receiving the serial character ("1") from Number1.
What should I do for this code to work properly?
/*
This sketch sends a message to a AP ("""client mode (no.2)""")
*/
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
char val;
const int led = 2;
void setup()
{
Serial.begin(9600);
delay(10);
pinMode(led, OUTPUT);
// We start by connecting to a WiFi network
WiFiMulti.addAP("ESP32", "11111111");
Serial.println();
Serial.println();
Serial.print("Waiting for WiFi... ");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void loop()
{
// const uint16_t port = 80;
// const char * host = "192.168.1.1"; // ip or dns
const uint16_t port = 80;
const char * host = "192.168.4.1"; // ip or dns
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("Connection failed.");
Serial.println("Waiting 5 seconds before retrying...");
delay(5000);
return;
} //
while (client.connected()) {
while (client.available()) {
char c = client.read();
if (c == '1') {
digitalWrite(led, HIGH);// perform some action
}
else {
digitalWrite(led, LOW);// maybe perform some other action
}
}
}
// This will send a request to the server
//uncomment this line to send an arbitrary string to the server
//client.print("Send this data to the server");
//uncomment this line to send a basic document request to the server
client.print("GET /index.html HTTP/1.1\n\n");
int maxloops = 0;
//wait for the server's reply to become available
while (!client.available() && maxloops < 1000)
{
maxloops++;
delay(1); //delay 1 msec
}
if (client.available() > 0)
{
//read back one line from the server
String line = client.readStringUntil('\r');
Serial.println(line);
}
else
{
Serial.println("client.available() timed out ");
}
Serial.println("Closing connection.");
client.stop();
Serial.println("Waiting 5 seconds before restarting...");
delay(5000);
}
and, below no.1 (AP mode)'s code.
you can find there control + F < if (currentSensorState == 1) >
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
const char *ssid = "ESP32";
const char *password = "11111111";
WiFiServer server(80);
#define SWITCH_PIN 21
int lastSensorState;
int currentSensorState;
int stateChangeCnt = 0;
void setup() {
pinMode(SWITCH_PIN, INPUT);
pinMode(2, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.println("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
int switchValue = digitalRead(SWITCH_PIN);
currentSensorState = digitalRead(SWITCH_PIN);
WiFiClient client = server.available();
{
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
{
if (currentSensorState != lastSensorState) {
if (currentSensorState == 1) // heat switch off
{
client.println("switch1.");
client.write("1");
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
}
if (lastSensorState == 1) //switch on
{
client.println("switch2.");
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, HIGH);
}
lastSensorState = currentSensorState;
stateChangeCnt++;
}
else {
digitalWrite(2, HIGH);
}
// close the connection:
client.stop();
// Check to see if the client request was "GET /H" or "GET /L":
}
}
}
}
}
}
thanks!