personas:johnny:proyectos:indoor_diy_autosostenible
Diferencias
Muestra las diferencias entre dos versiones de la página.
Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previaPróxima revisiónAmbos lados, revisión siguiente | ||
personas:johnny:proyectos:indoor_diy_autosostenible [2019/06/19 07:36] – [primer prototipo de control automatico] kz | personas:johnny:proyectos:indoor_diy_autosostenible [2019/06/19 16:16] – [Firmware para el ESP8266] kz | ||
---|---|---|---|
Línea 48: | Línea 48: | ||
dht.begin(); | dht.begin(); | ||
// nombre del wifi y clave del wifi al cual se va a conectar el esp | // nombre del wifi y clave del wifi al cual se va a conectar el esp | ||
- | WiFi.begin(" | + | WiFi.begin(" |
while (WiFi.status() != WL_CONNECTED) { | while (WiFi.status() != WL_CONNECTED) { | ||
Línea 208: | Línea 208: | ||
Se intenta manipular relays, mosttar datos en pantallay enviar datos a una base de datos influxdb | Se intenta manipular relays, mosttar datos en pantallay enviar datos a una base de datos influxdb | ||
- | // A ESTE CÖDIGO FALTA IMPLEMENTAR ENVIO DE DATOS AL INFLUX CON WEBSOCKETS | + | A ESTE CÖDIGO FALTA IMPLEMENTAR ENVIO DE DATOS AL INFLUX CON WEBSOCKETS. |
- | <code c++> | + | [[https://gist.github.com/Kaziuz/d00f4e2e29e292fd1009cc028dc4aecd|Gist al código]] |
- | + | ||
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | #include < | + | |
- | //#include < | + | |
- | #include " | + | |
- | + | ||
- | #define DHTPIN D6 // Pin que va conectado al sensor | + | |
- | #define DHTTYPE DHT11 // Tipo de sensor que estamos usando | + | |
- | #define HTTP_TIMEOUT 1000 * 60 // cada minuto | + | |
- | DHT dht(DHTPIN, DHTTYPE); | + | |
- | + | ||
- | // If using software SPI (the default case): | + | |
- | #define OLED_MOSI | + | |
- | #define OLED_CLK | + | |
- | #define OLED_DC | + | |
- | #define OLED_CS | + | |
- | #define OLED_RESET D0 | + | |
- | + | ||
- | Adafruit_SSD1306 display(OLED_MOSI, | + | |
- | + | ||
- | 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: | + | |
- | </style> | + | |
- | < | + | |
- | var websock; | + | |
- | var webSockInflux; | + | |
- | 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(' | + | |
- | var f = document.getElementById(' | + | |
- | + | ||
- | if (evt.data === ' | + | |
- | e.style.color = ' | + | |
- | } | + | |
- | else if (evt.data === ' | + | |
- | e.style.color = ' | + | |
- | } | + | |
- | else if (evt.data === ' | + | |
- | f.style.color = ' | + | |
- | } | + | |
- | else if (evt.data === ' | + | |
- | f.style.color = ' | + | |
- | } | + | |
- | else { | + | |
- | console.log(' | + | |
- | } | + | |
- | }; | + | |
- | } | + | |
- | + | ||
- | function buttonclick(e) { | + | |
- | websock.send(e.id); | + | |
- | } | + | |
- | + | ||
- | </script> | + | |
- | </ | + | |
- | <body onload=" | + | |
- | <img src=" | + | |
- | < | + | |
- | <div id=" | + | |
- | <button id=" | + | |
- | <button id=" | + | |
- | <br /> | + | |
- | <br /> | + | |
- | + | ||
- | <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 = D1; | + | |
- | const int LEDPIN1 = D3; | + | |
- | // Current LED status | + | |
- | bool LEDStatus; | + | |
- | bool LEDStatus1; | + | |
- | + | ||
- | // Commands sent through Web Socket | + | |
- | const char LEDON[] = " | + | |
- | const char LEDOFF[] = " | + | |
- | + | ||
- | // Commands sent through Web Socket | + | |
- | const char LEDON1[] = " | + | |
- | const char LEDOFF1[] = " | + | |
- | + | ||
- | 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, | + | |
- | } | + | |
- | + | ||
- | // Send the current LED status | + | |
- | if (LEDStatus1) { | + | |
- | 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 if (strcmp(LEDON1, | + | |
- | writeLED1(true); | + | |
- | } | + | |
- | else if (strcmp(LEDOFF1, | + | |
- | writeLED1(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, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | static void writeLED1(bool LEDon) | + | |
- | { | + | |
- | LEDStatus1 = LEDon; | + | |
- | // Note inverted logic for Adafruit HUZZAH board | + | |
- | if (LEDon) { | + | |
- | digitalWrite(LEDPIN1, | + | |
- | } | + | |
- | else { | + | |
- | digitalWrite(LEDPIN1, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | // función que prepara la trama de datos para hacer un POST a endpoint del influx | + | |
- | String influxFrame( String dht11_humidity, | + | |
- | // este es el nombre del sensor | + | |
- | // Siempre que se quema la primera vez, se debe de cambiar el nombre del sensor | + | |
- | const String SENSOR_ID = " | + | |
- | + | ||
- | const String STR_COMMA = ","; | + | |
- | const String STR_SLASH = "/"; | + | |
- | const String STR_DOT = " | + | |
- | const String STR_COLON = ":"; | + | |
- | const String STR_NULL = " | + | |
- | const String STR_ZERO = " | + | |
- | const String STR_SPACE = " "; | + | |
- | + | ||
- | // El primer dato en el squema de la DB es el id del sensor | + | |
- | String frame = SENSOR_ID + STR_COMMA + " | + | |
- | + | ||
- | // Add GPS data | + | |
- | frame += " | + | |
- | frame += " | + | |
- | frame += " | + | |
- | frame += " | + | |
- | frame += " | + | |
- | frame += STR_ZERO + STR_COMMA; | + | |
- | frame += " | + | |
- | frame += STR_ZERO + STR_COMMA; | + | |
- | frame += " | + | |
- | frame += STR_ZERO + STR_COMMA; | + | |
- | + | ||
- | //Add DHT11 data | + | |
- | //if | + | |
- | frame += " | + | |
- | frame += dht11_humidity + STR_COMMA; | + | |
- | frame += " | + | |
- | frame += dht11_temperature + STR_COMMA; | + | |
- | // } else { | + | |
- | // frame += " | + | |
- | // } | + | |
- | + | ||
- | // Add Plantower data | + | |
- | // if | + | |
- | frame += " | + | |
- | frame += STR_ZERO + STR_COMMA; | + | |
- | frame += " | + | |
- | frame += STR_ZERO + STR_COMMA; | + | |
- | frame += " | + | |
- | frame += STR_ZERO; | + | |
- | // } else { | + | |
- | // frame += " | + | |
- | // } | + | |
- | + | ||
- | return frame; | + | |
- | } | + | |
- | + | ||
- | // función que envía la trama de datos | + | |
- | void sendDataInflux ( String humidity, String temperature ) { | + | |
- | /* | + | |
- | El post a la base de datos tiene una trama siguiente: | + | |
- | // volker0001, | + | |
- | Para nuestro caso que SOLO es el envío de datos del dht_11 que es humedad y temperatura la trama es la siguiente | + | |
- | // DHT11_llanadas, | + | |
- | */ | + | |
- | HTTPClient http; | + | |
- | // _testsensorhumedad es el nombre de la DB donde se almacenan estos datos | + | |
- | http.begin(" | + | |
- | http.setTimeout(HTTP_TIMEOUT); | + | |
- | http.addHeader(" | + | |
- | + | ||
- | String frame = influxFrame(humidity, | + | |
- | + | ||
- | int httpCode = http.POST(frame); | + | |
- | + | ||
- | if(httpCode > 0) { | + | |
- | String payload = http.getString(); | + | |
- | Serial.println(payload); | + | |
- | Serial.println(" | + | |
- | } else { | + | |
- | Serial.print(" | + | |
- | Serial.println(http.errorToString(httpCode).c_str()); | + | |
- | } | + | |
- | + | ||
- | http.end(); | + | |
- | delay(60000); | + | |
- | } | + | |
- | + | ||
- | void setup() | + | |
- | { | + | |
- | pinMode(LEDPIN, | + | |
- | pinMode(LEDPIN1, | + | |
- | writeLED(false); | + | |
- | writeLED1(false); | + | |
- | + | ||
- | Serial.begin(115200); | + | |
- | + | ||
- | // init pantalla | + | |
- | display.begin(SSD1306_SWITCHCAPVCC); | + | |
- | display.display(); | + | |
- | delay(1000); | + | |
- | display.clearDisplay(); | + | |
- | display.setTextSize(1); | + | |
- | display.setTextColor(WHITE); | + | |
- | + | ||
- | Serial.println(F(" | + | |
- | dht.begin(); | + | |
- | + | ||
- | // | + | |
- | + | ||
- | 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(); | + | |
- | + | ||
- | // dht11 y pantall | + | |
- | static unsigned int h = 0; | + | |
- | static unsigned int t = 0; | + | |
- | + | ||
- | h = dht.readHumidity(); | + | |
- | t = dht.readTemperature(); | + | |
- | /* | + | |
- | // Si las lecturas fallan, salimos, no mandamos nada y volvemos a intentarlo | + | |
- | if (isnan(h) || isnan(t)) { | + | |
- | Serial.println(F(" | + | |
- | return; | + | |
- | } | + | |
- | */ | + | |
- | + | ||
- | display.clearDisplay(); | + | |
- | display.setCursor(0, | + | |
- | display.print(" | + | |
- | display.print(" | + | |
- | display.print(h); | + | |
- | display.print(" | + | |
- | display.print(" | + | |
- | display.print(t); | + | |
- | display.print(" | + | |
- | display.print(" | + | |
- | display.print(WiFi.localIP()); | + | |
- | display.display(); | + | |
- | + | ||
- | // sendDataInflux(String(h), | + | |
- | } | + | |
- | + | ||
- | + | ||
- | </ | + | |
personas/johnny/proyectos/indoor_diy_autosostenible.txt · Última modificación: 2020/08/13 06:00 por kz