├── 00 - Introducción └── 03 - como-convertir-codigo-del-esp8266-al-esp32.md ├── 03 - Hardware ├── 00 - esp32-gpio.md ├── 01 - esp32-adc.md ├── 02 - esp32-pwm.md ├── 03 - esp32-servo.md ├── 04 - esp32-dac.md ├── 05 - esp32-touch-pins.md ├── 06 - esp32-interrupciones-hardware.md ├── 07 - esp32-timers.md ├── 08 - esp32-uart.md ├── 09 - esp32-i2c.md ├── 10 - esp32-spi.md ├── 11 - esp32-i2s.md ├── 12 - esp32-sensor-hall-integrado.md └── 13 - esp32-sensor-temperatura-integrado.md ├── 04 - Consumo energía ├── 01 - esp32-cambiar-frecuencia-procesador.md ├── 02 - esp32-sleep-modes.md ├── 03 - esp32-light-sleep.md └── 04 - esp32-deep-sleep.md ├── 05 - Programación ├── 00 - como-programar-esp32-con-el-ide-de-arduino.md ├── 01 - guia-de-programacion-del-esp32-en-entorno-arduino.md ├── 02 - esp32-eeprom.md ├── 03 - esp32-preferences.md └── 04 - esp32-random-numbers.md ├── 06 - Comunicación WiFi ├── 00_Connect_Wifi_STA │ ├── 00_Connect_Wifi_STA.ino │ ├── ESP32_Utils.hpp │ └── config.h ├── 01_Connect_WiFi_AP │ ├── 01_Connect_WiFi_AP.ino │ ├── ESP32_Utils.hpp │ └── config.h ├── 02_Static_IP │ ├── 02_Static_IP.ino │ ├── ESP32_Utils.hpp │ └── config.h ├── 03_MDNS │ ├── 03_MDNS.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_mDNS.hpp │ └── config.h ├── 04_UDP │ ├── 20_UDP.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_UDP.hpp │ ├── Python │ │ ├── recieveUDP.py │ │ └── sendUDP.py │ ├── UDP.hpp │ └── config.h └── 05_OTA │ ├── 12_OTA.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_OTA.hpp │ └── config.h ├── 07 - Servidor ├── 04_Client │ ├── 04_Client.ino │ ├── Client.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_Client.hpp │ └── config.h ├── 05_Server_Simple │ ├── 05_Server_Simple.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 06_Server_Parameters │ ├── 06_Server_Parameters.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 07_Server_Dynamic_Content │ ├── 07_Server_Dynamic_Content.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 08_Server_from_Flash │ ├── 08_Server_from_Flash.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── index.h ├── 09_Server_SPIFFS │ ├── 09_Server_SPIFFS.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_Server.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html ├── 10_Server_SPIFFS_Gzip │ ├── 10_Server_SPIFFS_Gzip.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_Server.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html.gz ├── 11_AsyncServer │ ├── 11_AsyncServer.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html.gz └── 23_MaterialDesign │ ├── 23_MaterialDesign.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ ├── favicon.ico │ ├── index.html │ └── vendor │ ├── google-fonts.css.gz │ ├── material.css.gz │ └── material.js.gz ├── 08 - Formularios Web ├── 13_Forms_Text │ ├── 13_Forms_Text.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html ├── 14_Forms_LED │ ├── 14_Forms_LED.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html ├── 15_Forms_LED_PWM │ ├── 15_Forms_LED_PWM.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html └── 16_Forms_RGB │ ├── 16_Forms_RGB.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ └── index.html ├── 09 - API Rest ├── 17_Ajax │ ├── 17_Ajax.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 21_API_REST_Client │ ├── 21_API_REST_Client.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ └── config.h ├── 22_API_REST_Server │ ├── 22_API_REST_Server.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_APIREST.hpp │ ├── Server.hpp │ └── config.h ├── 24_Json_Ajax │ ├── 24_Json_Ajax.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_APIREST.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js └── 29_Axios │ ├── 29_Axios.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_APIREST.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ ├── favicon.ico │ ├── index.html │ ├── js │ └── API.js │ └── vendor │ └── axios.min.js.gz ├── 10 - Websockets ├── 18_Websocket │ ├── 18_Websocket.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_WS.hpp │ ├── Server.hpp │ ├── WebSockets.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 19_AsyncWebsocket │ ├── 19_AsyncWebsocket.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── Websocket.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 25_Json_AsyncWebsocket │ ├── 25_Json_AsyncWebsocket.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── WebSockets.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js └── 26_Json_AsyncWebsocket_GPIO │ ├── 26_Json_AsyncWebsocket_GPIO.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── WebSockets.hpp │ ├── config.h │ └── data │ ├── css │ └── main.css │ ├── index.html │ ├── js │ └── main.js │ └── vendor │ ├── google-fonts.css │ ├── google-fonts.css.gz │ ├── material.css │ ├── material.css.gz │ ├── material.js │ └── material.js.gz ├── 11 - ESP32 y VueJS ├── 27_VueJs │ ├── 27_VueJs.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ └── app.js │ │ └── vendor │ │ └── vue.min.js.gz ├── 28_Vuetify │ ├── 28_Vuetify.ino │ ├── ESP32_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ └── app.js │ │ └── vendor │ │ ├── google-fonts.css.gz │ │ ├── vue.min.js.gz │ │ ├── vuetify.min.css.gz │ │ └── vuetify.min.js.gz ├── 30_Vue_Axios │ ├── 30_Vue_Axios.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_APIREST.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ ├── API.js │ │ └── app.js │ │ └── vendor │ │ ├── axios.min.js.gz │ │ └── vue.min.js.gz ├── 31_Vuetify_Axios │ ├── 31_Vuetify_Axios.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_APIREST.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ ├── API.js │ │ └── app.js │ │ └── vendor │ │ ├── axios.min.js.gz │ │ ├── google-fonts.css.gz │ │ ├── vue.min.js.gz │ │ ├── vuetify.min.css.gz │ │ └── vuetify.min.js.gz ├── 32_Vue_NativeWebsocket │ ├── 32_Vue_NativeWebsocket.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── Websockets.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ └── app.js │ │ └── vendor │ │ ├── nativeWs.min.js.gz │ │ └── vue.min.js.gz ├── 33_Vue_NativeWebsocket_GPIO │ ├── 33_Vue_NativeWebsocket_GPIO.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── Websockets.hpp │ ├── config.h │ └── data │ │ ├── css │ │ └── main.css │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ └── app.js │ │ └── vendor │ │ ├── google-fonts.css.gz │ │ ├── material.css.gz │ │ ├── material.js.gz │ │ ├── nativeWs.min.js.gz │ │ └── vue.min.js.gz └── 34_Vuetify_NativeWebsocket_GPIO │ ├── 34_Vuetify_NativeWebsocket_GPIO.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_AWS.hpp │ ├── Server.hpp │ ├── Websockets.hpp │ ├── config.h │ └── data │ ├── css │ └── main.css │ ├── favicon.ico │ ├── index.html │ ├── js │ └── app.js │ └── vendor │ ├── google-fonts.css.gz │ ├── nativeWs.min.js.gz │ ├── vue.min.js.gz │ ├── vuetify.min.css.gz │ └── vuetify.min.js.gz ├── 12 - ESP32 y MQTT ├── 35_Mqtt │ ├── 35_Mqtt.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT.hpp │ ├── MQTT.hpp │ └── config.h ├── 36_Async Mqtt │ ├── 36_Mqtt.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ └── config.h ├── 37_Mqtt_Json │ ├── 37_Mqtt_Json.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ └── config.h ├── 38_Mqtt_Json_web │ ├── 38_Mqtt_Json_web.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ ├── API.js │ │ └── app.js │ │ └── vendor │ │ ├── mqttws31.min.js.gz │ │ └── nativeWs.min.js.gz ├── 39_Mqtt_Json_Vue │ ├── 39_Mqtt_Json_Vue.ino │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── css │ │ └── main.css │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ ├── API.js │ │ └── app.js │ │ └── vendor │ │ ├── google-fonts.css.gz │ │ ├── mqttws31.min.js.gz │ │ ├── nativeWs.min.js.gz │ │ ├── vue.min.js.gz │ │ ├── vuetify.min.css.gz │ │ └── vuetify.min.js.gz └── 40_Mqtt_Json_GPIO │ ├── 40_Mqtt_Json_GPIO.ino │ ├── API.hpp │ ├── ESP32_Utils.hpp │ ├── ESP32_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ ├── css │ └── main.css │ ├── favicon.ico │ ├── index.html │ ├── js │ ├── API.js │ └── app.js │ └── vendor │ ├── google-fonts.css.gz │ ├── mqttws31.min.js.gz │ ├── nativeWs.min.js.gz │ ├── vue.min.js.gz │ ├── vuetify.min.css.gz │ └── vuetify.min.js.gz ├── 13 - ESP32 y .NET con C# ├── 0 - esp32-net6-serial.md ├── 1 - esp32-net6-http.md ├── 2 - esp32-net6-websockets.md └── 3 - esp32-net6-mqtt.md ├── LICENSE └── README.md /00 - Introducción/03 - como-convertir-codigo-del-esp8266-al-esp32.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/como-convertir-codigo-del-esp8266-al-esp32/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Librerías WiFi 8 | ```cpp 9 | #if defined(ESP8266) 10 | #define HARDWARE "ESP8266" 11 | #include "ESP8266WiFi.h" 12 | 13 | #elif defined(ESP32) 14 | #define HARDWARE "ESP32" 15 | #include "WiFi.h" 16 | 17 | #endif 18 | ``` 19 | 20 | 21 | -------------------------------------------------------------------------------- /03 - Hardware/02 - esp32-pwm.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-pwm/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Cómo usar una salida PWM el ESP32 8 | ```cpp 9 | const int ledChannel = 0; // Canal PWM, puede ser de 0 a 15 10 | const int ledPin = 5; // Pin al que está conectado el dispositivo 11 | const int frequency = 5000; // Frecuencia en Hz 12 | const int resolution = 8; // Resolución en bits (de 1 a 15) 13 | 14 | ledcSetup(ledChannel, frequency, resolution); 15 | ``` 16 | 17 | ```cpp 18 | int dutyCycle = 128; // Valor del ciclo de trabajo (0 a 255) 19 | 20 | ledcWrite(ledChannel, dutyCycle); 21 | ``` 22 | 23 | 24 | ## Ejemplo de código 25 | ```cpp 26 | const int ledChannel = 0; 27 | const int ledPin = 5; 28 | const int frequency = 5000; 29 | const int resolution = 8; 30 | 31 | void setup() { 32 | ledcSetup(ledChannel, frequency, resolution); 33 | ledcAttachPin(ledPin, ledChannel); 34 | } 35 | 36 | void loop() { 37 | // Incrementar el ciclo de trabajo gradualmente para aumentar la intensidad del LED 38 | for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) { 39 | ledcWrite(ledChannel, dutyCycle); 40 | delay(10); 41 | } 42 | 43 | // Disminuir el ciclo de trabajo gradualmente para reducir la intensidad del LED 44 | for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) { 45 | ledcWrite(ledChannel, dutyCycle); 46 | delay(10); 47 | } 48 | } 49 | ``` 50 | 51 | 52 | ## Alternativa con una librería 53 | ```cpp 54 | #include 55 | #include 56 | 57 | int brightStep = 1; 58 | int brightness = 0; 59 | 60 | void setup() { 61 | // Set resolution for a specific pin 62 | analogWriteResolution(LED_BUILTIN, 12); 63 | } 64 | 65 | void loop() { 66 | brightness += brightStep; 67 | if ( brightness == 0 || brightness == 255 ) { 68 | brightStep = -brightStep; 69 | } 70 | 71 | analogWrite(LED_BUILTIN, brightness); 72 | 73 | delay(10); 74 | } 75 | ``` 76 | 77 | 78 | -------------------------------------------------------------------------------- /03 - Hardware/03 - esp32-servo.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-servo/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Incluir la biblioteca Servo 8 | ```cpp 9 | // para la librermadhephaestus 10 | #include 11 | 12 | // para la libreria RoboticsBrno 13 | #include 14 | ``` 15 | 16 | 17 | ## Crear un objeto Servo 18 | ```cpp 19 | Servo miServo; 20 | ``` 21 | 22 | 23 | ## Configurar el pin del servo 24 | ```cpp 25 | const int pinServo = 18; // Pin GPIO al que está conectado el servo 26 | 27 | void setup() 28 | { 29 | miServo.attach(pinServo); 30 | } 31 | ``` 32 | 33 | 34 | ## Controlar el servo 35 | ```cpp 36 | void loop() 37 | { 38 | miServo.write(0); // Mover el servo a 0 grados 39 | delay(1000); // Esperar 1 segundo 40 | miServo.write(90); // Mover el servo a 90 grados 41 | delay(1000); // Esperar 1 segundo 42 | miServo.write(180); // Mover el servo a 180 grados 43 | delay(1000); // Esperar 1 segundo 44 | } 45 | ``` 46 | 47 | 48 | -------------------------------------------------------------------------------- /03 - Hardware/04 - esp32-dac.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-dac/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Uso del DAC en el ESP32 8 | ```cpp 9 | const int raw = 1500 * 255 / 3300; 10 | 11 | void setup() { 12 | // Inicialización del DAC 13 | dacWrite(DAC1, raw); // Genera un voltaje de aproximadamente 1.5V en DAC1 14 | } 15 | 16 | void loop() { 17 | // No es necesario realizar nada en el bucle principal 18 | } 19 | ``` 20 | 21 | 22 | ## Generación de señales más complejas 23 | ```cpp 24 | const int tableSize = 100; 25 | const uint8_t sinTable[tableSize] = {127, 139, 150, ...}; // Valores precalculados 26 | 27 | int index = 0; 28 | 29 | void setup() { 30 | // Configuración del DAC 31 | } 32 | 33 | void loop() { 34 | // Generación de la señal 35 | dacWrite(DAC1, sinTable[index]); 36 | index = (index + 1) % tableSize; 37 | delay(10); // Controla la frecuencia de la onda 38 | } 39 | ``` 40 | 41 | 42 | -------------------------------------------------------------------------------- /03 - Hardware/05 - esp32-touch-pins.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-touch-pins/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Cómo leer los pines táctiles del ESP32 8 | ```cpp 9 | touchRead(int pin); 10 | ``` 11 | 12 | 13 | ## Lectura de pines táctiles 14 | ```cpp 15 | const int touchPin0 = 4; 16 | 17 | void setup() 18 | { 19 | Serial.begin(115200); 20 | delay(1000); 21 | Serial.println("ESP32 - Touch Raw Test"); 22 | } 23 | 24 | void loop() { 25 | auto touch0raw = touchRead(touchPin0); // realizar la lectura 26 | Serial.println(touch0raw); 27 | delay(1000); 28 | } 29 | ``` 30 | 31 | 32 | ## Lectura de pines táctiles con umbral 33 | ```cpp 34 | const int touchPin0 = 4; 35 | const int touchThreshold = 40; // Umbral del sensor 36 | 37 | void setup() { 38 | Serial.begin(115200); 39 | delay(1000); // Delay to launch the serial monitor 40 | Serial.println("ESP32 - Touch Threshold Demo"); 41 | } 42 | 43 | void loop() { 44 | auto touch0raw = touchRead(touchPin0); // realizar la lectura 45 | 46 | if(touch0raw < touchThreshold ) 47 | { 48 | Serial.println("Toque detectado"); 49 | } 50 | delay(500); 51 | } 52 | ``` 53 | 54 | 55 | ## Lectura pines táctiles con interrupciones 56 | ```cpp 57 | int threshold = 40; 58 | volatile bool touch1detected = false; 59 | 60 | void gotTouch1(){ 61 | touch1detected = true; 62 | } 63 | 64 | void setup() { 65 | Serial.begin(115200); 66 | delay(1000); // give me time to bring up serial monitor 67 | 68 | Serial.println("ESP32 Touch Interrupt Test"); 69 | touchAttachInterrupt(T2, gotTouch1, threshold); 70 | } 71 | 72 | void loop(){ 73 | if(touch1detected) 74 | { 75 | touch1detected = false; 76 | Serial.println("Touch 1 detected"); 77 | } 78 | } 79 | ``` 80 | 81 | 82 | -------------------------------------------------------------------------------- /03 - Hardware/06 - esp32-interrupciones-hardware.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-interrupciones-hardware/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Definir la función de callback (ISR) 8 | ```cpp 9 | void IRAM_ATTR miFuncionInterrupcion() { 10 | // Aquí puedes agregar el código que se ejecutará cuando ocurra la interrupción 11 | } 12 | ``` 13 | 14 | 15 | ## 3. Configurar la interrupción 16 | ```cpp 17 | const int pinInterrupcion = 12; // Pin GPIO donde configurar la interrupción 18 | 19 | void setup() { 20 | pinMode(pinInterrupcion, INPUT_PULLUP); // Configurar el pin como entrada con resistencia pull-up interna 21 | attachInterrupt(digitalPinToInterrupt(pinInterrupcion), miFuncionInterrupcion, RISING); // Configurar la interrupción 22 | } 23 | ``` 24 | 25 | 26 | ## 4. Desactivar la interrupción (opcional) 27 | ```cpp 28 | detachInterrupt(digitalPinToInterrupt(pinInterrupcion)); // Desactivar la interrupción 29 | ``` 30 | 31 | 32 | ## Ejemplo de código 33 | ```cpp 34 | const int pinBoton = 12; // Pin GPIO donde está conectado el botón 35 | 36 | volatile bool has_interruped = false; 37 | void IRAM_ATTR miFuncionInterrupcion() { 38 | has_interrupted = true; 39 | } 40 | 41 | void setup() 42 | { 43 | Serial.begin(115200); 44 | pinMode(pinBoton, INPUT_PULLUP); // Configurar el pin como entrada con resistencia pull-up interna 45 | attachInterrupt(digitalPinToInterrupt(pinBoton), miFuncionInterrupcion, RISING); // Configurar la interrupción 46 | } 47 | 48 | void loop() 49 | { 50 | if(has_interrupted) 51 | { 52 | Serial.println("¡Botón presionado!"); 53 | has_interrupted = false; 54 | } 55 | } 56 | ``` 57 | 58 | 59 | -------------------------------------------------------------------------------- /03 - Hardware/07 - esp32-timers.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-timers/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Ejemplo de código 8 | ```cpp 9 | hw_timer_t *timer = NULL; 10 | 11 | volatiel void has_expired = false; 12 | void IRAM_ATTR timerInterrupcion() { 13 | has_expired = true; 14 | } 15 | 16 | void setup() { 17 | Serial.begin(9600); 18 | pinMode(LED_BUILTIN, OUTPUT); 19 | 20 | timer = timerBegin(0, 80, true); // Timer 0, divisor de reloj 80 21 | timerAttachInterrupt(timer, &timerInterrupcion, true); // Adjuntar la función de manejo de interrupción 22 | timerAlarmWrite(timer, 1000000, true); // Interrupción cada 1 segundo 23 | timerAlarmEnable(timer); // Habilitar la alarma 24 | } 25 | 26 | void loop() { 27 | if(has_expired) 28 | { 29 | // Tareas a realizar cuando se activa la interrupción del Timer 30 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Alternar el estado del LED 31 | has_expired = false; 32 | } 33 | 34 | } 35 | ``` 36 | 37 | 38 | -------------------------------------------------------------------------------- /03 - Hardware/08 - esp32-uart.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-uart/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Cómo usar el UART en el ESP32 en el entorno de Arduino 8 | ```cpp 9 | Serial.println("¡Hola, UART!"); 10 | ``` 11 | 12 | 13 | ## Usando múltiples UART en el ESP32 14 | ```cpp 15 | include 16 | 17 | HardwareSerial MySerial(1); // definir un Serial para UART1 18 | const int MySerialRX = 16; 19 | const int MySerialTX = 17; 20 | 21 | void setup() 22 | { 23 | // inicializar el Serial a los pines 24 | MySerial.begin(11500, SERIAL_8N1, MySerialRX, MySerialTX); 25 | } 26 | 27 | void loop() 28 | { 29 | // aqui podríamos usar nuestro MySerial con normalidad 30 | while (MySerial.available() > 0) { 31 | uint8_t byteFromSerial = MySerial.read(); 32 | //y lo que sea 33 | } 34 | 35 | MySerial.write(...); 36 | } 37 | ``` 38 | 39 | 40 | -------------------------------------------------------------------------------- /03 - Hardware/09 - esp32-i2c.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-i2c/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## El bus I2C en el ESP32 8 | ```cpp 9 | void setup() { 10 | // Iniciar comunicación I2C 11 | Wire.begin(); 12 | } 13 | ``` 14 | 15 | ```cpp 16 | Wire.begin(I2C_SDA, I2C_SCL); 17 | ``` 18 | 19 | 20 | ## Enviar Datos 21 | ```cpp 22 | byte dataToSend = 0x42; // Datos a enviar 23 | byte slaveAddress = 0x50; // Dirección del dispositivo esclavo 24 | 25 | Wire.beginTransmission(slaveAddress); // Iniciar transmisión al dispositivo 26 | Wire.write(dataToSend); // Enviar datos 27 | Wire.endTransmission(); // Finalizar transmisión 28 | ``` 29 | 30 | 31 | ## Recibir Datos 32 | ```cpp 33 | byte receivedData; 34 | int bytesToRead = 1; // Cantidad de bytes a leer 35 | 36 | Wire.requestFrom(slaveAddress, bytesToRead); // Solicitar datos al dispositivo 37 | if (Wire.available()) { 38 | receivedData = Wire.read(); // Leer datos recibidos 39 | } 40 | ``` 41 | 42 | 43 | ## Usando múltiples bus I2C en el ESp32 44 | ```cpp 45 | #include 46 | 47 | // reemplazar por los pines que queráis usar 48 | const int SDA_1 = 0; 49 | const int SCL_1 = 0; 50 | const int SDA_2 = 0; 51 | const int SCL_2 = 0; 52 | 53 | TwoWire MyI2C_1 = TwoWire(0); 54 | TwoWire MyI2C_2 = TwoWire(1); 55 | 56 | void setup() { 57 | MyI2C_1.begin(SDA_1, SCL_1, freq1); 58 | MyI2C_2.begin(SDA_2, SCL_2, freq2); 59 | } 60 | 61 | void loop() 62 | { 63 | delay(1000); // do nothing 64 | } 65 | ``` 66 | 67 | 68 | -------------------------------------------------------------------------------- /03 - Hardware/10 - esp32-spi.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-spi/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## El bus SPI en el ESP32 8 | ```cpp 9 | void setup() { 10 | // Inicializar comunicación SPI 11 | SPI.begin(); 12 | } 13 | ``` 14 | 15 | ```cpp 16 | SPIClass vspi = SPIClass(VSPI); 17 | 18 | MySPI.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); 19 | ``` 20 | 21 | 22 | ## Enviar Datos 23 | ```cpp 24 | byte dataToSend = 0xAA; 25 | byte receivedData; 26 | 27 | digitalWrite(SS, LOW); // Bajar línea CS/SS para seleccionar el dispositivo 28 | receivedData = SPI.transfer(dataToSend); // Enviar y recibir datos 29 | digitalWrite(SS, HIGH); // Subir línea CS/SS para finalizar la comunicación 30 | ``` 31 | 32 | 33 | ## Usando múltiples SPI en el ESP32 34 | ```cpp 35 | #include 36 | 37 | #define HSPI 2 // 2 para S2 y S3, 1 para S1 38 | #define VSPI 3 39 | 40 | // reemplazar por los pines que queráis usar 41 | const int HSPI_MISO = 0; 42 | const int HSPI_MOSI = 0; 43 | const int HSPI_SCLK = 0; 44 | const int HSPI_SS = 0; 45 | 46 | const int VSPI_MISO = 0; 47 | const int VSPI_MOSI = 0; 48 | const int VSPI_SCLK = 0; 49 | const int VSPI_SS = 0; 50 | 51 | SPIClass vspi = SPIClass(VSPI); 52 | SPIClass hspi = SPIClass(HSPI); 53 | 54 | void setup() 55 | { 56 | vspi.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); 57 | hspi.begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); 58 | } 59 | 60 | void loop() 61 | { 62 | delay(1000); // do nothing 63 | } 64 | ``` 65 | 66 | 67 | -------------------------------------------------------------------------------- /03 - Hardware/12 - esp32-sensor-hall-integrado.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-sensor-hall-integrado/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Configuración del Sensor Hall 8 | ```cpp 9 | void setup() 10 | { 11 | Serial.begin(9600); 12 | } 13 | 14 | void loop() 15 | { 16 | // read hall effect sensor value 17 | int val = hallRead(); 18 | 19 | // print the results to the serial monitor 20 | Serial.println(val); 21 | delay(1000); 22 | } 23 | ``` 24 | 25 | 26 | -------------------------------------------------------------------------------- /03 - Hardware/13 - esp32-sensor-temperatura-integrado.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-sensor-temperatura-integrado/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Lectura de la Temperatura 8 | ```cpp 9 | void setup() { 10 | Serial.begin(115200); 11 | } 12 | 13 | void loop() { 14 | float temp_celsius = temperatureRead(); 15 | 16 | Serial.print("Temp onBoard "); 17 | Serial.print(temp_celsius); 18 | Serial.println("°C"); 19 | 20 | delay(1000); 21 | } 22 | ``` 23 | 24 | 25 | -------------------------------------------------------------------------------- /04 - Consumo energía/01 - esp32-cambiar-frecuencia-procesador.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-cambiar-frecuencia-procesador/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Cómo variar la frecuencia del CPU del ESP32 8 | ```cpp 9 | // La función acepta las siguientes frecuencias como valores válidos: 10 | // 240, 160, 80 <<< Para todos los tipos de cristal XTAL 11 | // 40, 20, 10 <<< Para XTAL de 40MHz 12 | // 26, 13 <<< Para XTAL de 26MHz 13 | // 24, 12 <<< Para XTAL de 24MHz 14 | bool setCpuFrequencyMhz(uint32_t cpu_freq_mhz); 15 | ``` 16 | 17 | ```cpp 18 | uint32_t getXtalFrequencyMhz(); // En MHz 19 | ``` 20 | 21 | ```cpp 22 | uint32_t getCpuFrequencyMhz(); // En MHz 23 | ``` 24 | 25 | 26 | ## Ejemplo de código 27 | ```cpp 28 | void setup() { 29 | Serial.begin(115200); 30 | 31 | // Establecer la frecuencia de la CPU a 80 MHz para optimización de consumo 32 | setCpuFrequencyMhz(80); 33 | 34 | // Imprimir la frecuencia del cristal XTAL 35 | Serial.print("Frecuencia del Cristal XTAL: "); 36 | Serial.print(getXtalFrequencyMhz()); 37 | Serial.println(" MHz"); 38 | 39 | // Imprimir la frecuencia de la CPU 40 | Serial.print("Frecuencia de la CPU: "); 41 | Serial.print(getCpuFrequencyMhz()); 42 | Serial.println(" MHz"); 43 | 44 | // Imprimir la frecuencia del bus APB 45 | Serial.print("Frecuencia del Bus APB: "); 46 | Serial.print(getApbFrequency()); 47 | Serial.println(" Hz"); 48 | } 49 | 50 | void loop() { 51 | // en este ejemplo aqui nada 52 | } 53 | ``` 54 | 55 | 56 | -------------------------------------------------------------------------------- /04 - Consumo energía/03 - esp32-light-sleep.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-light-sleep/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Uso del modo Light Sleep en el IDE de Arduino 8 | ```cpp 9 | /** 10 | * @brief Enter light sleep with the configured wakeup options 11 | */ 12 | esp_err_t esp_light_sleep_start(void); 13 | ``` 14 | 15 | 16 | ## Uso del modo Light Sleep 17 | ```cpp 18 | void setup() 19 | { 20 | Serial.begin(115200); 21 | delay(5000); 22 | } 23 | 24 | int counter = 0; 25 | 26 | void loop() { 27 | Serial.println(counter); 28 | counter ++; 29 | 30 | esp_sleep_enable_timer_wakeup(2 * 1000000); //light sleep durante 2 segundos 31 | esp_light_sleep_start(); 32 | } 33 | ``` 34 | 35 | 36 | -------------------------------------------------------------------------------- /05 - Programación/00 - como-programar-esp32-con-el-ide-de-arduino.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/como-programar-esp32-con-el-ide-de-arduino/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Instalar ESP32 en IDE Arduino 8 | ``` 9 | https://espressif.github.io/arduino-esp32/package_esp32_index.json 10 | ``` 11 | 12 | ``` 13 | https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json 14 | ``` 15 | 16 | 17 | ## Hola mundo en el ESP32 18 | ```cpp 19 | void setup() 20 | { 21 | pinMode(LED_BUILTIN, OUTPUT); // Configurar el pin del LED incorporado como salida 22 | } 23 | 24 | void loop() 25 | { 26 | digitalWrite(LED_BUILTIN, HIGH); // Encender el LED 27 | delay(1000); // Esperar un segundo 28 | digitalWrite(LED_BUILTIN, LOW); // Apagar el LED 29 | delay(1000); // Esperar un segundo 30 | } 31 | ``` 32 | 33 | 34 | -------------------------------------------------------------------------------- /05 - Programación/01 - guia-de-programacion-del-esp32-en-entorno-arduino.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/guia-de-programacion-del-esp32-en-entorno-arduino/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Designación de los pines 8 | ```cpp 9 | digitalWrite(14, LOW); // GPIO14 10 | ``` 11 | 12 | 13 | ## Salidas y entradas digitales (GPIO) 14 | ```cpp 15 | pinMode(pin, mode) 16 | ``` 17 | 18 | ```cpp 19 | digitalWrite(pin, output) 20 | ``` 21 | 22 | ```cpp 23 | digitalRead(pin) 24 | ``` 25 | 26 | 27 | ## Entradas analógicas (ADC) 28 | ```cpp 29 | analogRead(A0) 30 | ``` 31 | 32 | 33 | ## Interrupciones 34 | ```cpp 35 | attachInterrupt(digitalPinToInterrupt(interruptPin), handler, FALLING); 36 | ``` 37 | 38 | 39 | ## PROGMEM 40 | ```cpp 41 | string text1 = F("hello"); 42 | string text2 = F("hello"); 43 | ``` 44 | 45 | ```cpp 46 | const char hello[] PROGMEM = "hello"; 47 | string texto1 = FSPTR(hello); 48 | string texto2 = FSPTR(hello); 49 | ``` 50 | 51 | 52 | -------------------------------------------------------------------------------- /05 - Programación/04 - esp32-random-numbers.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-random-numbers/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Cómo generar números aleatorios en ESP32 8 | ```cpp 9 | void setup() 10 | { 11 | Serial.begin(115200); 12 | } 13 | 14 | void loop() 15 | { 16 | auto randomNumber = esp_random(); 17 | Serial.println(randomNumber); 18 | delay(2000); 19 | } 20 | ``` 21 | 22 | ```cpp 23 | void setup() 24 | { 25 | Serial.begin(115200); 26 | } 27 | 28 | void loop() 29 | { 30 | auto randomNumber0_10 = random(10); // numero aleatorio entre 0-10 31 | auto randomNumber10_20 = random(10, 20); // numero aleatorio entre 10-20 32 | Serial.println(randomNumber0_10); 33 | Serial.println(randomNumber10_20); 34 | 35 | delay(1000); 36 | } 37 | ``` 38 | 39 | 40 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/00_Connect_Wifi_STA/00_Connect_Wifi_STA.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "config.h" // Sustituir con datos de vuestra red 4 | #include "ESP32_Utils.hpp" 5 | 6 | void setup() 7 | { 8 | Serial.begin(115200); 9 | 10 | ConnectWiFi_STA(); 11 | } 12 | 13 | void loop() 14 | { 15 | } 16 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/00_Connect_Wifi_STA/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/00_Connect_Wifi_STA/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/01_Connect_WiFi_AP/01_Connect_WiFi_AP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "config.h" // Sustituir con datos de vuestra red 4 | #include "ESP32_Utils.hpp" 5 | 6 | void setup() 7 | { 8 | Serial.begin(115200); 9 | 10 | ConnectWiFi_AP(); 11 | } 12 | 13 | void loop() 14 | { 15 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/01_Connect_WiFi_AP/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/01_Connect_WiFi_AP/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/02_Static_IP/02_Static_IP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "config.h" // Sustituir con datos de vuestra red 4 | #include "ESP32_Utils.hpp" 5 | 6 | void setup() 7 | { 8 | Serial.begin(115200); 9 | 10 | // Para modo STA 11 | ConnectWiFi_STA(true); 12 | 13 | // Para modo AP 14 | //ConnectWiFi_AP(true); 15 | } 16 | 17 | void loop() 18 | { 19 | } 20 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/02_Static_IP/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/02_Static_IP/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/03_MDNS/03_MDNS.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "ESP32_Utils.hpp" 6 | #include "ESP32_Utils_mDNS.hpp" 7 | 8 | void setup() 9 | { 10 | Serial.begin(115200); 11 | 12 | ConnectWiFi_STA(); 13 | 14 | InitMDNS(); 15 | } 16 | 17 | void loop() 18 | { 19 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/03_MDNS/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/03_MDNS/ESP32_Utils_mDNS.hpp: -------------------------------------------------------------------------------- 1 | void InitMDNS() 2 | { 3 | if (!MDNS.begin(hostname)) 4 | { 5 | Serial.println("Error iniciando mDNS"); 6 | } 7 | Serial.println("mDNS iniciado"); 8 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/03_MDNS/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/20_UDP.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "UDP.hpp" 6 | #include "ESP32_Utils.hpp" 7 | #include "ESP32_Utils_UDP.hpp" 8 | 9 | void setup() 10 | { 11 | Serial.begin(115200); 12 | 13 | ConnectWiFi_STA(); 14 | ConnectUDP(); 15 | } 16 | 17 | void loop() 18 | { 19 | GetUDP_Packet(); 20 | 21 | //SendUDP_Packet("abcde"); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/ESP32_Utils_UDP.hpp: -------------------------------------------------------------------------------- 1 | boolean ConnectUDP() { 2 | Serial.println(); 3 | Serial.println("Starting UDP"); 4 | 5 | // in UDP error, block execution 6 | if (UDP.begin(localPort) != 1) 7 | { 8 | Serial.println("Connection failed"); 9 | while (true) { delay(1000); } 10 | } 11 | 12 | Serial.println("UDP successful"); 13 | } 14 | 15 | void SendUDP_ACK() 16 | { 17 | UDP.beginPacket(UDP.remoteIP(), remotePort); 18 | UDP.write("ACK"); 19 | UDP.endPacket(); 20 | } 21 | 22 | void SendUDP_Packet(String content) 23 | { 24 | UDP.beginPacket(UDP.remoteIP(), remotePort); 25 | UDP.write(content.c_str()); 26 | UDP.endPacket(); 27 | } 28 | 29 | void GetUDP_Packet(bool sendACK = true) 30 | { 31 | int packetSize = UDP.parsePacket(); 32 | if (packetSize) 33 | { 34 | // read the packet into packetBufffer 35 | UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 36 | 37 | Serial.println(); 38 | Serial.print("Received packet of size "); 39 | Serial.print(packetSize); 40 | Serial.print(" from "); 41 | Serial.print(UDP.remoteIP()); 42 | Serial.print(":"); 43 | Serial.println(UDP.remotePort()); 44 | Serial.print("Payload: "); 45 | Serial.write((uint8_t*)packetBuffer, (size_t)packetSize); 46 | Serial.println(); 47 | ProcessPacket(String(packetBuffer)); 48 | 49 | //// send a reply, to the IP address and port that sent us the packet we received 50 | if(sendACK) SendUDP_ACK(); 51 | } 52 | delay(10); 53 | } 54 | 55 | 56 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/Python/recieveUDP.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | UDP_PORT = 8889 4 | 5 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | sock.bind(('', UDP_PORT)) 7 | 8 | while True: 9 | data, addr = sock.recvfrom(1024) 10 | print("received message:") 11 | print(data.decode('utf-8')) -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/Python/sendUDP.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | UDP_IP = '192.168.43.237' 4 | UDP_PORT = 8888 5 | UDP_PAYLOAD = 'abcdef' 6 | 7 | def yes_or_no(question): 8 | reply = str(input(question)).lower().strip() 9 | if reply[0] == 'y': 10 | return 0 11 | elif reply[0] == 'n': 12 | return 1 13 | else: 14 | return yes_or_no("Please Enter (y/n) ") 15 | 16 | while True: 17 | try: 18 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 19 | sock.sendto(bytes(UDP_PAYLOAD, "utf-8"), (UDP_IP, UDP_PORT)) 20 | sock.close() 21 | print("UDP target IP:", UDP_IP) 22 | print("UDP target port:", UDP_PORT) 23 | print("message:", UDP_PAYLOAD) 24 | if(yes_or_no('Send again (y/n): ')): 25 | break 26 | except: 27 | pass 28 | 29 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/UDP.hpp: -------------------------------------------------------------------------------- 1 | // UDP variables 2 | WiFiUDP UDP; 3 | 4 | unsigned int localPort = 8888; 5 | unsigned int remotePort = 8889; 6 | char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, 7 | 8 | void ProcessPacket(String response) 9 | { 10 | Serial.println(response); 11 | } 12 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/04_UDP/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/05_OTA/12_OTA.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "ESP32_Utils.hpp" 6 | #include "ESP32_Utils_OTA.hpp" 7 | 8 | void setup(){ 9 | Serial.begin(115200); 10 | 11 | ConnectWiFi_STA(); 12 | 13 | InitOTA(); 14 | } 15 | 16 | void loop(){ 17 | ArduinoOTA.handle(); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/05_OTA/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/05_OTA/ESP32_Utils_OTA.hpp: -------------------------------------------------------------------------------- 1 | void InitOTA() 2 | { 3 | // Port defaults to 8266 4 | // ArduinoOTA.setPort(8266); 5 | 6 | // Hostname defaults to ESP32-[ChipID] 7 | // ArduinoOTA.setHostname(hostname); 8 | 9 | // No authentication by default 10 | // ArduinoOTA.setPassword("admin"); 11 | 12 | // Password can be set with it's md5 value as well 13 | // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 14 | // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); 15 | 16 | ArduinoOTA.onStart([]() { 17 | String type; 18 | if (ArduinoOTA.getCommand() == U_FLASH) { 19 | type = "sketch"; 20 | } else { // U_SPIFFS 21 | type = "filesystem"; 22 | } 23 | 24 | // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() 25 | Serial.println("Start updating " + type); 26 | }); 27 | 28 | ArduinoOTA.onEnd([]() { 29 | Serial.println("\nEnd"); 30 | }); 31 | 32 | ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { 33 | Serial.printf("Progress: %u%%\r", (progress / (total / 100))); 34 | }); 35 | 36 | ArduinoOTA.onError([](ota_error_t error) { 37 | Serial.printf("Error[%u]: ", error); 38 | if (error == OTA_AUTH_ERROR) { 39 | Serial.println("Auth Failed"); 40 | } else if (error == OTA_BEGIN_ERROR) { 41 | Serial.println("Begin Failed"); 42 | } else if (error == OTA_CONNECT_ERROR) { 43 | Serial.println("Connect Failed"); 44 | } else if (error == OTA_RECEIVE_ERROR) { 45 | Serial.println("Receive Failed"); 46 | } else if (error == OTA_END_ERROR) { 47 | Serial.println("End Failed"); 48 | } 49 | }); 50 | 51 | ArduinoOTA.begin(); 52 | Serial.println(""); 53 | Serial.println("OTA iniciado"); 54 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/05_OTA/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/04_Client/04_Client.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | String url = "http://www.google.com"; 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "Client.hpp" 8 | #include "ESP32_Utils.hpp" 9 | #include "ESP32_Utils_Client.hpp" 10 | 11 | void setup() 12 | { 13 | Serial.begin(115200); 14 | 15 | ConnectWiFi_STA(); 16 | } 17 | 18 | void loop() 19 | { 20 | ClientRequest(); 21 | 22 | delay(30000); 23 | } 24 | -------------------------------------------------------------------------------- /07 - Servidor/04_Client/Client.hpp: -------------------------------------------------------------------------------- 1 | void ProcessResponse(String response) 2 | { 3 | Serial.println(response); // Mostrar respuesta por serial 4 | } 5 | -------------------------------------------------------------------------------- /07 - Servidor/04_Client/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/04_Client/ESP32_Utils_Client.hpp: -------------------------------------------------------------------------------- 1 | //void ClientRequest() 2 | //{ 3 | // HTTPClient http; 4 | // WiFiClient client; 5 | // 6 | // if (http.begin(client, url)) //Iniciar conexión 7 | // { 8 | // Serial.print("[HTTP] GET...\n"); 9 | // int httpCode = http.GET(); // Realizar petición 10 | // 11 | // if (httpCode > 0) { 12 | // Serial.printf("[HTTP] GET... code: %d\n", httpCode); 13 | // 14 | // if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { 15 | // String payload = http.getString(); // Obtener respuesta 16 | // ProcessResponse(payload); 17 | // } 18 | // } 19 | // else { 20 | // Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); 21 | // } 22 | // 23 | // http.end(); 24 | // } 25 | // else { 26 | // Serial.printf("[HTTP} Unable to connect\n"); 27 | // } 28 | // 29 | //} 30 | -------------------------------------------------------------------------------- /07 - Servidor/04_Client/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/05_Server_Simple/05_Server_Simple.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "Server.hpp" 6 | #include "ESP32_Utils.hpp" 7 | 8 | void setup(void) 9 | { 10 | Serial.begin(115200); 11 | 12 | ConnectWiFi_STA(); 13 | 14 | InitServer(); 15 | } 16 | 17 | void loop() 18 | { 19 | server.handleClient(); 20 | } 21 | -------------------------------------------------------------------------------- /07 - Servidor/05_Server_Simple/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/05_Server_Simple/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | // Funcion que se ejecutara en la URI '/' 4 | void handleRoot() 5 | { 6 | server.send(200, "text/plain", "Hola mundo!"); 7 | } 8 | 9 | // Funcion que se ejecutara en URI desconocida 10 | void handleNotFound() 11 | { 12 | server.send(404, "text/plain", "Not found"); 13 | } 14 | 15 | void InitServer() 16 | { 17 | // Ruteo para '/' 18 | server.on("/", handleRoot); 19 | 20 | // Ruteo para '/inline' usando función lambda 21 | server.on("/inline", []() { 22 | server.send(200, "text/plain", "Esto tambien funciona"); 23 | }); 24 | 25 | // Ruteo para URI desconocida 26 | server.onNotFound(handleNotFound); 27 | 28 | // Iniciar servidor 29 | server.begin(); 30 | Serial.println("HTTP server started"); 31 | } 32 | -------------------------------------------------------------------------------- /07 - Servidor/05_Server_Simple/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/06_Server_Parameters/06_Server_Parameters.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "Server.hpp" 6 | #include "ESP32_Utils.hpp" 7 | 8 | void setup(void) 9 | { 10 | Serial.begin(115200); 11 | 12 | ConnectWiFi_STA(); 13 | 14 | InitServer(); 15 | } 16 | 17 | void loop() 18 | { 19 | server.handleClient(); 20 | } 21 | -------------------------------------------------------------------------------- /07 - Servidor/06_Server_Parameters/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/06_Server_Parameters/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | // Funcion al recibir petición GET 4 | void getLED() 5 | { 6 | // devolver respuesta 7 | server.send(200, "text/plain", String("GET ") + server.arg(String("Id"))); 8 | } 9 | 10 | // Funcion al recibir petición POST 11 | void setLED() 12 | { 13 | // mostrar por puerto serie 14 | Serial.println(server.argName(0)); 15 | 16 | // devolver respuesta 17 | server.send(200, "text/plain", String("POST ") + server.arg(String("Id")) + " " + server.arg(String("Status"))); 18 | } 19 | 20 | // Funcion que se ejecutara en la URI '/' 21 | void handleRoot() 22 | { 23 | server.send(200, "text/plain", "Hola mundo!"); 24 | } 25 | 26 | void handleNotFound() 27 | { 28 | server.send(404, "text/plain", "Not found"); 29 | } 30 | 31 | void InitServer() 32 | { 33 | // Ruteo para '/' 34 | server.on("/", handleRoot); 35 | 36 | // Definimos dos routeos 37 | server.on("/led", HTTP_GET, getLED); 38 | server.on("/led", HTTP_POST, setLED); 39 | 40 | server.onNotFound(handleNotFound); 41 | 42 | server.begin(); 43 | Serial.println("HTTP server started"); 44 | } -------------------------------------------------------------------------------- /07 - Servidor/06_Server_Parameters/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/07_Server_Dynamic_Content/07_Server_Dynamic_Content.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool ledStatus = false; // Variable de ejemplo 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "Server.hpp" 8 | #include "ESP32_Utils.hpp" 9 | 10 | void setup(void) 11 | { 12 | Serial.begin(115200); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop() 20 | { 21 | server.handleClient(); 22 | } 23 | -------------------------------------------------------------------------------- /07 - Servidor/07_Server_Dynamic_Content/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/07_Server_Dynamic_Content/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | void handleRoot() { 4 | String response = "LED: "; 5 | response.concat(ledStatus ? "OFF" : "ON"); 6 | 7 | server.send(200, "text/plain", response); 8 | } 9 | 10 | void handleNotFound() { 11 | server.send(404, "text/plain", "Not found"); 12 | } 13 | 14 | void InitServer() 15 | { 16 | server.on("/", handleRoot); 17 | server.onNotFound(handleNotFound); 18 | server.begin(); 19 | Serial.println("HTTP server started"); 20 | } -------------------------------------------------------------------------------- /07 - Servidor/07_Server_Dynamic_Content/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/08_Server_from_Flash/08_Server_from_Flash.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool ledStatus = false; // Variable de ejemplo 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "index.h" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | 15 | ConnectWiFi_STA(); 16 | 17 | InitServer(); 18 | } 19 | 20 | void loop() 21 | { 22 | server.handleClient(); 23 | } 24 | -------------------------------------------------------------------------------- /07 - Servidor/08_Server_from_Flash/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/08_Server_from_Flash/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | // Ejemplo 1 4 | /*void handleRoot() { 5 | String response = MAIN_page; 6 | server.send(200, "text/html", MAIN_page); 7 | }*/ 8 | 9 | // Ejemplo 2 10 | void handleRoot() { 11 | String response = ledStatus ? "ON" : "OFF"; 12 | 13 | server.setContentLength(sizeof(HTML_PART_1) + sizeof(response) + sizeof(HTML_PART_2)); 14 | server.send(200, "text/html", HTML_PART_1); 15 | server.sendContent(response); 16 | server.sendContent(HTML_PART_2); 17 | } 18 | 19 | void handleNotFound() { 20 | server.send(404, "text/plain", "Not found"); 21 | } 22 | 23 | void InitServer() 24 | { 25 | server.on("/", handleRoot); 26 | server.onNotFound(handleNotFound); 27 | server.begin(); 28 | Serial.println("HTTP server started"); 29 | } 30 | -------------------------------------------------------------------------------- /07 - Servidor/08_Server_from_Flash/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/08_Server_from_Flash/index.h: -------------------------------------------------------------------------------- 1 | const char MAIN_page[] PROGMEM = R"=====( 2 | 3 | 4 | 5 | 6 |
7 |

Hola mundo!

8 |
9 | 10 | 11 | 12 | )====="; 13 | 14 | 15 | const char HTML_PART_1[] PROGMEM = R"=====( 16 | 17 | 18 | 19 | 20 |
21 |

Hola mundo!

22 |

LED: 23 | )====="; 24 | 25 | 26 | const char HTML_PART_2[] PROGMEM = R"=====( 27 |

28 |
29 | 30 | 31 | 32 | )====="; 33 | 34 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/09_Server_SPIFFS.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // Include the SPIFFS library 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | server.handleClient(); 22 | } 23 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | #include "ESP32_Utils_Server.hpp" 4 | 5 | void handleNotFound() { 6 | server.send(404, "text/plain", "Not found"); 7 | } 8 | 9 | void InitServer() 10 | { 11 | server.onNotFound([]() { // If the client requests any URI 12 | if (!HandleFileRead(server.uri())) // send it if it exists 13 | handleNotFound(); // otherwise, respond with a 404 (Not Found) error 14 | }); 15 | 16 | server.begin(); 17 | Serial.println("HTTP server started"); 18 | } 19 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ¡Hola mundo! 7 | 8 | 9 | 10 | 11 | 12 |

Esta es tu página web. ¡Enhorabuena!

13 | 14 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/10_Server_SPIFFS_Gzip.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include // Include the SPIFFS library 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | server.handleClient(); 22 | } 23 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/Server.hpp: -------------------------------------------------------------------------------- 1 | WebServer server(80); 2 | 3 | #include "ESP32_Utils_Server.hpp" 4 | 5 | void handleNotFound() { 6 | server.send(404, "text/plain", "Not found"); 7 | } 8 | 9 | void InitServer() 10 | { 11 | server.onNotFound([]() { // If the client requests any URI 12 | if (!HandleFileReadGzip(server.uri())) // send it if it exists 13 | handleNotFound(); // otherwise, respond with a 404 (Not Found) error 14 | }); 15 | 16 | server.begin(); 17 | Serial.println("HTTP server started"); 18 | } 19 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/data/index.html.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/10_Server_SPIFFS_Gzip/data/index.html.gz -------------------------------------------------------------------------------- /07 - Servidor/11_AsyncServer/11_AsyncServer.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } -------------------------------------------------------------------------------- /07 - Servidor/11_AsyncServer/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/11_AsyncServer/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request) { 8 | request->send(200, "text/plain", "Hola mundo (desde ESP32)"); 9 | }); 10 | 11 | server.onNotFound([](AsyncWebServerRequest *request) { 12 | request->send(400, "text/plain", "Not found"); 13 | }); 14 | 15 | server.begin(); 16 | Serial.println("HTTP server started"); 17 | } -------------------------------------------------------------------------------- /07 - Servidor/11_AsyncServer/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/11_AsyncServer/data/index.html.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/11_AsyncServer/data/index.html.gz -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/23_MaterialDesign.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/23_MaterialDesign/data/favicon.ico -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/23_MaterialDesign/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/material.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/23_MaterialDesign/data/vendor/material.css.gz -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/material.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/07 - Servidor/23_MaterialDesign/data/vendor/material.js.gz -------------------------------------------------------------------------------- /08 - Formularios Web/13_Forms_Text/13_Forms_Text.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /08 - Formularios Web/13_Forms_Text/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /08 - Formularios Web/13_Forms_Text/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void handleFormText(AsyncWebServerRequest *request) 4 | { 5 | String MiText = request->arg("miText"); 6 | 7 | Serial.print("MiText:\t"); 8 | Serial.println(MiText); 9 | 10 | //String response = " Go Back "; 11 | //request->send(200, "text/html", response); 12 | 13 | request->redirect("/"); 14 | } 15 | 16 | void InitServer() 17 | { 18 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 19 | 20 | server.on("/SetText", HTTP_POST, handleFormText); 21 | 22 | server.onNotFound([](AsyncWebServerRequest *request) { 23 | request->send(400, "text/plain", "Not found"); 24 | }); 25 | 26 | server.begin(); 27 | Serial.println("HTTP server started"); 28 | } 29 | -------------------------------------------------------------------------------- /08 - Formularios Web/13_Forms_Text/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /08 - Formularios Web/13_Forms_Text/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Forms 7 | 8 | 9 | 10 | 11 | 12 |

Form example

13 |
14 | Texto:
15 |
16 | 17 |
18 | 19 | -------------------------------------------------------------------------------- /08 - Formularios Web/14_Forms_LED/14_Forms_LED.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | const int LED_PIN = 0; // Sustituir con datos de vuestra placa 10 | 11 | void setup(void) 12 | { 13 | pinMode(LED_PIN, OUTPUT); 14 | Serial.begin(115200); 15 | SPIFFS.begin(); 16 | 17 | ConnectWiFi_STA(); 18 | 19 | InitServer(); 20 | } 21 | 22 | void loop(void) 23 | { 24 | } 25 | -------------------------------------------------------------------------------- /08 - Formularios Web/14_Forms_LED/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /08 - Formularios Web/14_Forms_LED/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void handleFormLed(AsyncWebServerRequest *request) 4 | { 5 | String ledStatus = request->arg("status"); 6 | 7 | Serial.print("Status:\t"); 8 | Serial.println(ledStatus); 9 | 10 | digitalWrite(LED_PIN, ledStatus == "ON" ? LOW : HIGH); 11 | 12 | request->redirect("/"); 13 | } 14 | 15 | void InitServer() 16 | { 17 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 18 | 19 | server.on("/LED", HTTP_POST, handleFormLed); 20 | 21 | server.onNotFound([](AsyncWebServerRequest *request) { 22 | request->send(400, "text/plain", "Not found"); 23 | }); 24 | 25 | server.begin(); 26 | Serial.println("HTTP server started"); 27 | } 28 | -------------------------------------------------------------------------------- /08 - Formularios Web/14_Forms_LED/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /08 - Formularios Web/14_Forms_LED/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Forms 7 | 8 | 9 | 10 | 11 | 12 |

Form example

13 |
14 | Radio:
15 | 16 |
17 | 18 |
19 | 20 |
21 | 22 | -------------------------------------------------------------------------------- /08 - Formularios Web/15_Forms_LED_PWM/15_Forms_LED_PWM.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int LED_PIN = 0; 6 | const int freq = 5000; 7 | const int ledChannel = 0; 8 | const int resolution = 8; 9 | 10 | #include "config.h" // Sustituir con datos de vuestra red 11 | #include "Server.hpp" 12 | #include "ESP32_Utils.hpp" 13 | 14 | void setup(void) 15 | { 16 | ledcSetup(ledChannel, freq, resolution); 17 | ledcAttachPin(LED_PIN, ledChannel); 18 | 19 | Serial.begin(115200); 20 | SPIFFS.begin(); 21 | 22 | ConnectWiFi_STA(); 23 | 24 | InitServer(); 25 | } 26 | 27 | void loop(void) 28 | { 29 | } 30 | -------------------------------------------------------------------------------- /08 - Formularios Web/15_Forms_LED_PWM/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /08 - Formularios Web/15_Forms_LED_PWM/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void handleFormPWM(AsyncWebServerRequest *request) 4 | { 5 | String pwmValue = request->arg("pwmValue"); 6 | 7 | Serial.print("PWM:\t"); 8 | Serial.println(pwmValue); 9 | 10 | ledcWrite(ledChannel, 1023 - pwmValue.toInt()); 11 | 12 | request->redirect("/"); 13 | } 14 | 15 | void InitServer() 16 | { 17 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 18 | 19 | server.on("/LED", HTTP_POST, handleFormPWM); 20 | 21 | server.onNotFound([](AsyncWebServerRequest *request) { 22 | request->send(400, "text/plain", "Not found"); 23 | }); 24 | 25 | server.begin(); 26 | Serial.println("HTTP server started"); 27 | } 28 | -------------------------------------------------------------------------------- /08 - Formularios Web/15_Forms_LED_PWM/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /08 - Formularios Web/15_Forms_LED_PWM/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Forms 7 | 8 | 9 | 10 | 11 | 12 |

Form example

13 |
14 | Slider:
15 | 16 | 17 |
18 | 19 |
20 | 27 | 28 | -------------------------------------------------------------------------------- /08 - Formularios Web/16_Forms_RGB/16_Forms_RGB.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "ColorConverterLib.h" 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "Server.hpp" 8 | #include "ESP32_Utils.hpp" 9 | 10 | void setup(void) 11 | { 12 | Serial.begin(115200); 13 | SPIFFS.begin(); 14 | 15 | ConnectWiFi_STA(); 16 | 17 | InitServer(); 18 | } 19 | 20 | void loop(void) 21 | { 22 | } 23 | -------------------------------------------------------------------------------- /08 - Formularios Web/16_Forms_RGB/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /08 - Formularios Web/16_Forms_RGB/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void handleFormLed(AsyncWebServerRequest *request) 4 | { 5 | String rgb = request->arg("rgb"); 6 | 7 | uint8_t red = 0; 8 | uint8_t green = 0; 9 | uint8_t blue = 0; 10 | 11 | ColorConverter::HexToRgb(rgb, red, green, blue); 12 | 13 | Serial.println(red); 14 | Serial.println(green); 15 | Serial.println(blue); 16 | 17 | request->redirect("/"); 18 | } 19 | 20 | void InitServer() 21 | { 22 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 23 | 24 | server.on("/RGB", HTTP_POST, handleFormLed); 25 | 26 | server.onNotFound([](AsyncWebServerRequest *request) { 27 | request->send(400, "text/plain", "Not found"); 28 | }); 29 | 30 | server.begin(); 31 | Serial.println("HTTP server started"); 32 | } -------------------------------------------------------------------------------- /08 - Formularios Web/16_Forms_RGB/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /08 - Formularios Web/16_Forms_RGB/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Forms 7 | 8 | 9 | 10 | 11 | 12 |

Form example

13 |
14 | Color:
15 |
16 | 17 |
18 | 19 | -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/17_Ajax.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | String GetMillis() 4 | { 5 | return String(millis(), DEC); 6 | } 7 | 8 | void InitServer() 9 | { 10 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 11 | 12 | server.on("/GetMillis", HTTP_GET, [](AsyncWebServerRequest *request) { 13 | request->send(200, "text/plain", GetMillis()); 14 | }); 15 | 16 | server.onNotFound([](AsyncWebServerRequest *request) { 17 | request->send(400, "text/plain", "Not found"); 18 | }); 19 | 20 | server.begin(); 21 | Serial.println("HTTP server started"); 22 | } -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Ajax 7 | 8 | 9 | 10 | 11 | 12 |

Millis

13 |
---
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /09 - API Rest/17_Ajax/data/js/main.js: -------------------------------------------------------------------------------- 1 | var myDiv = document.getElementById('counterDiv'); 2 | 3 | function updateCounterUI(counter) 4 | { 5 | myDiv.innerHTML = counter; 6 | } 7 | 8 | function ajaxCall() { 9 | var xmlhttp = new XMLHttpRequest(); 10 | 11 | xmlhttp.onreadystatechange = function() { 12 | if (xmlhttp.readyState == XMLHttpRequest.DONE) { 13 | if (xmlhttp.status == 200) { 14 | updateCounterUI(xmlhttp.responseText); 15 | } 16 | else { 17 | console.log('error', xmlhttp); 18 | } 19 | } 20 | }; 21 | 22 | xmlhttp.open("GET", "GetMillis", true); 23 | xmlhttp.send(); 24 | } 25 | 26 | (function scheduleAjax() { 27 | ajaxCall(); 28 | setTimeout(scheduleAjax, 1000); 29 | })(); 30 | -------------------------------------------------------------------------------- /09 - API Rest/21_API_REST_Client/21_API_REST_Client.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "API.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup() 10 | { 11 | Serial.begin(115200); 12 | 13 | ConnectWiFi_STA(); 14 | 15 | GetAll(); 16 | GetItem(1); 17 | GetQuery("ABC"); 18 | Create("New item"); 19 | ReplaceById(2, "New item"); 20 | UpdateById(2, "New item"); 21 | DeleteById(5); 22 | } 23 | 24 | void loop() 25 | { 26 | } 27 | -------------------------------------------------------------------------------- /09 - API Rest/21_API_REST_Client/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /09 - API Rest/21_API_REST_Client/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /09 - API Rest/22_API_REST_Server/22_API_REST_Server.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup() 12 | { 13 | Serial.begin(115200); 14 | 15 | ConnectWiFi_STA(); 16 | 17 | InitServer(); 18 | } 19 | 20 | void loop() 21 | { 22 | } 23 | 24 | -------------------------------------------------------------------------------- /09 - API Rest/22_API_REST_Server/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /09 - API Rest/22_API_REST_Server/ESP32_Utils_APIREST.hpp: -------------------------------------------------------------------------------- 1 | int GetIdFromURL(AsyncWebServerRequest *request, String root) 2 | { 3 | String string_id = request->url(); 4 | string_id.replace(root, ""); 5 | int id = string_id.toInt(); 6 | return id; 7 | } 8 | 9 | String GetBodyContent(uint8_t *data, size_t len) 10 | { 11 | String content = ""; 12 | for (size_t i = 0; i < len; i++) { 13 | content .concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | -------------------------------------------------------------------------------- /09 - API Rest/22_API_REST_Server/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void homeRequest(AsyncWebServerRequest *request) { 4 | request->send(200, "text/plain", "Hello, world"); 5 | } 6 | 7 | void notFound(AsyncWebServerRequest *request) { 8 | request->send(404, "text/plain", "Not found"); 9 | } 10 | 11 | void InitServer() 12 | { 13 | server.on("/", HTTP_GET, homeRequest); 14 | server.on("/item", HTTP_GET, getRequest); 15 | server.on("/item", HTTP_POST, [](AsyncWebServerRequest * request){}, NULL, postRequest); 16 | server.on("/item", HTTP_PUT, [](AsyncWebServerRequest * request){}, NULL, putRequest); 17 | server.on("/item", HTTP_PATCH, [](AsyncWebServerRequest * request){}, NULL, patchRequest); 18 | server.on("/item", HTTP_DELETE, deleteRequest); 19 | 20 | server.onNotFound(notFound); 21 | 22 | server.begin(); 23 | Serial.println("HTTP server started"); 24 | } -------------------------------------------------------------------------------- /09 - API Rest/22_API_REST_Server/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/24_Json_Ajax.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitServer(); 19 | } 20 | 21 | void loop(void) 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/API.hpp: -------------------------------------------------------------------------------- 1 | #include "ESP32_Utils_APIREST.hpp" 2 | 3 | void getData(AsyncWebServerRequest *request) 4 | { 5 | AsyncResponseStream *response = request->beginResponseStream("application/json"); 6 | 7 | // obtendríamos datos de GPIO, estado... 8 | StaticJsonDocument<300> jsonDoc; 9 | jsonDoc["id"] = random(0,10); 10 | jsonDoc["status"] = random(0,2); 11 | serializeJson(jsonDoc, *response); 12 | 13 | request->send(response); 14 | } 15 | 16 | void setData(AsyncWebServerRequest * request, uint8_t *data, size_t len, size_t index, size_t total) 17 | { 18 | String bodyContent = GetBodyContent(data, len); 19 | 20 | StaticJsonDocument<200> doc; 21 | DeserializationError error = deserializeJson(doc, bodyContent); 22 | if (error) { request->send(400); return;} 23 | 24 | int id = doc["id"]; 25 | bool ledStatus = doc["status"]; 26 | Serial.println(id); 27 | Serial.println(ledStatus); 28 | // hacer acciones con los datos recibidos 29 | 30 | request->send(200); 31 | } 32 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/ESP32_Utils_APIREST.hpp: -------------------------------------------------------------------------------- 1 | int GetIdFromURL(AsyncWebServerRequest *request, String root) 2 | { 3 | String string_id = request->url(); 4 | string_id.replace(root, ""); 5 | int id = string_id.toInt(); 6 | return id; 7 | } 8 | 9 | String GetBodyContent(uint8_t *data, size_t len) 10 | { 11 | String content = ""; 12 | for (size_t i = 0; i < len; i++) { 13 | content .concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.on("/LED", HTTP_GET, getData); 8 | server.on("/LED", HTTP_POST, [](AsyncWebServerRequest * request){}, NULL, setData); 9 | 10 | server.onNotFound([](AsyncWebServerRequest *request) { 11 | request->send(400, "text/plain", "Not found"); 12 | }); 13 | 14 | server.begin(); 15 | Serial.println("HTTP server started"); 16 | } 17 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Json Ajax 7 | 8 | 9 | 10 | 11 | 12 |
13 | Radio:
14 |
15 | 16 |
17 | 18 |
19 | 20 |
21 |
22 |
23 | 24 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /09 - API Rest/24_Json_Ajax/data/js/main.js: -------------------------------------------------------------------------------- 1 | function sendData() 2 | { 3 | var xhttp = new XMLHttpRequest(); 4 | 5 | let ledNumber = document.getElementById('ledNumber'); 6 | let ledStatus = document.querySelector('input[name="status"]:checked'); 7 | let ledData = { 8 | id: ledNumber.value, 9 | status: ledStatus.value 10 | } 11 | let data = JSON.stringify(ledData); 12 | 13 | xhttp.addEventListener('load', function(event) { 14 | console.log('OK', xhttp); 15 | }); 16 | 17 | xhttp.addEventListener('error', function(event) { 18 | console.log('error', xhttp); 19 | }); 20 | 21 | xhttp.open('POST', 'LED'); 22 | xhttp.setRequestHeader('Content-Type', 'application/json'); 23 | xhttp.send(data); 24 | } 25 | 26 | function getData() 27 | { 28 | var xhttp = new XMLHttpRequest(); 29 | 30 | xhttp.onreadystatechange = function() { 31 | if (xhttp.readyState == XMLHttpRequest.DONE) { 32 | if (xhttp.status == 200) { 33 | console.log((xhttp.responseText)); 34 | let json = JSON.parse(xhttp.responseText); 35 | console.log(); 36 | 37 | let receivedMsg = 'Received: GPIO ' + json.id + ' ' + (json.status == 1 ? "ON" : "OFF"); 38 | document.getElementById('receivedText').textContent = receivedMsg; 39 | } 40 | else { 41 | console.log('error', xhttp); 42 | } 43 | } 44 | }; 45 | 46 | xhttp.open("GET", "LED", true); 47 | xhttp.send(); 48 | } -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/29_Axios.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitServer(); 19 | } 20 | 21 | void loop(void) 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/ESP32_Utils_APIREST.hpp: -------------------------------------------------------------------------------- 1 | int GetIdFromURL(AsyncWebServerRequest *request, String root) 2 | { 3 | String string_id = request->url(); 4 | string_id.replace(root, ""); 5 | int id = string_id.toInt(); 6 | return id; 7 | } 8 | 9 | String GetBodyContent(uint8_t *data, size_t len) 10 | { 11 | String content = ""; 12 | for (size_t i = 0; i < len; i++) { 13 | content .concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void notFound(AsyncWebServerRequest *request) { 4 | request->send(404, "text/plain", "Not found"); 5 | } 6 | 7 | void InitServer() 8 | { 9 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 10 | 11 | server.on("/item", HTTP_GET, getRequest); 12 | server.on("/item", HTTP_POST, [](AsyncWebServerRequest * request){}, NULL, postRequest); 13 | server.on("/item", HTTP_PUT, [](AsyncWebServerRequest * request){}, NULL, putRequest); 14 | server.on("/item", HTTP_PATCH, [](AsyncWebServerRequest * request){}, NULL, patchRequest); 15 | server.on("/item", HTTP_DELETE, deleteRequest); 16 | 17 | server.onNotFound(notFound); 18 | 19 | server.begin(); 20 | Serial.println("HTTP server started"); 21 | } -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/09 - API Rest/29_Axios/data/favicon.ico -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP32 Axios 5 | 6 | 7 | 8 | 9 |
10 |
11 | 12 | 13 | 14 |
15 |
16 | 17 |
18 |
19 | 20 |
21 |
22 | 23 |
24 |
25 | 26 |
27 |
28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/09 - API Rest/29_Axios/data/vendor/axios.min.js.gz -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/18_Websocket.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "WebSockets.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | #include "ESP32_Utils_WS.hpp" 11 | 12 | void setup(void) 13 | { 14 | Serial.begin(115200); 15 | SPIFFS.begin(); 16 | 17 | ConnectWiFi_STA(); 18 | 19 | InitWebSockets(); 20 | InitServer(); 21 | } 22 | 23 | void loop(void) 24 | { 25 | webSocket.loop(); 26 | 27 | // Ejemplo 2, llamada desde servidor 28 | String message = GetMillis(); 29 | webSocket.broadcastTXT(message); 30 | } 31 | -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/ESP32_Utils_WS.hpp: -------------------------------------------------------------------------------- 1 | WebSocketsServer webSocket = WebSocketsServer(81); 2 | 3 | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) 4 | { 5 | switch(type) { 6 | case WStype_DISCONNECTED: 7 | break; 8 | case WStype_CONNECTED: 9 | //IPAddress ip = webSocket.remoteIP(num); 10 | //webSocket.sendTXT(num, "Connected"); 11 | break; 12 | case WStype_TEXT: 13 | String response = ProcessRequest(); 14 | webSocket.sendTXT(num, response); 15 | break; 16 | } 17 | } 18 | 19 | void InitWebSockets() { 20 | webSocket.begin(); 21 | webSocket.onEvent(webSocketEvent); 22 | Serial.println("WebSocket server started"); 23 | } -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/WebSockets.hpp: -------------------------------------------------------------------------------- 1 | String GetMillis() 2 | { 3 | return String(millis(), DEC); 4 | } 5 | 6 | String ProcessRequest() 7 | { 8 | return GetMillis(); 9 | } 10 | -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 WebSockets 7 | 8 | 9 | 10 | 11 | 12 |

Millis

13 |
---
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /10 - Websockets/18_Websocket/data/js/main.js: -------------------------------------------------------------------------------- 1 | var counterDiv = document.getElementById('counterDiv'); 2 | 3 | function updateCounterUI(counter) 4 | { 5 | counterDiv.innerHTML = counter; 6 | } 7 | 8 | var connection = new WebSocket('ws://' + location.hostname + ':81/', ['arduino']); 9 | 10 | connection.onopen = function () { 11 | console.log('Connected: '); 12 | 13 | // Ejemplo 1, peticion desde cliente 14 | //(function scheduleRequest() { 15 | // connection.send(""); 16 | // setTimeout(scheduleRequest, 100); 17 | //})(); 18 | }; 19 | 20 | connection.onerror = function (error) { 21 | console.log('WebSocket Error ', error); 22 | }; 23 | 24 | connection.onmessage = function (e) { 25 | updateCounterUI(e.data); 26 | console.log('Server: ', e.data); 27 | }; 28 | 29 | connection.onclose = function () { 30 | console.log('WebSocket connection closed'); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/19_AsyncWebsocket.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Websocket.hpp" 7 | #include "Server.hpp" 8 | #include "ESP32_Utils.hpp" 9 | #include "ESP32_Utils_AWS.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitWebSockets(); 19 | InitServer(); 20 | } 21 | 22 | void loop(void) 23 | { 24 | // Ejemplo 2, llamada desde servidor 25 | ws.textAll(GetMillis()); 26 | delay(100); 27 | } 28 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/Websocket.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | String GetMillis() 4 | { 5 | return String(millis(), DEC); 6 | } 7 | 8 | void ProcessRequest(AsyncWebSocketClient * client, String request) 9 | { 10 | String response = GetMillis(); 11 | client->text(response); 12 | } 13 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Websocket Async 7 | 8 | 9 | 10 | 11 | 12 |

Millis

13 |
---
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/data/js/main.js: -------------------------------------------------------------------------------- 1 | var myDiv = document.getElementById('counterDiv'); 2 | 3 | function updateCounterUI(counter) 4 | { 5 | myDiv.innerHTML = counter; 6 | } 7 | 8 | var connection = new WebSocket('ws://' + location.hostname + '/ws', ['arduino']); 9 | 10 | connection.onopen = function () { 11 | console.log('Connected: '); 12 | 13 | // Ejemplo 1, peticion desde cliente 14 | //(function scheduleRequest() { 15 | // connection.send(""); 16 | // setTimeout(scheduleRequest, 100); 17 | //})(); 18 | }; 19 | 20 | connection.onerror = function (error) { 21 | console.log('WebSocket Error ', error); 22 | }; 23 | 24 | connection.onmessage = function (e) { 25 | updateCounterUI(e.data); 26 | console.log('Server: ', e.data); 27 | }; 28 | 29 | connection.onclose = function () { 30 | console.log('WebSocket connection closed'); 31 | }; 32 | 33 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/25_Json_AsyncWebsocket.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "config.h" // Sustituir con datos de vuestra red 8 | #include "API.hpp" 9 | #include "WebSockets.hpp" 10 | #include "Server.hpp" 11 | #include "ESP32_Utils.hpp" 12 | #include "ESP32_Utils_AWS.hpp" 13 | 14 | void setup(void) 15 | { 16 | Serial.begin(115200); 17 | SPIFFS.begin(); 18 | 19 | ConnectWiFi_STA(); 20 | 21 | InitServer(); 22 | InitWebSockets(); 23 | } 24 | 25 | void loop(void) 26 | { 27 | } 28 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/API.hpp: -------------------------------------------------------------------------------- 1 | void setData(AsyncWebSocketClient * client, String request) 2 | { 3 | StaticJsonDocument<200> doc; 4 | DeserializationError error = deserializeJson(doc, request); 5 | if (error) { return; } 6 | 7 | int id = doc["id"]; 8 | bool ledStatus = doc["status"]; 9 | Serial.println(id); 10 | Serial.println(ledStatus); 11 | } 12 | 13 | void getData(AsyncWebSocketClient * client, String request) 14 | { 15 | String response; 16 | StaticJsonDocument<300> jsonDoc; 17 | jsonDoc["id"] = random(0,10); 18 | jsonDoc["status"] = random(0,2); 19 | serializeJson(jsonDoc, response); 20 | 21 | client->text(response); 22 | } 23 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/WebSockets.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | void ProcessRequest(AsyncWebSocketClient * client, String request) 4 | { 5 | Serial.println(request); 6 | StaticJsonDocument<200> doc; 7 | DeserializationError error = deserializeJson(doc, request); 8 | if (error) { return; } 9 | 10 | String command = doc["command"]; 11 | if(command == "Set") 12 | { 13 | setData(client, request); 14 | } 15 | if(command == "Get") 16 | { 17 | getData(client, request); 18 | } 19 | } -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ESP32 Json Websocket 7 | 8 | 9 | 10 | 11 | 12 |
13 | Radio:
14 |
15 | 16 |
17 | 18 |
19 | 20 |
21 |
22 |
23 | 24 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /10 - Websockets/25_Json_AsyncWebsocket/data/js/main.js: -------------------------------------------------------------------------------- 1 | var connection = new WebSocket('ws://' + location.hostname + '/ws', ['arduino']); 2 | 3 | connection.onopen = function () { 4 | console.log('Connected: '); 5 | }; 6 | 7 | connection.onerror = function (error) { 8 | console.log('WebSocket Error ', error); 9 | }; 10 | 11 | connection.onmessage = function (e) { 12 | console.log('Server: ', e.data); 13 | processData(e.data); 14 | }; 15 | 16 | connection.onclose = function () { 17 | console.log('WebSocket connection closed'); 18 | }; 19 | 20 | function sendData() 21 | { 22 | let ledNumber = document.getElementById('ledNumber'); 23 | let ledStatus = document.querySelector('input[name="status"]:checked'); 24 | 25 | let data = { 26 | command : "Set", 27 | id: ledNumber.value, 28 | status: ledStatus.value 29 | } 30 | let json = JSON.stringify(data); 31 | 32 | connection.send(json); 33 | } 34 | 35 | function getData() 36 | { 37 | let data = { 38 | command : "Get" 39 | } 40 | let json = JSON.stringify(data); 41 | 42 | connection.send(json); 43 | } 44 | 45 | function processData(data) 46 | { 47 | let json = JSON.parse(data); 48 | console.log(json); 49 | let receivedMsg = 'Received: GPIO ' + json.id + ' ' + (json.status == 1 ? "ON" : "OFF"); 50 | document.getElementById('receivedText').textContent = receivedMsg; 51 | } -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/26_Json_AsyncWebsocket_GPIO.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "WebSockets.hpp" 9 | #include "Server.hpp" 10 | #include "ESP32_Utils.hpp" 11 | #include "ESP32_Utils_AWS.hpp" 12 | #include "ReactiveArduinoLib.h" 13 | 14 | auto obsD0 = Reactive::FromDigitalInput(D0); 15 | auto obsD5 = Reactive::FromDigitalInput(D5); 16 | auto obsD6 = Reactive::FromDigitalInput(D6); 17 | auto obsD7 = Reactive::FromDigitalInput(D7); 18 | 19 | 20 | void setup(void) 21 | { 22 | Serial.begin(115200); 23 | SPIFFS.begin(); 24 | 25 | ConnectWiFi_STA(); 26 | 27 | InitServer(); 28 | InitWebSockets(); 29 | 30 | obsD0.Distinct().Do([](int i) { updateGPIO("D0", i); }); 31 | obsD5.Distinct().Do([](int i) { updateGPIO("D5", i); }); 32 | obsD6.Distinct().Do([](int i) { updateGPIO("D6", i); }); 33 | obsD7.Distinct().Do([](int i) { updateGPIO("D7", i); }); 34 | } 35 | 36 | void loop(void) 37 | { 38 | obsD0.Next(); 39 | obsD5.Next(); 40 | obsD6.Next(); 41 | obsD7.Next(); 42 | } 43 | -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/API.hpp: -------------------------------------------------------------------------------- 1 | void setGPIO(String id, bool state) 2 | { 3 | Serial.print("Set GPIO "); 4 | Serial.print(id); 5 | Serial.print(": "); 6 | Serial.println(state); 7 | } 8 | 9 | void setPWM(String id, int pwm) 10 | { 11 | Serial.print("Set PWM "); 12 | Serial.print(id); 13 | Serial.print(": "); 14 | Serial.println(pwm); 15 | } 16 | 17 | void doAction(String actionId) 18 | { 19 | Serial.print("Doing action: "); 20 | Serial.println(actionId); 21 | } 22 | -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/WebSockets.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | void ProcessRequest(AsyncWebSocketClient *client, String request) 4 | { 5 | StaticJsonDocument<200> doc; 6 | DeserializationError error = deserializeJson(doc, request); 7 | if (error) { return; } 8 | 9 | String command = doc["command"]; 10 | if(command == "setGPIO") 11 | setGPIO(doc["id"], (bool)doc["status"]); 12 | else if(command == "setPWM") 13 | setPWM(doc["id"], (int)doc["pwm"]); 14 | else if(command == "doAction") 15 | doAction(doc["id"]); 16 | } 17 | 18 | void updateGPIO(String input, bool value) 19 | { 20 | String response; 21 | StaticJsonDocument<300> doc; 22 | doc["command"] = "updateGPIO"; 23 | doc["id"] = input; 24 | doc["status"] = value ? String("ON") : String("OFF"); 25 | serializeJson(doc, response); 26 | 27 | ws.textAll(response); 28 | 29 | Serial.print(input); 30 | Serial.println(value ? String(" ON") : String(" OFF")); 31 | } 32 | -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/css/main.css: -------------------------------------------------------------------------------- 1 | .label-big { 2 | border-radius: 3px; 3 | font-size: 16px; 4 | font-weight: 600; 5 | line-height: 2; 6 | padding: 0 8px; 7 | transition: opacity .2s linear; 8 | color: #fff 9 | } 10 | 11 | .On-style { 12 | background-color: #006b75; 13 | } 14 | 15 | .Off-style { 16 | background-color: #b60205; 17 | } -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/material.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/material.css.gz -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/material.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/material.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/27_VueJs.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/27_VueJs/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ESP32 VueJS 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
    16 | 17 | 18 |
19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/data/js/app.js: -------------------------------------------------------------------------------- 1 | // Definicion de un componente 2 | Vue.component('todo-item', { 3 | props: ['todo'], 4 | template: '
  • {{ todo.text }} \ 5 | \ 6 | {{ todo.hidden }} \ 7 |
  • ' 8 | }) 9 | 10 | // Definicion de nuestra app de ejemplo 11 | var app = new Vue({ 12 | el: '#app', 13 | data: { 14 | myList: [ 15 | { id: 0, text: 'Item1', hidden: 'Hidden1' }, 16 | { id: 1, text: 'Item2', hidden: 'Hidden2' }, 17 | { id: 2, text: 'Item3', hidden: 'Hidden3' } 18 | ] 19 | } 20 | }) -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/27_VueJs/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/27_VueJs/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/28_Vuetify.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP32_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | Serial.begin(115200); 12 | SPIFFS.begin(); 13 | 14 | ConnectWiFi_STA(); 15 | 16 | InitServer(); 17 | } 18 | 19 | void loop(void) 20 | { 21 | } 22 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/28_Vuetify/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/js/app.js: -------------------------------------------------------------------------------- 1 | new Vue({ 2 | el: '#app', 3 | data: () => ({ 4 | tasks: [ 5 | { 6 | done: false, 7 | text: 'Foobar' 8 | }, 9 | { 10 | done: false, 11 | text: 'Fizzbuzz' 12 | } 13 | ], 14 | task: null 15 | }), 16 | 17 | computed: { 18 | completedTasks () { 19 | return this.tasks.filter(task => task.done).length 20 | }, 21 | progress () { 22 | return this.completedTasks / this.tasks.length * 100 23 | }, 24 | remainingTasks () { 25 | return this.tasks.length - this.completedTasks 26 | } 27 | }, 28 | 29 | methods: { 30 | create () { 31 | this.tasks.push({ 32 | done: false, 33 | text: this.task 34 | }) 35 | 36 | this.task = null 37 | } 38 | } 39 | }) -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/28_Vuetify/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/28_Vuetify/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/28_Vuetify/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/28_Vuetify/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/28_Vuetify/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/30_Vue_Axios.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitServer(); 19 | } 20 | 21 | void loop(void) 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/ESP32_Utils_APIREST.hpp: -------------------------------------------------------------------------------- 1 | int GetIdFromURL(AsyncWebServerRequest *request, String root) 2 | { 3 | String string_id = request->url(); 4 | string_id.replace(root, ""); 5 | int id = string_id.toInt(); 6 | return id; 7 | } 8 | 9 | String GetBodyContent(uint8_t *data, size_t len) 10 | { 11 | String content = ""; 12 | for (size_t i = 0; i < len; i++) { 13 | content .concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void notFound(AsyncWebServerRequest *request) { 4 | request->send(404, "text/plain", "Not found"); 5 | } 6 | 7 | void InitServer() 8 | { 9 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 10 | 11 | server.on("/item", HTTP_GET, getRequest); 12 | server.on("/item", HTTP_POST, [](AsyncWebServerRequest * request){}, NULL, postRequest); 13 | server.on("/item", HTTP_PUT, [](AsyncWebServerRequest * request){}, NULL, putRequest); 14 | server.on("/item", HTTP_PATCH, [](AsyncWebServerRequest * request){}, NULL, patchRequest); 15 | server.on("/item", HTTP_DELETE, deleteRequest); 16 | 17 | server.onNotFound(notFound); 18 | 19 | server.begin(); 20 | Serial.println("HTTP server started"); 21 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/30_Vue_Axios/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP32 Vue + Axios 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
    13 |
    14 | 15 |
    16 |
    17 | 18 | 19 |
    20 |
    21 | 22 | 23 |
    24 |
    25 | 26 |
    27 |
    28 | 29 |
    30 |
    31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/data/js/app.js: -------------------------------------------------------------------------------- 1 | Vue.component('my-component', { 2 | props: ['my'], 3 | template: '
    {{my.text}}
    ', 4 | methods: { 5 | get: function (id) { 6 | API_get(id); 7 | }, 8 | 9 | update: function (id) { 10 | API_update(id, 'NewItem'); 11 | }, 12 | 13 | replac: function (id) { 14 | API_replace(id, 'NewItem'); 15 | }, 16 | 17 | delet: function (id) { 18 | API_delete(id); 19 | } 20 | } 21 | }) 22 | 23 | var app = new Vue({ 24 | el: '#app', 25 | data: { 26 | filter: "", 27 | getId: "", 28 | items: [ 29 | { id: 0, text: 'Item1'}, 30 | { id: 1, text: 'Item2' }, 31 | { id: 2, text: 'Item3'} 32 | ] 33 | }, 34 | 35 | methods: { 36 | getAll() { 37 | API_getAll(); 38 | }, 39 | 40 | get() { 41 | API_get(this.$data.getId); 42 | }, 43 | 44 | 45 | getFiltered() { 46 | API_getFiltered(this.$data.filter); 47 | }, 48 | 49 | create() { 50 | API_create('NewItem'); 51 | }, 52 | } 53 | }) 54 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/30_Vue_Axios/data/vendor/axios.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/30_Vue_Axios/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/30_Vue_Axios/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/31_Vuetify_Axios.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "Server.hpp" 9 | #include "ESP32_Utils.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitServer(); 19 | } 20 | 21 | void loop(void) 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/ESP32_Utils_APIREST.hpp: -------------------------------------------------------------------------------- 1 | int GetIdFromURL(AsyncWebServerRequest *request, String root) 2 | { 3 | String string_id = request->url(); 4 | string_id.replace(root, ""); 5 | int id = string_id.toInt(); 6 | return id; 7 | } 8 | 9 | String GetBodyContent(uint8_t *data, size_t len) 10 | { 11 | String content = ""; 12 | for (size_t i = 0; i < len; i++) { 13 | content .concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void notFound(AsyncWebServerRequest *request) { 4 | request->send(404, "text/plain", "Not found"); 5 | } 6 | 7 | void InitServer() 8 | { 9 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 10 | 11 | server.on("/item", HTTP_GET, getRequest); 12 | server.on("/item", HTTP_POST, [](AsyncWebServerRequest * request){}, NULL, postRequest); 13 | server.on("/item", HTTP_PUT, [](AsyncWebServerRequest * request){}, NULL, putRequest); 14 | server.on("/item", HTTP_PATCH, [](AsyncWebServerRequest * request){}, NULL, patchRequest); 15 | server.on("/item", HTTP_DELETE, deleteRequest); 16 | 17 | server.onNotFound(notFound); 18 | 19 | server.begin(); 20 | Serial.println("HTTP server started"); 21 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/js/app.js: -------------------------------------------------------------------------------- 1 | new Vue({ 2 | el: '#app', 3 | 4 | data: () => ({ 5 | items: [ 6 | { 7 | id: 0, 8 | done: false, 9 | text: 'Init 0' 10 | } 11 | ], 12 | 13 | newItem: null, 14 | getId: null, 15 | updateId: null, 16 | updateText: null, 17 | 18 | snackbar: false, 19 | timeout: 1500, 20 | text: '', 21 | 22 | lastId : 0, 23 | }), 24 | 25 | computed: { 26 | }, 27 | 28 | methods: { 29 | create () { 30 | lastId = Math.max(...this.items.map(d => d.id)); 31 | this.items.push({ 32 | id: ++this.lastId, 33 | done: false, 34 | text: this.newItem 35 | }) 36 | 37 | API_post(this.newItem) 38 | this.newItem = null 39 | }, 40 | getAll() { 41 | this.text = "Getting all" 42 | this.snackbar = true 43 | 44 | API_getAll() 45 | 46 | lastId = Math.max(...this.items.map(d => d.id)); 47 | this.items.push({id: ++this.lastId, text: 'Loaded all '}) 48 | this.items.push({id: ++this.lastId, text: 'Loaded all '}) 49 | }, 50 | getById() { 51 | if(this.getId == null) return; 52 | if(((typeof this.items.find(x => x.id === this.getId) !== 'undefined'))) return; 53 | 54 | this.text = "Getting by Id " + this.getId 55 | this.snackbar = true 56 | 57 | API_get(this.getId) 58 | 59 | this.items.push({id: this.getId, text: 'Loaded by Id'}) 60 | }, 61 | updateById(id) { 62 | if(this.updateId == null || this.updateId == null) return; 63 | 64 | this.text = "Updating " + this.updateId + " with " + this.updateText 65 | this.snackbar = true 66 | 67 | API_put(this.updateId, this.updateText) 68 | 69 | this.items.find(x => x.id === this.updateId).text = this.updateText 70 | }, 71 | remove(item) { 72 | this.text = "Deleting " + item.id 73 | this.snackbar = true 74 | 75 | API_delete(item.id) 76 | 77 | this.items.splice(this.items.indexOf(item), 1) 78 | } 79 | } 80 | }) -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/axios.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/32_Vue_NativeWebsocket.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Websockets.hpp" 7 | #include "Server.hpp" 8 | #include "ESP32_Utils.hpp" 9 | #include "ESP32_Utils_AWS.hpp" 10 | 11 | void setup(void) 12 | { 13 | Serial.begin(115200); 14 | SPIFFS.begin(); 15 | 16 | ConnectWiFi_STA(); 17 | 18 | InitWebSockets(); 19 | InitServer(); 20 | } 21 | 22 | void loop(void) 23 | { 24 | // Ejemplo 2, llamada desde servidor 25 | ws.textAll(GetMillis()); 26 | delay(100); 27 | } 28 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/Websockets.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | String GetMillis() 4 | { 5 | return String(millis(), DEC); 6 | } 7 | 8 | void ProcessRequest(AsyncWebSocketClient * client, String request) 9 | { 10 | String response = GetMillis(); 11 | client->text(response); 12 | } 13 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP32 Vue + Axios 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
    13 |
    This is a counter {{counter}}
    14 |
    15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/js/app.js: -------------------------------------------------------------------------------- 1 | Vue.use(VueNativeSock.default, 'ws://' + location.hostname + '/ws') 2 | 3 | var app = new Vue({ 4 | el: '#app', 5 | data : function() { 6 | return { 7 | counter: 0 8 | }; 9 | }, 10 | mounted() { 11 | this.$socket.onmessage = (dr) => { 12 | console.log(dr.data); 13 | this.$set(this, 'counter', dr.data); 14 | } 15 | } 16 | }) 17 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/32_Vue_NativeWebsocket/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/33_Vue_NativeWebsocket_GPIO.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "WebSockets.hpp" 9 | #include "Server.hpp" 10 | #include "ESP32_Utils.hpp" 11 | #include "ESP32_Utils_AWS.hpp" 12 | #include "ReactiveArduinoLib.h" 13 | 14 | auto obsD0 = Reactive::FromDigitalInput(D0); 15 | auto obsD5 = Reactive::FromDigitalInput(D5); 16 | auto obsD6 = Reactive::FromDigitalInput(D6); 17 | auto obsD7 = Reactive::FromDigitalInput(D7); 18 | 19 | void setup(void) 20 | { 21 | Serial.begin(115200); 22 | SPIFFS.begin(); 23 | 24 | ConnectWiFi_STA(); 25 | 26 | InitServer(); 27 | InitWebSockets(); 28 | 29 | obsD0.Distinct().Do([](int i) { updateGPIO("D0", i); }); 30 | obsD5.Distinct().Do([](int i) { updateGPIO("D5", i); }); 31 | obsD6.Distinct().Do([](int i) { updateGPIO("D6", i); }); 32 | obsD7.Distinct().Do([](int i) { updateGPIO("D7", i); }); 33 | } 34 | 35 | void loop(void) 36 | { 37 | obsD0.Next(); 38 | obsD5.Next(); 39 | obsD6.Next(); 40 | obsD7.Next(); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/API.hpp: -------------------------------------------------------------------------------- 1 | void setGPIO(String id, bool state) 2 | { 3 | Serial.print("Set GPIO "); 4 | Serial.print(id); 5 | Serial.print(": "); 6 | Serial.println(state); 7 | } 8 | 9 | void setPWM(String id, int pwm) 10 | { 11 | Serial.print("Set PWM "); 12 | Serial.print(id); 13 | Serial.print(": "); 14 | Serial.println(pwm); 15 | } 16 | 17 | void doAction(String actionId) 18 | { 19 | Serial.print("Doing action: "); 20 | Serial.println(actionId); 21 | } 22 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/Websockets.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | void ProcessRequest(AsyncWebSocketClient *client, String request) 4 | { 5 | StaticJsonDocument<200> doc; 6 | DeserializationError error = deserializeJson(doc, request); 7 | if (error) { return; } 8 | 9 | String command = doc["command"]; 10 | if(command == "setGPIO") 11 | setGPIO(doc["id"], (bool)doc["status"]); 12 | else if(command == "setPWM") 13 | setPWM(doc["id"], (int)doc["pwm"]); 14 | else if(command == "doAction") 15 | doAction(doc["id"]); 16 | } 17 | 18 | void updateGPIO(String input, bool value) 19 | { 20 | String response; 21 | StaticJsonDocument<300> doc; 22 | doc["command"] = "updateGPIO"; 23 | doc["id"] = input; 24 | doc["status"] = value; 25 | serializeJson(doc, response); 26 | 27 | ws.textAll(response); 28 | 29 | Serial.print(input); 30 | Serial.println(value ? String(" ON") : String(" OFF")); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/css/main.css: -------------------------------------------------------------------------------- 1 | .label-big { 2 | border-radius: 3px; 3 | font-size: 16px; 4 | font-weight: 600; 5 | line-height: 2; 6 | padding: 0 8px; 7 | transition: opacity .2s linear; 8 | color: #fff 9 | } 10 | 11 | .On-style { 12 | background-color: #006b75; 13 | } 14 | 15 | .Off-style { 16 | background-color: #b60205; 17 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/34_Vuetify_NativeWebsocket_GPIO.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "API.hpp" 8 | #include "WebSockets.hpp" 9 | #include "Server.hpp" 10 | #include "ESP32_Utils.hpp" 11 | #include "ESP32_Utils_AWS.hpp" 12 | #include "ReactiveArduinoLib.h" 13 | 14 | auto obsD0 = Reactive::FromDigitalInput(D0); 15 | auto obsD5 = Reactive::FromDigitalInput(D5); 16 | auto obsD6 = Reactive::FromDigitalInput(D6); 17 | auto obsD7 = Reactive::FromDigitalInput(D7); 18 | 19 | void setup(void) 20 | { 21 | Serial.begin(115200); 22 | SPIFFS.begin(); 23 | 24 | ConnectWiFi_STA(); 25 | 26 | InitServer(); 27 | InitWebSockets(); 28 | 29 | obsD0.Distinct().Do([](int i) { updateGPIO("D0", i); }); 30 | obsD5.Distinct().Do([](int i) { updateGPIO("D5", i); }); 31 | obsD6.Distinct().Do([](int i) { updateGPIO("D6", i); }); 32 | obsD7.Distinct().Do([](int i) { updateGPIO("D7", i); }); 33 | } 34 | 35 | void loop(void) 36 | { 37 | obsD0.Next(); 38 | obsD5.Next(); 39 | obsD6.Next(); 40 | obsD7.Next(); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/API.hpp: -------------------------------------------------------------------------------- 1 | void setGPIO(String id, bool state) 2 | { 3 | Serial.print("Set GPIO "); 4 | Serial.print(id); 5 | Serial.print(": "); 6 | Serial.println(state); 7 | } 8 | 9 | void setPWM(String id, int pwm) 10 | { 11 | Serial.print("Set PWM "); 12 | Serial.print(id); 13 | Serial.print(": "); 14 | Serial.println(pwm); 15 | } 16 | 17 | void doAction(String actionId) 18 | { 19 | Serial.print("Doing action: "); 20 | Serial.println(actionId); 21 | } 22 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/Websockets.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebSocket ws("/ws"); 2 | 3 | void ProcessRequest(AsyncWebSocketClient *client, String request) 4 | { 5 | StaticJsonDocument<200> doc; 6 | DeserializationError error = deserializeJson(doc, request); 7 | if (error) { return; } 8 | 9 | String command = doc["command"]; 10 | if(command == "setGPIO") 11 | setGPIO(doc["id"], (bool)doc["status"]); 12 | else if(command == "setPWM") 13 | setPWM(doc["id"], (int)doc["pwm"]); 14 | else if(command == "doAction") 15 | doAction(doc["id"]); 16 | } 17 | 18 | 19 | void updateGPIO(String input, bool value) 20 | { 21 | String response; 22 | StaticJsonDocument<300> doc; 23 | doc["command"] = "updateGPIO"; 24 | doc["id"] = input; 25 | doc["status"] = value; 26 | serializeJson(doc, response); 27 | 28 | ws.textAll(response); 29 | 30 | Serial.print(input); 31 | Serial.println(value ? String(" ON") : String(" OFF")); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/css/main.css: -------------------------------------------------------------------------------- 1 | .label-big { 2 | border-radius: 3px; 3 | font-size: 16px; 4 | font-weight: 600; 5 | line-height: 2; 6 | padding: 0 8px; 7 | transition: opacity .2s linear; 8 | color: #fff 9 | } 10 | 11 | .On-style { 12 | background-color: #006b75; 13 | } 14 | 15 | .Off-style { 16 | background-color: #b60205; 17 | } -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/11 - ESP32 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/35_Mqtt/35_Mqtt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "MQTT.hpp" 7 | #include "ESP32_Utils.hpp" 8 | #include "ESP32_Utils_MQTT.hpp" 9 | 10 | void setup(void) 11 | { 12 | Serial.begin(115200); 13 | SPIFFS.begin(); 14 | 15 | ConnectWiFi_STA(true); 16 | 17 | InitMqtt(); 18 | } 19 | 20 | void loop() 21 | { 22 | HandleMqtt(); 23 | 24 | PublisMqtt(millis()); 25 | 26 | delay(1000); 27 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/35_Mqtt/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/35_Mqtt/ESP32_Utils_MQTT.hpp: -------------------------------------------------------------------------------- 1 | 2 | void InitMqtt() 3 | { 4 | mqttClient.setServer(MQTT_BROKER_ADRESS, MQTT_PORT); 5 | SuscribeMqtt(); 6 | mqttClient.setCallback(OnMqttReceived); 7 | } 8 | 9 | 10 | void ConnectMqtt() 11 | { 12 | while (!mqttClient.connected()) 13 | { 14 | Serial.print("Starting MQTT connection..."); 15 | if (mqttClient.connect(MQTT_CLIENT_NAME)) 16 | { 17 | SuscribeMqtt(); 18 | } 19 | else 20 | { 21 | Serial.print("Failed MQTT connection, rc="); 22 | Serial.print(mqttClient.state()); 23 | Serial.println(" try again in 5 seconds"); 24 | 25 | delay(5000); 26 | } 27 | } 28 | } 29 | 30 | 31 | void HandleMqtt() 32 | { 33 | if (!mqttClient.connected()) 34 | { 35 | ConnectMqtt(); 36 | } 37 | mqttClient.loop(); 38 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/35_Mqtt/MQTT.hpp: -------------------------------------------------------------------------------- 1 | 2 | const char* MQTT_BROKER_ADRESS = "192.168.1.150"; 3 | const uint16_t MQTT_PORT = 1883; 4 | const char* MQTT_CLIENT_NAME = "ESP32Client_1"; 5 | 6 | WiFiClient espClient; 7 | PubSubClient mqttClient(espClient); 8 | 9 | void SuscribeMqtt() 10 | { 11 | mqttClient.subscribe("hello/world"); 12 | } 13 | 14 | String payload; 15 | void PublisMqtt(unsigned long data) 16 | { 17 | payload = ""; 18 | payload = String(data); 19 | mqttClient.publish("hello/world", (char*)payload.c_str()); 20 | } 21 | 22 | String content = ""; 23 | void OnMqttReceived(char* topic, byte* payload, unsigned int length) 24 | { 25 | Serial.print("Received on "); 26 | Serial.print(topic); 27 | Serial.print(": "); 28 | 29 | content = ""; 30 | for (size_t i = 0; i < length; i++) { 31 | content.concat((char)payload[i]); 32 | } 33 | Serial.print(content); 34 | Serial.println(); 35 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/35_Mqtt/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/36_Async Mqtt/36_Mqtt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "MQTT.hpp" 7 | #include "ESP32_Utils.hpp" 8 | #include "ESP32_Utils_MQTT_Async.hpp" 9 | 10 | void setup() 11 | { 12 | Serial.begin(115200); 13 | 14 | delay(500); 15 | 16 | WiFi.onEvent(WiFiEvent); 17 | InitMqtt(); 18 | 19 | ConnectWiFi_STA(); 20 | } 21 | 22 | void loop() 23 | { 24 | delay(1000); 25 | 26 | PublishMqtt(millis()); 27 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/36_Async Mqtt/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/36_Async Mqtt/MQTT.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const IPAddress MQTT_HOST(192, 168, 1, 150); 4 | const int MQTT_PORT = 1883; 5 | 6 | AsyncMqttClient mqttClient; 7 | 8 | String GetPayloadContent(char* data, size_t len) 9 | { 10 | String content = ""; 11 | for(size_t i = 0; i < len; i++) 12 | { 13 | content.concat(data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | uint16_t packetIdSub = mqttClient.subscribe("hello/world", 0); 21 | Serial.print("Subscribing at QoS 2, packetId: "); 22 | Serial.println(packetIdSub); 23 | } 24 | 25 | String payload; 26 | void PublishMqtt(unsigned long data) 27 | { 28 | String payload = String(data); 29 | mqttClient.publish("hello/world", 0, true, (char*)payload.c_str()); 30 | } 31 | 32 | void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) 33 | { 34 | Serial.print("Received on "); 35 | Serial.print(topic); 36 | Serial.print(": "); 37 | 38 | String content = GetPayloadContent(payload, len); 39 | Serial.print(content); 40 | Serial.println(); 41 | } 42 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/36_Async Mqtt/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/37_Mqtt_Json/37_Mqtt_Json.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "MQTT.hpp" 7 | #include "ESP32_Utils.hpp" 8 | #include "ESP32_Utils_MQTT_Async.hpp" 9 | 10 | void setup(void) 11 | { 12 | Serial.begin(115200); 13 | 14 | delay(500); 15 | 16 | WiFi.onEvent(WiFiEvent); 17 | InitMqtt(); 18 | 19 | ConnectWiFi_STA(); 20 | } 21 | 22 | void loop() 23 | { 24 | PublishMqtt(); 25 | 26 | delay(500); 27 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/37_Mqtt_Json/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/37_Mqtt_Json/MQTT.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const IPAddress MQTT_HOST(192, 168, 1, 150); 4 | const int MQTT_PORT = 1883; 5 | 6 | AsyncMqttClient mqttClient; 7 | 8 | String GetPayloadContent(char* data, size_t len) 9 | { 10 | String content = ""; 11 | for(size_t i = 0; i < len; i++) 12 | { 13 | content.concat(data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | uint16_t packetIdSub = mqttClient.subscribe("hello/world", 0); 21 | Serial.print("Subscribing at QoS 2, packetId: "); 22 | Serial.println(packetIdSub); 23 | } 24 | 25 | void PublishMqtt() 26 | { 27 | String payload = ""; 28 | 29 | StaticJsonDocument<300> jsonDoc; 30 | jsonDoc["data"] = millis(); 31 | serializeJson(jsonDoc, payload); 32 | 33 | mqttClient.publish("hello/world", 0, true, (char*)payload.c_str()); 34 | } 35 | 36 | void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) 37 | { 38 | Serial.print("Received on "); 39 | Serial.print(topic); 40 | Serial.print(": "); 41 | 42 | String content = GetPayloadContent(payload, len); 43 | 44 | StaticJsonDocument<200> doc; 45 | DeserializationError error = deserializeJson(doc, content); 46 | if(error) return; 47 | 48 | unsigned long data = doc["data"]; 49 | Serial.print("Millis:"); 50 | Serial.println(data); 51 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/37_Mqtt_Json/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/38_Mqtt_Json_web.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "config.h" // Sustituir con datos de vuestra red 8 | #include "Server.hpp" 9 | #include "MQTT.hpp" 10 | #include "ESP32_Utils.hpp" 11 | #include "ESP32_Utils_MQTT_Async.hpp" 12 | 13 | void setup(void) 14 | { 15 | Serial.begin(115200); 16 | SPIFFS.begin(); 17 | 18 | delay(500); 19 | 20 | WiFi.onEvent(WiFiEvent); 21 | InitMqtt(); 22 | 23 | ConnectWiFi_STA(); 24 | InitServer(); 25 | } 26 | 27 | void loop() 28 | { 29 | PublishMqtt(); 30 | 31 | delay(1000); 32 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/MQTT.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const IPAddress MQTT_HOST(192, 168, 1, 150); 4 | const int MQTT_PORT = 1883; 5 | 6 | AsyncMqttClient mqttClient; 7 | 8 | String GetPayloadContent(char* data, size_t len) 9 | { 10 | String content = ""; 11 | for(size_t i = 0; i < len; i++) 12 | { 13 | content.concat(data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | uint16_t packetIdSub = mqttClient.subscribe("hello/world", 0); 21 | Serial.print("Subscribing at QoS 2, packetId: "); 22 | Serial.println(packetIdSub); 23 | } 24 | 25 | void PublishMqtt() 26 | { 27 | String payload = ""; 28 | 29 | StaticJsonDocument<300> jsonDoc; 30 | jsonDoc["data"] = millis(); 31 | serializeJson(jsonDoc, payload); 32 | 33 | mqttClient.publish("hello/world", 0, true, (char*)payload.c_str()); 34 | } 35 | 36 | void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) 37 | { 38 | Serial.print("Received on "); 39 | Serial.print(topic); 40 | Serial.print(": "); 41 | 42 | String content = GetPayloadContent(payload, len); 43 | 44 | StaticJsonDocument<200> doc; 45 | DeserializationError error = deserializeJson(doc, content); 46 | if(error) return; 47 | 48 | unsigned long data = doc["data"]; 49 | Serial.print("Millis:"); 50 | Serial.println(data); 51 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/38_Mqtt_Json_web/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ESP32 MQTT 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
    15 |
    16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/js/API.js: -------------------------------------------------------------------------------- 1 | function onConnect() { 2 | 3 | var options = { 4 | qos: 0, 5 | onSuccess: onSubSuccess, 6 | onFailure: onSubFailure 7 | }; 8 | client.subscribe('hello/world', options); 9 | } 10 | 11 | function onFailure(message) { 12 | console.log(message) 13 | } 14 | 15 | function onConnectionLost(responseObject) { 16 | if (responseObject.errorCode !== 0) { 17 | console.log("onConnectionLost:" + responseObject.errorMessage); 18 | } 19 | } 20 | 21 | function onMessageArrived(message) { 22 | console.log(message) 23 | var topic = message.destinationName; 24 | var payload = message.payloadString; 25 | 26 | let json = JSON.parse(payload); 27 | 28 | var element = document.getElementById('app'); 29 | var newElement = document.createElement('div'); 30 | newElement.appendChild(document.createTextNode(json.data)); 31 | element.appendChild(newElement); 32 | } 33 | 34 | function onSubFailure(message) { 35 | console.log(message) 36 | } 37 | 38 | function onSubSuccess(message) { 39 | console.log(message) 40 | } 41 | 42 | function createGuid() { 43 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { 44 | var r = Math.random() * 16 | 0, 45 | v = c === 'x' ? r : (r & 0x3 | 0x8); 46 | return v.toString(16); 47 | }); 48 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/js/app.js: -------------------------------------------------------------------------------- 1 | client = new Paho.MQTT.Client("192.168.1.150", 9001, createGuid()) 2 | 3 | var options = { 4 | onSuccess: onConnect, 5 | onFailure: onFailure 6 | }; 7 | 8 | client.onConnectionLost = onConnectionLost; 9 | client.onMessageArrived = onMessageArrived; 10 | 11 | client.connect(options); -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/38_Mqtt_Json_web/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/38_Mqtt_Json_web/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/38_Mqtt_Json_web/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/39_Mqtt_Json_Vue.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "config.h" // Sustituir con datos de vuestra red 8 | #include "Server.hpp" 9 | #include "MQTT.hpp" 10 | #include "ESP32_Utils.hpp" 11 | #include "ESP32_Utils_MQTT_Async.hpp" 12 | 13 | void setup(void) 14 | { 15 | Serial.begin(115200); 16 | SPIFFS.begin(); 17 | 18 | delay(500); 19 | 20 | WiFi.onEvent(WiFiEvent); 21 | InitMqtt(); 22 | 23 | ConnectWiFi_STA(); 24 | InitServer(); 25 | } 26 | 27 | void loop() 28 | { 29 | PublishMqtt(); 30 | 31 | delay(1000); 32 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/MQTT.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const IPAddress MQTT_HOST(192, 168, 1, 150); 4 | const int MQTT_PORT = 1883; 5 | 6 | AsyncMqttClient mqttClient; 7 | 8 | String GetPayloadContent(char* data, size_t len) 9 | { 10 | String content = ""; 11 | for(size_t i = 0; i < len; i++) 12 | { 13 | content.concat(data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | uint16_t packetIdSub = mqttClient.subscribe("hello/world", 0); 21 | Serial.print("Subscribing at QoS 2, packetId: "); 22 | Serial.println(packetIdSub); 23 | } 24 | 25 | void PublishMqtt() 26 | { 27 | String payload = ""; 28 | 29 | // obtendriamos datos de GPIO, estado... 30 | StaticJsonDocument<300> jsonDoc; 31 | jsonDoc["id"] = random(0, 10); 32 | jsonDoc["status"] = random(0, 2); 33 | serializeJson(jsonDoc, payload); 34 | 35 | mqttClient.publish("hello/world", 0, true, (char*)payload.c_str()); 36 | } 37 | 38 | void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) 39 | { 40 | Serial.print("Received on "); 41 | Serial.print(topic); 42 | Serial.print(": "); 43 | 44 | String content = GetPayloadContent(payload, len); 45 | 46 | StaticJsonDocument<200> doc; 47 | DeserializationError error = deserializeJson(doc, content); 48 | if(error) return; 49 | 50 | int id = doc["id"]; 51 | bool ledStatus = doc["status"]; 52 | 53 | Serial.print(" Id:"); 54 | Serial.print(id); 55 | Serial.print(" Status:"); 56 | Serial.println(ledStatus); 57 | } 58 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/css/main.css: -------------------------------------------------------------------------------- 1 | .label-big { 2 | border-radius: 3px; 3 | font-size: 16px; 4 | font-weight: 600; 5 | line-height: 2; 6 | padding: 0 8px; 7 | transition: opacity .2s linear; 8 | color: #fff 9 | } 10 | 11 | .On-style { 12 | background-color: #006b75; 13 | } 14 | 15 | .Off-style { 16 | background-color: #b60205; 17 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ESP32 MQTT 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | ESP32 MQTT 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Received 28 | 29 | 30 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/js/API.js: -------------------------------------------------------------------------------- 1 | function onConnect() { 2 | 3 | var options = { 4 | qos: 0, 5 | onSuccess: onSubSuccess, 6 | onFailure: onSubFailure 7 | }; 8 | client.subscribe('hello/world', options); 9 | } 10 | 11 | function onFailure(message) { 12 | console.log(message) 13 | } 14 | 15 | function onConnectionLost(responseObject) { 16 | if (responseObject.errorCode !== 0) { 17 | console.log("onConnectionLost:" + responseObject.errorMessage); 18 | } 19 | } 20 | 21 | function onMessageArrived(message) { 22 | console.log(message) 23 | var topic = message.destinationName; 24 | var payload = message.payloadString; 25 | 26 | let json = JSON.parse(payload); 27 | 28 | var mqtt_message = new Object(); 29 | mqtt_message.id = json.id; 30 | mqtt_message.status = json.status; 31 | mqtt_message.date = new Date().toISOString().replace("T", " ").replace("Z", " "); 32 | app.mqtt_message_list.unshift(mqtt_message); 33 | } 34 | 35 | function onSubFailure(message) { 36 | console.log(message) 37 | } 38 | 39 | function onSubSuccess(message) { 40 | console.log(message) 41 | } 42 | 43 | function createGuid() { 44 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { 45 | var r = Math.random() * 16 | 0, 46 | v = c === 'x' ? r : (r & 0x3 | 0x8); 47 | return v.toString(16); 48 | }); 49 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/js/app.js: -------------------------------------------------------------------------------- 1 | Vue.component('mqtt-message', { 2 | props: ['mqtt_message'], 3 | template: ` 4 | 8 | 9 | 10 | {{mqtt_message.date}} 11 | 12 | 13 | GPIO: {{mqtt_message.id}} 14 |
    Status: {{mqtt_message.status}}
    15 |
    16 |
    17 |
    18 | ` 19 | }) 20 | 21 | var app = new Vue({ 22 | el: '#app', 23 | data: function () { 24 | return { 25 | mqtt_message_list: [ 26 | 27 | ] 28 | } 29 | }, 30 | mounted() { 31 | client = new Paho.MQTT.Client("192.168.1.150", 9001, createGuid()) 32 | 33 | var options = { 34 | onSuccess: onConnect, 35 | onFailure: onFailure 36 | }; 37 | 38 | client.onConnectionLost = onConnectionLost; 39 | client.onMessageArrived = onMessageArrived; 40 | 41 | client.connect(options); 42 | }, 43 | }) -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/40_Mqtt_Json_GPIO.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "config.h" // Sustituir con datos de vuestra red 8 | #include "API.hpp" 9 | #include "MQTT.hpp" 10 | #include "Server.hpp" 11 | #include "ESP32_Utils.hpp" 12 | #include "ESP32_Utils_MQTT_Async.hpp" 13 | #include "ReactiveArduinoLib.h" 14 | 15 | auto obsD0 = Reactive::FromDigitalInput(0); 16 | auto obsD5 = Reactive::FromDigitalInput(5); 17 | auto obsD6 = Reactive::FromDigitalInput(6); 18 | auto obsD7 = Reactive::FromDigitalInput(7); 19 | 20 | void setup(void) 21 | { 22 | Serial.begin(115200); 23 | SPIFFS.begin(); 24 | 25 | delay(500); 26 | 27 | WiFi.onEvent(WiFiEvent); 28 | InitMqtt(); 29 | 30 | ConnectWiFi_STA(); 31 | InitServer(); 32 | 33 | obsD0.Distinct().Do([](int i) { updateGPIO("D0", i); }); 34 | obsD5.Distinct().Do([](int i) { updateGPIO("D5", i); }); 35 | obsD6.Distinct().Do([](int i) { updateGPIO("D6", i); }); 36 | obsD7.Distinct().Do([](int i) { updateGPIO("D7", i); }); 37 | } 38 | 39 | void loop() 40 | { 41 | obsD0.Next(); 42 | obsD5.Next(); 43 | obsD6.Next(); 44 | obsD7.Next(); 45 | } 46 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/API.hpp: -------------------------------------------------------------------------------- 1 | void setGPIO(String id, bool state) 2 | { 3 | Serial.print("Set GPIO "); 4 | Serial.print(id); 5 | Serial.print(": "); 6 | Serial.println(state); 7 | } 8 | 9 | void setPWM(String id, int pwm) 10 | { 11 | Serial.print("Set PWM "); 12 | Serial.print(id); 13 | Serial.print(": "); 14 | Serial.println(pwm); 15 | } 16 | 17 | void doAction(String actionId) 18 | { 19 | Serial.print("Doing action: "); 20 | Serial.println(actionId); 21 | } 22 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/ESP32_Utils.hpp: -------------------------------------------------------------------------------- 1 | void ConnectWiFi_STA(bool useStaticIP = false) 2 | { 3 | Serial.println(""); 4 | WiFi.mode(WIFI_STA); 5 | WiFi.begin(ssid, password); 6 | if(useStaticIP) WiFi.config(ip, gateway, subnet); 7 | while (WiFi.status() != WL_CONNECTED) 8 | { 9 | delay(100); 10 | Serial.print('.'); 11 | } 12 | 13 | Serial.println(""); 14 | Serial.print("Iniciado STA:\t"); 15 | Serial.println(ssid); 16 | Serial.print("IP address:\t"); 17 | Serial.println(WiFi.localIP()); 18 | } 19 | 20 | void ConnectWiFi_AP(bool useStaticIP = false) 21 | { 22 | Serial.println(""); 23 | WiFi.mode(WIFI_AP); 24 | while(!WiFi.softAP(ssid, password)) 25 | { 26 | Serial.println("."); 27 | delay(100); 28 | } 29 | if(useStaticIP) WiFi.softAPConfig(ip, gateway, subnet); 30 | 31 | Serial.println(""); 32 | Serial.print("Iniciado AP:\t"); 33 | Serial.println(ssid); 34 | Serial.print("IP address:\t"); 35 | Serial.println(WiFi.softAPIP()); 36 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/MQTT.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | const IPAddress MQTT_HOST(192, 168, 1, 150); 4 | const int MQTT_PORT = 1883; 5 | 6 | AsyncMqttClient mqttClient; 7 | 8 | String GetPayloadContent(char* data, size_t len) 9 | { 10 | String content = ""; 11 | for(size_t i = 0; i < len; i++) 12 | { 13 | content.concat(data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | uint16_t packetIdSub = mqttClient.subscribe("device/0/#", 0); 21 | Serial.print("Subscribing at QoS 2, packetId: "); 22 | Serial.println(packetIdSub); 23 | } 24 | 25 | void updateGPIO(String input, bool value) 26 | { 27 | String payload; 28 | StaticJsonDocument<300> doc; 29 | doc["command"] = "updateGPIO"; 30 | doc["id"] = input; 31 | doc["status"] = value; 32 | serializeJson(doc, payload); 33 | 34 | mqttClient.publish("device/0/GPIO", 0, true, (char*)payload.c_str()); 35 | 36 | Serial.print(input); 37 | Serial.println(value ? String(" ON") : String(" OFF")); 38 | } 39 | 40 | void OnMqttReceived(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) 41 | { 42 | Serial.print("Received on "); 43 | Serial.print(topic); 44 | Serial.print(": "); 45 | 46 | String content = GetPayloadContent(payload, len); 47 | 48 | StaticJsonDocument<200> doc; 49 | DeserializationError error = deserializeJson(doc, content); 50 | if(error) return; 51 | 52 | int id = doc["id"]; 53 | bool ledStatus = doc["status"]; 54 | 55 | String command = doc["command"]; 56 | if(content.indexOf("GPIO") > 0 && command == "setGPIO") 57 | setGPIO(doc["id"], (bool)doc["status"]); 58 | else if(content.indexOf("PWM") > 0 && command == "setPWM") 59 | setPWM(doc["id"], (int)doc["pwm"]); 60 | else if(content.indexOf("Action") > 0 && command == "doAction") 61 | doAction(doc["id"]); 62 | else 63 | { 64 | //otras acciones 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/Server.hpp: -------------------------------------------------------------------------------- 1 | AsyncWebServer server(80); 2 | 3 | void InitServer() 4 | { 5 | server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html"); 6 | 7 | server.onNotFound([](AsyncWebServerRequest *request) { 8 | request->send(400, "text/plain", "Not found"); 9 | }); 10 | 11 | server.begin(); 12 | Serial.println("HTTP server started"); 13 | } 14 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP32_1"; 4 | 5 | IPAddress ip(192, 168, 1, 200); 6 | IPAddress gateway(192, 168, 1, 1); 7 | IPAddress subnet(255, 255, 255, 0); 8 | -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/css/main.css: -------------------------------------------------------------------------------- 1 | .label-big { 2 | border-radius: 3px; 3 | font-size: 16px; 4 | font-weight: 600; 5 | line-height: 2; 6 | padding: 0 8px; 7 | transition: opacity .2s linear; 8 | color: #fff 9 | } 10 | 11 | .On-style { 12 | background-color: #006b75; 13 | } 14 | 15 | .Off-style { 16 | background-color: #b60205; 17 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/js/API.js: -------------------------------------------------------------------------------- 1 | function onConnect() { 2 | var options = { 3 | qos: 0, 4 | onSuccess: onSubSuccess, 5 | onFailure: onSubFailure 6 | }; 7 | client.subscribe('device/0/#', options); 8 | } 9 | 10 | function onFailure(message) { 11 | console.log(message) 12 | } 13 | 14 | function onConnectionLost(responseObject) { 15 | if (responseObject.errorCode !== 0) { 16 | console.log("onConnectionLost:" + responseObject.errorMessage); 17 | } 18 | } 19 | 20 | function onMessageArrived(message) { 21 | console.log(message) 22 | var topic = message.destinationName; 23 | var json = message.payloadString; 24 | 25 | let payload = JSON.parse(json); 26 | console.log(payload) 27 | if(payload.command.includes("updateGPIO")) 28 | { 29 | let gpio = app.gpio_input_list.find(gpio => gpio.text == payload.id); 30 | gpio.status = payload.status; 31 | } 32 | } 33 | 34 | function onSubFailure(message) { 35 | console.log(message) 36 | } 37 | 38 | function onSubSuccess(message) { 39 | console.log(message) 40 | } 41 | 42 | function createGuid() { 43 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { 44 | var r = Math.random() * 16 | 0, 45 | v = c === 'x' ? r : (r & 0x3 | 0x8); 46 | return v.toString(16); 47 | }); 48 | } -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP32-Examples/782e5b82b52d99b3ce357bfa06fcb433776fc544/12 - ESP32 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /13 - ESP32 y .NET con C#/ 0 - esp32-net6-serial.md: -------------------------------------------------------------------------------- 1 | > Códigos de ejemplo de los tutoriales de www.luisllamas.es 2 | > 3 | > Enlace entrada: https://www.luisllamas.es/esp32-net6-serial/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | 8 | ## Código ESP32 9 | ```cpp 10 | void Update() 11 | { 12 | if(isOn == false && GetIsTumbado() == false) 13 | { 14 | isOn = true; 15 | Serial.println("A"); 16 | } 17 | else if(isOn == true && GetIsTumbado() == true) 18 | { 19 | isOn = false; 20 | Serial.println("B"); 21 | } 22 | } 23 | ``` 24 | 25 | ```cpp 26 | void Update() 27 | { 28 | if (Serial.available()) 29 | { 30 | auto content = Serial.readStringUntil(‘\n’); 31 | if(content == "A") isOn = true; 32 | if(content == "B") isOn = false; 33 | 34 | Render(); 35 | } 36 | } 37 | ``` 38 | 39 | 40 | 41 | ## Código NET6 42 | ```csharp 43 | ArduinoPort arduinoPort = new ArduinoPort(); 44 | arduinoPort.DataArrived += ArduinoPort_DataArrived; 45 | 46 | // ejemplo USB port en Windows 47 | arduinoPort.Open("COM4", 115200); 48 | 49 | // ejemplo USB port en Linux 50 | //arduinoPort.Open("/dev/ttyUSB0", 115200); 51 | 52 | Console.ReadLine(); 53 | 54 | void ArduinoPort_DataArrived(object? sender, EventArgs? e) 55 | { 56 | var lastRecieved = arduinoPort.LastRecieved; 57 | Console.WriteLine(lastRecieved); 58 | } 59 | ``` 60 | 61 | ```csharp 62 | var isOn = false; 63 | var timer = Observable.Interval(TimeSpan.FromSeconds(2)) 64 | .Subscribe(async _ => 65 | { 66 | var message = isOn ? "A" : "B"; 67 | isOn = !isOn; 68 | 69 | arduinoPort.Write(message); 70 | Console.WriteLine(message); 71 | }); 72 | ``` 73 | 74 | 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Recopilatorio de códigos de los tutoriales de ESP32 2 | 3 | [![www.luisllamas.es](https://github.com/luisllamasbinaburo/tutoriales-arduino-luisllamas_es/blob/main/img/screenshot.png)](https://www.luisllamas.es) 4 | 5 | ❤️ Códigos de ejemplo y ejercicios de la sección del ESP32 en https://www.luisllamas.es/esp8266-esp32/ 6 | 7 | 🔗 Para más información de cada código en el tutorial de su respectiva entrada 8 | 9 | ⭐️¡Si te gusta dale a la estrellita! 10 | 11 |

    12 | 13 | **Repos con tutoriales de otras secciones** 14 | - Tutoriales Arduino: https://github.com/luisllamasbinaburo/tutoriales-arduino-luisllamas_es 15 | - Tutoriales ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples 16 | - Tutoriales ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples 17 | 18 |

    19 | **Licencia** 20 | 21 | [![License: CC BY-NC-SA 4.0](https://licensebuttons.net/l/by-nc-sa/4.0/80x15.png)](https://creativecommons.org/licenses/by-nc-sa/4.0/) 22 | Todo el contenido distribuido bajo licencia CC BY-NC-SA, salvo indicación expresa. 23 | 24 | Las referencias y librerías empleadas, si es el caso, se indican en el tutorial correspondiente. Estarán sujetas a sus propia licencia, y no se incluyen en este repo. 25 | --------------------------------------------------------------------------------