that is strange...
I have some serial debugging info in my code like this, you might want to include it in your code as well to understand what is going on...
Here is my full sketch so you can understand better where the above code is located, but there a few other sensors as well as the OLED added.
Code: Select all
// V2.0
/*
* 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"
#include "ZUNO_OLED_I2C.h"
#define DEBUG
#define DEBUG_BMP180
//#define DEBUG_DS18B20
// HW declaration
// use pin 11 for DHT sensor
DHT dht11_sensor(11, DHT11);
ZUNO_BMP180 bmp;
OLED oled;
#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);
boolean InitDone = false;
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
word lastTempBMP180 = 1990; // variable to store the temperature from the BMP180, precision is 1 digit -> 199.0 C as default value
word 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
#define T_READANALOG 500
unsigned long WaitMillis_tReport;
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_TWO_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_TWO_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);
#define ZUNO_CHANNEL_DIMMER 1
#define ZUNO_CHANNEL_TEMP 2
#define ZUNO_CHANNEL_HUM 3
#define ZUNO_CHANNEL_LUM 4
#define ZUNO_CHANNEL_MOTION 5
#define ZUNO_CHANNEL_SWITCH 6
#define ZUNO_CHANNEL_TEMP_BMP180 7
#define ZUNO_CHANNEL_PRESSURE 8
#define ZUNO_CHANNEL_TEMPDS18B20 9
void setup() {
#ifdef DEBUG
Serial.begin();
Serial.println("start");
#endif
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
dht11_sensor.begin();
bmp.begin(); // calls internally Wire.begin()
// ds1820.scanAloneSensor(addrDS18B20); // static address defined
oled.begin();
oled.clrscr();
}
void myInit() {
// initial setup for timer values
WaitMillis_tReport = millis() + 5000;
WaitMillis_tReadAnalog = millis() + 5000;
}
void loop() {
if (InitDone != true) {
myInit();
InitDone = true;
}
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(); // read values for CH1 / CH2
readLuminance(); // read value for CH4
readBMP180(); // read values for CH7 / CH8
readDS18B20(); // read value for CH9
// zunoSendReport(ZUNO_CHANNEL_DIMMER); // report the state of the dimmer
zunoSendReport(ZUNO_CHANNEL_TEMP); // report the temperature from the DHT11
zunoSendReport(ZUNO_CHANNEL_HUM); // report the relative humidity from the DHT11
zunoSendReport(ZUNO_CHANNEL_LUM); // report the luminance
// zunoSendReport(ZUNO_CHANNEL_MOTION); // report the motion
// zunoSendReport(ZUNO_CHANNEL_SWITCH); // report the state of the switch
zunoSendReport(ZUNO_CHANNEL_TEMP_BMP180); // report the temperature from the BMP180
zunoSendReport(ZUNO_CHANNEL_PRESSURE); // report the pressure from the BMP180
zunoSendReport(ZUNO_CHANNEL_TEMPDS18B20); // report the temperature from the DS8B20
printOLED();
WaitMillis_tReport += T_REPORT; // set next timer value
}
delay(200); // wait at least 200 ms
}
void printOLED() {
// oled.clrscr();
oled.gotoXY(0,0);
oled.print("Millis:");
oled.print(millis());
oled.gotoXY(0,1);
oled.print("Temperature ");
oled.print(lastTempBMP180/10.0);
oled.println(" *C");
oled.print("Pressure ");
oled.print(lastPressureBMP180/100.0);
oled.println(" kpa");
}
void readDS18B20() {
// ds1820.scanAloneSensor(addrDS18B20);
float temp = ds1820.getTemperature(addrDS18B20);
lastTempDS18B20 = int(100.0 * temp);
#ifdef DEBUG
#ifdef DEBUG_DS18B20
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);
#endif
#endif
}
void readBMP180() {
lastTempBMP180 = bmp.readTemperature()*10; // precision is 1 digit, so value is at a factor of 10
lastPressureBMP180 = bmp.readPressure()/10; // precision is 2 digits, so value is at a factor of 100 (in kpa)
#ifdef DEBUG
#ifdef DEBUG_BMP180
Serial.println("readBMP180");
// bmp.dumpInternal();
// Serial.println(bmp.readRawTemperature());
Serial.print("BMP 180: Temperatur: ");
Serial.print(lastTempBMP180/10.0);
Serial.print(" C, Druck: ");
Serial.print(lastPressureBMP180/100.0);
Serial.println(" kpa");
#endif
#endif
}
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() {
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() {
return lastTempBMP180;
}
// CH8 - Pressure
//getterPressure
long getterPressure() {
return lastPressureBMP180;
}
// CH9 - TempDS18B20
word getterTempDS18B20() {
word t;
t = (word)(lastTempDS18B20);
return t;
}
Andreas.