Hello friends,
NodeMcu as SoftAP.
Three ways to use NodeMcu in a network:
1.- Router connected to the Internet and NodeMcu client of that router.
- NodeMcu works as WebServer in that local network.
2.- Router not connected to the Internet and NodeMcu client of that router.
- NodeMcu works as WebServer in that local network.
- You do not need internet.
3.- NodeMcu as SoftAP (Access Point).
- You do not need Router.
- NodeMcu creates network 192.168.4.X
- NodeMcu works as WebServer in that local network.
- In this example, when you Click Button1,2,3,4 in App Inventor (Android), it sends a static message ("Juan Antonio", "
KIO4.COM"...)
- When Click Button5, it sends text in TextBox1.
- These messages, NodeMcu shows them on an LCD
- Look IP, 192.168.4.22
- Code for NodeMcu:
- NodeMcu is a SoftAP and a Web Server.
- Name network: ESPsoftAP_01 (without password).
// Juan A. Villalpando
// NodeMcu como Punto de Acceso y Servidor Web.
#include <ESP8266WiFi.h>
// SoftAP
IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);
// Web Server
WiFiServer server(80);
// LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include <Wire.h>
String value = "-";
void setup()
{
Serial.begin(115200);
lcd.begin(16,2);// Columnas y filas de LCD
delay(10);
Serial.println();
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP("ESPsoftAP_01") ? "Ready" : "Failed!");
Serial.print("Soft-AP IP address = ");
Serial.println(WiFi.softAPIP());
server.begin();
}
void loop() {
// Consulta si se ha conectado algún cliente.
WiFiClient client = server.available();
if (!client) {
return;
}
// Espera hasta que el cliente envíe datos.
Serial.println("Nuevo cliente.");
while(!client.available()){
delay(1);
}
// Lee la primera línea de la petición.
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Consulta la petición
if (request.startsWith("mensaje=1", 5)) { value = "Juan Antonio";}
if (request.startsWith("mensaje=2", 5)) { value = "KIO4.COM.";}
if (request.startsWith("mensaje=3", 5)) { value = "Buen tiempo.";}
if (request.startsWith("mensaje=4", 5)) { value = "Puerto Real";}
if (request.startsWith("x", 15)) { // Si en la posición 15 viene una x...
value = request.substring(17); // ... un mensaje comienza en la 17.
value.replace("+", " "); // Para que los espacios no salgan con +
value.replace(" HTTP/1.1", " "); // Para quitar HTTP/1.1
}
Serial.println(value);
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print(value);
// Devuelve el mensaje.
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // Comillas importantes.
client.print(value);
Serial.println("Cliente desconectado.");
}
////////////////////////////////////////////////////////////////////////
- To check the operation, install the application (not emulate with MIT Companion) and in your Wifi network configuration, establish the network "ESPsoftAP_01" (192.168.4.x)
- More information:
Regards.
Juan Antonio Villalpando.