Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Created November 16, 2016 15:07
Show Gist options
  • Save lvidarte/c8a8f9f76e53be641deb7130d56298a3 to your computer and use it in GitHub Desktop.
Save lvidarte/c8a8f9f76e53be641deb7130d56298a3 to your computer and use it in GitHub Desktop.
DHT21 server
/**
DHTServer - ESP8266 Webserver with a DHT sensor as an input
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h"
#include "Adafruit_Sensor.h"
#include "DHT.h"
#include "DHT_U.h"
#define AIO_HOST "io.adafruit.com"
#define AIO_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#define AIO_SSL_FINGERPRINT "26 96 1C 2A 51 07 FD 15 80 96 93 AE F7 32 CE B9 0D 01 55 C4"
#define DHTTYPE DHT21
#define DHTPIN 2
ESP8266WebServer server(80);
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 11);
float humidity, temperature; // Values read from sensor
String json = ""; // String to display
// Read interval
unsigned long previousMillis = 0;
const long interval = 2000; // interval at which to read sensor
// Log interval
unsigned long ioPreviousMillis = 0;
const long ioInterval = 60000; // interval at which to send data to adafruit
void io (String feedName, float value)
{
WiFiClientSecure client;
if ( ! client.connect(AIO_HOST, 443))
{
Serial.println("connection failed");
return;
}
if ( ! client.verify(AIO_SSL_FINGERPRINT, AIO_HOST))
{
Serial.println("connection insecure! abort");
return;
}
String aioKey = AIO_KEY;
String path = "/api/feeds/" + feedName + "/data";
String data = "value=" + String(value, 1);
client.print("POST " + path + " HTTP/1.1\r\n");
client.print("Host: io.adafruit.com:443\r\n");
client.print("x-aio-key: " + aioKey + "\r\n");
client.print("Connection: close\r\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: " + String(data.length()) + "\r\n\r\n");
client.print(data);
Serial.println(feedName + " = " + String(value, 1));
}
void adafruitIO ()
{
unsigned long ioCurrentMillis = millis();
if (ioCurrentMillis - ioPreviousMillis >= ioInterval)
{
// save the last time you read the sensor
ioPreviousMillis = ioCurrentMillis;
Serial.println("logging to adafruit.io");
readSensor();
io("temperature", temperature);
io("humidity", humidity);
}
}
void handle_root ()
{
readSensor();
json = "{\"temperature\": {\"value\":" +
String(temperature, 1) +
", \"unit\":\"celsius\"},\"humidity\": {\"value\":" +
String(humidity, 1) +
", \"unit\":\"percent\"}}";
server.send(200, "application/json", json);
}
void handle_temperature ()
{
readSensor();
json = "{\"temperature\":{\"value\":" + String(temperature, 1) + ",\"unit\":\"celsius\"}}";
server.send(200, "application/json", json);
}
void handle_humidity ()
{
readSensor();
json = "{\"humidity\":{\"value\":" + String(humidity, 1) + ",\"unit\":\"percent\"}}";
server.send(200, "application/json", json);
}
void readSensor ()
{
// Wait at least 2 seconds seconds between measurements.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temperature) || isnan(humidity))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
void setup (void)
{
Serial.begin(115200);
dht.begin();
/* Initialize wifiManager */
WiFiManager wifiManager;
//wifiManager.resetSettings();
wifiManager.autoConnect("WifiButtons-AP", "01234567");
server.on("/", handle_root);
server.on("/temperature", handle_temperature);
server.on("/humidity", handle_humidity);
server.begin();
Serial.println("HTTP server started");
}
void loop (void)
{
server.handleClient();
adafruitIO();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment