Sensor dht+relay won't send data or present
-
Hi everyone,
Started a challenging sensor immediately: RF24+relay+dht11.
Radio seems to work, sensor gets power and the debug-mqtt appears to show all proper presentations. Using Domoticz, the node is seen but only s_hum and s_binary. The humidity sensor wont show in devices, the relay is seen but doesnt respond.
I think I have a problem with my code. I'm using a millis-counter to send temp+hum every 60s and have the relay at the ready constantly.Pls help.
// Enable debug prints #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 //#define MY_RS485 #include <SPI.h> #include <MySensors.h> #include <DHT.h> // Set this to the pin you connected the DHT's data pin to #define DHT_DATA_PIN 4 // Set this offset if the sensor has a permanent small offset to the real temperatures #define SENSOR_TEMP_OFFSET 0 // Sleep time between sensor updates (in milliseconds) // Must be >1000ms for DHT22 and >2000ms for DHT11 static const uint64_t UPDATE_INTERVAL = 60000; // Force sending an update of the temperature after n sensor reads, so a controller showing the // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that // the value didn't change since; // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms] static const uint8_t FORCE_UPDATE_N_READS = 10; #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 float lastTemp; float lastHum; uint8_t nNoUpdatesTemp; uint8_t nNoUpdatesHum; bool metric = true; // Generally, you should use "unsigned long" for variables that hold time // The value will quickly become too large for an int to store unsigned long previousMillis = 0; // will store last time DHT was updated // constants won't change : const long interval = 60000; // interval at which to read DHT (milliseconds) MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); DHT dht; #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc) #define NUMBER_OF_RELAYS 1 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay void before() { for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) { // Then set relay pins in output mode pinMode(pin, OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF); } } void presentation() { // Send the sketch version information to the gateway sendSketchInfo("TemperatureAndHumidity", "1.1"); // Register all sensors to gw (they will be created as child devices) present(CHILD_ID_HUM, S_HUM); present(CHILD_ID_TEMP, S_TEMP); metric = getControllerConfig().isMetric; // Send the sketch version information to the gateway and Controller sendSketchInfo("Relay", "1.0"); for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_BINARY); } } void setup() { dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) { Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!"); } // Sleep for the time of the minimum sampling period to give the sensor time to power up // (otherwise, timeout errors might occure for the first reading) sleep(dht.getMinimumSamplingPeriod()); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you checked DHT previousMillis = currentMillis; // Force reading sensor, so it works also after sleep() dht.readSensor(true); // Get temperature from DHT library float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT!"); } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) { // Only send temperature if it changed since the last measurement or if we didn't send an update for n times lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } // Reset no updates counter nNoUpdatesTemp = 0; temperature += SENSOR_TEMP_OFFSET; send(msgTemp.set(temperature, 1)); #ifdef MY_DEBUG Serial.print("T: "); Serial.println(temperature); #endif } else { // Increase no update counter if the temperature stayed the same nNoUpdatesTemp++; } // Get humidity from DHT library float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) { // Only send humidity if it changed since the last measurement or if we didn't send an update for n times lastHum = humidity; // Reset no updates counter nNoUpdatesHum = 0; send(msgHum.set(humidity, 1)); #ifdef MY_DEBUG Serial.print("H: "); Serial.println(humidity); #endif } else { // Increase no update counter if the humidity stayed the same nNoUpdatesHum++; } // Sleep for a while to save energy //sleep(UPDATE_INTERVAL); } } void receive(const MyMessage &message) { // We only expect one type of message from controller. But we better check anyway. if (message.type==V_STATUS) { // Change relay state digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF); // Store state in eeprom saveState(message.sensor, message.getBool()); // Write some debug info Serial.print("Incoming change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); } }
-
On my Domoticz the dht11 shows as a combined temphum and it shows both data even if I present it separately
-
I think there's a bigger clitch in my code somewhere that is causing that. I'm not sure if the millis-timer is properly used here or if that is somehow interfering with the other code.
-
I'd use a wait (500) or (1000) at the end of the loop, to slow things down a bit
-
@gohan you mean "delay(500)"? Will that not affect the radio function if it is waiting for a command for the relay for example?
-
The startup serial "seems" OK, but the relay function is not there. The MQTT log appears to send empty strings "" rather than 0 or 1 when I attempt to activate the relay from Domoticz.
Just in case, the startup serial is as follows:0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1 3 MCO:BGN:BFR 4 TSM:INIT 5 TSF:WUR:MS=0 12 TSM:INIT:TSP OK 14 TSF:SID:OK,ID=1 16 TSM:FPAR 52 TSF:MSG:SEND,1-1-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK: 319 TSF:MSG:READ,0-0-1,s=255,c=3,t=8,pt=1,l=1,sg=0:0 324 TSF:MSG:FPAR OK,ID=0,D=1 2059 TSM:FPAR:OK 2060 TSM:ID 2061 TSM:ID:OK 2063 TSM:UPL 2068 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1 2075 TSF:MSG:READ,0-0-1,s=255,c=3,t=25,pt=1,l=1,sg=0:1 2080 TSF:MSG:PONG RECV,HP=1 2082 TSM:UPL:OK 2084 TSM:READY:ID=1,PAR=0,DIS=1 2089 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100 2098 TSF:MSG:READ,0-0-1,s=255,c=3,t=15,pt=6,l=2,sg=0:0100 2105 TSF:MSG:SEND,1-1-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1 2113 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0 2240 TSF:MSG:READ,0-0-1,s=255,c=3,t=6,pt=0,l=1,sg=0:M 2247 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=11,pt=0,l=22,sg=0,ft=0,st=OK:TemperatureAndHumidity 2257 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.1 2266 TSF:MSG:SEND,1-1-0-0,s=0,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=OK: 2273 TSF:MSG:SEND,1-1-0-0,s=1,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=OK: 2281 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=11,pt=0,l=5,sg=0,ft=0,st=OK:Relay 2289 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=OK:1.0 2297 TSF:MSG:SEND,1-1-0-0,s=1,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=OK: 2304 MCO:REG:REQ 2307 TSF:MSG:SEND,1-1-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2 2345 TSF:MSG:READ,0-0-1,s=255,c=3,t=27,pt=1,l=1,sg=0:1 2351 MCO:PIM:NODE REG=1 2353 MCO:BGN:STP 2355 MCO:SLP:MS=2000,SMS=0,I1=255,M1=255,I2=255,M2=255 2360 MCO:SLP:TPD 2362 MCO:SLP:WUP=-1 2364 MCO:BGN:INIT OK,TSP=1```
-
@MasMat said in Sensor dht+relay won't send data or present:
@gohan you mean "delay(500)"? Will that not affect the radio function if it is waiting for a command for the relay for example?
That's what wait() is for (mysensors specific) , it will not interfere with the radio commumication.
-
Thanks for the tip. Good to learn these things as I go.
I'm to the point of rebuilding the sensor (or building a duplicate really) to see if this could be a hardware glitch.
-
You could try ethernet gateway instead of mqtt if it works better and move to mqtt later on
-
@gohan I cant see the upside to going back since I couldnt get the Ethernet gw working but the mqtt appears to be working AND Domoticz supports it apparently.
I did a quick breadboard setup and it seems the problem might be hardware rather than sketch.Question about the wait command: could I use wait (=soooo simple) rather than a millis timer to have a sensor/node be ready to activate a relay at any time but to only send DHT data every 60s or so?
-
sure you can use the wait()
-
To follow up. It was hardware: broken vcc pin on the dht11 corrupted the whole sensor code badly. Resolder and added wait-code, inverted the on-off 0-vs-1 and I have a good working sensor. The duplicate is also installed and working reliably.
A 6-relay board is also working nicely and Domoticz is great. My old system is nearly replaced and I couldnt be happier.Respect and thanks all around!