Wemos D1 mini with DHT22
Posted: Tue Mar 03, 2020 3:56 pm
Hello I am new here.
I would like to read a temperature and humidity with a Wemos D1 mini with Domoticz.
The Wemos do connect with Domoticz, but I get strange values.
The temperature is positive on one occasion and negative on the other.
The value is also incorrect.
Now I am not a good programmer, but I think there is something wrong in the formula.
Who wants to help me, sorry for my english.
Program see below.
#include <ESP8266WiFi.h>
#include <DHT.h>
// Domoticz settings.
const char* server = "***.***.*.*"; //IP address from your Domoticz.
const int port = 8080; //Port number of your Domoticz.
const char* authcode = "***********************";// Your base64 login username:password decodebase64.com.
const int sensorid = 1; // Replace with your sensor ID,
const int sleeptime = 1000;// Set the delay between updates 1sec = 1000.
// Wifi settings.
const char* ssid = "********"; // Your ssid.
const char* password = "**********************"; // Your Password.
WiFiClient client;
// DHT sensor settings.
//#define DHTTYPE DHT11 // DHT 11.
#define DHTTYPE DHT22 // DHT 22.
#define DHTPIN 2 // What pin the DHT is connected to.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("Service started");
Serial.println(WiFi.localIP());
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (!client.connect(server, port)) {
Serial.println("connection failed");
return;
}
int s = 0; //Humidity status.
if ( h > 70 ) {
s = 3;
} else if ( h < 40 ) {
s = 2;
} else if ( h > 40 || h < 70 ) {
s = 1;
}
String url = "/json.htm?type=command¶m=udevice&idx=";
url += sensorid;
url += "&nvalue=0&svalue=";
url += String(t, 2);
url += ";";
url += String(h, 2);
url += ";";
url += (s);
Serial.print("URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
//"Authorization: Basic " + authcode + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Domoticz");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("Feedback from Domoticz:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
client.stop();
Serial.println("Waiting");
delay(sleeptime);
}
With kind regards,
Robert
I would like to read a temperature and humidity with a Wemos D1 mini with Domoticz.
The Wemos do connect with Domoticz, but I get strange values.
The temperature is positive on one occasion and negative on the other.
The value is also incorrect.
Now I am not a good programmer, but I think there is something wrong in the formula.
Who wants to help me, sorry for my english.
Program see below.
#include <ESP8266WiFi.h>
#include <DHT.h>
// Domoticz settings.
const char* server = "***.***.*.*"; //IP address from your Domoticz.
const int port = 8080; //Port number of your Domoticz.
const char* authcode = "***********************";// Your base64 login username:password decodebase64.com.
const int sensorid = 1; // Replace with your sensor ID,
const int sleeptime = 1000;// Set the delay between updates 1sec = 1000.
// Wifi settings.
const char* ssid = "********"; // Your ssid.
const char* password = "**********************"; // Your Password.
WiFiClient client;
// DHT sensor settings.
//#define DHTTYPE DHT11 // DHT 11.
#define DHTTYPE DHT22 // DHT 22.
#define DHTPIN 2 // What pin the DHT is connected to.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.mode(WIFI_STA);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("Service started");
Serial.println(WiFi.localIP());
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (!client.connect(server, port)) {
Serial.println("connection failed");
return;
}
int s = 0; //Humidity status.
if ( h > 70 ) {
s = 3;
} else if ( h < 40 ) {
s = 2;
} else if ( h > 40 || h < 70 ) {
s = 1;
}
String url = "/json.htm?type=command¶m=udevice&idx=";
url += sensorid;
url += "&nvalue=0&svalue=";
url += String(t, 2);
url += ";";
url += String(h, 2);
url += ";";
url += (s);
Serial.print("URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
//"Authorization: Basic " + authcode + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Domoticz");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
String line = client.readStringUntil('\n');
Serial.println("Feedback from Domoticz:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
client.stop();
Serial.println("Waiting");
delay(sleeptime);
}
With kind regards,
Robert