Created
December 16, 2020 14:43
-
-
Save mzhaase/f0a1be7f29f2074fac6ad19e65cc8ca8 to your computer and use it in GitHub Desktop.
ESP8266 / DHT22 Temperature Sensor Simple Website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DHT.h" | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#define DHTPIN 5 | |
#define DHTTYPE DHT22 | |
#define RATE 115200 | |
#define DEBUG true | |
#define SSID "szvsyGm6E74c6HnevvfBHuuY6NG3otjOyxBpEyb9" | |
#define PASS "szvsyGm6E74c6HnevvfBHuuY6NG3otjOyxBpEyb9" | |
DHT myDht(DHTPIN, DHTTYPE); | |
ESP8266WebServer server(80); | |
void setup() { | |
myDht.begin(); | |
if (DEBUG == true) { | |
Serial.begin(RATE); | |
Serial.println(""); | |
Serial.println("Connecting to WiFI"); | |
} | |
WiFi.begin(SSID, PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(10); | |
if (DEBUG == true) { | |
Serial.print("."); | |
} | |
} | |
if (DEBUG == true) { | |
Serial.println(""); | |
Serial.println("Connected"); | |
Serial.println(WiFi.localIP()); | |
} | |
server.on("/measurement", HTTP_GET, returnMeasurement); | |
server.begin(); | |
if (DEBUG == true) { | |
Serial.println("HTTP server started"); | |
} | |
} | |
void returnMeasurement() { | |
if (DEBUG == true) { | |
Serial.println("GET on /measurement"); | |
} | |
float temperature = myDht.readTemperature(); | |
float humidity = myDht.readHumidity(); | |
float heatIndex = myDht.computeHeatIndex(temperature, humidity, false); | |
String json = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + ",\"heatIndex\":" + String(heatIndex) + "}"; | |
server.send(200, "application/json", json); | |
} | |
void loop() { | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment