ESP8266 + DHT22 Sensor

participation

(Anorak1) #1

In the past I have used various ESP8266 modules (Wemos D1 Mini) and sensors, DHT22, 18B20, BME280 etc to provide intrumentation distributed over a fairly wide range (connected by WiFi.
I have done a quick and scruffy cut & paste (hack) to get a DHT22 temp/humidity sensor up and running. The only section I’m not familiar with is the interface to my Pi3B based GateWay.

I’ve included the code below and if anyone could suggest a neater way of sending data / interfacing to the gateway I would be obliged… The code works by the way but I get a disconnected message despite the data being updated?

(Code Hacked from various sources)

//**** ESP8266_DHT22_Moz02
//**** 17/11/2018 ********
//**** R Freeman *********
//
/////////////////////////////////////////////////////////////////////
//
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 2 // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
// https://learn.adafruit.com/dht/overview

DHT_Unified dht(DHTPIN, DHTTYPE);
//
/////////////////////////////////////////////////////////////////////
//
#include “Thing.h”
#include “ESPWebThingAdapter.h”
//
//TODO: Hardcode wifi credentials here (and keep it private)
//
const char* ssid = “xxxxxxxx”;
const char* password = “yyyyyyyyy”;
//
/////////////////////////////////////////////////////////////////////
//
const char* deviceTypes[] = {“TempHumidSensor”, “Sensor”, nullptr};
ThingDevice device( “ESP_DHT_01”,“Saloon”, deviceTypes);
ThingProperty propertyT(“Temperature”, “DHT22 Temp”, NUMBER, “TemperatureProperty”);
ThingProperty propertyH(“Humidity”, “DHT22 Humid”, NUMBER, “HumidityProperty”);
//
WebThingAdapter* adapter = NULL;
//
/////////////////////////////////////////////////////////////////////
//
float TempData = 0.0;
//int TempData = 0;
float HumidData = 0.0;
const int ledPin = 13; // manually configure LED pin
const int long delayMS = 30 * 1000;
//
/////////////////////////////////////////////////////////////////////
//
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(115200);
Serial.println("");
Serial.print(“Connecting to “”);
Serial.print(ssid);
Serial.println(”"");
//
WiFi.mode(WIFI_STA);
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
//
//
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print(“IP address: “);
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter(“ESP8266-DHT22”, WiFi.localIP());
device.addProperty(&propertyT);
device.addProperty(&propertyH);
adapter->addDevice(&device);
//
adapter->begin();
Serial.println(“HTTP server started”);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.print(”/things/”);
Serial.println(device.id);

}

void loop()
{
Read_Sensor();
Send_Data();
delay(delayMS);
}

//
/////////////////////////////// FUNCTIONS //////////////////////////////////////////
//
void Read_Sensor()
{
dht.begin();
delay(100);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
TempData = event.temperature;
if (isnan(event.temperature))
{
Serial.println(“Error reading temperature!”);
}
else
{
Serial.print(“Temperature: “);
Serial.print(event.temperature);
Serial.println(” *C”);
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
HumidData = event.relative_humidity;
if (isnan(event.relative_humidity))
{
Serial.println(“Error reading humidity!”);
}
else
{
Serial.print(“Humidity: “);
Serial.print(event.relative_humidity);
Serial.println(” %RH \n”);
}
}
//
/////////////////////////////////////////////////////////////////////
//
void Send_Data()
{
ThingPropertyValue TempValue;
TempValue.number = TempData;
propertyT.setValue(TempValue);
Serial.println(TempData);
//
ThingPropertyValue HumidValue;
HumidValue.number = HumidData;
propertyH.setValue(HumidValue);
Serial.println(HumidData);
//
adapter->update();
//
}