personas:johnny:proyectos:esp8266
Diferencias
Muestra las diferencias entre dos versiones de la página.
Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previaÚltima revisiónAmbos lados, revisión siguiente | ||
personas:johnny:proyectos:esp8266 [2019/05/29 22:17] – kz | personas:johnny:proyectos:esp8266 [2019/06/04 19:08] – [Conexión LM35] kz | ||
---|---|---|---|
Línea 1: | Línea 1: | ||
- | ===== Configuración inicial, blink con ESP8266 | + | ====== Configuración inicial |
===== Materiales ===== | ===== Materiales ===== | ||
Línea 122: | Línea 122: | ||
- | {{ : | + | {{ : |
- | ==== Como encender un led con el esp 8266 + la aplicación blink ==== | + | ====== Como encender un led con el esp 8266 + la aplicación blink ====== |
Toda la información inicia desde aqui [[http:// | Toda la información inicia desde aqui [[http:// | ||
Línea 235: | Línea 235: | ||
http:// | http:// | ||
- | ===== Cómo programar sensor AQA en rama AQAkit | + | ====== Simple led on off página incrustada |
- | ===== pasos ===== | + | <code c++> |
+ | |||
+ | #include < | ||
+ | |||
+ | const char* ssid = "Your SSID"; | ||
+ | const char* password | ||
+ | |||
+ | int ledPin | ||
+ | WiFiServer server(80); | ||
+ | |||
+ | void setup() { | ||
+ | |||
+ | pinMode(ledPin, | ||
+ | digitalWrite(ledPin, | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.println( ssid ); | ||
+ | |||
+ | WiFi.begin(ssid, | ||
+ | |||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | |||
+ | while( WiFi.status() != WL_CONNECTED ){ | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | |||
+ | |||
+ | Serial.println(); | ||
+ | |||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP() ); | ||
+ | |||
+ | server.begin(); | ||
+ | Serial.println(" | ||
+ | |||
+ | // Print the IP address | ||
+ | Serial.print(" | ||
+ | Serial.print(" | ||
+ | Serial.print(WiFi.localIP()); | ||
+ | Serial.println("/" | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | |||
+ | // Check if a client has connected | ||
+ | WiFiClient client | ||
+ | if (!client) { | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | // Wait until the client sends some data | ||
+ | Serial.println(" | ||
+ | while(!client.available()){ | ||
+ | delay(1); | ||
+ | } | ||
+ | |||
+ | // Read the first line of the request | ||
+ | String request | ||
+ | Serial.println(request); | ||
+ | client.flush(); | ||
+ | |||
+ | // Match the request | ||
+ | |||
+ | int value = LOW; | ||
+ | if (request.indexOf("/ | ||
+ | digitalWrite(ledPin, | ||
+ | value = HIGH; | ||
+ | } | ||
+ | if (request.indexOf("/ | ||
+ | digitalWrite(ledPin, | ||
+ | value = LOW; | ||
+ | } | ||
+ | |||
+ | // Set ledPin according to the request | ||
+ | // | ||
+ | |||
+ | // Return the response | ||
+ | client.println(" | ||
+ | client.println(" | ||
+ | client.println("" | ||
+ | client.println("< | ||
+ | client.println("< | ||
+ | |||
+ | client.print(" | ||
+ | |||
+ | if(value == HIGH) { | ||
+ | client.print(" | ||
+ | } else { | ||
+ | client.print(" | ||
+ | } | ||
+ | client.println("< | ||
+ | client.println("< | ||
+ | client.println("< | ||
+ | client.println("</ | ||
+ | |||
+ | delay(1); | ||
+ | Serial.println(" | ||
+ | Serial.println("" | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Cómo programar sensor AQA en rama AQAkit ====== | ||
+ | |||
+ | ==== pasos ==== | ||
**En archivo main.ino** | **En archivo main.ino** | ||
- | Línea 21 Escribir el nombre del sensor | + | - Línea 21 Escribir el nombre del sensor |
- | Línea 397 Escribir latitud | + | |
- | Línea 400 escribir longitud | + | |
**En archivo aqawifi** | **En archivo aqawifi** | ||
- | Línea 115 y 116 configuración de las credenciales del wifi | + | - Línea 115 y 116 configuración de las credenciales del wifi |
+ | |||
+ | ====== códigos websockets ====== | ||
+ | |||
+ | ===== Código websocket para led ON OFF ===== | ||
+ | |||
+ | Este código fue tomado de: [[https:// | ||
+ | |||
+ | Para usar este código se asigna la red al esp, y luego se debe de entrar al esp y entrar a la ip asignada por el esp para usar. | ||
+ | |||
+ | <code c++> | ||
+ | |||
+ | /* | ||
+ | * configuration WebSocketServer_STA | ||
+ | * | ||
+ | * ESP8266 Web server with Web Socket to control an LED. | ||
+ | * | ||
+ | * The web server keeps all clients' | ||
+ | * turn the LED on or off. | ||
+ | * | ||
+ | * For example, clientA connects and turns the LED on. This changes the word | ||
+ | * " | ||
+ | * " | ||
+ | * the LED off, the word LED changes color to black on clientA and clientB web | ||
+ | * pages. | ||
+ | * | ||
+ | * References: | ||
+ | * | ||
+ | * https:// | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | /* | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | static const char ssid[] = " | ||
+ | static const char password[] = " | ||
+ | MDNSResponder mdns; | ||
+ | |||
+ | static void writeLED(bool); | ||
+ | |||
+ | ESP8266WiFiMulti WiFiMulti; | ||
+ | |||
+ | ESP8266WebServer server(80); | ||
+ | WebSocketsServer webSocket = WebSocketsServer(81); | ||
+ | |||
+ | static const char PROGMEM INDEX_HTML[] = R" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <meta name = " | ||
+ | < | ||
+ | < | ||
+ | "body { background-color: | ||
+ | </ | ||
+ | < | ||
+ | var websock; | ||
+ | function start() { | ||
+ | websock = new WebSocket(' | ||
+ | websock.onopen = function(evt) { console.log(' | ||
+ | websock.onclose = function(evt) { console.log(' | ||
+ | websock.onerror = function(evt) { console.log(evt); | ||
+ | websock.onmessage = function(evt) { | ||
+ | console.log(evt); | ||
+ | var e = document.getElementById(' | ||
+ | if (evt.data === ' | ||
+ | e.style.color = ' | ||
+ | } | ||
+ | else if (evt.data === ' | ||
+ | e.style.color = ' | ||
+ | } | ||
+ | else { | ||
+ | console.log(' | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | function buttonclick(e) { | ||
+ | websock.send(e.id); | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | <body onload=" | ||
+ | < | ||
+ | <div id=" | ||
+ | <button id=" | ||
+ | <button id=" | ||
+ | </ | ||
+ | </ | ||
+ | )rawliteral"; | ||
+ | |||
+ | // GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board LED might be on 13. | ||
+ | const int LEDPIN = D4; | ||
+ | // Current LED status | ||
+ | bool LEDStatus; | ||
+ | |||
+ | // Commands sent through Web Socket | ||
+ | const char LEDON[] = " | ||
+ | const char LEDOFF[] = " | ||
+ | |||
+ | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) | ||
+ | { | ||
+ | Serial.printf(" | ||
+ | switch(type) { | ||
+ | case WStype_DISCONNECTED: | ||
+ | Serial.printf(" | ||
+ | break; | ||
+ | case WStype_CONNECTED: | ||
+ | { | ||
+ | IPAddress ip = webSocket.remoteIP(num); | ||
+ | Serial.printf(" | ||
+ | // Send the current LED status | ||
+ | if (LEDStatus) { | ||
+ | webSocket.sendTXT(num, | ||
+ | } | ||
+ | else { | ||
+ | webSocket.sendTXT(num, | ||
+ | } | ||
+ | } | ||
+ | break; | ||
+ | case WStype_TEXT: | ||
+ | Serial.printf(" | ||
+ | |||
+ | if (strcmp(LEDON, | ||
+ | writeLED(true); | ||
+ | } | ||
+ | else if (strcmp(LEDOFF, | ||
+ | writeLED(false); | ||
+ | } | ||
+ | else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | // send data to all connected clients | ||
+ | webSocket.broadcastTXT(payload, | ||
+ | break; | ||
+ | case WStype_BIN: | ||
+ | Serial.printf(" | ||
+ | hexdump(payload, | ||
+ | |||
+ | // echo data back to browser | ||
+ | webSocket.sendBIN(num, | ||
+ | break; | ||
+ | default: | ||
+ | Serial.printf(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void handleRoot() | ||
+ | { | ||
+ | server.send_P(200, | ||
+ | } | ||
+ | |||
+ | void handleNotFound() | ||
+ | { | ||
+ | String message = "File Not Found\n\n"; | ||
+ | message += "URI: "; | ||
+ | message += server.uri(); | ||
+ | message += " | ||
+ | message += (server.method() == HTTP_GET)?" | ||
+ | message += " | ||
+ | message += server.args(); | ||
+ | message += " | ||
+ | for (uint8_t i=0; i< | ||
+ | message += " " + server.argName(i) + ": " + server.arg(i) + " | ||
+ | } | ||
+ | server.send(404, | ||
+ | } | ||
+ | |||
+ | static void writeLED(bool LEDon) | ||
+ | { | ||
+ | LEDStatus = LEDon; | ||
+ | // Note inverted logic for Adafruit HUZZAH board | ||
+ | if (LEDon) { | ||
+ | digitalWrite(LEDPIN, | ||
+ | } | ||
+ | else { | ||
+ | digitalWrite(LEDPIN, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | pinMode(LEDPIN, | ||
+ | writeLED(false); | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | |||
+ | // | ||
+ | |||
+ | Serial.println(); | ||
+ | Serial.println(); | ||
+ | Serial.println(); | ||
+ | |||
+ | for(uint8_t t = 4; t > 0; t--) { | ||
+ | Serial.printf(" | ||
+ | Serial.flush(); | ||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | WiFiMulti.addAP(ssid, | ||
+ | |||
+ | while(WiFiMulti.run() != WL_CONNECTED) { | ||
+ | Serial.print(" | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | if (mdns.begin(" | ||
+ | Serial.println(" | ||
+ | mdns.addService(" | ||
+ | mdns.addService(" | ||
+ | } | ||
+ | else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | |||
+ | server.on("/", | ||
+ | server.onNotFound(handleNotFound); | ||
+ | |||
+ | server.begin(); | ||
+ | |||
+ | webSocket.begin(); | ||
+ | webSocket.onEvent(webSocketEvent); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | webSocket.loop(); | ||
+ | server.handleClient(); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Esta otra opción permite que el esp no sea visible por todo mundo, solo se tiene que estar conectado a la misma red que el esp y luego entrar a la ip en un en browser asignada por el esp para entrar. | ||
+ | |||
+ | <code c++> | ||
+ | |||
+ | /* | ||
+ | * WebSocketServer_softAP | ||
+ | **/ | ||
+ | |||
+ | # | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define USE_SERIAL Serial | ||
+ | |||
+ | static const char ssid[] = " | ||
+ | static const char password[] = " | ||
+ | MDNSResponder mdns; | ||
+ | |||
+ | static void writeLED(bool); | ||
+ | |||
+ | ESP8266WiFiMulti WiFiMulti; | ||
+ | |||
+ | ESP8266WebServer server(80); | ||
+ | WebSocketsServer webSocket = WebSocketsServer(81); | ||
+ | |||
+ | static const char PROGMEM INDEX_HTML[] = R" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <meta name = " | ||
+ | < | ||
+ | < | ||
+ | "body { background-color: | ||
+ | </ | ||
+ | < | ||
+ | var websock; | ||
+ | function start() { | ||
+ | websock = new WebSocket(' | ||
+ | websock.onopen = function(evt) { console.log(' | ||
+ | websock.onclose = function(evt) { console.log(' | ||
+ | websock.onerror = function(evt) { console.log(evt); | ||
+ | websock.onmessage = function(evt) { | ||
+ | console.log(evt); | ||
+ | var e = document.getElementById(' | ||
+ | if (evt.data === ' | ||
+ | e.style.color = ' | ||
+ | } | ||
+ | else if (evt.data === ' | ||
+ | e.style.color = ' | ||
+ | } | ||
+ | else { | ||
+ | console.log(' | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | function buttonclick(e) { | ||
+ | websock.send(e.id); | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | <body onload=" | ||
+ | < | ||
+ | <div id=" | ||
+ | <button id=" | ||
+ | <button id=" | ||
+ | </ | ||
+ | </ | ||
+ | )rawliteral"; | ||
+ | |||
+ | // GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board LED might be on 13. | ||
+ | const int LEDPIN = D4; | ||
+ | // Current LED status | ||
+ | bool LEDStatus; | ||
+ | |||
+ | // Commands sent through Web Socket | ||
+ | const char LEDON[] = " | ||
+ | const char LEDOFF[] = " | ||
+ | |||
+ | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) | ||
+ | { | ||
+ | | ||
+ | switch (type) { | ||
+ | case WStype_DISCONNECTED: | ||
+ | USE_SERIAL.printf(" | ||
+ | break; | ||
+ | case WStype_CONNECTED: | ||
+ | { | ||
+ | | ||
+ | | ||
+ | // Send the current LED status | ||
+ | if (LEDStatus) { | ||
+ | | ||
+ | } | ||
+ | else { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | break; | ||
+ | case WStype_TEXT: | ||
+ | USE_SERIAL.printf(" | ||
+ | |||
+ | if (strcmp(LEDON, | ||
+ | writeLED(true); | ||
+ | } | ||
+ | else if (strcmp(LEDOFF, | ||
+ | writeLED(false); | ||
+ | } | ||
+ | else { | ||
+ | USE_SERIAL.println(" | ||
+ | } | ||
+ | // send data to all connected clients | ||
+ | webSocket.broadcastTXT(payload, | ||
+ | break; | ||
+ | case WStype_BIN: | ||
+ | USE_SERIAL.printf(" | ||
+ | hexdump(payload, | ||
+ | |||
+ | // echo data back to browser | ||
+ | webSocket.sendBIN(num, | ||
+ | break; | ||
+ | default: | ||
+ | USE_SERIAL.printf(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void handleRoot() | ||
+ | { | ||
+ | server.send_P(200, | ||
+ | } | ||
+ | |||
+ | void handleNotFound() | ||
+ | { | ||
+ | String message = "File Not Found\n\n"; | ||
+ | message += "URI: "; | ||
+ | message += server.uri(); | ||
+ | message += " | ||
+ | message += (server.method() == HTTP_GET) ? " | ||
+ | message += " | ||
+ | message += server.args(); | ||
+ | message += " | ||
+ | for (uint8_t i = 0; i< | ||
+ | message += " " + server.argName(i) + ": " + server.arg(i) + " | ||
+ | } | ||
+ | server.send(404, | ||
+ | } | ||
+ | |||
+ | static void writeLED(bool LEDon) | ||
+ | { | ||
+ | LEDStatus = LEDon; | ||
+ | // Note inverted logic for Adafruit HUZZAH board | ||
+ | if (LEDon) { | ||
+ | digitalWrite(LEDPIN, | ||
+ | } | ||
+ | else { | ||
+ | digitalWrite(LEDPIN, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | pinMode(LEDPIN, | ||
+ | writeLED(false); | ||
+ | |||
+ | USE_SERIAL.begin(115200); | ||
+ | |||
+ | // | ||
+ | |||
+ | USE_SERIAL.println(); | ||
+ | USE_SERIAL.println(); | ||
+ | USE_SERIAL.println(); | ||
+ | |||
+ | for (uint8_t t = 4; t > 0; t--) { | ||
+ | USE_SERIAL.printf(" | ||
+ | USE_SERIAL.flush(); | ||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | // WiFiMulti.addAP(ssid, | ||
+ | // | ||
+ | // while (WiFiMulti.run() != WL_CONNECTED) { | ||
+ | // Serial.print(" | ||
+ | // delay(100); | ||
+ | // } | ||
+ | |||
+ | WiFi.softAP(ssid, | ||
+ | IPAddress myIP = WiFi.softAPIP(); | ||
+ | USE_SERIAL.print(" | ||
+ | USE_SERIAL.println(myIP); | ||
+ | |||
+ | USE_SERIAL.println("" | ||
+ | USE_SERIAL.print(" | ||
+ | USE_SERIAL.println(ssid); | ||
+ | USE_SERIAL.print(" | ||
+ | USE_SERIAL.println(WiFi.localIP()); | ||
+ | |||
+ | if (mdns.begin(" | ||
+ | USE_SERIAL.println(" | ||
+ | mdns.addService(" | ||
+ | mdns.addService(" | ||
+ | } | ||
+ | else { | ||
+ | USE_SERIAL.println(" | ||
+ | } | ||
+ | USE_SERIAL.print(" | ||
+ | USE_SERIAL.println(WiFi.localIP()); | ||
+ | |||
+ | server.on("/", | ||
+ | server.onNotFound(handleNotFound); | ||
+ | |||
+ | server.begin(); | ||
+ | |||
+ | webSocket.begin(); | ||
+ | webSocket.onEvent(webSocketEvent); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | webSocket.loop(); | ||
+ | server.handleClient(); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Código websocket para led RGB ===== | ||
+ | |||
+ | Mas información acerca de este código: [[https:// | ||
+ | |||
+ | Otros enlaces: | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | <code c++> | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define LED_RED | ||
+ | #define LED_GREEN | ||
+ | #define LED_BLUE | ||
+ | |||
+ | const char* ssid = " | ||
+ | const char* password = " | ||
+ | int contconexion = 0; | ||
+ | |||
+ | String pagina ="< | ||
+ | "< | ||
+ | "< | ||
+ | "var connection = new WebSocket(' | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " var r = parseInt(document.getElementById(' | ||
+ | " var g = parseInt(document.getElementById(' | ||
+ | " var b = parseInt(document.getElementById(' | ||
+ | " if(r.length < 2) { r = ' | ||
+ | " if(g.length < 2) { g = ' | ||
+ | " if(b.length < 2) { b = ' | ||
+ | " var rgb = '#' | ||
+ | " console.log(' | ||
+ | " connection.send(rgb);" | ||
+ | " | ||
+ | "</ | ||
+ | "</ | ||
+ | "< | ||
+ | "LED Control:< | ||
+ | "R: <input id=' | ||
+ | "G: <input id=' | ||
+ | "B: <input id=' | ||
+ | "</ | ||
+ | "</ | ||
+ | |||
+ | ESP8266WebServer server = ESP8266WebServer(80); | ||
+ | WebSocketsServer webSocket = WebSocketsServer(81); | ||
+ | |||
+ | void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { | ||
+ | |||
+ | switch(type) { | ||
+ | case WStype_DISCONNECTED: | ||
+ | Serial.printf(" | ||
+ | break; | ||
+ | case WStype_CONNECTED: | ||
+ | IPAddress ip = webSocket.remoteIP(num); | ||
+ | Serial.printf(" | ||
+ | |||
+ | // send message to client | ||
+ | webSocket.sendTXT(num, | ||
+ | } | ||
+ | break; | ||
+ | case WStype_TEXT: | ||
+ | Serial.printf(" | ||
+ | |||
+ | if(payload[0] == '#' | ||
+ | // we get RGB data | ||
+ | |||
+ | // decode rgb data | ||
+ | uint32_t rgb = (uint32_t) strtol((const char *) & | ||
+ | |||
+ | analogWrite(LED_RED, | ||
+ | analogWrite(LED_GREEN, | ||
+ | analogWrite(LED_BLUE, | ||
+ | } | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | Serial.println(); | ||
+ | |||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(ssid, | ||
+ | while (WiFi.status() != WL_CONNECTED and contconexion <50) { //Cuenta hasta 50 si no se puede conectar lo cancela | ||
+ | ++contconexion; | ||
+ | delay(500); | ||
+ | Serial.print(" | ||
+ | } | ||
+ | if (contconexion <50) { | ||
+ | //para usar con ip fija | ||
+ | IPAddress Ip(192, | ||
+ | IPAddress Gateway(192, | ||
+ | IPAddress Subnet(255, | ||
+ | WiFi.config(Ip, | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | } | ||
+ | else { | ||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | pinMode(LED_RED, | ||
+ | pinMode(LED_GREEN, | ||
+ | pinMode(LED_BLUE, | ||
+ | |||
+ | // start webSocket server | ||
+ | webSocket.begin(); | ||
+ | webSocket.onEvent(webSocketEvent); | ||
+ | |||
+ | if(MDNS.begin(" | ||
+ | Serial.println(" | ||
+ | } | ||
+ | |||
+ | // handle index | ||
+ | server.on("/", | ||
+ | server.send(200, | ||
+ | }); | ||
+ | |||
+ | server.begin(); | ||
+ | |||
+ | // Add service to MDNS | ||
+ | MDNS.addService(" | ||
+ | MDNS.addService(" | ||
+ | |||
+ | digitalWrite(LED_RED, | ||
+ | digitalWrite(LED_GREEN, | ||
+ | digitalWrite(LED_BLUE, | ||
+ | |||
+ | analogWriteRange(255); | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | webSocket.loop(); | ||
+ | server.handleClient(); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== conectar ESp8266 a telegram ====== | ||
+ | |||
+ | Para la configuración del bot debemos usar botfather y configurar nuestro bot usando este servicio. Mas información sobre la configuración [[https:// | ||
+ | |||
+ | ===== pasos para configurar el bot ===== | ||
+ | |||
+ | Cuando estemos con Botfather, podremos escribirle ** /mybots ** para que el bot nos diga si tenemos bots configurados. Como este es el primero, entonces el nos dirá que no tiene ninguno. Entonces paramos a escribir ** /newbot ** para decirle a Botfather que nos haga un nuevo bot. Seguidamente, | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Después Botfather nos pedirá un '' | ||
+ | |||
+ | Cuando tengamos el apodó para nuestro bot, Botfather nos felicitará diciendonos que nuestro bot ya esta listo para usarse. Nos dirá que hay que buscarlo con el nombre que le asignamos y ademas de eso nos brindará un token para usarlo en nuestro código. Tanto //el apodo, el token y el nombre// los necesitamos para usarlos mas tarde en nuestro código. | ||
+ | |||
+ | Los pasos anteriores se muestran en esta imagen | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== código telegram bot esp8266 ===== | ||
+ | |||
+ | Se usa esta librería para el [[https:// | ||
+ | |||
+ | <code c++> | ||
+ | |||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // Initialize Telegram BOT | ||
+ | // your Bot Token (Get from Botfather) | ||
+ | #define BOTtoken " | ||
+ | #define BOTname " | ||
+ | #define BOTusername " | ||
+ | |||
+ | TelegramBOT bot(BOTtoken, | ||
+ | |||
+ | int Bot_mtbs = 1000; //mean time between scan messages | ||
+ | long Bot_lasttime; | ||
+ | bool Start = false; | ||
+ | |||
+ | // Initialize Wifi connection to the router | ||
+ | char ssid[] = " | ||
+ | char password[] = " | ||
+ | |||
+ | int el_reset = D2; | ||
+ | |||
+ | / | ||
+ | * EchoMessages - function to Echo messages * | ||
+ | | ||
+ | void Bot_ExecMessages() { | ||
+ | for (int i = 1; i < bot.message[0][0].toInt() + 1; i++) { | ||
+ | bot.message[i][5]=bot.message[i][5].substring(1, | ||
+ | Serial.println(" | ||
+ | if (bot.message[i][5] == " | ||
+ | digitalWrite(el_reset, | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | } | ||
+ | if (bot.message[i][5] == " | ||
+ | digitalWrite(el_reset, | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | } | ||
+ | if (bot.message[i][5] == " | ||
+ | digitalWrite(el_reset, | ||
+ | delay(1000); | ||
+ | digitalWrite(el_reset, | ||
+ | |||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | } | ||
+ | |||
+ | if (bot.message[i][5] == " | ||
+ | String wellcome = " | ||
+ | String wellcome1 = "/ | ||
+ | String wellcome2 = "/ | ||
+ | String wellcome3 = "/ | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | bot.sendMessage(bot.message[i][4], | ||
+ | Start = true; | ||
+ | } | ||
+ | } | ||
+ | bot.message[0][0] = ""; | ||
+ | } | ||
+ | |||
+ | void setup(){ | ||
+ | |||
+ | Serial.begin(115200); | ||
+ | |||
+ | // Set WiFi to station mode and disconnect from an AP if it was Previously | ||
+ | // connected | ||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.disconnect(); | ||
+ | delay(100); | ||
+ | |||
+ | // attempt to connect to Wifi network: | ||
+ | Serial.print(" | ||
+ | Serial.println(ssid); | ||
+ | WiFi.begin(ssid, | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | Serial.print(" | ||
+ | delay(500); | ||
+ | } | ||
+ | |||
+ | Serial.println("" | ||
+ | Serial.println(" | ||
+ | Serial.print(" | ||
+ | Serial.println(WiFi.localIP()); | ||
+ | // | ||
+ | bot.begin(); | ||
+ | pinMode(el_reset, | ||
+ | delay(2); | ||
+ | digitalWrite(el_reset, | ||
+ | |||
+ | } | ||
+ | |||
+ | void loop(){ | ||
+ | |||
+ | |||
+ | if (millis() > Bot_lasttime + Bot_mtbs) | ||
+ | bot.getUpdates(bot.message[0][1]); | ||
+ | Bot_ExecMessages(); | ||
+ | Bot_lasttime = millis(); | ||
+ | } | ||
+ | |||
+ | /* | ||
+ | digitalWrite(el_reset, | ||
+ | delay(1000); | ||
+ | digitalWrite(el_reset, | ||
+ | delay(1000); | ||
+ | */ | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== conexionado de algunos sensores ====== | ||
+ | |||
+ | ===== Conexión relevador ===== | ||
+ | |||
+ | Se puede reemplazar el transistor de la imagen por un ** 2N2222 **. para ensayar el Relay, se puede usar el código __blink__ de arduino | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== Conexión DTH11 ===== | ||
+ | |||
+ | Este sensor es un sensor de temperatura y humedad en el aire.Toda la información para conectar el sensor en este [[http:// | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Así conectamos el sensor al arduino o al esp. La resistencia tiene de que ser 10K | ||
+ | |||
+ | {{: | ||
+ | |||
+ | El código que necesitamos para leer los valores del sensor es este: | ||
+ | |||
+ | <code c++> | ||
+ | #include < | ||
+ | |||
+ | dht DHT; | ||
+ | |||
+ | #define DHT11_PIN 7 | ||
+ | |||
+ | void setup(){ | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | int chk = DHT.read11(DHT11_PIN); | ||
+ | Serial.print(" | ||
+ | Serial.println(DHT.temperature); | ||
+ | Serial.print(" | ||
+ | Serial.println(DHT.humidity); | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Conexión LM35 ===== | ||
+ | |||
+ | [[https:// | ||
+ | a toda la información. En este caso hemos optado por un sensor bastante normal que detecta temperaturas desde -55ºC a 150ºC, 1ºC equivale a 10mV y soporta voltajes de entre 4V y 30V. Todo esta información se ha obtenido de la ficha técnica del LM35. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | <code c++> | ||
+ | /* | ||
+ | Creado: Luis del Valle (ldelvalleh@programarfacil.com) | ||
+ | https:// | ||
+ | */ | ||
+ | |||
+ | // Declaracion de variables globales | ||
+ | float tempC; // Variable para almacenar el valor obtenido del sensor (0 a 1023) | ||
+ | int pinLM35 = 0; // Variable del pin de entrada del sensor (A0) | ||
+ | |||
+ | void setup() { | ||
+ | // Cambiamos referencia de las entradas analógicas | ||
+ | analogReference(INTERNAL); | ||
+ | |||
+ | // Configuramos el puerto serial a 9600 bps | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // Con analogRead leemos el sensor, recuerda que es un valor de 0 a 1023 | ||
+ | tempC = analogRead(pinLM35); | ||
+ | |||
+ | // Calculamos la temperatura con la fórmula | ||
+ | tempC = (1.1 * tempC * 100.0)/ | ||
+ | |||
+ | // Envia el dato al puerto serial | ||
+ | Serial.print(tempC); | ||
+ | // Salto de línea | ||
+ | Serial.print(" | ||
+ | |||
+ | // Esperamos un tiempo para repetir el loop | ||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Conexión pantalla oled SSD1306 ===== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Links con información interesante: | ||
+ | |||
+ | - [[https:// | ||
+ | - [[https:// | ||
+ | - [[https:// | ||
+ | |||
+ | Este es el esquema de conexión: | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Este código funciona con la librería de adafruit. | ||
+ | |||
+ | <code c++> | ||
+ | /* | ||
+ | * Demo for SSD1306 based 128x64 OLED module using Adafruit SSD1306 | ||
+ | * library (https:// | ||
+ | * | ||
+ | * See https:// | ||
+ | * for more information. | ||
+ | * | ||
+ | */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | // If using software SPI (the default case): | ||
+ | #define OLED_MOSI | ||
+ | #define OLED_CLK | ||
+ | #define OLED_DC | ||
+ | #define OLED_CS | ||
+ | #define OLED_RESET 10 | ||
+ | |||
+ | Adafruit_SSD1306 display(OLED_MOSI, | ||
+ | |||
+ | void setup() | ||
+ | // Serial.begin(9600); | ||
+ | display.begin(SSD1306_SWITCHCAPVCC); | ||
+ | display.display(); | ||
+ | delay(1000); | ||
+ | display.clearDisplay(); | ||
+ | display.setTextSize(1); | ||
+ | display.setTextColor(WHITE); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | static unsigned long thisMicros = 0; | ||
+ | static unsigned long lastMicros = 0; | ||
+ | display.clearDisplay(); | ||
+ | display.setCursor(0, | ||
+ | display.print(" | ||
+ | display.print(" | ||
+ | display.print(thisMicros - lastMicros); | ||
+ | display.print(" | ||
+ | display.display(); | ||
+ | lastMicros = thisMicros; | ||
+ | thisMicros = micros(); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
personas/johnny/proyectos/esp8266.txt · Última modificación: 2019/06/04 19:14 por kz