Skip to content

Instantly share code, notes, and snippets.

@PeskyPotato
Created June 2, 2020 14:53
Show Gist options
  • Save PeskyPotato/fad4f84a4be5499852b0ccb80e42d666 to your computer and use it in GitHub Desktop.
Save PeskyPotato/fad4f84a4be5499852b0ccb80e42d666 to your computer and use it in GitHub Desktop.
Setup ESP32 as Web of Things device to record temperature and humidity using the DHT11
#define LARGE_JSON_BUFFERS 1
#define ARDUINOJSON_USE_LONG_LONG 1
#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 23
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
const char *ssid = "";
const char *password = "";
const String mDNSHostname = "weatherstation";
const int ledPin = 13;
WebThingAdapter *adapter;
const char* sensorTypes[] = {"TemperatureSensor", "MultiLevelSensor", "OnOffSwitch", "Light", nullptr};
ThingDevice sensor("weather", "Weather", sensorTypes);
ThingProperty sensorTemp("temperature", "", NUMBER, "TemperatureProperty");
ThingProperty sensorHumi("humid", "", NUMBER, "MultiLevelSensor");
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");
bool lastOn = true;
void setup(void) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
bool blink = true;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
blink = !blink;
}
digitalWrite(ledPin, HIGH); // active low led
// Setup DHT11
dht.begin();
Serial.println(F("DHTxx Unified Sensor Example"));
sensor_t dht_sensor;
dht.temperature().getSensor(&dht_sensor);
Serial.print (F("Sensor Type: ")); Serial.println(dht_sensor.name);
dht.humidity().getSensor(&dht_sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(dht_sensor.name);
delayMS = dht_sensor.min_delay / 1000;
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter(mDNSHostname, WiFi.localIP());
sensor.addProperty(&ledOn);
sensorTemp.unit = "celsius";
sensor.addProperty(&sensorTemp);
sensorHumi.unit = "%";
sensor.addProperty(&sensorHumi);
adapter->addDevice(&sensor);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.print(sensor.id);
}
void loop(void) {
adapter->update();
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
ThingPropertyValue tpVal;
tpVal.number = event.temperature;
sensorTemp.setValue(tpVal);
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
ThingPropertyValue huVal;
huVal.number = event.relative_humidity;
sensorHumi.setValue(huVal);
}
bool on = ledOn.getValue().boolean;
digitalWrite(ledPin, on ? LOW : HIGH); // active low led
if (on != lastOn) {
Serial.print(sensor.id);
Serial.print(": ");
Serial.println(on);
}
lastOn = on;
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment