Hi,
i haven't tried the original example but have now included a DS18B20 sensor in my mulit-sensor test sketch. My code is based on the example, I redefined the pin to 16, changed some names, but basically the code is the same.
One thing I changed is that after I had it up-and-running, I made the address of the DS18B20 static and removed the "ds1820.scanAloneSensor(addr1);" statement. I just realized now that p0lyg0n1 has proposed this in the revised example.
I have made the change for the delay time in Onewire as mentioned by p0lyg0n1.
I also added some delays in my sketch, in the setup() I added a 2 second delay (dealy(2000)) before initializing the DTH sensor and tbe BMP180 sensor. I also initialize my waiting functions to have the first readings after 5 seconds.
Maybe these delays do the trick...
With my current sketch I can't confirm your behavior. As I have already 9 channels created and integrated the sensors in my system, I can not test this sketch specifically without loosing my setup...
During my initial testing, sometimes the first reading was -327.67 but after that it is ok. One thing I noted was that in my first (HW)setup I forgot the pull-up resistor, leading to an Address of "all zeros" and the value was then also -327.67 as this is the default fault value. But without the pull-up sensor there was no measurement possible at all...
If you like you can have a look at my current sketch (I increased the waiting timt to 15 seconds, but my current sketch is still programmed with 5 seconds, I didn't updated it yet)
Code: Select all
/*
* This is based on the Simple Multilevel Dimmer example,
* the dht22_test example, the Simple MultiSensor example
* and the 1-wire DS18B20 example...
* It gives you the ability to control the
* built in LED brightness via Z-Wave commands
* It should report values from the DHT11 sensor on the multisensor channnel
*/
#include "ZUNO_DHT.h"
#include "Wire.h"
#include "ZUNO_BMP180.h"
#include "ZUNO_DS18B20.h"
// HW declaration
// use pin 11 for DHT sensor
DHT dht11_sensor(11, DHT11);
ZUNO_BMP180 bmp;
#define LUMINANCE_PIN 6
#define SWITCH_LED_PIN 9
#define MOTION_LED_PIN 10
#define MOTION_PIN 12
#define LED_PIN 13
#define SCL_PIN 14
#define SDA_PIN 15
#define PIN_DS18B20 16
OneWire ow(PIN_DS18B20);
DS18B20Sensor ds1820(&ow);
I2CDriver alternative_I2C(SCL_PIN, SDA_PIN);
byte lastSetDimmer = 0; // variable to store current dimmer value
float temperature = -99.0; // variable to store the temperature
float humidity = -99.0; // variable to store the humidity
byte currentLEDValue = 0; // variables to store the current switch state
byte lastLuminanceValue = 0; // variable to store the last luminance
byte lastMotionValue = 0; // motion
float lastTempBMP180 = -99.0; // variable to store the temperature from the BMP180
long lastPressureBMP180 = 0; // variable to store the pressure from the BMP180
int lastTempDS18B20 = 0; // variable to store the temperature from the DS18B20
byte addrDS18B20[8] = {0x28, 0x8c, 0xde, 0x26, 0x00, 0x00, 0x80, 0x80}; // static address of "my" DS18B20
// timer values
#define T_REPORT 30000
unsigned long WaitMillis_tReport;
#define T_READANALOG 500
unsigned long WaitMillis_tReadAnalog;
// set up the Z-Uno channels
// definition converted to "one" line to get warnings/error reported with correct line numbers
// CH1 - Dimmer: ZUNO_SWITCH_MULTILEVEL(getterDim, setterDim)
// CH2 - Temperature: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterTemp)
// CH3 - Humidity: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterHum)
// CH4 - Luminance: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_ONE_BYTE, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterLum)
// CH5 - Motion: ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_MOTION, getterMotion)
// CH6 - Switch: ZUNO_SWITCH_BINARY(getterSwitch, setterSwitch)
// CH7 - TempBMP180: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterTempBMP180)
// CH8 - Pressure: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_ATMOSPHERIC_PRESSURE,SENSOR_MULTILEVEL_SCALE_KILOPASCAL, SENSOR_MULTILEVEL_SIZE_FOUR_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterPressure)
// CH9 - TempDS18B20: ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTempDS18B20)
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_MULTILEVEL(getterDim, setterDim), ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE, SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterTemp), ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_RELATIVE_HUMIDITY, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterHum), ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_LUMINANCE, SENSOR_MULTILEVEL_SCALE_PERCENTAGE_VALUE, SENSOR_MULTILEVEL_SIZE_ONE_BYTE, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterLum), ZUNO_SENSOR_BINARY(ZUNO_SENSOR_BINARY_TYPE_MOTION, getterMotion), ZUNO_SWITCH_BINARY(getterSwitch, setterSwitch), ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_ONE_DECIMAL, getterTempBMP180),ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_ATMOSPHERIC_PRESSURE,SENSOR_MULTILEVEL_SCALE_KILOPASCAL, SENSOR_MULTILEVEL_SIZE_FOUR_BYTES, SENSOR_MULTILEVEL_PRECISION_ZERO_DECIMALS, getterPressure), ZUNO_SENSOR_MULTILEVEL(ZUNO_SENSOR_MULTILEVEL_TYPE_TEMPERATURE,SENSOR_MULTILEVEL_SCALE_CELSIUS, SENSOR_MULTILEVEL_SIZE_TWO_BYTES, SENSOR_MULTILEVEL_PRECISION_TWO_DECIMALS, getterTempDS18B20));
ZUNO_SETUP_DEBUG_MODE(DEBUG_ON);
void setup() {
// Serial.begin();
// Serial.println("start");
pinMode(LUMINANCE_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(SWITCH_LED_PIN, OUTPUT);
pinMode(MOTION_LED_PIN, OUTPUT);
pinMode(MOTION_PIN, INPUT);
Wire.bindDriver(&alternative_I2C); // This instructs to use other pins than default
// initial setup for timer values
WaitMillis_tReport = millis() + 15000;
WaitMillis_tReadAnalog = millis() + 15000;
delay(2000); // wait that sensors are ready ??
dht11_sensor.begin();
bmp.begin(); // calls internally Wire.begin()
// ds1820.scanAloneSensor(addrDS18B20); // static address defined
}
void loop() {
if ( (long)( millis() - WaitMillis_tReadAnalog ) >= 0) {
// don't check in every loop...
checkMotion(); // check motion sensor
WaitMillis_tReadAnalog += T_READANALOG; // set next timer value
}
// update values and send out reports
if ( (long)( millis() - WaitMillis_tReport ) >= 0) {
// Loop for reports with timer every T_REPORTS
read_DHT();
readLuminance();
readBMP180();
readDS18B20();
// zunoSendReport(1); // report the state of the dimmer in channel 1
zunoSendReport(2); // report the temperature from the DHT11 in channel 2
zunoSendReport(3); // report the relative humidity from the DHT11 in channel 3
zunoSendReport(4); // report the luminance in channel 4
// zunoSendReport(5); // report the motion in channel 5
// zunoSendReport(6); // report the state of the switch in channel 6
zunoSendReport(7); // report the temperature from the BMP180 in channel 7
zunoSendReport(8); // report the pressure from the BMP180 in channel 8
zunoSendReport(9); // report the temperature from the DS8B20 in channel 9
WaitMillis_tReport += T_REPORT; // set next timer value
}
delay(200); // wait at least 200 ms
}
void readDS18B20() {
// ds1820.scanAloneSensor(addrDS18B20);
float temp = ds1820.getTemperature(addrDS18B20);
lastTempDS18B20 = int(100.0 * temp);
// Serial.print("Your sensor address is: ");
// for(int i = 0; i < 8; i++) {
// // print OneWire code
// Serial.print(addrDS18B20[i], HEX);
// Serial.print(" ");
// }
// Serial.println();
//
// Serial.print("Temperature: ");
// Serial.println(temp);
}
void readBMP180() {
lastTempBMP180 = bmp.readTemperature();
lastPressureBMP180 = bmp.readPressure();
// bmp.dumpInternal();
// Serial.println(bmp.readRawTemperature());
//
// Serial.print("BMP 180: Temperatur: ");
// Serial.print(lastTempBMP180);
// Serial.print(" C, Druck: ");
// Serial.print(lastPressureBMP180);
// Serial.println(" kpa");
}
void read_DHT() {
byte result;
result = dht11_sensor.read(true);
if (result == 0) {
// only update temperature if reading is ok
temperature = dht11_sensor.readTemperature();
humidity = dht11_sensor.readHumidity();
}
}
void readLuminance() {
lastLuminanceValue = (byte) ((0x3FF - analogRead(A3)) / 10.23); // scale 0..1023 to 0..100 (%), sensor is inverted 0=light, 1023=dark
}
void checkMotion() {
byte currentSensorValue = digitalRead(MOTION_PIN);
if (currentSensorValue != lastMotionValue) {
lastMotionValue = currentSensorValue;
zunoSendReport(5); // send report directly, motion is not reported in reporting loop
if (currentSensorValue == HIGH) {
digitalWrite(MOTION_LED_PIN, HIGH);
} else {
digitalWrite(MOTION_LED_PIN, LOW);
}
}
}
// getter / setter function calls
// CH1 - Dimmer
byte getterDim(void) {
return lastSetDimmer;
}
void setterDim(byte value) {
byte tempVariable;
if (value > 99) {
// by Z-Wave specification, this value can't be more then 99
value = 99;
}
// set the LED brightness scaled to 0..99
analogWrite(PWM1, ((long)value) * 255 / 99);
lastSetDimmer = value;
// // report the state in channel 1
// zunoSendReport(1);
}
// CH2 - Temperature
word getterTemp() {
// read_DHT(); // Update value before sending, normal update only during report loop
word t;
t = (word)(10.0 * temperature);
return t;
}
// CH3 - Humidity
word getterHum() {
word h;
h = (word)(10.0 * humidity);
return h;
}
// CH4 - Luminance
byte getterLum(void) {
return lastLuminanceValue;
}
// CH5 - Motion
byte getterMotion() {
if (lastMotionValue == 1) { // if motion is detected
return 0xff; // return "Triggered" state to the controller
} else { // if motion stopped
return 0; // return "Idle" state to the controller
}
}
// CH6 - Switch
byte getterSwitch () {
return currentLEDValue;
}
void setterSwitch (byte value) {
if (value > 0) {
digitalWrite (SWITCH_LED_PIN, HIGH); //turn LED on
} else {
digitalWrite(SWITCH_LED_PIN, LOW); //turn LED off
}
currentLEDValue = value;
}
// CH7 - TempBMP180
word getterTempBMP180() {
word t;
t = (word)(10.0 * lastTempBMP180);
return t;
}
// CH8 - Pressure
//getterPressure
long getterPressure() {
return lastPressureBMP180;
}
// CH9 - TempDS18B20
word getterTempDS18B20() {
word t;
t = (word)(lastTempDS18B20);
return t;
}
// getterTempDS18B20
There is still something to clean up and some room for optimization, but for now it is working quite good.
Regards,
Andreas.