Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Created October 24, 2020 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsampallo/06e9538b329038be4ee73d928ccac996 to your computer and use it in GitHub Desktop.
Save gsampallo/06e9538b329038be4ee73d928ccac996 to your computer and use it in GitHub Desktop.
DPRE - APP Inventor - Control temperatura
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "RED";
const char* password = "CLAVE";
ESP8266WebServer servidor(80);
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid,password);
Serial.print("Conectando");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Conectado");
Serial.print("Direccion IP: ");
Serial.println(WiFi.localIP());
servidor.on("/temperatura", temperatura);
servidor.on("/humedad", humedad);
servidor.begin();
}
void temperatura() {
float t = dht.readTemperature();
String respuesta = "";
if (isnan(t)) {
Serial.println("Error al leer datos del sensor");
respuesta += "Error";
} else {
respuesta = String(t);
Serial.println(respuesta);
}
servidor.send(200, "text/plain", respuesta);
}
void humedad() {
float h = dht.readHumidity();
String respuesta = "";
if (isnan(h)) {
Serial.println("Error al leer datos del sensor");
respuesta += "Error";
} else {
respuesta = String(h);
Serial.println(respuesta);
}
servidor.send(200, "text/plain", respuesta);
}
void loop() {
servidor.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment