Skip to content

Instantly share code, notes, and snippets.

@mzhaase
Created December 16, 2020 14:43
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 mzhaase/f0a1be7f29f2074fac6ad19e65cc8ca8 to your computer and use it in GitHub Desktop.
Save mzhaase/f0a1be7f29f2074fac6ad19e65cc8ca8 to your computer and use it in GitHub Desktop.
ESP8266 / DHT22 Temperature Sensor Simple Website
#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