I tried to combine some scripts. first I had a 4 in 1: dht22, motion sensor and lux. Now I wanted to add a ds1820 for temperatur of warm water.
the idea is to send every 2 a 3 minutes all readings to domoticz. execpt motion that must be immediately.
this is my script:
#include <SPI.h>
#include <MySensor.h>
#include <DHT.h>
#include <BH1750.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
#define ONE_WIRE_BUS 5
#define MAX_ATTACHED_DS18B20 1
#define CHILD_ID_LIGHT 0
#define CHILD_ID_TEMP2 5
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_MOT 2 // Id of the sensor child
#define HUMIDITY_SENSOR_DIGITAL_PIN 4
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
BH1750 lightSensor;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
MySensor gw;
DHT dht;
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
boolean receivedConfig = false;
boolean metric = true;
float lastTemp;
float lastHum;
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgTemperature(CHILD_ID_TEMP2,V_TEMP);
uint16_t lastlux;
void setup()
{
// Startup up the OneWire library
sensors.begin();
// requestTemperatures() will not block current thread
sensors.setWaitForConversion(false);
// Startup and initialize MySensors library. Set callback for incoming messages.
gw.begin();
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
// Send the Sketch Version Information to the Gateway
gw.sendSketchInfo("Temp/Temp/Humidity/Motion/Lux", "1.0");
pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
// Register all sensors to gw (they will be created as child devices)
gw.present(CHILD_ID_TEMP2, S_TEMP);
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_MOT, S_MOTION);
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
metric = gw.getConfig().isMetric;
lightSensor.begin();
}
void loop()
{
// Read digital motion value
boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
Serial.println(tripped);
gw.send(msgMot.set(tripped?"1":"0")); // Send tripped value to gw
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("Temp: ");
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("Hum: ");
Serial.println(humidity);
}
uint16_t lux = lightSensor.readLightLevel();// Get Lux value
if (lux != lastlux) {
gw.send(msg.set(lux));
lastlux = lux;
Serial.print("lux: ");
Serial.println(lux);
}
// Ds1820
// Process incoming messages (like config from server)
gw.process();
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// query conversion time and sleep until conversion completed
int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
// sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
gw.sleep(conversionTime);
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
#if COMPARE_TEMP == 1
if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
#else
if (temperature != -127.00 && temperature != 85.00) {
#endif
// Send in the new temperature
gw.send(msg.setSensor(i).set(temperature,1));
// Save new temperatures for next compare
lastTemperature[i]=temperature;
Serial.print("DS1820: ");
Serial.println(temperature);
}
}
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
}
and this is the serial output:
send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
read: 0-0-1 s=255,c=3,t=6,pt=0,l=1,sg=0:M
sensor started, id=1, parent=0, distance=1
send: 1-1-0-0 s=255,c=3,t=11,pt=0,l=25,sg=0,st=ok:Temp/Temp/Humidity/Motion
send: 1-1-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
send: 1-1-0-0 s=5,c=0,t=6,pt=0,l=0,sg=0,st=ok:
send: 1-1-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
send: 1-1-0-0 s=1,c=0,t=6,pt=0,l=0,sg=0,st=ok:
send: 1-1-0-0 s=2,c=0,t=1,pt=0,l=0,sg=0,st=ok:
send: 1-1-0-0 s=0,c=0,t=16,pt=0,l=0,sg=0,st=ok:
0
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
send: 1-1-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:22.9
Temp: 22.90
send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:41.8
Hum: 41.80
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:984
lux: 984
1
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:977
lux: 977
0
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:41.9
Hum: 41.90
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:979
lux: 979
0
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:41.8
Hum: 41.80
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:978
lux: 978
0
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
send: 1-1-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:41.9
Hum: 41.90
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:977
lux: 977
1
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=fail:1
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=ok:660
lux: 660
0
send: 1-1-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
send: 1-1-0-0 s=0,c=1,t=23,pt=3,l=2,sg=0,st=fail:593
lux: 593
1
what is wrong?