From 92fb46ea0f630e5cea64964d33799cf39eb88dc9 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 31 May 2016 21:11:53 +0200 Subject: [PATCH] Fixed some issues with oregon protocoll and first version of HalMultiSensor done. --- arduino/HalMultiSensor/HalConfiguration.h | 12 +- arduino/HalMultiSensor/HalInterfaces.h | 3 +- arduino/HalMultiSensor/HalMultiSensor.ino | 6 +- arduino/HalMultiSensor/Interrupt.cpp | 2 +- arduino/HalMultiSensor/ProtocolOregon.cpp | 20 +- arduino/HalMultiSensor/SensorPhotocell.cpp | 5 + arduino/PowerTransmitter/BH1750.cpp | 93 ------ arduino/PowerTransmitter/BH1750.h | 73 ----- .../{PowerTransmitter => }/BH1750FVI.cpp | 0 .../{PowerTransmitter => }/BH1750FVI.h | 0 arduino/PowerTransmitter/PowerTransmitter.ino | 308 ++++++++++++++++-- .../PowerTransmitter/PowerTransmitter.ino | 295 ----------------- src/se/hal/ControllerManager.java | 4 +- .../tellstick/protocols/Oregon0x1A2D.java | 48 ++- 14 files changed, 333 insertions(+), 536 deletions(-) delete mode 100755 arduino/PowerTransmitter/BH1750.cpp delete mode 100755 arduino/PowerTransmitter/BH1750.h rename arduino/PowerTransmitter/{PowerTransmitter => }/BH1750FVI.cpp (100%) rename arduino/PowerTransmitter/{PowerTransmitter => }/BH1750FVI.h (100%) delete mode 100755 arduino/PowerTransmitter/PowerTransmitter/PowerTransmitter.ino diff --git a/arduino/HalMultiSensor/HalConfiguration.h b/arduino/HalMultiSensor/HalConfiguration.h index 13f852dc..0b1b0400 100755 --- a/arduino/HalMultiSensor/HalConfiguration.h +++ b/arduino/HalMultiSensor/HalConfiguration.h @@ -1,19 +1,19 @@ #ifndef HALCONFIGURATION_H #define HALCONFIGURATION_H -#define ENABLE_DEBUG // comment out to disable debug +//#define ENABLE_DEBUG // comment out to disable debug -#define TIMER_MILLISECOND 10*1000 // poling in minutes -#define INDICATOR_PIN 13 // diod +#define TIMER_MILLISECOND 60*1000 // poling in minutes +#define INDICATOR_PIN 13 // diode // POWER CONSUMPTION SENSOR #define POWERCON_ENABLED // comment out to disable sensor -#define POWERCON_SENSOR SensorBH1750() -#define POWERCON_PROTOCOL ProtocolOregon(11, 118) +#define POWERCON_SENSOR SensorPhotocell() +#define POWERCON_PROTOCOL ProtocolOregon(11, 186) #define POWER_TIMER_MULTIPLIER 1 // TEMPERATURE SENSOR -#define TEMPERATURE_ENABLED // comment out to disable sensor +//#define TEMPERATURE_ENABLED // comment out to disable sensor #define TEMPERATURE_SENSOR SensorDHT(DHT22, 10) #define TEMPERATURE_PROTOCOL ProtocolOregon(11, 100) #define TEMPERATURE_TIMER_MULTIPLIER 1 diff --git a/arduino/HalMultiSensor/HalInterfaces.h b/arduino/HalMultiSensor/HalInterfaces.h index a4966c72..2b858a98 100755 --- a/arduino/HalMultiSensor/HalInterfaces.h +++ b/arduino/HalMultiSensor/HalInterfaces.h @@ -1,13 +1,12 @@ #ifndef HALINTERFACES_H #define HALINTERFACES_H +#include #include "HalConfiguration.h" // Utility functions #ifdef ENABLE_DEBUG - #include - #define DEBUG(msg) \ Serial.println(msg); \ Serial.flush(); diff --git a/arduino/HalMultiSensor/HalMultiSensor.ino b/arduino/HalMultiSensor/HalMultiSensor.ino index 39955ece..07ea10f3 100755 --- a/arduino/HalMultiSensor/HalMultiSensor.ino +++ b/arduino/HalMultiSensor/HalMultiSensor.ino @@ -60,7 +60,7 @@ void setup() lightProtocol->setup(); #endif - DEBUG("Setup INTERRUPT"); + DEBUG("Setup SLEEP_INTERRUPT"); Interrupt::setWatchDogCallback(timerInterruptFunc); Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt @@ -80,7 +80,7 @@ void timerInterruptFunc() void loop() { digitalWrite(INDICATOR_PIN, HIGH); - noInterrupts(); + //noInterrupts(); // Send power consumption #ifdef POWERCON_ENABLED @@ -115,7 +115,7 @@ void loop() } #endif - interrupts(); + //interrupts(); digitalWrite(INDICATOR_PIN, LOW); DEBUG("Sleeping"); diff --git a/arduino/HalMultiSensor/Interrupt.cpp b/arduino/HalMultiSensor/Interrupt.cpp index 21f944c9..d02cd9ca 100755 --- a/arduino/HalMultiSensor/Interrupt.cpp +++ b/arduino/HalMultiSensor/Interrupt.cpp @@ -71,7 +71,7 @@ void Interrupt::setupPinInterrupt(int pin) * * In all but the IDLE sleep modes only LOW can be used. */ - attachInterrupt((pin == PIND2 ? 0 : 1), Interrupt::handlePinInterrupt, LOW); + attachInterrupt((pin == PIND2 ? 0 : 1), Interrupt::handlePinInterrupt, RISING); //detachInterrupt(0); // disables interrupt 0 on pin 2 so the // wakeUpNow code will not be executed diff --git a/arduino/HalMultiSensor/ProtocolOregon.cpp b/arduino/HalMultiSensor/ProtocolOregon.cpp index 21ec4166..42212f91 100755 --- a/arduino/HalMultiSensor/ProtocolOregon.cpp +++ b/arduino/HalMultiSensor/ProtocolOregon.cpp @@ -49,7 +49,7 @@ void ProtocolOregon::send(float temperature, short humidity) * \param data Oregon message * \param type Sensor type */ -inline void ProtocolOregon::setType(byte *data, byte b1, byte b2) +inline void ProtocolOregon::setType(byte data[], byte b1, byte b2) { data[0] = b1; data[1] = b2; @@ -60,13 +60,13 @@ inline void ProtocolOregon::setType(byte *data, byte b1, byte b2) * \param data Oregon message * \param channel Sensor channel (0x10, 0x20, 0x30) */ -inline void ProtocolOregon::setChannel(byte *data, byte channel) +inline void ProtocolOregon::setChannel(byte data[], byte channel) { data[2] = channel; } -inline void ProtocolOregon::setId(byte *data, byte id) +inline void ProtocolOregon::setId(byte data[], byte id) { data[3] = id; } @@ -74,13 +74,13 @@ inline void ProtocolOregon::setId(byte *data, byte id) /** * \param level false: low, true: high */ -inline void ProtocolOregon::setBatteryLevel(byte *data, bool level) +inline void ProtocolOregon::setBatteryLevel(byte data[], bool level) { if(!level) data[4] = 0x0C; else data[4] = 0x00; } -inline void ProtocolOregon::setTemperature(byte *data, float temp) +inline void ProtocolOregon::setTemperature(byte data[], float temp) { // Set temperature sign if(temp < 0) @@ -108,13 +108,13 @@ inline void ProtocolOregon::setTemperature(byte *data, float temp) data[4] |= (tempFloat << 4); } -inline void ProtocolOregon::setHumidity(byte* data, byte hum) +inline void ProtocolOregon::setHumidity(byte data[], byte hum) { data[7] = (hum/10); data[6] |= (hum - data[7]*10) << 4; } -inline void ProtocolOregon::calculateAndSetChecksum(byte* data) +inline void ProtocolOregon::calculateAndSetChecksum(byte data[]) { int sum = 0; for(byte i = 0; i<8;i++) @@ -173,9 +173,9 @@ inline void ProtocolOregon::sendOne(void) * \param data Data to send * \param length size of data array */ -void ProtocolOregon::sendData(byte *data, byte length) +void ProtocolOregon::sendData(byte data[], byte length) { - for(byte i = 0; i < length; ++i) + for (byte i=0; i +unsigned int SensorPhotocell::pulse = 0; + void SensorPhotocell::interruptHandler() { + digitalWrite(INDICATOR_PIN, HIGH); + DEBUG("PHCELL: INTERRUPT"); ++pulse; + digitalWrite(INDICATOR_PIN, LOW); } diff --git a/arduino/PowerTransmitter/BH1750.cpp b/arduino/PowerTransmitter/BH1750.cpp deleted file mode 100755 index 306838bc..00000000 --- a/arduino/PowerTransmitter/BH1750.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - -This is a library for the BH1750FVI Digital Light Sensor -breakout board. - -The board uses I2C for communication. 2 pins are required to -interface to the device. - - -Written by Christopher Laws, March, 2013. - -*/ - -#include "BH1750.h" -#include - - -BH1750::BH1750() {} - -void BH1750::begin(uint8_t mode) { - Wire.begin(); - //write8(mode); - configure(mode); -} - - -void BH1750::configure(uint8_t mode) { - switch (mode) { - case BH1750_CONTINUOUS_HIGH_RES_MODE: - case BH1750_CONTINUOUS_HIGH_RES_MODE_2: - case BH1750_CONTINUOUS_LOW_RES_MODE: - case BH1750_ONE_TIME_HIGH_RES_MODE: - case BH1750_ONE_TIME_HIGH_RES_MODE_2: - case BH1750_ONE_TIME_LOW_RES_MODE: - // apply a valid mode change - write8(mode); - _delay_ms(10); - break; - default: - // Invalid measurement mode - #if BH1750_DEBUG == 1 - Serial.println("Invalid measurement mode"); - #endif - break; - } -} - - -uint16_t BH1750::readLightLevel(void) { - uint16_t level; - - Wire.beginTransmission(BH1750_I2CADDR); - Wire.requestFrom(BH1750_I2CADDR, 2); -#if (ARDUINO >= 100) - level = Wire.read(); - level <<= 8; - level |= Wire.read(); -#else - level = Wire.receive(); - level <<= 8; - level |= Wire.receive(); -#endif - Wire.endTransmission(); - -#if BH1750_DEBUG == 1 - Serial.print("Raw light level: "); - Serial.println(level); -#endif - - level = level/1.2; // convert to lux - -#if BH1750_DEBUG == 1 - Serial.print("Light level: "); - Serial.println(level); -#endif - return level; -} - - - -/*********************************************************************/ - - -void BH1750::write8(uint8_t d) { - Wire.beginTransmission(BH1750_I2CADDR); -#if (ARDUINO >= 100) - Wire.write(d); -#else - Wire.send(d); -#endif - Wire.endTransmission(); -} - diff --git a/arduino/PowerTransmitter/BH1750.h b/arduino/PowerTransmitter/BH1750.h deleted file mode 100755 index 68364fcd..00000000 --- a/arduino/PowerTransmitter/BH1750.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - -This is a library for the BH1750FVI Digital Light Sensor -breakout board. - -The board uses I2C for communication. 2 pins are required to -interface to the device. - -Datasheet: -http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf - -Written by Christopher Laws, March, 2013. - -*/ - -#ifndef BH1750_h -#define BH1750_h - -#if (ARDUINO >= 100) -#include -#else -#include -#endif -#include "Wire.h" - -#define BH1750_DEBUG 0 - -#define BH1750_I2CADDR 0x23 - -// No active state -#define BH1750_POWER_DOWN 0x00 - -// Wating for measurment command -#define BH1750_POWER_ON 0x01 - -// Reset data register value - not accepted in POWER_DOWN mode -#define BH1750_RESET 0x07 - -// Start measurement at 1lx resolution. Measurement time is approx 120ms. -#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 - -// Start measurement at 0.5lx resolution. Measurement time is approx 120ms. -#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 - -// Start measurement at 4lx resolution. Measurement time is approx 16ms. -#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 - -// Start measurement at 1lx resolution. Measurement time is approx 120ms. -// Device is automatically set to Power Down after measurement. -#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 - -// Start measurement at 0.5lx resolution. Measurement time is approx 120ms. -// Device is automatically set to Power Down after measurement. -#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 - -// Start measurement at 1lx resolution. Measurement time is approx 120ms. -// Device is automatically set to Power Down after measurement. -#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 - -class BH1750 { - public: - BH1750(); - void begin(uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE); - void configure(uint8_t mode); - uint16_t readLightLevel(void); - - private: - void write8(uint8_t data); - -}; - -#endif - diff --git a/arduino/PowerTransmitter/PowerTransmitter/BH1750FVI.cpp b/arduino/PowerTransmitter/BH1750FVI.cpp similarity index 100% rename from arduino/PowerTransmitter/PowerTransmitter/BH1750FVI.cpp rename to arduino/PowerTransmitter/BH1750FVI.cpp diff --git a/arduino/PowerTransmitter/PowerTransmitter/BH1750FVI.h b/arduino/PowerTransmitter/BH1750FVI.h similarity index 100% rename from arduino/PowerTransmitter/PowerTransmitter/BH1750FVI.h rename to arduino/PowerTransmitter/BH1750FVI.h diff --git a/arduino/PowerTransmitter/PowerTransmitter.ino b/arduino/PowerTransmitter/PowerTransmitter.ino index 555fbd77..c7a826db 100755 --- a/arduino/PowerTransmitter/PowerTransmitter.ino +++ b/arduino/PowerTransmitter/PowerTransmitter.ino @@ -1,37 +1,295 @@ /* - -Example of BH1750 library usage. - -This example initalises the BH1750 object using the default -high resolution mode and then makes a light level reading every second. - -Connection: - VCC-5v - GND-GND - SCL-SCL(analog pin 5) - SDA-SDA(analog pin 4) - ADD-NC or GND - +* Protocol: Oregon V2.1 +* Emulating sensor: THGR2228N */ #include -#include "BH1750.h" +#include "BH1750FVI.h" +BH1750FVI LightSensor; +const byte TX_PIN = 11; +const byte LED_PIN = 13; +const unsigned long TIME = 512; +const unsigned long TWOTIME = TIME*2; +#define SEND_HIGH() digitalWrite(TX_PIN, HIGH) +#define SEND_LOW() digitalWrite(TX_PIN, LOW) +byte OregonMessageBuffer[9]; +unsigned long previousTime = 0; +unsigned long currentTime = millis(); +int impulseCount = 0; -BH1750 lightMeter; - -void setup(){ +void setup() +{ Serial.begin(9600); - lightMeter.begin(BH1750_ONE_TIME_LOW_RES_MODE); - Serial.println("Running..."); + + pinMode(TX_PIN, OUTPUT); + pinMode(LED_PIN, OUTPUT); + SEND_LOW(); + byte ID[] = { 0x1A,0x2D }; //temperature/humidity sensor (THGR2228N) + setType(OregonMessageBuffer, ID); + setChannel(OregonMessageBuffer, 0x20); + + LightSensor.begin(); + LightSensor.SetAddress(Device_Address_L); + LightSensor.SetMode(Continuous_H_resolution_Mode); + Serial.print("Started"); } -void loop() { - uint16_t lux = lightMeter.readLightLevel(); - Serial.print("Light: "); - Serial.print(lux); - Serial.println(" lx"); - delay(1000); +boolean light = false; +void loop() +{ + currentTime = millis(); + uint16_t lux = LightSensor.GetLightIntensity(); + //Serial.print("lux="); + //Serial.println(lux); + if(lux > 40 && !light){ + light = true; + impulseCount++; + }else if(lux < 40){ + light = false; + } + if(currentTime - previousTime > 60000) { + previousTime = currentTime; + Serial.print("total impulses = "); + Serial.println(impulseCount); + send433(impulseCount+5,0,0xBA); + impulseCount = 0; + delay(500); + } } +void send433(float temperature, byte humidity, byte Identitet) +{ + digitalWrite(LED_PIN, HIGH); + setId(OregonMessageBuffer, Identitet); //set id of the sensor, BB=187 + setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high + setTemperature(OregonMessageBuffer, temperature); //org setTemperature(OregonMessageBuffer, 55.5); + setHumidity(OregonMessageBuffer, humidity); + calculateAndSetChecksum(OregonMessageBuffer); + + // Show the Oregon Message + for (byte i = 0; i < sizeof(OregonMessageBuffer); ++i) { + Serial.print(OregonMessageBuffer[i] >> 4, HEX); + Serial.print(OregonMessageBuffer[i] & 0x0F, HEX); + } + Serial.println(); + + // Send the Message over RF + sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer)); + // Send a "pause" + SEND_LOW(); + delayMicroseconds(TWOTIME*8); + // Send a copie of the first message. The v2.1 protocol send the message two time + sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer)); + SEND_LOW(); + digitalWrite(LED_PIN, LOW); +} + +inline void setId(byte *data, byte ID) +{ + data[3] = ID; +} + +void setBatteryLevel(byte *data, byte level) +{ + if(!level) data[4] = 0x0C; + else data[4] = 0x00; +} + +void setTemperature(byte *data, float temp) +{ + // Set temperature sign + if(temp < 0) + { + data[6] = 0x08; + temp *= -1; + } + else + { + data[6] = 0x00; + } + + // Determine decimal and float part + int tempInt = (int)temp; + int td = (int)(tempInt / 10); + int tf = (int)round((float)((float)tempInt/10 - (float)td) * 10); + + int tempFloat = (int)round((float)(temp - (float)tempInt) * 10); + + // Set temperature decimal part + data[5] = (td << 4); + data[5] |= tf; + + // Set temperature float part + data[4] |= (tempFloat << 4); +} + +void setHumidity(byte* data, byte hum) +{ + data[7] = (hum/10); + data[6] |= (hum - data[7]*10) << 4; +} + +void calculateAndSetChecksum(byte* data) +{ + int sum = 0; + for(byte i = 0; i<8;i++) + { + sum += (data[i]&0xF0) >> 4; + sum += (data[i]&0xF); + } + data[8] = ((sum - 0xa) & 0xFF); +} + + + + +//********************************************************************************************************* + + +/** + * \brief Send logical "0" over RF + * \details azero bit be represented by an off-to-on transition + * \ of the RF signal at the middle of a clock period. + * \ Remenber, the Oregon v2.1 protocol add an inverted bit first + */ +inline void sendZero(void) +{ + SEND_HIGH(); + delayMicroseconds(TIME); + SEND_LOW(); + delayMicroseconds(TWOTIME); + SEND_HIGH(); + delayMicroseconds(TIME); +} + +/** + * \brief Send logical "1" over RF + * \details a one bit be represented by an on-to-off transition + * \ of the RF signal at the middle of a clock period. + * \ Remenber, the Oregon v2.1 protocol add an inverted bit first + */ +inline void sendOne(void) +{ + SEND_LOW(); + delayMicroseconds(TIME); + SEND_HIGH(); + delayMicroseconds(TWOTIME); + SEND_LOW(); + delayMicroseconds(TIME); +} + +/** + * \brief Send a bits quarter (4 bits = MSB from 8 bits value) over RF + * \param data Data to send + */ +inline void sendQuarterMSB(const byte data) +{ + (bitRead(data, 4)) ? sendOne() : sendZero(); + (bitRead(data, 5)) ? sendOne() : sendZero(); + (bitRead(data, 6)) ? sendOne() : sendZero(); + (bitRead(data, 7)) ? sendOne() : sendZero(); +} + +/** + * \brief Send a bits quarter (4 bits = LSB from 8 bits value) over RF + * \param data Data to send + */ +inline void sendQuarterLSB(const byte data) +{ + (bitRead(data, 0)) ? sendOne() : sendZero(); + (bitRead(data, 1)) ? sendOne() : sendZero(); + (bitRead(data, 2)) ? sendOne() : sendZero(); + (bitRead(data, 3)) ? sendOne() : sendZero(); +} + +/******************************************************************/ +/******************************************************************/ +/******************************************************************/ + +/** + * \brief Send a buffer over RF + * \param data Data to send + * \param size size of data to send + */ +void sendData(byte *data, byte size) +{ + for(byte i = 0; i < size; ++i) + { + sendQuarterLSB(data[i]); + sendQuarterMSB(data[i]); + } +} + +/** + * \brief Send an Oregon message + * \param data The Oregon message + */ +void sendOregon(byte *data, byte size) +{ + sendPreamble(); + //sendSync(); + sendData(data, size); + sendPostamble(); +} + +/** + * \brief Send preamble + * \details The preamble consists of 16 "1" bits + */ +inline void sendPreamble(void) +{ + byte PREAMBLE[]={ + 0xFF,0xFF }; + sendData(PREAMBLE, 2); +} + +/** + * \brief Send postamble + * \details The postamble consists of 8 "0" bits + */ +inline void sendPostamble(void) +{ + byte POSTAMBLE[]={ + 0x00 }; + sendData(POSTAMBLE, 1); +} + +/** + * \brief Send sync nibble + * \details The sync is 0xA. It is not use in this version since the sync nibble + * \ is include in the Oregon message to send. + */ +inline void sendSync(void) +{ + sendQuarterLSB(0xA); +} + +/******************************************************************/ +/******************************************************************/ +/******************************************************************/ + +/** + * \brief Set the sensor type + * \param data Oregon message + * \param type Sensor type + */ +inline void setType(byte *data, byte* type) +{ + data[0] = type[0]; + data[1] = type[1]; +} + +/** + * \brief Set the sensor channel + * \param data Oregon message + * \param channel Sensor channel (0x10, 0x20, 0x30) + */ +inline void setChannel(byte *data, byte channel) +{ + data[2] = channel; +} + + + diff --git a/arduino/PowerTransmitter/PowerTransmitter/PowerTransmitter.ino b/arduino/PowerTransmitter/PowerTransmitter/PowerTransmitter.ino deleted file mode 100755 index 65f51c97..00000000 --- a/arduino/PowerTransmitter/PowerTransmitter/PowerTransmitter.ino +++ /dev/null @@ -1,295 +0,0 @@ -/* -* Protocol: Oregon V2.1 -* Emulating sensor: THGR2228N -*/ - -#include -#include "BH1750FVI.h" - -BH1750FVI LightSensor; -const byte TX_PIN = 10; -const byte LED_PIN = 13; -const unsigned long TIME = 512; -const unsigned long TWOTIME = TIME*2; -#define SEND_HIGH() digitalWrite(TX_PIN, HIGH) -#define SEND_LOW() digitalWrite(TX_PIN, LOW) -byte OregonMessageBuffer[9]; -unsigned long previousTime = 0; -unsigned long currentTime = millis(); -int impulseCount = 0; - -void setup() -{ - Serial.begin(9600); - - pinMode(TX_PIN, OUTPUT); - pinMode(LED_PIN, OUTPUT); - SEND_LOW(); - byte ID[] = { 0x1A,0x2D }; //temperature/humidity sensor (THGR2228N) - setType(OregonMessageBuffer, ID); - setChannel(OregonMessageBuffer, 0x20); - - LightSensor.begin(); - LightSensor.SetAddress(Device_Address_L); - LightSensor.SetMode(Continuous_H_resolution_Mode); - Serial.print("Started"); -} - - -boolean light = false; -void loop() -{ - currentTime = millis(); - uint16_t lux = LightSensor.GetLightIntensity(); - //Serial.print("lux="); - //Serial.println(lux); - if(lux > 40 && !light){ - light = true; - impulseCount++; - }else if(lux < 40){ - light = false; - } - if(currentTime - previousTime > 60000) { - previousTime = currentTime; - Serial.print("total impulses = "); - Serial.println(impulseCount); - send433(impulseCount,0,0xBA); - impulseCount = 0; - delay(500); - } -} - -void send433(float temperature, byte humidity, byte Identitet) -{ - digitalWrite(LED_PIN, HIGH); - setId(OregonMessageBuffer, Identitet); //set id of the sensor, BB=187 - setBatteryLevel(OregonMessageBuffer, 1); // 0 : low, 1 : high - setTemperature(OregonMessageBuffer, temperature); //org setTemperature(OregonMessageBuffer, 55.5); - setHumidity(OregonMessageBuffer, humidity); - calculateAndSetChecksum(OregonMessageBuffer); - - // Show the Oregon Message - for (byte i = 0; i < sizeof(OregonMessageBuffer); ++i) { - Serial.print(OregonMessageBuffer[i] >> 4, HEX); - Serial.print(OregonMessageBuffer[i] & 0x0F, HEX); - } - Serial.println(); - - // Send the Message over RF - sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer)); - // Send a "pause" - SEND_LOW(); - delayMicroseconds(TWOTIME*8); - // Send a copie of the first message. The v2.1 protocol send the message two time - sendOregon(OregonMessageBuffer, sizeof(OregonMessageBuffer)); - SEND_LOW(); - digitalWrite(LED_PIN, LOW); -} - -inline void setId(byte *data, byte ID) -{ - data[3] = ID; -} - -void setBatteryLevel(byte *data, byte level) -{ - if(!level) data[4] = 0x0C; - else data[4] = 0x00; -} - -void setTemperature(byte *data, float temp) -{ - // Set temperature sign - if(temp < 0) - { - data[6] = 0x08; - temp *= -1; - } - else - { - data[6] = 0x00; - } - - // Determine decimal and float part - int tempInt = (int)temp; - int td = (int)(tempInt / 10); - int tf = (int)round((float)((float)tempInt/10 - (float)td) * 10); - - int tempFloat = (int)round((float)(temp - (float)tempInt) * 10); - - // Set temperature decimal part - data[5] = (td << 4); - data[5] |= tf; - - // Set temperature float part - data[4] |= (tempFloat << 4); -} - -void setHumidity(byte* data, byte hum) -{ - data[7] = (hum/10); - data[6] |= (hum - data[7]*10) << 4; -} - -void calculateAndSetChecksum(byte* data) -{ - int sum = 0; - for(byte i = 0; i<8;i++) - { - sum += (data[i]&0xF0) >> 4; - sum += (data[i]&0xF); - } - data[8] = ((sum - 0xa) & 0xFF); -} - - - - -//********************************************************************************************************* - - -/** - * \brief Send logical "0" over RF - * \details azero bit be represented by an off-to-on transition - * \ of the RF signal at the middle of a clock period. - * \ Remenber, the Oregon v2.1 protocol add an inverted bit first - */ -inline void sendZero(void) -{ - SEND_HIGH(); - delayMicroseconds(TIME); - SEND_LOW(); - delayMicroseconds(TWOTIME); - SEND_HIGH(); - delayMicroseconds(TIME); -} - -/** - * \brief Send logical "1" over RF - * \details a one bit be represented by an on-to-off transition - * \ of the RF signal at the middle of a clock period. - * \ Remenber, the Oregon v2.1 protocol add an inverted bit first - */ -inline void sendOne(void) -{ - SEND_LOW(); - delayMicroseconds(TIME); - SEND_HIGH(); - delayMicroseconds(TWOTIME); - SEND_LOW(); - delayMicroseconds(TIME); -} - -/** - * \brief Send a bits quarter (4 bits = MSB from 8 bits value) over RF - * \param data Data to send - */ -inline void sendQuarterMSB(const byte data) -{ - (bitRead(data, 4)) ? sendOne() : sendZero(); - (bitRead(data, 5)) ? sendOne() : sendZero(); - (bitRead(data, 6)) ? sendOne() : sendZero(); - (bitRead(data, 7)) ? sendOne() : sendZero(); -} - -/** - * \brief Send a bits quarter (4 bits = LSB from 8 bits value) over RF - * \param data Data to send - */ -inline void sendQuarterLSB(const byte data) -{ - (bitRead(data, 0)) ? sendOne() : sendZero(); - (bitRead(data, 1)) ? sendOne() : sendZero(); - (bitRead(data, 2)) ? sendOne() : sendZero(); - (bitRead(data, 3)) ? sendOne() : sendZero(); -} - -/******************************************************************/ -/******************************************************************/ -/******************************************************************/ - -/** - * \brief Send a buffer over RF - * \param data Data to send - * \param size size of data to send - */ -void sendData(byte *data, byte size) -{ - for(byte i = 0; i < size; ++i) - { - sendQuarterLSB(data[i]); - sendQuarterMSB(data[i]); - } -} - -/** - * \brief Send an Oregon message - * \param data The Oregon message - */ -void sendOregon(byte *data, byte size) -{ - sendPreamble(); - //sendSync(); - sendData(data, size); - sendPostamble(); -} - -/** - * \brief Send preamble - * \details The preamble consists of 16 "1" bits - */ -inline void sendPreamble(void) -{ - byte PREAMBLE[]={ - 0xFF,0xFF }; - sendData(PREAMBLE, 2); -} - -/** - * \brief Send postamble - * \details The postamble consists of 8 "0" bits - */ -inline void sendPostamble(void) -{ - byte POSTAMBLE[]={ - 0x00 }; - sendData(POSTAMBLE, 1); -} - -/** - * \brief Send sync nibble - * \details The sync is 0xA. It is not use in this version since the sync nibble - * \ is include in the Oregon message to send. - */ -inline void sendSync(void) -{ - sendQuarterLSB(0xA); -} - -/******************************************************************/ -/******************************************************************/ -/******************************************************************/ - -/** - * \brief Set the sensor type - * \param data Oregon message - * \param type Sensor type - */ -inline void setType(byte *data, byte* type) -{ - data[0] = type[0]; - data[1] = type[1]; -} - -/** - * \brief Set the sensor channel - * \param data Oregon message - * \param channel Sensor channel (0x10, 0x20, 0x30) - */ -inline void setChannel(byte *data, byte channel) -{ - data[2] = channel; -} - - - diff --git a/src/se/hal/ControllerManager.java b/src/se/hal/ControllerManager.java index c8fb9664..d751cb59 100755 --- a/src/se/hal/ControllerManager.java +++ b/src/se/hal/ControllerManager.java @@ -106,7 +106,7 @@ public class ControllerManager implements HalSensorReportListener, Sensor sensor = findSensor(sensorData, registeredSensors); if (sensor != null) { - logger.finest("Received report from sensor: "+ sensorData); + logger.finest("Received report from sensor("+sensorData.getClass().getSimpleName()+"): "+ sensorData); PreparedStatement stmt = db.getPreparedStatement("INSERT INTO sensor_data_raw (timestamp, sensor_id, data) VALUES(?, ?, ?)"); stmt.setLong(1, sensorData.getTimestamp()); @@ -192,7 +192,7 @@ public class ControllerManager implements HalSensorReportListener, Event event = findEvent(eventData, registeredEvents); if (event != null) { - logger.finest("Received report from event: "+ eventData); + logger.finest("Received report from event("+eventData.getClass().getSimpleName()+"): "+ eventData); PreparedStatement stmt = db.getPreparedStatement("INSERT INTO event_data_raw (timestamp, event_id, data) VALUES(?, ?, ?)"); stmt.setLong(1, eventData.getTimestamp()); diff --git a/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java b/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java index 1008c557..f9262fa1 100755 --- a/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java +++ b/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java @@ -2,12 +2,16 @@ package se.hal.plugin.tellstick.protocols; import se.hal.plugin.tellstick.TellstickProtocol; import se.hal.struct.PowerConsumptionSensorData; +import zutil.log.LogUtil; import zutil.ui.Configurator; +import java.util.logging.Logger; + /** * Created by Ziver on 2015-11-19. */ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionSensorData { + private static final Logger logger = LogUtil.getLogger(); @Configurator.Configurable("Address") private int address = 0; @@ -29,34 +33,26 @@ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionS public void decode(byte[] data) { //class:sensor;protocol:oregon;model:0x1A2D;data:20BA000000002700; - // checksum2 not used yet - // int checksum2 = data[0]; - int checksum1 = data[1]; + // int channel = (data[0] >> 4) & 0x7; // channel not used + address = data[1] & 0xFF; + int temp3 = (data[2] >> 4) & 0xF; + int temp1 = (data[3] >> 4) & 0xF; + int temp2 = data[3] & 0xF; + int hum2 = (data[4] >> 4) & 0xF; + boolean negative = (data[4] & (1 << 3)) > 0; + int hum1 = data[5] & 0xF; + int checksum = data[6]; - int checksum = ((data[2] >> 4) & 0xF) + (data[2] & 0xF); - int hum1 = data[2] & 0xF; + int calcChecksum = ((data[5] >> 4) & 0xF) + (data[5] & 0xF); + calcChecksum += ((data[4] >> 4) & 0xF) + (data[4] & 0xF); + calcChecksum += ((data[3] >> 4) & 0xF) + (data[3] & 0xF); + calcChecksum += ((data[2] >> 4) & 0xF) + (data[2] & 0xF); + calcChecksum += ((data[1] >> 4) & 0xF) + (data[1] & 0xF); + calcChecksum += ((data[0] >> 4) & 0xF) + (data[0] & 0xF); + calcChecksum += 0x1 + 0xA + 0x2 + 0xD - 0xA; - checksum += ((data[3] >> 4) & 0xF) + (data[3] & 0xF); - boolean negative = (data[3] & (1 << 3)) > 0; - int hum2 = (data[3] >> 4) & 0xF; - - checksum += ((data[4] >> 4) & 0xF) + (data[4] & 0xF); - int temp2 = data[4] & 0xF; - int temp1 = (data[4] >> 4) & 0xF; - - checksum += ((data[5] >> 4) & 0xF) + (data[5] & 0xF); - int temp3 = (data[5] >> 4) & 0xF; - - checksum += ((data[6] >> 4) & 0xF) + (data[6] & 0xF); - address = data[6] & 0xFF; - - checksum += ((data[7] >> 4) & 0xF) + (data[7] & 0xF); - // channel not used - // uint8_t channel = (data[7] >> 4) & 0x7; - - checksum += 0x1 + 0xA + 0x2 + 0xD - 0xA; - - if (checksum != checksum1) { + if (calcChecksum != checksum) { + logger.fine("Checksum failed, address: "+address); return; }