implementing multiple sensors
-
I combined Humidity and Lux code. Serial monitor works ok (see below) but for some reason when I try to add node to Vera I get only node, temp and lux (no humidity). I guess something is wrong in combined sketch?
serial monitor
sensor started, id 1 send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1 send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0 read: 0-0-1 s=255,c=3,t=6,pt=0,l=1:M send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=12,st=ok:Humidity/Lux send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0 send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=5,st=ok:1.4.1 send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=5,st=ok:1.4.1 send: 1-1-0-0 s=2,c=0,t=16,pt=0,l=5,st=ok:1.4.1 send: 1-1-0-0 s=1,c=1,t=0,pt=7,l=5,st=ok:28.6 T: 28.60 send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,st=ok:61.8 H: 61.80 2 send: 1-1-0-0 s=2,c=1,t=23,pt=3,l=2,st=ok:2 2sketch
#include <SPI.h> #include <MySensor.h> #include <DHT.h> #include <BH1750.h> #include <Wire.h> #define CHILD_ID_HUM 0 #define CHILD_ID_TEMP 1 #define CHILD_ID_LIGHT 2 #define HUMIDITY_SENSOR_DIGITAL_PIN 3 #define LIGHT_SENSOR_ANALOG_PIN 0 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) BH1750 lightSensor; MySensor gw; DHT dht; float lastTemp; float lastHum; boolean metric = true; MyMessage msgHum(CHILD_ID_HUM, V_HUM); MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); uint16_t lastlux; void setup() { gw.begin(); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); // Send the Sketch Version Information to the Gateway gw.sendSketchInfo("Humidity/Lux", "1.0"); // Register all sensors to gw (they will be created as child devices) gw.present(CHILD_ID_HUM, S_HUM); gw.present(CHILD_ID_TEMP, S_TEMP); gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); metric = gw.getConfig().isMetric; lightSensor.begin(); } void loop() { delay(dht.getMinimumSamplingPeriod()); float temperature = dht.getTemperature(); if (isnan(temperature)) { Serial.println("Failed reading temperature from DHT"); } else if (temperature != lastTemp) { lastTemp = temperature; if (!metric) { temperature = dht.toFahrenheit(temperature); } gw.send(msgTemp.set(temperature, 1)); Serial.print("T: "); Serial.println(temperature); } float humidity = dht.getHumidity(); if (isnan(humidity)) { Serial.println("Failed reading humidity from DHT"); } else if (humidity != lastHum) { lastHum = humidity; gw.send(msgHum.set(humidity, 1)); Serial.print("H: "); Serial.println(humidity); } uint16_t lux = lightSensor.readLightLevel();// Get Lux value Serial.println(lux); if (lux != lastlux) { gw.send(msg.set(lux)); lastlux = lux; } gw.sleep(SLEEP_TIME); //sleep a bit }@niccodemi Check:
gw.send(msgTemp.set(temperature, 1));
gw.send(msgHum.set(humidity, 1));
gw.send(msg.set(lux)); I miss here ",1" -
@niccodemi Check:
gw.send(msgTemp.set(temperature, 1));
gw.send(msgHum.set(humidity, 1));
gw.send(msg.set(lux)); I miss here ",1" -
One more combined sketch - UV / Light Lux. Everything seems to work ok except only 2 items show up in vera: node and lux sensor. UV sensor is missing.
serial output
sensor started, id 6 send: 6-6-0-0 s=255,c=0,t=17,pt=0,l=5,st=ok:1.4.1 send: 6-6-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0 read: 0-0-6 s=255,c=3,t=6,pt=0,l=1:M send: 6-6-0-0 s=255,c=3,t=11,pt=0,l=13,st=ok:UV/Lux Sensor send: 6-6-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0 send: 6-6-0-0 s=0,c=0,t=16,pt=0,l=5,st=ok:1.4.1 send: 6-6-0-0 s=0,c=0,t=11,pt=0,l=5,st=ok:1.4.1 Lux reading: 48 send: 6-6-0-0 s=0,c=1,t=23,pt=7,l=5,st=ok:48.0 Uv reading: 583 Uv index: 5 send: 6-6-0-0 s=0,c=1,t=11,pt=7,l=5,st=ok:5.0sketch code
/* Vera Arduino BH1750FVI Light sensor communicate using I2C Protocol this library enable 2 slave device addresses Main address 0x23 secondary address 0x5C connect the sensor as follows : Light sensor VCC >>> 5V Gnd >>> Gnd ADDR >>> NC or GND SCL >>> A5 SDA >>> A4 UV sensor VCC >>> 5V Gnd >>> Gnd OUT >>> A3 Contribution: idefix */ #include <SPI.h> #include <MySensor.h> #include <BH1750.h> #include <Wire.h> #define CHILD_ID_LIGHT 0 #define CHILD_ID_UV 0 #define LIGHT_SENSOR_ANALOG_PIN 0 #define UV_SENSOR_ANALOG_PIN 3 unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) BH1750 lightSensor; MySensor gw; MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL); MyMessage uvMsg(CHILD_ID_UV, V_UV); uint16_t lastlux; int lastUV = -1; int uvIndexValue [13] = { 50, 227, 318, 408, 503, 606, 696, 795, 881, 976, 1079, 1170, 3000}; int uvIndex; void setup() { gw.begin(); // Send the sketch version information to the gateway and Controller gw.sendSketchInfo("UV/Lux Sensor", "1.0"); // Register all sensors to gateway (they will be created as child devices) gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); gw.present(CHILD_ID_UV, S_UV); lightSensor.begin(); } void loop() { uint16_t lux = lightSensor.readLightLevel();// Get Lux value Serial.print("Lux reading: "); Serial.println(lux); if (lux != lastlux) { gw.send(msg.set(lux, 1)); lastlux = lux; } uint16_t uv = analogRead(0);// Get UV value Serial.print("Uv reading: "); Serial.println(uv); for (int i = 0; i < 13; i++) { if (uv <= uvIndexValue[i]) { uvIndex = i; break; } } Serial.print("Uv index: "); Serial.println(uvIndex); if (uvIndex != lastUV) { gw.send(uvMsg.set(uvIndex, 1)); lastUV = uvIndex; } gw.sleep(SLEEP_TIME); } -
There is a perfectly good explanation why UV sensor isn't showing up. MySensors does not distribute any device files for it. It might work with the rfx-plugin-device:
I remember @epierre working on the sensor, I helped him with the little sort logic on the sensor readings to determine the index... I cannot recall what device he used to present the index within Vera...
In china right now, the Great Firewall preventing a good search on the forum for that project...
perhaps @epierre can recall for you
-
@hek @BulldogLowell UV sensor works fine as standalone sensor / node. After trying different things I noticed that if I switch order of
these lines
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL); gw.present(CHILD_ID_UV, S_UV);to these below
gw.present(CHILD_ID_UV, S_UV); gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);then node and UV sensor show up in vera, but lux light is missing. It seems that sensor presented 1st gets recognized but second one omitted. Could it have to do anything with both devices using analog pins?

-
CHILD_ID_LIGHT and CHILD_ID_UV are defined as two different nonzero numbers, right?
Sorry I cannot read your sketch here in China!!
-
CHILD_ID_LIGHT and CHILD_ID_UV are defined as two different nonzero numbers, right?
Sorry I cannot read your sketch here in China!!
@BulldogLowell no, they were both defined as 0. I changed them to 1 and 2 and now three devices show up. Thanks.
-
Good. You may be able to use zero but certainly not for both!
-
I think my biggest challenge is understanding when to combine the additional sensor in the main loop or when to put it in another. The whole idea of interrupts is baffling to me too.