Created
June 16, 2019 08:52
-
-
Save shamasis/7415de7074a441444d89cc4b2b5fb83b to your computer and use it in GitHub Desktop.
ESP8266 Remote DHT Sensor
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 <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include "DHT.h" | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
int LED_PIN = LED_BUILTIN; // amica | |
const char* ssid = "Your wifi SSID"; // Enter SSID here | |
const char* password = "Your wifi password"; //Enter Password here | |
const char* host = "weather-station"; | |
uint8_t DHTPin = D2; | |
ESP8266WebServer server(80); // wev-server | |
DHT dht(DHTPin, DHTTYPE); // dht | |
void setup() { | |
pinMode(LED_PIN, OUTPUT); | |
blink_led(1, 100); | |
init_serial(115200); | |
init_wifi(ssid, password, host); | |
pinMode(DHTPin, INPUT); | |
dht.begin(); | |
server.on("/", handle_OnStatus); | |
server.on("/data", handle_OnConnect); | |
server.onNotFound(handle_NotFound); | |
server.begin(); | |
Serial.println("server ready"); | |
blink_led(4,500); | |
} | |
void loop() { | |
server.handleClient(); | |
} | |
/** | |
* allows blinking LED | |
*/ | |
void blink_led(int count, int wait) { | |
while (count >= 0) { | |
count -= 1; | |
digitalWrite(LED_PIN, LOW); | |
delay(wait / 2); | |
digitalWrite(LED_PIN, HIGH); | |
delay(wait / 2); | |
} | |
} | |
/** | |
* Initialise serial communication | |
* | |
* @param {int} baud - the communication baud rate | |
*/ | |
void init_serial(int baud) { | |
int wait = 0; | |
Serial.begin(baud); | |
while (!Serial) { // wait for connection | |
if (++wait > 100) { | |
Serial.println("serial communication readiness timed out. it may not work."); | |
return; | |
} | |
delay(10); | |
} | |
Serial.println("serial communication ready."); | |
} | |
/** | |
* initialise wifi communication | |
*/ | |
void init_wifi(const char* ssid, const char* pass, const char* host) { | |
Serial.print("connecting to \""); | |
Serial.print(ssid); | |
Serial.print("\""); | |
WiFi.hostname(host); | |
WiFi.begin(ssid, pass); | |
while (WiFi.status() != WL_CONNECTED) { | |
blink_led(1,100); | |
delay(900); | |
Serial.print("."); | |
} | |
Serial.print(" "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin(host)) { // start mdns | |
Serial.print("mdns identifying as: "); | |
Serial.println(host); | |
} | |
} | |
void handle_OnStatus() { | |
float temperature = dht.readTemperature(); | |
float humidity = dht.readHumidity(); | |
String response; | |
if (isnan(temperature) || isnan(humidity)) { | |
response = "{\"ready\":false}"; | |
} | |
else { | |
response = "{\"ready\":true}"; | |
} | |
// Serial.println(std::to_string(emperature) + "C, " + std::to_string(humidity) + "%"); | |
server.send(200, "application/json", response); | |
blink_led(1,10); | |
} | |
void handle_OnConnect() { | |
float temperature = dht.readTemperature(); | |
float humidity = dht.readHumidity(); | |
String response; | |
response = "{\"temperature\":"; | |
if (isnan(temperature)) { response += "null"; } | |
else { response += temperature; } | |
response += ", \"humidity\":"; | |
if (isnan(humidity)) { response += "null"; } | |
else { response += humidity; } | |
response += "}"; | |
// Serial.println(std::to_string(temperature) + "C, " + std::to_string(humidity) + "%"); | |
server.send(200, "application/json", response); | |
blink_led(1,10); | |
} | |
void handle_NotFound(){ | |
server.send(404, "application/json", "{\"error\":true}"); | |
blink_led(1,10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment