Explicación:Cpp
#include #include #include #include #define GPIO0 0 #define GPIO2 2 ESP8266WebServer server(80); void handleRoot() { server.send(200, "text/plain", "Hello, you can control GPIO here."); } // función para controlar GPIO0 void handleGPIO0On() { digitalWrite(GPIO0, LOW); // activar el pin server.send(200, "text/plain", "GPIO0 is ON"); } void handleGPIO0Off() { digitalWrite(GPIO0, HIGH); // desactivar el pin server.send(200, "text/plain", "GPIO0 is OFF"); } // función para controlar GPIO2 void handleGPIO2On() { digitalWrite(GPIO2, LOW); // activar el pin server.send(200, "text/plain", "GPIO2 is ON"); } void handleGPIO2Off() { digitalWrite(GPIO2, HIGH); // desactivar el pin server.send(200, "text/plain", "GPIO2 is OFF"); } void setup() { pinMode(GPIO0, OUTPUT); pinMode(GPIO2, OUTPUT); digitalWrite(GPIO0, HIGH); // inicialmente desactivado digitalWrite(GPIO2, HIGH); // inicialmente desactivado Serial.begin(115200); WiFiManager wifiManager; // Esta linea eliminará cualquier configuración previa wifiManager.resetSettings(); // Inicia el punto de acceso y solicita los datos de la red wifiManager.autoConnect("AutoConnectAP"); // Una vez recogida la información, se conecta a la red WiFi y se inicia el servidor web Serial.println("Conectado a WiFi!"); server.on("/", handleRoot); // Rutas para controlar GPIO0 server.on("/gpio0/on", handleGPIO0On); server.on("/gpio0/off", handleGPIO0Off); // Rutas para controlar GPIO2 server.on("/gpio2/on", handleGPIO2On); server.on("/gpio2/off", handleGPIO2Off); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); }
/gpio0/on
, /gpio0/off
, /gpio2/on
, /gpio2/off
).Explicación Adicional:Cpp
#include #include #include // Definir pines #define GPIO0 0 #define GPIO2 2 // Certificado TLS para HTTPS const char* cert = "-----BEGIN CERTIFICATE-----\n" \ "YOUR_CERTIFICATE_HERE\n" \ "-----END CERTIFICATE-----\n"; const char* privateKey = "-----BEGIN PRIVATE KEY-----\n" \ "YOUR_PRIVATE_KEY_HERE\n" \ "-----END PRIVATE KEY-----\n"; // Configuración del servidor ESP8266WebServerSecure server(443); IotWebConf iotWebConf("YourProductName", &server, nullptr); void handleRoot() { server.send(200, "text/html", "Control de GPIO"); } // función para controlar GPIO0 void handleGPIO0On() { digitalWrite(GPIO0, LOW); // activar el pin server.send(200, "text/html", "GPIO0 is ON"); } void handleGPIO0Off() { digitalWrite(GPIO0, HIGH); // desactivar el pin server.send(200, "text/html", "GPIO0 is OFF"); } // función para controlar GPIO2 void handleGPIO2On() { digitalWrite(GPIO2, LOW); // activar el pin server.send(200, "text/html", "GPIO2 is ON"); } void handleGPIO2Off() { digitalWrite(GPIO2, HIGH); // desactivar el pin server.send(200, "text/html", "GPIO2 is OFF"); } void setup() { pinMode(GPIO0, OUTPUT); pinMode(GPIO2, OUTPUT); digitalWrite(GPIO0, HIGH); // inicialmente desactivado digitalWrite(GPIO2, HIGH); // inicialmente desactivado Serial.begin(115200); // Inicializa el servidor seguro con el certificado y la clave privada server.getServer().begin(cert, privateKey); // Configuración de IotWebConf iotWebConf.init(); // Definir rutas server.on("/", handleRoot); server.on("/gpio0/on", handleGPIO0On); server.on("/gpio0/off", handleGPIO0Off); server.on("/gpio2/on", handleGPIO2On); server.on("/gpio2/off", handleGPIO2Off); Serial.println("HTTP server started"); } void loop() { iotWebConf.doLoop(); server.handleClient(); }
YOUR_CERTIFICATE_HERE
y YOUR_PRIVATE_KEY_HERE
con tus propios certificados TLS.Puntos Clave para la Seguridad de Credenciales:Cpp
#include #include // Definir pines #define GPIO0 0 #define GPIO2 2 WiFiManager wifiManager; // Servidor HTTP ESP8266WebServer server(80); // Función para manejar la raíz void handleRoot() { server.send(200, "text/html", "Control de GPIOEncender GPIO0Apagar GPIO0Encender GPIO2Apagar GPIO2"); } void handleGPIO0On() { digitalWrite(GPIO0, LOW); // activar el pin server.send(200, "text/plain", "GPIO0 is ON"); } void handleGPIO0Off() { digitalWrite(GPIO0, HIGH); // desactivar el pin server.send(200, "text/plain", "GPIO0 is OFF"); } void handleGPIO2On() { digitalWrite(GPIO2, LOW); // activar el pin server.send(200, "text/plain", "GPIO2 is ON"); } void handleGPIO2Off() { digitalWrite(GPIO2, HIGH); // desactivar el pin server.send(200, "text/plain", "GPIO2 is OFF"); } void setup() { pinMode(GPIO0, OUTPUT); pinMode(GPIO2, OUTPUT); digitalWrite(GPIO0, HIGH); // inicialmente desactivado digitalWrite(GPIO2, HIGH); // inicialmente desactivado Serial.begin(115200); // Resetea las configuraciones anteriores (útil para pruebas) // wifiManager.resetSettings(); // Inicia AP para recoger credenciales if (!wifiManager.autoConnect("AutoConnectAP")) { Serial.println("Falló la conexión, reiniciando..."); delay(3000); ESP.restart(); } Serial.println("Conectado a WiFi!"); // Configuración del servidor HTTP server.on("/", handleRoot); server.on("/gpio0/on", handleGPIO0On); server.on("/gpio0/off", handleGPIO0Off); server.on("/gpio2/on", handleGPIO2On); server.on("/gpio2/off", handleGPIO2Off); server.begin(); Serial.println("Servidor HTTP iniciado"); } void loop() { server.handleClient(); }
wifiManager.resetSettings();
para eliminar configuraciones previas durante las pruebas; asegúrate de comentarlo/tratarlo adecuadamente en producción.wifiManager.autoConnect("AutoConnectAP")
crea un punto de acceso para capturar el SSID y la contraseña. Estos datos se almacenan de manera segura en la memoria flash.5V (Pin 1)
: The input voltage to power the board through a USB connection or an external 5V source.3V3 (Pin 16)
: 3.3V output that can be used to power external components.GND (Pin 5)
: Ground.D0 to D8 (Pins 14, 10, 11, 12, 13, 6, 7, 8, 9)
: These are GPIO pins with specific functions like CS/D8
, MOSI/D7
, MISO/D6
, and SCK/D5
for SPI communication, also SDA/D2
, SCL/D1
for I2C communication.A0 (Pin 15)
: Analog input pin.TX (Pin 4)
: Transmit pin for serial communication.RX (Pin 3)
: Receive pin for serial communication.RST (Pin 2)
: Used to reset the microcontroller.Cpp
#include #include
Cpp
const char *ssid = "Your_SSID"; const char *password = "Your_PASSWORD"; void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(IP); }
Cpp
AsyncWebServer server(80); void setup() { // Tu configuración de red aquí server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", "Captive PortalUsted ha sido redirigido aquí!"); }); server.begin(); }
Cpp
void setup() { // Tu configuración de red y servidor aquí dnsServer.start(53, "*", WiFi.softAPIP()); while (true) { dnsServer.processNextRequest(); } }
Your_SSID
y Your_PASSWORD
por los datos de tu red. Con esto, cualquier dispositivo que se conecte a tu AP será redirigido a la página que especifiques.dnsmasq
si estás manejando un servidor local.192.168.4.1
. Puedes intentar ingresar esta dirección en tu navegador para ver si puedes acceder al portal cautivo.192.168.4.1
en la barra de direcciones.192.168.4.1
no funciona, puedes intentar otras direcciones IP comunes, como 192.168.0.1
o 192.168.1.1
, aunque es mucho menos probable que sean las configuradas en el modo AP del ESP8266.192.168.4.1
. Esto te puede ayudar a verificar si la dirección IP es la correcta.192.168.4.2
es la IP asignada a tu celular por el punto de acceso del ESP8266. Deberías intentar acceder a la IP del punto de acceso, la cual típicamente es 192.168.4.1
.192.168.4.1
no te lleva al portal cautivo, aquí hay algunos pasos adicionales para solucionar el problema:Soluciones Adicionales:Cpp
#include #include const char *ssid = "Your_SSID"; const char *password = "Your_PASSWORD"; // Create an instance of the server AsyncWebServer server(80); void setup() { Serial.begin(115200); // Setting up the WiFi WiFi.mode(WIFI_AP); WiFi.softAP(ssid, password); // Print the IP address IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(IP); // Define the behavior for when the root URL is accessed server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", "Captive PortalYou have been redirected here!"); }); // Start server server.begin(); } void loop() { // Nothing needs to be done in the loop for this server }
Serial.println()
para imprimir mensajes que confirmen que el ESP8266 está configurado correctamente y que el servidor web está corriendo.ESPAsyncWiFiManager
para crear un portal cautivo de manera más estructurada y confiable.Cpp
#include #include AsyncWebServer server(80); DNSServer dns; void setup() { Serial.begin(115200); // Create an AsyncWiFiManager object AsyncWiFiManager wifiManager(&server, &dns); // Reset saved settings (for development purposes) wifiManager.resetSettings(); // Start the portal wifiManager.autoConnect("AutoConnectAP"); Serial.println("Connected."); } void loop() { // Your code here }
Archivo > Preferencias
.Gestor de URLs de placas adicionales
, pega la siguiente URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
.OK
.Herramientas > Placa > Gestor de placas
.esp8266
y selecciona ESP8266 by ESP8266 Community
.Instalar
.Herramientas > Placa
y selecciona LOLIN(WEMOS) D1 R2 & mini
.Herramientas > Puerto
y selecciona el puerto que corresponda a tu placa (p.ej., COM3 en Windows, /dev/ttyUSB0 en Linux).✓
(verificar) para compilar el código.→
(subir) para cargar el código en tu placa WeMos D1 mini.Herramientas > Monitor Serial
.115200 baud
, que es la velocidad que configuraste en Serial.begin$$115200$$
.AutoConnectAP
. Conéctate a este AP utilizando tu dispositivo (teléfono, portátil, etc.).http:///gpio0/on
http:///gpio0/off
http:///gpio2/on
http:///gpio2/off
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Cpp
#include // Replace with your network credentials const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASS"; void setup() { Serial.begin(115200); // Connect to Wi-Fi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Your loop code here }
YOUR_SSID
y YOUR_PASS
con el nombre de tu red Wi-Fi y la contraseña, respectivamente.WiFiManager
, el código inicia un servidor HTTP en el puerto 80 que permite controlar los pines GPIO0 y GPIO2 a través de una interfaz web simple.WiFiManager
.http:///gpio0/on
http:///gpio0/off
http:///gpio2/on
http:///gpio2/off
192.168.0.10
:Descripción del flujo del programaPlaintext
http://192.168.0.10/gpio0/on http://192.168.0.10/gpio0/off http://192.168.0.10/gpio2/on http://192.168.0.10/gpio2/off
WiFiManager
para gestionar la conexión WiFi.server.handleClient()
para gestionar las solicitudes HTTP entrantes.LED:+
a U1:GPIO0
.LED:-
a R1:1
.R1:2
a U1:GND
.Sensor:Data
a U1:GPIO2
.Sensor:VCC
a U1:3V3
.Sensor:GND
a U1:GND
.Image
Thinking...
Integration and TestingCpp
#include #include #define SERVO_PIN1 D1 #define SERVO_PIN2 D2 #define SERVO_PIN3 D5 #define SERVO_PIN4 D6 Servo servo1; Servo servo2; Servo servo3; Servo servo4; void setup() { Serial.begin(115200); servo1.attach(SERVO_PIN1); servo2.attach(SERVO_PIN2); servo3.attach(SERVO_PIN3); servo4.attach(SERVO_PIN4); // Initial servo positions servo1.write(0); servo2.write(0); servo3.write(0); servo4.write(0); } void loop() { // Example of moving servo to open position if(detectedPlastic()) { servo1.write(90); // Open the plastic bin delay(2000); // Keep it open for 2 seconds servo1.write(0); // Close the bin } // Add similar logic for other waste types } // Example function to check for plastic detection bool detectedPlastic() { // Add your sensor detection logic here return false; }
WeMos D1 mini template
Controls
Properties
Availability & Pricing
Distributor | Qty 1 |
---|
Assets
id=wemosd1mini-template
Simulation
dd33dd33
copilot
Table
dd33dd33
copilot