Fixed some issues with oregon protocoll and first version of HalMultiSensor done.
This commit is contained in:
parent
4f6066fe7a
commit
92fb46ea0f
14 changed files with 333 additions and 536 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
#ifndef HALINTERFACES_H
|
||||
#define HALINTERFACES_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "HalConfiguration.h"
|
||||
|
||||
// Utility functions
|
||||
|
||||
#ifdef ENABLE_DEBUG
|
||||
#include <Arduino.h>
|
||||
|
||||
#define DEBUG(msg) \
|
||||
Serial.println(msg); \
|
||||
Serial.flush();
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<length; ++i)
|
||||
{
|
||||
(bitRead(data[i], 0)) ? sendOne() : sendZero();
|
||||
(bitRead(data[i], 1)) ? sendOne() : sendZero();
|
||||
|
|
@ -192,7 +192,7 @@ void ProtocolOregon::sendData(byte *data, byte length)
|
|||
* \brief Send an Oregon message
|
||||
* \param data The Oregon message
|
||||
*/
|
||||
void ProtocolOregon::rfSend(byte *data, byte size)
|
||||
void ProtocolOregon::rfSend(byte data[], byte size)
|
||||
{
|
||||
// Send preamble
|
||||
byte preamble[] = { 0xFF,0xFF };
|
||||
|
|
|
|||
|
|
@ -1,9 +1,14 @@
|
|||
#include "SensorPhotocell.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
unsigned int SensorPhotocell::pulse = 0;
|
||||
|
||||
void SensorPhotocell::interruptHandler()
|
||||
{
|
||||
digitalWrite(INDICATOR_PIN, HIGH);
|
||||
DEBUG("PHCELL: INTERRUPT");
|
||||
++pulse;
|
||||
digitalWrite(INDICATOR_PIN, LOW);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <util/delay.h>
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
@ -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 <Arduino.h>
|
||||
#else
|
||||
#include <WProgram.h>
|
||||
#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
|
||||
|
||||
|
|
@ -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 <Wire.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,295 +0,0 @@
|
|||
/*
|
||||
* Protocol: Oregon V2.1
|
||||
* Emulating sensor: THGR2228N
|
||||
*/
|
||||
|
||||
#include <Wire.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue