- Sun Apr 09, 2017 2:12 pm
#64772
Well... thanks for sharing!
I'm no expert either
but working on a similar project with Domoticz.
I use a Wemos D1 r2 with DHT22 and BH1750 (lux) sensors which values i send to Domoticz using GET method.
I had my test setup running pretty stabile, so when i put it all together in a box, placed my sensors outside and guess what... i see it locking
Has happened already 3 times after about 18 hours of operating and i don't have a good indication of what's going on. I figured that it might have to do something with the Wifi signal, but anyway i am still looking.
Here's my code
Code: Select all#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BH1750.h>
// ========================= LED PIN ========================
#define LED_PIN D5 // Led pin
// ======================= DHT11 / 22 =======================
#define DHTPIN D3
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ==================== lightmeter BH1750 ===================
// BH1750 Light Sensor connection: VCC to 5v+ | GND to GND | SCL to pin SCL/D1 | SDA to pin SDA/D2 | ADD not used
BH1750 lightMeter;
// ========================== Wifi ==========================
const char* ssid = "ssid";
const char* password = "password";
// ============= Domoticz host address and port =============
const char* host = "192.168.0.123"; // NOTE: There should not be http:// before the address!!!
const int httpPort = 80;
// =============== Domoticz devices to update ===============
const char* domoticz_temp_idx = "133";
const char* domoticz_hum_idx = "167";
const char* domoticz_lux_idx = "168";
// ==========================================================
void setup(){
Serial.begin(115200);
delay(10);
// starting DHT sensor
dht.begin();
// starting lightMeter BH1750
lightMeter.begin();
WiFi.begin(ssid, password);
Serial.println();
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.print("Device IP: ");
Serial.println(WiFi.localIP());
Serial.println("=============================================");
Serial.println("");
}
// ==========================================================
void loop(){
float hum = dht.readHumidity();
float temp = dht.readTemperature();
int hum_status = 0;
if (isnan(hum) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// === BH1750 ===
uint16_t lux = lightMeter.readLightLevel();
int luxnum = lux;
if (isnan(lux)) {
Serial.println("Failed to read from LUX sensor!");
return;
}
// === TEMPERATURE ===
Serial.print("Sensor read temperature: ");
Serial.print(temp);
Serial.println(" degrees Celsius");
Serial.print(temp);
Serial.println(" degrees Celsius");
// Define a new client
WiFiClient client;
unsigned long timeout = millis();
// Get Connected to the Server
if (!client.connect(host, httpPort)) {
Serial.println("[connection failed]");
return;
}
// LED turns on when we connect
digitalWrite(LED_PIN, HIGH);
/* example url for domoticz temperature device:
http://192.168.0.123/json.htm?type=command¶m=udevice&idx=133&nvalue=0&svalue=24
*/
// Create a URI for the request
String url = "/json.htm?type=command¶m=udevice&idx=";
url += domoticz_temp_idx;
url += "&nvalue=0&svalue=";
url += temp;
Serial.print("Requesting URL: ");
Serial.println(url);
Serial.println();
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// set the timeout value
int TIMEOUT1 = millis() + 5000;
// Read all the lines of the reply from server and print them to Serial
while(client.available() == 0){
// check for timeout
if (TIMEOUT1 - millis() < 0) {
Serial.println(">>> CLIENT TIMEOUT!");
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
client.stop();
return;
}
String line = client.readStringUntil('\r');
Serial.print(line);
}
// Disconnect from the server
client.stop();
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
// ------------------
Serial.println();
Serial.println("Waiting 20 secs");
// 20 seconds pause
delay(20000);
Serial.println();
// ------------------
// === HUMIDITY ===
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" Percent");
/* -- Mapping for Humidity_status
* ----------------------------------------------------------------------------------------------------
-- 0 = Normal
-- 1 <> 46-70% = Comfortable
-- 2 < 46 = Dry
-- 3 > 70% = Wet
* ---------------------------------------------------------------------------------------------------- */
if (hum > 0){
if ((hum >= 46) and (hum <= 70)) {
hum_status = 1; /* comfortable */
} else if (hum < 46) {
hum_status = 2; /* dry */
} else if (hum > 70)
hum_status = 3; /* wet */
}
// Get Connected to the Server
if (!client.connect(host, httpPort)) {
Serial.println("[connection failed]");
return;
}
// LED turns on when we connect
digitalWrite(LED_PIN, HIGH);
/* example url for domoticz humidity device:
http://192.168.0.123/json.htm?type=command¶m=udevice&idx=IDX&nvalue=HUM&svalue=0
*/
// We now create a URI for the request
String url_hum = "/json.htm?type=command¶m=udevice&idx=";
url_hum += domoticz_hum_idx;
url_hum += "&nvalue=";
url_hum += hum; // Humidity value
url_hum += "&svalue=";
url_hum += hum_status; // Humidity_status can be: 0=Normal | 1=Comfortable | 2=Dry | 3=Wet
Serial.print("Requesting URL: ");
Serial.println(url_hum);
Serial.println();
// This will send the request to the server
client.print(String("GET ") + url_hum + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// set the timeout value
int TIMEOUT2 = millis() + 5000;
// Read all the lines of the reply from server and print them to Serial
while(client.available() == 0){
// check for timeout
if (TIMEOUT2 - millis() < 0) {
Serial.println(">>> CLIENT TIMEOUT!");
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
client.stop();
return;
}
String line = client.readStringUntil('\r');
Serial.print(line);
}
// Disconnect from the server
client.stop();
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
// ------------------
Serial.println();
Serial.println("Waiting 20 secs");
// 20 seconds pause
delay(20000);
Serial.println();
// ------------------
// === LUX ===
Serial.print("Lux: ");
Serial.println(lux);
// Get Connected to the Server
if (!client.connect(host, httpPort)) {
Serial.println("[connection failed]");
return;
}
// LED turns on when we connect
digitalWrite(LED_PIN, HIGH);
/* example url for domoticz humidity device:
http://192.168.0.123/json.htm?type=command¶m=udevice&idx=IDX&svalue=VALUE
*/
// We now create a URI for the request
String url_lux = "/json.htm?type=command¶m=udevice&idx=";
url_lux += domoticz_lux_idx;
url_lux += "&svalue=";
url_lux += lux;
Serial.print("Requesting URL: ");
Serial.println(url_lux);
Serial.println();
// This will send the request to the server
client.print(String("GET ") + url_lux + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// set the timeout value
int TIMEOUT3 = millis() + 5000;
// Read all the lines of the reply from server and print them to Serial
while(client.available() == 0){
// check for timeout
if (TIMEOUT3 - millis() < 0) {
Serial.println(">>> CLIENT TIMEOUT!");
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
client.stop();
return;
}
String line = client.readStringUntil('\r');
Serial.print(line);
}
// Disconnect from the server
client.stop();
// Before we exit, turn the LED off.
digitalWrite(LED_PIN, LOW);
Serial.println();
Serial.println("Waiting 20 secs");
// 20 seconds pause
delay(20000);
Serial.println();
}