The project is made with 2 NodeMCU. One of them is set as AP, and the other one is the client.
NodeMCU AP:2 LEDs strips are connected to a relay 5V. When it receives an order to switch on the lights, it opens the relay.
NodeMCU Client:On starting this module, it connects to the AP and it shows connection info on the LDC. This NodeMCU has a touch sensor connected and when it detects a touch, a signal to AP is sent to switch on the light for 15 seconds.
#include <ESP8266WiFi.h>
#include <Wire.h> // This library is already built in to the Arduino IDE
#include <LiquidCrystal_I2C.h> //This library you can add via Include Library > Manage Library >
LiquidCrystal_I2C lcd(0x3F, 20, 4);
const char* ssid = "PACHACHO";
const char* password = "xxxxxx";
WiFiClient espClient;
int TouchSensor=12;
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(9600);
pinMode(TouchSensor,INPUT);
setup_wifi();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
lcd.init(); // initializing the LCD
lcd.backlight(); // Enable or Turn On the backlight
lcd.setCursor(0, 0);
lcd.print("Conectando a... "); // Start Print text to Line 1
lcd.setCursor(0, 1);
lcd.print(ssid); // Start Print Test to Line 2
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
lcd.setCursor(0, 0);
lcd.print("Conectado ");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
}
void enviarMensaje (String position)
{
WiFiClient client;
const char* host = "192.168.4.1";
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/";
url += "?pos=";
url += position;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
}
void loop() {
if(digitalRead(TouchSensor)==HIGH)
{
Serial.println("ON");
enviarMensaje("/LED=ON");
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Encender Luces ");
lcd.setCursor(0, 1);
lcd.print(" ");
delay(3000);
lcd.setCursor(0, 0);
lcd.print("");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.noBacklight();
}
delay(400);
}
#include <ESP8266WiFi.h> //Incluye la librería ESP8266WiFi
const char ssid[] = "PACHACHO"; //Definimos la SSDI de nuestro servidor WiFi -nombre de red-
const char password[] = "xxxx"; //Definimos la contraseña de nuestro servidor
WiFiServer server(80); //Definimos el puerto de comunicaciones
int PinLED = 12; //Definimos el pin de salida - GPIO2 / D4
int estado = LOW; //Definimos la variable que va a recoger el estado del LED
void setup() {
Serial.begin(9600);
pinMode(PinLED, OUTPUT); //Inicializamos el GPIO2 como salida
digitalWrite(PinLED, LOW); //Dejamos inicialmente el GPIO2 apagado
server.begin(); //inicializamos el servidor
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password); //Red con clave, en el canal 1 y //Red abierta
Serial.println();
Serial.print("Direccion IP Access Point - por defecto: "); //Imprime la dirección IP
Serial.println(WiFi.softAPIP());
Serial.print("Direccion MAC Access Point: "); //Imprime la dirección MAC
Serial.println(WiFi.softAPmacAddress());
}
void loop()
{
// Comprueba si el cliente ha conectado
WiFiClient client = server.available();
if (!client) {
return;
}
// Espera hasta que el cliente envía alguna petición
Serial.println("nuevo cliente");
while(!client.available()){
delay(1);
}
// Imprime el número de clientes conectados
Serial.printf("Clientes conectados al Access Point: %dn", WiFi.softAPgetStationNum());
// Lee la petición
String peticion = client.readStringUntil('r');
Serial.println(peticion);
client.flush();
// Comprueba la petición
if (peticion.indexOf('/LED=ON') != -1) {
estado = HIGH;
digitalWrite(PinLED, estado);
delay(15000);
digitalWrite(PinLED, LOW);
}
if (peticion.indexOf('/LED=OFF') != -1){
estado = LOW;
digitalWrite(PinLED, estado);
delay(1000);
}
client.println("HTTP/1.1 200 OK");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<meta charset='UTF-8'>");
client.println("<html>");
//Imprime el estado del led
client.print("<h1>PACHACHOOOOOOO ");
client.println("</html>");
delay(1);
Serial.println("Petición finalizada");
}















Comments