Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Marmachine
#64865 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 i've put it all together in a box, placed my sensors outside and guess what... i see it locking :? Even though it does work successful for about 15 hours, it stops every night for some reason.

I don't have a good indication of what's going on, i figured that it could be related to the Wifi signal, since it's in a different spot than where i tested before. Or otherwise could it be related to the sensors? I've soldered them to a 3m long (USB) cable.

Below is my (current) sketch, thanks for your ideas!

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      = "myssid";
const char* password  = "mypass";

// ============= Domoticz host address and port =============
const char* host = "192.168.2.100";     // NOTE: There should not be http:// before the address!!!
const int httpPort = 8080;

// =============== Domoticz devices to update ===============
int domoticz_temp_idx = 133;
int domoticz_hum_idx = 167;
int 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(){

  Serial.println("Reading sensors:");

  // === DHT22 ===

  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;
  } else {

      // === TEMPERATURE ===
      Serial.print("Temperature: ");
      Serial.print(temp);
      Serial.println(" degrees Celsius");
   
      // When we set a correction...
      // Serial.print("After (-4%) correction, temperature is: ");
      // temp = temp*0.96; /* 4 precent correction */
      // Serial.print(temp);
      // Serial.println(" degrees Celsius");

      // === HUMIDITY ===
      Serial.print("Humidity: ");
      Serial.print(hum);
      Serial.println(" Percent");
   
  }

  // === BH1750 ===
 
  uint16_t lux = lightMeter.readLightLevel();
  int luxnum = lux;

  if (isnan(lux)) {
    Serial.println("Failed to read from LUX sensor!");
    return;
  } else {

    Serial.print("Lux: ");
    Serial.println(lux);
   
  }

  Serial.println("");

  Serial.println("Update values in Domoticz:");

  // 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);

  // === TEMPERATURE ===

  Serial.println("Temperature:");

  // get url to set temperature
  String url = gettempurl(temp, domoticz_temp_idx);
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // 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();

  Serial.println("");

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);
 
  delay(500);


  // === HUMIDITY ===

  // 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);

  Serial.println("Humidity:");

  /* -- 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 url to set humidity
  String url_hum = gethumurl(hum, hum_status, domoticz_hum_idx);
  Serial.print("Requesting URL: ");
  Serial.println(url_hum);
 
  // 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();

  Serial.println("");

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);
 
  delay(500);
 
  // === 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);
  Serial.println("Lux:");

  String url_lux = getluxurl(lux, domoticz_lux_idx);
  Serial.print("Requesting URL: ");
  Serial.println(url_lux);
 
  // 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);
  }

  Serial.println("");
 
  // Disconnect from the server
  client.stop();

  Serial.println("");

  // Before we exit, turn the LED off.
  digitalWrite(LED_PIN, LOW);

  delay(500);

  // === PAUZE ===

  Serial.println("Waiting 5 mins for next update");
  // 5*60 seconds pause (5 minutes)
  delay(300000);

  Serial.println("");

}

// ==========================================================

String gettempurl(int temperature, int idx){
  // this function returns url to set temp_value in domoticz
 
  /* example url for domoticz temperature device:
  http://192.168.2.100:8080/json.htm?type=command&param=udevice&idx=133&nvalue=0&svalue=24
  */

  // Create a URI for the request
  String url = "/json.htm?type=command&param=udevice&idx=";
  url += idx;
  url += "&nvalue=0&svalue=";
  url += temperature;

  return url;
}

String gethumurl(int humidity, int humstatus, int idx){
  // this function returns url to set hum_value in domoticz
 
  /* example url for domoticz humidity device:
  http://192.168.2.100:8080/json.htm?type=command&param=udevice&idx=IDX&nvalue=HUM&svalue=0
  */

  // We now create a URI for the request
  String url = "/json.htm?type=command&param=udevice&idx=";
  url += idx;
  url += "&nvalue=";
  url += humidity; // Humidity value
  url += "&svalue=";
  url += humstatus; // Humidity_status can be: 0=Normal | 1=Comfortable | 2=Dry | 3=Wet

  return url;
}

String getluxurl(int lux, int idx){
  // this function returns url to set lux_value in domoticz
 
  /* example url for domoticz lux device:
  http://192.168.2.100:8080/json.htm?type=command&param=udevice&idx=IDX&svalue=VALUE
  */

  // We now create a URI for the request
  String url = "/json.htm?type=command&param=udevice&idx=";
  url += idx;
  url += "&svalue=";
  url += lux;

  return url;
}
User avatar
By Marmachine
#64871
Barnabybear wrote:Hi, check your router/Access point logs and see if it fails to renew its lease and gets kicked off the network. I had this problem and had to change some settings but can't remember what thet were.


;) good point, thanks! I did install a new router a couple of weeks ago, haven't had any problems though, but this might explain why it doesn't respond to a ping. I'll look into it!
User avatar
By Marmachine
#64884
Marmachine wrote:
Barnabybear wrote:Hi, check your router/Access point logs and see if it fails to renew its lease and gets kicked off the network. I had this problem and had to change some settings but can't remember what thet were.


;) good point, thanks! I did install a new router a couple of weeks ago, haven't had any problems though, but this might explain why it doesn't respond to a ping. I'll look into it!


Had another look into my ZTE router, i already set a static IP so it should have an infinity lease.
Then looking at my code again, let's say the wifi signal gets lost, how could i create a fall back? In other words, can i test my connection in every loop and re-connect in case it got lost? Would this be a good example?

:roll: being a newbie, does digitalWrite(2,1); initiate a reboot so that it will go through setup() again?