Hi
I'm trying to do a simple example of communication between an application and ESP32 through an Access Point.
I made a basic example of turning LEDs on and off.
The application works, I can turn the led on/off, but an error message appears on the screen:
And I'm also not able to make the app receive any data from ESP32.
Could anyone help me on this one?
AppInventor Code:
Arduino Code:
#include <WiFi.h> //Inclui a biblioteca
const char* ssid = "ESP32-AP";
const char* pass = "12345678";
WiFiServer sv(80);
int LED_Pin = 2;
void setup() {
Serial.begin(115200);
pinMode(LED_Pin, OUTPUT);
WiFi.softAP(ssid, pass);
Serial.print("Se conectando a: ");
Serial.println(ssid);
IPAddress ip = WiFi.softAPIP();
Serial.print("Endereço de IP: ");
Serial.println(ip);
sv.begin();
Serial.println("Servidor online");
}
void loop() {
WiFiClient client = sv.available();
if (client) {
String line = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (line.length() == 0) {
break;
} else {
line = "";
}
} else if (c != '\r') {
line += c;
}
if (line.indexOf("LEDON") > 0) {
digitalWrite(LED_Pin, HIGH);
client.println("Ligado");
}
if (line.indexOf("LEDOFF") > 0) {
digitalWrite(LED_Pin, LOW);
client.println("Desligado");
}
}
}
client.stop();
}
}
Thank you!
PhilipKerplunk:
client.stop();
I'm not a WiFi expert but your Sketch loop does not have any duration control, it just continuously runs at warp speed. So I think the client.stop() function could be triggering the error message.
Concerning sending data to the App, just send '1' for on and '0' for off. In Web1 Got Text, collect all the values to see what you actually get.
Try this
#include <WiFi.h> //Inclui a biblioteca
const char* ssid = "ESP32-AP";
const char* pass = "12345678";
WiFiServer sv(80);
int LED_Pin = 2;
void setup() {
Serial.begin(115200);
pinMode(LED_Pin, OUTPUT);
WiFi.softAP(ssid, pass);
Serial.print("Se conectando a: ");
Serial.println(ssid);
IPAddress ip = WiFi.softAPIP();
Serial.print("Endereço de IP: ");
Serial.println(ip);
sv.begin();
Serial.println("Servidor online");
}
void loop() {
WiFiClient client = sv.available();
if (client) {
String line = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (line.length() == 0) {
break;
} else {
line = "";
}
} else if (c != '\r') {
line += c;
}
if (line.indexOf("LEDON") > 0) {
digitalWrite(LED_Pin, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
client.println("Ligado"); // Return status.
client.stop();
}
if (line.indexOf("LEDOFF") > 0) {
digitalWrite(LED_Pin, LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
client.println("Desligado"); // Return status.
client.stop();
}
}
}
}
}
Other examples:
6.- App get status of 2 Push Buttons. WiFi. WebServer. Static IP.
ESP32 is a Station, client of Router.
Static IP 192.168.1.115
Web Server, port 80.
Check status Push Buttons.
[esp32_wifi_pulsadores]
[esp32_wifi_pulsadores2]
// Juan A. Villalpando.
// KIO4.COM
// Estado de dos pulsadores
#include <WiFi.h>
const char* ssid = "Nombre_de_tu_red_wifi";
const char* password = "La_clave_de_tu_red_wifi";
// Setting Static IP.
IPAddress local_IP(192, 168, 1, 115);
IPAddress gatew…