Fixed some issues with oregon protocoll and first version of HalMultiSensor done.

This commit is contained in:
Ziver Koc 2016-05-31 21:11:53 +02:00
parent 4f6066fe7a
commit 92fb46ea0f
14 changed files with 333 additions and 536 deletions

View file

@ -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();
}

View file

@ -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

View file

@ -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;
}

View file

@ -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;
}