├── 00 - Introducción ├── 03 - como-convertir-codigo-del-esp8266-al-esp32.md └── 3 - como-convertir-codigo-del-esp8266-al-esp32.md ├── 01 - ESP8266 ├── 05 - guia-de-programacion-del-esp8266-en-entorno-arduino.md └── 06 - programar-esp8266-con-el-ide-de-arduino.md ├── 06 - Comunicación WiFi ├── 00_Connect_Wifi_STA │ ├── 00_Connect_Wifi_STA.ino │ ├── ESP8266_Utils.hpp │ └── config.h ├── 01_Connect_WiFi_AP │ ├── 01_Connect_WiFi_AP.ino │ ├── ESP8266_Utils.hpp │ └── config.h ├── 02_Static_IP │ ├── 02_Static_IP.ino │ ├── ESP8266_Utils.hpp │ └── config.h ├── 03_MDNS │ ├── 03_MDNS.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_mDNS.hpp │ └── config.h ├── 04_UDP │ ├── 20_UDP.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_UDP.hpp │ ├── Python │ │ ├── recieveUDP.py │ │ └── sendUDP.py │ ├── UDP.hpp │ └── config.h └── 05_OTA │ ├── 12_OTA.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_OTA.hpp │ └── config.h ├── 07 - Servidor ├── 04_Client │ ├── 04_Client.ino │ ├── Client.hpp │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_Client.hpp │ └── config.h ├── 05_Server_Simple │ ├── 05_Server_Simple.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 06_Server_Parameters │ ├── 06_Server_Parameters.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 07_Server_Dynamic_Content │ ├── 07_Server_Dynamic_Content.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ └── config.h ├── 08_Server_from_Flash │ ├── 08_Server_from_Flash.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── index.h ├── 09_Server_SPIFFS │ ├── 09_Server_SPIFFS.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_Server.hpp │ ├── Server.hpp │ └── config.h ├── 10_Server_SPIFFS_Gzip │ ├── 10_Server_SPIFFS_Gzip.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_Server.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html.gz ├── 11_AsyncServer │ ├── 11_AsyncServer.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html.gz └── 23_MaterialDesign │ ├── 23_MaterialDesign.ino │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html ├── 14_Forms_LED │ ├── 14_Forms_LED.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html ├── 15_Forms_LED_PWM │ ├── 15_Forms_LED_PWM.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ └── index.html └── 16_Forms_RGB │ ├── 16_Forms_RGB.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ └── index.html ├── 09 - API Rest ├── 17_Ajax │ ├── 17_Ajax.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 21_API_REST_Client │ ├── 21_API_REST_Client.ino │ ├── API.hpp │ ├── ESP8266_Utils.hpp │ └── config.h ├── 22_API_REST_Server │ ├── 22_API_REST_Server.ino │ ├── API.hpp │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_APIREST.hpp │ ├── Server.hpp │ └── config.h ├── 24_Json_Ajax │ ├── 24_Json_Ajax.ino │ ├── API.hpp │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_APIREST.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js └── 29_Axios │ ├── 29_Axios.ino │ ├── API.hpp │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_WS.hpp │ ├── Server.hpp │ ├── WebSockets.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 19_AsyncWebsocket │ ├── 19_AsyncWebsocket.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_AWS.hpp │ ├── Server.hpp │ ├── Websocket.hpp │ ├── config.h │ └── data │ │ ├── index.html │ │ └── js │ │ └── main.js ├── 25_Json_AsyncWebsocket │ ├── 25_Json_AsyncWebsocket.ino │ ├── API.hpp │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_AWS.hpp │ ├── Server.hpp │ ├── WebSockets.hpp │ ├── config.h │ └── data │ ├── css │ └── main.css │ ├── index.html │ ├── js │ └── main.js │ └── vendor │ ├── google-fonts.css.gz │ ├── material.css.gz │ └── material.js.gz ├── 11 - ESP8266 y VueJS ├── 27_VueJs │ ├── 27_VueJs.ino │ ├── ESP8266_Utils.hpp │ ├── Server.hpp │ ├── config.h │ └── data │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── js │ │ └── app.js │ │ └── vendor │ │ └── vue.min.js.gz ├── 28_Vuetify │ ├── 28_Vuetify.ino │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_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 - ESP8266 y MQTT ├── 35_Mqtt │ ├── 36_Mqtt.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT.hpp │ ├── MQTT.hpp │ └── config.h ├── 36_Async Mqtt │ ├── 36_Mqtt.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ └── config.h ├── 37_Mqtt_Json │ ├── 37_Mqtt_Json.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT_Async.hpp │ ├── MQTT.hpp │ └── config.h ├── 38_Mqtt_Json_web │ ├── 38_Mqtt_Json_web.ino │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT.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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT.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 │ ├── ESP8266_Utils.hpp │ ├── ESP8266_Utils_MQTT.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 ├── 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 | -------------------------------------------------------------------------------- /00 - Introducción/3 - 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 | 8 | ## Librerías WiFi 9 | ```cpp 10 | #if defined(ESP8266) 11 | #define HARDWARE "ESP8266" 12 | #include "ESP8266WiFi.h" 13 | 14 | #elif defined(ESP32) 15 | #define HARDWARE "ESP32" 16 | #include "WiFi.h" 17 | 18 | #endif 19 | ``` 20 | 21 | 22 | -------------------------------------------------------------------------------- /01 - ESP8266/05 - guia-de-programacion-del-esp8266-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-esp8266-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(D5, LOW); // D5, que es igual a GPIO14 10 | 11 | digitalWrite(14, LOW); // GPIO14 directamente 12 | ``` 13 | 14 | 15 | ## Salidas y entradas digitales (GPIO) 16 | ```cpp 17 | pinMode(pin, mode) 18 | ``` 19 | 20 | ```cpp 21 | digitalWrite(pin, output) 22 | ``` 23 | 24 | ```cpp 25 | digitalRead(pin) 26 | ``` 27 | 28 | 29 | ## Salidas analógicas (PWM) 30 | ```cpp 31 | analogWrite(pin, value) 32 | ``` 33 | 34 | ```cpp 35 | analogWriteRange(range) 36 | ``` 37 | 38 | ```cpp 39 | analogWriteFreq(frequency) 40 | ``` 41 | 42 | ```cpp 43 | analogWrite(pin, 0) 44 | ``` 45 | 46 | 47 | ## Entradas analógicas (ADC) 48 | ```cpp 49 | analogRead(A0) 50 | ``` 51 | 52 | ```cpp 53 | ADC_MODE(ADC_VCC) 54 | ``` 55 | 56 | 57 | ## Interrupciones 58 | ```cpp 59 | attachInterrupt(digitalPinToInterrupt(interruptPin), handler, FALLING); 60 | ``` 61 | 62 | 63 | ## PROGMEM 64 | ```cpp 65 | string text1 = F("hello"); 66 | string text2 = F("hello"); 67 | ``` 68 | 69 | ```cpp 70 | const char hello[] PROGMEM = "hello"; 71 | string texto1 = FSPTR(hello); 72 | string texto2 = FSPTR(hello); 73 | ``` 74 | 75 | 76 | ## Puerto serie 77 | ```cpp 78 | Serial.swap() 79 | ``` 80 | 81 | ```cpp 82 | Serial1.begin(baud) 83 | ``` 84 | 85 | 86 | ## Bus I2C 87 | ```cpp 88 | Wire.begin(SDA, SCL) 89 | ``` 90 | 91 | 92 | ## Bus SPI 93 | ```cpp 94 | SPI.setFrequency(frequency) 95 | ``` 96 | 97 | 98 | ## Comunicación WiFI 99 | ```cpp 100 | ESP8266WiFi Class 101 | ``` 102 | 103 | ```cpp 104 | #include 105 | ``` 106 | 107 | 108 | -------------------------------------------------------------------------------- /01 - ESP8266/06 - programar-esp8266-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/programar-esp8266-con-el-ide-de-arduino/ 4 | > 5 | > Todo el contenido distribuido bajo licencia CCC, salvo indicación expresa 6 | 7 | ## Instalar ESP8266 en IDE Arduino 8 | ```bash 9 | http://arduino.esp8266.com/stable/package_esp8266com_index.json 10 | ``` 11 | 12 | 13 | -------------------------------------------------------------------------------- /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 "ESP8266_Utils.hpp" 5 | 6 | void setup() 7 | { 8 | Serial.begin(115200); 9 | 10 | ConnectWiFi_STA(); 11 | } 12 | 13 | void loop() 14 | { 15 | } -------------------------------------------------------------------------------- /06 - Comunicación WiFi/00_Connect_Wifi_STA/ESP8266_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 = "ESP8266_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 "ESP8266_Utils.hpp" 5 | 6 | void setup() 7 | { 8 | Serial.begin(115200); 9 | 10 | ConnectWiFi_AP(); 11 | } 12 | 13 | void loop() 14 | { 15 | } 16 | -------------------------------------------------------------------------------- /06 - Comunicación WiFi/01_Connect_WiFi_AP/ESP8266_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 = "ESP8266_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 "ESP8266_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/ESP8266_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 = "ESP8266_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 "ESP8266_Utils.hpp" 6 | #include "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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 "ESP8266_Utils.hpp" 7 | #include "ESP8266_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/ESP8266_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/ESP8266_Utils_UDP.hpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "ESP8266_Utils.hpp" 6 | #include "ESP8266_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/ESP8266_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/ESP8266_Utils_OTA.hpp: -------------------------------------------------------------------------------- 1 | void InitOTA() 2 | { 3 | // Port defaults to 8266 4 | // ArduinoOTA.setPort(8266); 5 | 6 | // Hostname defaults to esp8266-[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 = "ESP8266_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 "ESP8266_Utils.hpp" 9 | #include "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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 "ESP8266_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/ESP8266_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 | ESP8266WebServer 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 | } -------------------------------------------------------------------------------- /07 - Servidor/05_Server_Simple/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 "ESP8266_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/ESP8266_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 | ESP8266WebServer 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 = "ESP8266_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 "ESP8266_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/ESP8266_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 | ESP8266WebServer 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 = "ESP8266_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 "ESP8266_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/ESP8266_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 | ESP8266WebServer 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 = "ESP8266_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 "ESP8266_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 | 24 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/ESP8266_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/ESP8266_Utils_Server.hpp: -------------------------------------------------------------------------------- 1 | String GetContentType(String filename) 2 | { 3 | if(filename.endsWith(".htm")) return "text/html"; 4 | else if(filename.endsWith(".html")) return "text/html"; 5 | else if(filename.endsWith(".css")) return "text/css"; 6 | else if(filename.endsWith(".js")) return "application/javascript"; 7 | else if(filename.endsWith(".png")) return "image/png"; 8 | else if(filename.endsWith(".gif")) return "image/gif"; 9 | else if(filename.endsWith(".jpg")) return "image/jpeg"; 10 | else if(filename.endsWith(".ico")) return "image/x-icon"; 11 | else if(filename.endsWith(".xml")) return "text/xml"; 12 | else if(filename.endsWith(".pdf")) return "application/x-pdf"; 13 | else if(filename.endsWith(".zip")) return "application/x-zip"; 14 | else if(filename.endsWith(".gz")) return "application/x-gzip"; 15 | return "text/plain"; 16 | } 17 | 18 | void ServeFile(String path) 19 | { 20 | File file = SPIFFS.open(path, "r"); 21 | size_t sent = server.streamFile(file, GetContentType(path)); 22 | file.close(); 23 | } 24 | 25 | void ServeFile(String path, String contentType) 26 | { 27 | File file = SPIFFS.open(path, "r"); 28 | size_t sent = server.streamFile(file, contentType); 29 | file.close(); 30 | } 31 | 32 | bool HandleFileRead(String path) 33 | { 34 | if (path.endsWith("/")) path += "index.html"; 35 | Serial.println("handleFileRead: " + path); 36 | 37 | if (SPIFFS.exists(path)) 38 | { 39 | ServeFile(path); 40 | return true; 41 | } 42 | Serial.println("\tFile Not Found"); 43 | return false; 44 | } 45 | 46 | bool HandleFileReadGzip(String path) 47 | { 48 | if (path.endsWith("/")) path += "index.html"; 49 | Serial.println("handleFileRead: " + path); 50 | 51 | if (SPIFFS.exists(path)) 52 | { 53 | ServeFile(path, GetContentType(path)); 54 | return true; 55 | } 56 | else 57 | { 58 | String pathWithGz = path + ".gz"; 59 | if (SPIFFS.exists(pathWithGz)) 60 | { 61 | ServeFile(pathWithGz, GetContentType(path)); 62 | return true; 63 | } 64 | } 65 | Serial.println("\tFile Not Found"); 66 | return false; 67 | } 68 | -------------------------------------------------------------------------------- /07 - Servidor/09_Server_SPIFFS/Server.hpp: -------------------------------------------------------------------------------- 1 | ESP8266WebServer server(80); 2 | 3 | #include "ESP8266_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 = "ESP8266_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/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 "ESP8266_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 | 24 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/ESP8266_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/ESP8266_Utils_Server.hpp: -------------------------------------------------------------------------------- 1 | String GetContentType(String filename) 2 | { 3 | if(filename.endsWith(".htm")) return "text/html"; 4 | else if(filename.endsWith(".html")) return "text/html"; 5 | else if(filename.endsWith(".css")) return "text/css"; 6 | else if(filename.endsWith(".js")) return "application/javascript"; 7 | else if(filename.endsWith(".png")) return "image/png"; 8 | else if(filename.endsWith(".gif")) return "image/gif"; 9 | else if(filename.endsWith(".jpg")) return "image/jpeg"; 10 | else if(filename.endsWith(".ico")) return "image/x-icon"; 11 | else if(filename.endsWith(".xml")) return "text/xml"; 12 | else if(filename.endsWith(".pdf")) return "application/x-pdf"; 13 | else if(filename.endsWith(".zip")) return "application/x-zip"; 14 | else if(filename.endsWith(".gz")) return "application/x-gzip"; 15 | return "text/plain"; 16 | } 17 | 18 | void ServeFile(String path) 19 | { 20 | File file = SPIFFS.open(path, "r"); 21 | size_t sent = server.streamFile(file, GetContentType(path)); 22 | file.close(); 23 | } 24 | 25 | void ServeFile(String path, String contentType) 26 | { 27 | File file = SPIFFS.open(path, "r"); 28 | size_t sent = server.streamFile(file, contentType); 29 | file.close(); 30 | } 31 | 32 | bool HandleFileRead(String path) 33 | { 34 | if (path.endsWith("/")) path += "index.html"; 35 | Serial.println("handleFileRead: " + path); 36 | 37 | if (SPIFFS.exists(path)) 38 | { 39 | ServeFile(path); 40 | return true; 41 | } 42 | Serial.println("\tFile Not Found"); 43 | return false; 44 | } 45 | 46 | bool HandleFileReadGzip(String path) 47 | { 48 | if (path.endsWith("/")) path += "index.html"; 49 | Serial.println("handleFileRead: " + path); 50 | 51 | if (SPIFFS.exists(path)) 52 | { 53 | ServeFile(path, GetContentType(path)); 54 | return true; 55 | } 56 | else 57 | { 58 | String pathWithGz = path + ".gz"; 59 | if (SPIFFS.exists(pathWithGz)) 60 | { 61 | ServeFile(pathWithGz, GetContentType(path)); 62 | return true; 63 | } 64 | } 65 | Serial.println("\tFile Not Found"); 66 | return false; 67 | } 68 | -------------------------------------------------------------------------------- /07 - Servidor/10_Server_SPIFFS_Gzip/Server.hpp: -------------------------------------------------------------------------------- 1 | ESP8266WebServer server(80); 2 | 3 | #include "ESP8266_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 = "ESP8266_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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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 "ESP8266_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/ESP8266_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 ESP8266)"); 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 = "ESP8266_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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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 "ESP8266_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/ESP8266_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 = "ESP8266_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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/07 - Servidor/23_MaterialDesign/data/favicon.ico -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/07 - Servidor/23_MaterialDesign/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/material.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/07 - Servidor/23_MaterialDesign/data/vendor/material.css.gz -------------------------------------------------------------------------------- /07 - Servidor/23_MaterialDesign/data/vendor/material.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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 "ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 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 "ESP8266_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | pinMode(LED_BUILTIN, OUTPUT); 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/14_Forms_LED/ESP8266_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_BUILTIN, 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 = "ESP8266_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 | ESP8266 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 | #include "config.h" // Sustituir con datos de vuestra red 6 | #include "Server.hpp" 7 | #include "ESP8266_Utils.hpp" 8 | 9 | void setup(void) 10 | { 11 | pinMode(LED_BUILTIN, OUTPUT); 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/15_Forms_LED_PWM/ESP8266_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 | analogWrite(LED_BUILTIN, 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 = "ESP8266_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 | ESP8266 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 "ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 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 "ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 Ajax 7 | 8 | 9 | 10 | 11 | 12 |

Millis

13 |
---
14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /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 "ESP8266_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 | 28 | -------------------------------------------------------------------------------- /09 - API Rest/21_API_REST_Client/ESP8266_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 = "ESP8266_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 "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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 "ESP8266_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 "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 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 "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/09 - API Rest/29_Axios/data/favicon.ico -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP8266 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/js/API.js: -------------------------------------------------------------------------------- 1 | function getAllRequest() 2 | { 3 | axios.get('item') 4 | .then(function (response) { 5 | console.log(response); 6 | }) 7 | .catch(function (error) { 8 | console.log(error); 9 | }) 10 | .then(function () { 11 | }); 12 | } 13 | 14 | function getFilteredRequest() 15 | { 16 | axios.get('item', { 17 | params: { 18 | filter : 'myFilter' 19 | } 20 | }) 21 | .then(function (response) { 22 | console.log(response); 23 | }) 24 | .catch(function (error) { 25 | console.log(error); 26 | }) 27 | .then(function () { 28 | }); 29 | } 30 | 31 | function getByIdRequest() 32 | { 33 | id = 10; 34 | axios.get('item/' + id) 35 | .then(function (response) { 36 | console.log(response); 37 | }) 38 | .catch(function (error) { 39 | console.log(error); 40 | }) 41 | .then(function () { 42 | }); 43 | } 44 | 45 | function postRequest() 46 | { 47 | axios.post('item', { 48 | data: 'NewItem' 49 | }) 50 | .then(function (response) { 51 | console.log(response); 52 | }) 53 | .catch(function (error) { 54 | console.log(error); 55 | }) 56 | .then(function () { 57 | }); 58 | } 59 | 60 | function putRequest() 61 | { 62 | id = 10; 63 | axios.put('item/' + id, { 64 | data: 'NewItem' 65 | }) 66 | .then(function (response) { 67 | console.log(response); 68 | }) 69 | .catch(function (error) { 70 | console.log(error); 71 | }) 72 | .then(function () { 73 | }); 74 | } 75 | 76 | function patchRequest() 77 | { 78 | id = 10; 79 | axios.patch('item/' + id, { 80 | data: 'NewItem' 81 | }) 82 | .then(function (response) { 83 | console.log(response); 84 | }) 85 | .catch(function (error) { 86 | console.log(error); 87 | }) 88 | .then(function () { 89 | }); 90 | } 91 | 92 | function deleteRequest() 93 | { 94 | id = 10; 95 | axios.delete('item/' + id) 96 | .then(function (response) { 97 | console.log(response); 98 | }) 99 | .catch(function (error) { 100 | console.log(error); 101 | }) 102 | .then(function () { 103 | }); 104 | } 105 | -------------------------------------------------------------------------------- /09 - API Rest/29_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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 "ESP8266_Utils.hpp" 10 | #include "ESP8266_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/ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 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 | #include 5 | 6 | #include "config.h" // Sustituir con datos de vuestra red 7 | #include "Websocket.hpp" 8 | #include "Server.hpp" 9 | #include "ESP8266_Utils.hpp" 10 | #include "ESP8266_Utils_AWS.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 | // Ejemplo 2, llamada desde servidor 26 | ws.textAll(GetMillis()); 27 | } 28 | -------------------------------------------------------------------------------- /10 - Websockets/19_AsyncWebsocket/ESP8266_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/ESP8266_Utils_AWS.hpp: -------------------------------------------------------------------------------- 1 | void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ 2 | if(type == WS_EVT_CONNECT){ 3 | //Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); 4 | client->printf("Hello Client %u :)", client->id()); 5 | client->ping(); 6 | } else if(type == WS_EVT_DISCONNECT){ 7 | //Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); 8 | } else if(type == WS_EVT_ERROR){ 9 | //Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); 10 | } else if(type == WS_EVT_PONG){ 11 | //Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); 12 | } else if(type == WS_EVT_DATA){ 13 | AwsFrameInfo * info = (AwsFrameInfo*)arg; 14 | String msg = ""; 15 | if(info->final && info->index == 0 && info->len == len){ 16 | if(info->opcode == WS_TEXT){ 17 | for(size_t i=0; i < info->len; i++) { 18 | msg += (char) data[i]; 19 | } 20 | } else { 21 | char buff[3]; 22 | for(size_t i=0; i < info->len; i++) { 23 | sprintf(buff, "%02x ", (uint8_t) data[i]); 24 | msg += buff ; 25 | } 26 | } 27 | 28 | if(info->opcode == WS_TEXT) 29 | ProcessRequest(client, msg); 30 | 31 | } else { 32 | //message is comprised of multiple frames or the frame is split into multiple packets 33 | if(info->opcode == WS_TEXT){ 34 | for(size_t i=0; i < len; i++) { 35 | msg += (char) data[i]; 36 | } 37 | } else { 38 | char buff[3]; 39 | for(size_t i=0; i < len; i++) { 40 | sprintf(buff, "%02x ", (uint8_t) data[i]); 41 | msg += buff ; 42 | } 43 | } 44 | Serial.printf("%s\n",msg.c_str()); 45 | 46 | if((info->index + len) == info->len){ 47 | if(info->final){ 48 | if(info->message_opcode == WS_TEXT) 49 | ProcessRequest(client, msg); 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | void InitWebSockets() 57 | { 58 | ws.onEvent(onWsEvent); 59 | server.addHandler(&ws); 60 | Serial.println("WebSocket server started"); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /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 = "ESP8266_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 | ESP8266 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 "ESP8266_Utils.hpp" 12 | #include "ESP8266_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/ESP8266_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 = "ESP8266_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 | ESP8266 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 "ESP8266_Utils.hpp" 11 | #include "ESP8266_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/ESP8266_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 = "ESP8266_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/js/main.js: -------------------------------------------------------------------------------- 1 | var connection = new WebSocket('ws://' + location.hostname + '/ws', ['arduino']); 2 | 3 | connection.onopen = function () { 4 | connection.send('Received from Client'); 5 | console.log('Connected'); 6 | }; 7 | 8 | connection.onerror = function (error) { 9 | console.log('WebSocket Error', error); 10 | }; 11 | 12 | connection.onmessage = function (e) { 13 | console.log('Received from server: ', e.data); 14 | processReceived(e.data); 15 | }; 16 | 17 | connection.onclose = function () { 18 | console.log('WebSocket connection closed'); 19 | }; 20 | 21 | function processReceived(data) 22 | { 23 | json = JSON.parse(data) 24 | if(json.command == 'updateGPIO') 25 | { 26 | updateGPIO(json.id, json.status); 27 | } 28 | } 29 | 30 | function sendGPIO(id, status) 31 | { 32 | let data = { 33 | command : "setGPIO", 34 | id: id, 35 | status: status 36 | } 37 | 38 | let json = JSON.stringify(data); 39 | connection.send(json); 40 | } 41 | 42 | function sendPWM(id, pwm) 43 | { 44 | updateSliderText(id, pwm); 45 | 46 | let data = { 47 | command : "setPWM", 48 | id: id, 49 | pwm: pwm 50 | } 51 | 52 | let json = JSON.stringify(data); 53 | connection.send(json); 54 | } 55 | 56 | function sendAction(id) 57 | { 58 | let data = { 59 | command : "doAction", 60 | id: id, 61 | } 62 | 63 | let json = JSON.stringify(data); 64 | connection.send(json); 65 | } 66 | 67 | function updateGPIO(id, status) 68 | { 69 | document.getElementById('input-label-' + id).textContent = status; 70 | 71 | if(status == 'ON') 72 | { 73 | document.getElementById('input-label-' + id).classList.add('On-style'); 74 | document.getElementById('input-label-' + id).classList.remove('Off-style'); 75 | } 76 | else 77 | { 78 | document.getElementById('input-label-' + id).classList.add('Off-style'); 79 | document.getElementById('input-label-' + id).classList.remove('On-style'); 80 | } 81 | } 82 | 83 | function updateSliderText(id, value) { 84 | document.getElementById('slider-pwm-' + id).value = value; 85 | document.getElementById('slider-text-pwm-'+ id).value = value; 86 | } -------------------------------------------------------------------------------- /10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/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/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/10 - Websockets/26_Json_AsyncWebsocket_GPIO/data/vendor/material.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_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 - ESP8266 y VueJS/27_VueJs/ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/27_VueJs/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 y VueJS/27_VueJs/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/27_VueJs/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/27_VueJs/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/27_VueJs/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/27_VueJs/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_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 - ESP8266 y VueJS/28_Vuetify/ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/28_Vuetify/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 y VueJS/28_Vuetify/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/28_Vuetify/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 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 - ESP8266 y VueJS/28_Vuetify/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/28_Vuetify/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/28_Vuetify/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_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 - ESP8266 y VueJS/30_Vue_Axios/ESP8266_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 - ESP8266 y VueJS/30_Vue_Axios/ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/30_Vue_Axios/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 y VueJS/30_Vue_Axios/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/30_Vue_Axios/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/30_Vue_Axios/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/30_Vue_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/30_Vue_Axios/data/vendor/axios.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/30_Vue_Axios/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/30_Vue_Axios/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_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 - ESP8266 y VueJS/31_Vuetify_Axios/ESP8266_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 - ESP8266 y VueJS/31_Vuetify_Axios/ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/31_Vuetify_Axios/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 y VueJS/31_Vuetify_Axios/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 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 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/axios.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/axios.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/31_Vuetify_Axios/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/32_Vue_NativeWebsocket.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 "ESP8266_Utils.hpp" 10 | #include "ESP8266_Utils_AWS.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 | // Ejemplo 2, llamada desde servidor 26 | ws.textAll(GetMillis()); 27 | } 28 | -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/ESP8266_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 - ESP8266 y VueJS/32_Vue_NativeWebsocket/ESP8266_Utils_AWS.hpp: -------------------------------------------------------------------------------- 1 | void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ 2 | if(type == WS_EVT_CONNECT){ 3 | //Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); 4 | client->printf("Hello Client %u :)", client->id()); 5 | client->ping(); 6 | } else if(type == WS_EVT_DISCONNECT){ 7 | //Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); 8 | } else if(type == WS_EVT_ERROR){ 9 | //Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); 10 | } else if(type == WS_EVT_PONG){ 11 | //Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); 12 | } else if(type == WS_EVT_DATA){ 13 | AwsFrameInfo * info = (AwsFrameInfo*)arg; 14 | String msg = ""; 15 | if(info->final && info->index == 0 && info->len == len){ 16 | if(info->opcode == WS_TEXT){ 17 | for(size_t i=0; i < info->len; i++) { 18 | msg += (char) data[i]; 19 | } 20 | } else { 21 | char buff[3]; 22 | for(size_t i=0; i < info->len; i++) { 23 | sprintf(buff, "%02x ", (uint8_t) data[i]); 24 | msg += buff ; 25 | } 26 | } 27 | 28 | if(info->opcode == WS_TEXT) 29 | ProcessRequest(client, msg); 30 | 31 | } else { 32 | //message is comprised of multiple frames or the frame is split into multiple packets 33 | if(info->opcode == WS_TEXT){ 34 | for(size_t i=0; i < len; i++) { 35 | msg += (char) data[i]; 36 | } 37 | } else { 38 | char buff[3]; 39 | for(size_t i=0; i < len; i++) { 40 | sprintf(buff, "%02x ", (uint8_t) data[i]); 41 | msg += buff ; 42 | } 43 | } 44 | Serial.printf("%s\n",msg.c_str()); 45 | 46 | if((info->index + len) == info->len){ 47 | if(info->final){ 48 | if(info->message_opcode == WS_TEXT) 49 | ProcessRequest(client, msg); 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | void InitWebSockets() 57 | { 58 | ws.onEvent(onWsEvent); 59 | server.addHandler(&ws); 60 | Serial.println("WebSocket server started"); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /11 - ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/32_Vue_NativeWebsocket/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/32_Vue_NativeWebsocket/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_Utils.hpp" 11 | #include "ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/ESP8266_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 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/ESP8266_Utils_AWS.hpp: -------------------------------------------------------------------------------- 1 | void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ 2 | if(type == WS_EVT_CONNECT){ 3 | //Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); 4 | client->printf("Hello Client %u :)", client->id()); 5 | client->ping(); 6 | } else if(type == WS_EVT_DISCONNECT){ 7 | //Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); 8 | } else if(type == WS_EVT_ERROR){ 9 | //Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); 10 | } else if(type == WS_EVT_PONG){ 11 | //Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); 12 | } else if(type == WS_EVT_DATA){ 13 | AwsFrameInfo * info = (AwsFrameInfo*)arg; 14 | String msg = ""; 15 | if(info->final && info->index == 0 && info->len == len){ 16 | if(info->opcode == WS_TEXT){ 17 | for(size_t i=0; i < info->len; i++) { 18 | msg += (char) data[i]; 19 | } 20 | } else { 21 | char buff[3]; 22 | for(size_t i=0; i < info->len; i++) { 23 | sprintf(buff, "%02x ", (uint8_t) data[i]); 24 | msg += buff ; 25 | } 26 | } 27 | 28 | if(info->opcode == WS_TEXT) 29 | ProcessRequest(client, msg); 30 | 31 | } else { 32 | //message is comprised of multiple frames or the frame is split into multiple packets 33 | if(info->opcode == WS_TEXT){ 34 | for(size_t i=0; i < len; i++) { 35 | msg += (char) data[i]; 36 | } 37 | } else { 38 | char buff[3]; 39 | for(size_t i=0; i < len; i++) { 40 | sprintf(buff, "%02x ", (uint8_t) data[i]); 41 | msg += buff ; 42 | } 43 | } 44 | Serial.printf("%s\n",msg.c_str()); 45 | 46 | if((info->index + len) == info->len){ 47 | if(info->final){ 48 | if(info->message_opcode == WS_TEXT) 49 | ProcessRequest(client, msg); 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | void InitWebSockets() 57 | { 58 | ws.onEvent(onWsEvent); 59 | server.addHandler(&ws); 60 | Serial.println("WebSocket server started"); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /11 - ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/material.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/33_Vue_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 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 "ESP8266_Utils.hpp" 11 | #include "ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/ESP8266_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 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/ESP8266_Utils_AWS.hpp: -------------------------------------------------------------------------------- 1 | void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ 2 | if(type == WS_EVT_CONNECT){ 3 | //Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); 4 | client->printf("Hello Client %u :)", client->id()); 5 | client->ping(); 6 | } else if(type == WS_EVT_DISCONNECT){ 7 | //Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id()); 8 | } else if(type == WS_EVT_ERROR){ 9 | //Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data); 10 | } else if(type == WS_EVT_PONG){ 11 | //Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:""); 12 | } else if(type == WS_EVT_DATA){ 13 | AwsFrameInfo * info = (AwsFrameInfo*)arg; 14 | String msg = ""; 15 | if(info->final && info->index == 0 && info->len == len){ 16 | if(info->opcode == WS_TEXT){ 17 | for(size_t i=0; i < info->len; i++) { 18 | msg += (char) data[i]; 19 | } 20 | } else { 21 | char buff[3]; 22 | for(size_t i=0; i < info->len; i++) { 23 | sprintf(buff, "%02x ", (uint8_t) data[i]); 24 | msg += buff ; 25 | } 26 | } 27 | 28 | if(info->opcode == WS_TEXT) 29 | ProcessRequest(client, msg); 30 | 31 | } else { 32 | //message is comprised of multiple frames or the frame is split into multiple packets 33 | if(info->opcode == WS_TEXT){ 34 | for(size_t i=0; i < len; i++) { 35 | msg += (char) data[i]; 36 | } 37 | } else { 38 | char buff[3]; 39 | for(size_t i=0; i < len; i++) { 40 | sprintf(buff, "%02x ", (uint8_t) data[i]); 41 | msg += buff ; 42 | } 43 | } 44 | Serial.printf("%s\n",msg.c_str()); 45 | 46 | if((info->index + len) == info->len){ 47 | if(info->final){ 48 | if(info->message_opcode == WS_TEXT) 49 | ProcessRequest(client, msg); 50 | } 51 | } 52 | } 53 | } 54 | } 55 | 56 | void InitWebSockets() 57 | { 58 | ws.onEvent(onWsEvent); 59 | server.addHandler(&ws); 60 | Serial.println("WebSocket server started"); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /11 - ESP8266 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 - ESP8266 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 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/11 - ESP8266 y VueJS/34_Vuetify_NativeWebsocket_GPIO/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/35_Mqtt/36_Mqtt.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "config.h" // Sustituir con datos de vuestra red 5 | #include "MQTT.hpp" 6 | #include "ESP8266_Utils.hpp" 7 | #include "ESP8266_Utils_MQTT.hpp" 8 | 9 | 10 | void setup(void) 11 | { 12 | Serial.begin(115200); 13 | SPIFFS.begin(); 14 | 15 | ConnectWiFi_STA(true); 16 | 17 | InitMqtt(); 18 | } 19 | 20 | 21 | void loop() 22 | { 23 | HandleMqtt(); 24 | 25 | PublisMqtt(millis()); 26 | 27 | delay(1000); 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/35_Mqtt/ESP8266_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 - ESP8266 y MQTT/35_Mqtt/ESP8266_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 - ESP8266 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 = "ESP8266Client_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 - ESP8266 y MQTT/35_Mqtt/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 "ESP8266_Utils.hpp" 8 | #include "ESP8266_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 - ESP8266 y MQTT/36_Async Mqtt/ESP8266_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 - ESP8266 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 - ESP8266 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 - ESP8266 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 "ESP8266_Utils.hpp" 8 | #include "ESP8266_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 | PublishMqtt(); 25 | 26 | delay(500); 27 | } -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/37_Mqtt_Json/ESP8266_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 - ESP8266 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 - ESP8266 y MQTT/37_Mqtt_Json/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 "ESP8266_Utils.hpp" 11 | #include "ESP8266_Utils_MQTT.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 - ESP8266 y MQTT/38_Mqtt_Json_web/ESP8266_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 - ESP8266 y MQTT/38_Mqtt_Json_web/ESP8266_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 - ESP8266 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 - ESP8266 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 - ESP8266 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 - ESP8266 y MQTT/38_Mqtt_Json_web/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/38_Mqtt_Json_web/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 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 - ESP8266 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 - ESP8266 y MQTT/38_Mqtt_Json_web/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/38_Mqtt_Json_web/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/38_Mqtt_Json_web/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/38_Mqtt_Json_web/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 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 "MQTT.hpp" 9 | #include "Server.hpp" 10 | #include "ESP8266_Utils.hpp" 11 | #include "ESP8266_Utils_MQTT.hpp" 12 | 13 | void setup(void) 14 | { 15 | Serial.begin(115200); 16 | SPIFFS.begin(); 17 | 18 | ConnectWiFi_STA(true); 19 | 20 | InitServer(); 21 | 22 | InitMqtt(); 23 | } 24 | 25 | 26 | void loop() 27 | { 28 | HandleMqtt(); 29 | 30 | PublisMqtt(); 31 | 32 | delay(1000); 33 | } 34 | -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/ESP8266_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 - ESP8266 y MQTT/39_Mqtt_Json_Vue/ESP8266_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 - ESP8266 y MQTT/39_Mqtt_Json_Vue/MQTT.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/MQTT.hpp -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 y MQTT/39_Mqtt_Json_Vue/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 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 - ESP8266 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 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/39_Mqtt_Json_Vue/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 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 "ESP8266_Utils.hpp" 12 | #include "ESP8266_Utils_MQTT.hpp" 13 | #include "ReactiveArduinoLib.h" 14 | 15 | auto obsD0 = Reactive::FromDigitalInput(D0); 16 | auto obsD5 = Reactive::FromDigitalInput(D5); 17 | auto obsD6 = Reactive::FromDigitalInput(D6); 18 | auto obsD7 = Reactive::FromDigitalInput(D7); 19 | 20 | void setup(void) 21 | { 22 | Serial.begin(115200); 23 | SPIFFS.begin(); 24 | 25 | ConnectWiFi_STA(true); 26 | 27 | InitServer(); 28 | InitMqtt(); 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() 37 | { 38 | HandleMqtt(); 39 | 40 | obsD0.Next(); 41 | obsD5.Next(); 42 | obsD6.Next(); 43 | obsD7.Next(); 44 | } 45 | -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/ESP8266_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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/ESP8266_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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/MQTT.hpp: -------------------------------------------------------------------------------- 1 | const char *MQTT_BROKER_ADRESS = "192.168.1.150"; 2 | const uint16_t MQTT_PORT = 1883; 3 | const char *MQTT_CLIENT_NAME = "ESP8266Client_1"; 4 | 5 | WiFiClient espClient; 6 | PubSubClient mqttClient(espClient); 7 | 8 | String GetPayloadContent(uint8_t *data, size_t len) 9 | { 10 | String content = ""; 11 | for (size_t i = 0; i < len; i++) 12 | { 13 | content.concat((char)data[i]); 14 | } 15 | return content; 16 | } 17 | 18 | void SuscribeMqtt() 19 | { 20 | mqttClient.subscribe("device/0/#"); 21 | } 22 | 23 | void updateGPIO(String input, bool value) 24 | { 25 | String payload; 26 | StaticJsonDocument<300> doc; 27 | doc["command"] = "updateGPIO"; 28 | doc["id"] = input; 29 | doc["status"] = value; 30 | serializeJson(doc, payload); 31 | 32 | mqttClient.publish("device/0/GPIO", (char *)payload.c_str()); 33 | 34 | Serial.print(input); 35 | Serial.println(value ? String(" ON") : String(" OFF")); 36 | } 37 | 38 | void OnMqttReceived(char *topic, byte *payload, unsigned int length) 39 | { 40 | String content = GetPayloadContent(payload, length); 41 | Serial.print("Received on "); 42 | Serial.print(topic); 43 | Serial.print(": "); 44 | Serial.println(content); 45 | 46 | StaticJsonDocument<200> doc; 47 | DeserializationError error = deserializeJson(doc, content); 48 | if (error) { return; } 49 | 50 | String command = doc["command"]; 51 | if (content.indexOf("GPIO") > 0 && command == "setGPIO") 52 | setGPIO(doc["id"], (bool)doc["status"]); 53 | else if (content.indexOf("PWM") > 0 && command == "setPWM") 54 | setPWM(doc["id"], (int)doc["pwm"]); 55 | else if (content.indexOf("Action") > 0 && command == "doAction") 56 | doAction(doc["id"]); 57 | else 58 | 59 | } 60 | -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/config.h: -------------------------------------------------------------------------------- 1 | const char* ssid = "ssid"; 2 | const char* password = "password"; 3 | const char* hostname = "ESP8266_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 - ESP8266 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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/favicon.ico -------------------------------------------------------------------------------- /12 - ESP8266 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 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/google-fonts.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/google-fonts.css.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/mqttws31.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/mqttws31.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/nativeWs.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/nativeWs.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vue.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vue.min.js.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.css.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.css.gz -------------------------------------------------------------------------------- /12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisllamasbinaburo/ESP8266-Examples/9c591a5c40feaea354c1ad3b90fda1c1fdf13fcb/12 - ESP8266 y MQTT/40_Mqtt_Json_GPIO/data/vendor/vuetify.min.js.gz -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Recopilatorio de códigos de los tutoriales de ESP8266 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 ESP8266 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 | --------------------------------------------------------------------------------