From a6f8cf872a88851c8dbb9be229cce1ccd6c46290 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 1 Feb 2016 17:52:17 +0100 Subject: [PATCH] Initial structure for HalMultiSensor Former-commit-id: 22bc809d9e6de8a2994f1df699ae74bb7245378f --- arduino/HalMultiSensor/Configuration.h | 26 ++ arduino/HalMultiSensor/Definitions.h | 14 ++ arduino/HalMultiSensor/HalMultiSensor.ino | 16 ++ arduino/HalMultiSensor/HardwareBH1750.cpp | 93 +++++++ arduino/HalMultiSensor/HardwareBH1750.h | 74 ++++++ arduino/HalMultiSensor/HardwareDHT11.cpp | 85 +++++++ arduino/HalMultiSensor/HardwareDHT11.h | 37 +++ arduino/HalMultiSensor/HardwarePhotocell.cpp | 0 arduino/HalMultiSensor/HardwarePhotocell.h | 4 + arduino/HalMultiSensor/Protocol.h | 4 + arduino/HalMultiSensor/ProtocolNexa.cpp | 1 + arduino/HalMultiSensor/ProtocolNexa.h | 4 + arduino/HalMultiSensor/ProtocolOregon.cpp | 250 +++++++++++++++++++ arduino/HalMultiSensor/ProtocolOregon.h | 4 + 14 files changed, 612 insertions(+) create mode 100755 arduino/HalMultiSensor/Configuration.h create mode 100755 arduino/HalMultiSensor/Definitions.h create mode 100755 arduino/HalMultiSensor/HalMultiSensor.ino create mode 100755 arduino/HalMultiSensor/HardwareBH1750.cpp create mode 100755 arduino/HalMultiSensor/HardwareBH1750.h create mode 100755 arduino/HalMultiSensor/HardwareDHT11.cpp create mode 100755 arduino/HalMultiSensor/HardwareDHT11.h create mode 100755 arduino/HalMultiSensor/HardwarePhotocell.cpp create mode 100755 arduino/HalMultiSensor/HardwarePhotocell.h create mode 100755 arduino/HalMultiSensor/Protocol.h create mode 100755 arduino/HalMultiSensor/ProtocolNexa.cpp create mode 100755 arduino/HalMultiSensor/ProtocolNexa.h create mode 100755 arduino/HalMultiSensor/ProtocolOregon.cpp create mode 100755 arduino/HalMultiSensor/ProtocolOregon.h diff --git a/arduino/HalMultiSensor/Configuration.h b/arduino/HalMultiSensor/Configuration.h new file mode 100755 index 00000000..b84521d5 --- /dev/null +++ b/arduino/HalMultiSensor/Configuration.h @@ -0,0 +1,26 @@ +#ifndef CONFIGURATION_H +#define CONFIGURATION_H + +#include "Definitions.h" + +#define POLTIME 60*10 // poling in seconds + +// POWER CONSUMPTION SENSOR +#define POWERCON_ENABLED // comment out to disable sensor +#define POWERCON_HARDWARE HW_BH1750 +#define POWERCON_PROTOCOL PROT_OREGON + +// TEMPERATURE SENSOR +#define TEMPERATURE_ENABLED // comment out to disable sensor +#define TEMPERATURE_HARDWARE HW_DHT11 +#define TEMPERATURE_PROTOCOL PROT_OREGON +#define TEMPERATURE_POL_MULTIPLE 1 // poling in seconds + +// LIGHT SENSOR +#define LIGHT_ENABLED // comment out to disable sensor +#define LIGHT_HARDWARE HW_BHI750 +#define LIGHT_PROTOCOL PROT_OREGON +#define LIGHT_POL_MULTIPLE 1 // poling in seconds + + +#endif //CONFIGURATION_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/Definitions.h b/arduino/HalMultiSensor/Definitions.h new file mode 100755 index 00000000..892f5336 --- /dev/null +++ b/arduino/HalMultiSensor/Definitions.h @@ -0,0 +1,14 @@ +#ifndef DEFINITIONS_H +#define DEFINITIONS_H + +//////// PROTOCOLS +#define PROT_NEXA_SELFLEARNING +#define PROT_OREGON_SELFLEARNING + +/////// HARDWARE +#define HW_BH1750 +#define HW_DHT11 +#define HW_PHOTOCELL + + +#endif // DEFINITIONS_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/HalMultiSensor.ino b/arduino/HalMultiSensor/HalMultiSensor.ino new file mode 100755 index 00000000..7da87c45 --- /dev/null +++ b/arduino/HalMultiSensor/HalMultiSensor.ino @@ -0,0 +1,16 @@ +/* +A interrupt based sensor device that reads multiple sensors and transmits +the data to a central location. +*/ + + + +void setup(){ + +} + + +void loop() { + +} + diff --git a/arduino/HalMultiSensor/HardwareBH1750.cpp b/arduino/HalMultiSensor/HardwareBH1750.cpp new file mode 100755 index 00000000..647be34a --- /dev/null +++ b/arduino/HalMultiSensor/HardwareBH1750.cpp @@ -0,0 +1,93 @@ +/* + +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. + + +based on Christopher Laws, March, 2013 code. + +*/ + +#include "HardwareBH1750.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/HalMultiSensor/HardwareBH1750.h b/arduino/HalMultiSensor/HardwareBH1750.h new file mode 100755 index 00000000..dd00955d --- /dev/null +++ b/arduino/HalMultiSensor/HardwareBH1750.h @@ -0,0 +1,74 @@ +/* + +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 + +based on Christopher Laws, March, 2013 code. + +*/ + +#ifndef HARDWAREBH1750_H +#define HARDWAREBH1750_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 + +// Waiting for measurement 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 // HARDWAREBH1750_H + diff --git a/arduino/HalMultiSensor/HardwareDHT11.cpp b/arduino/HalMultiSensor/HardwareDHT11.cpp new file mode 100755 index 00000000..2e11cd4d --- /dev/null +++ b/arduino/HalMultiSensor/HardwareDHT11.cpp @@ -0,0 +1,85 @@ +// +// FILE: dht11.cpp +// VERSION: 0.3.2 +// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino +// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) +// +// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf +// +// HISTORY: +// George Hadjikyriacou - Original version (??) +// Mod by SimKard - Version 0.2 (24/11/2010) +// Mod by Rob Tillaart - Version 0.3 (28/03/2011) +// + added comments +// + removed all non DHT11 specific code +// + added references +// + +#include "dht11.h" + +// returnvalues: +// 0 : OK +// -1 : checksum error +// -2 : timeout +int dht11::read(int pin) +{ + // BUFFER TO RECEIVE + uint8_t bits[5]; + uint8_t cnt = 7; + uint8_t idx = 0; + + // EMPTY BUFFER + for (int i=0; i< 5; i++) bits[i] = 0; + + // REQUEST SAMPLE + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); + delay(18); + digitalWrite(pin, HIGH); + delayMicroseconds(40); + pinMode(pin, INPUT); + + // ACKNOWLEDGE or TIMEOUT + unsigned int loopCnt = 10000; + while(digitalRead(pin) == LOW) + if (loopCnt-- == 0) return -2; + + loopCnt = 10000; + while(digitalRead(pin) == HIGH) + if (loopCnt-- == 0) return -2; + + // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT + for (int i=0; i<40; i++) + { + loopCnt = 10000; + while(digitalRead(pin) == LOW) + if (loopCnt-- == 0) return -2; + + unsigned long t = micros(); + + loopCnt = 10000; + while(digitalRead(pin) == HIGH) + if (loopCnt-- == 0) return -2; + + if ((micros() - t) > 40) bits[idx] |= (1 << cnt); + if (cnt == 0) // next byte? + { + cnt = 7; // restart at MSB + idx++; // next byte! + } + else cnt--; + } + + // WRITE TO RIGHT VARS + // as bits[1] and bits[3] are allways zero they are omitted in formulas. + humidity = bits[0]; + temperature = bits[2]; + + uint8_t sum = bits[0] + bits[2]; + + if (bits[4] != sum) return -1; + return 0; +} +// +// END OF FILE +// \ No newline at end of file diff --git a/arduino/HalMultiSensor/HardwareDHT11.h b/arduino/HalMultiSensor/HardwareDHT11.h new file mode 100755 index 00000000..9e188508 --- /dev/null +++ b/arduino/HalMultiSensor/HardwareDHT11.h @@ -0,0 +1,37 @@ +// +// FILE: dht11.h +// VERSION: 0.3.2 +// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino +// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) +// +// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf +// +// URL: http://arduino.cc/playground/Main/DHT11Lib +// +// HISTORY: +// George Hadjikyriacou - Original version +// see dht.cpp file +// *** Terry King: Changed include Arduino.h for 1.0x +// include core Wiring API and now Arduino +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" + #else + #include "WProgram.h" + #endif + +#ifndef dht11_h +#define dht11_h + +#define DHT11LIB_VERSION "0.3.2" + +class dht11 +{ +public: + int read(int pin); + int humidity; + int temperature; +}; +#endif +// +// END OF FILE +// \ No newline at end of file diff --git a/arduino/HalMultiSensor/HardwarePhotocell.cpp b/arduino/HalMultiSensor/HardwarePhotocell.cpp new file mode 100755 index 00000000..e69de29b diff --git a/arduino/HalMultiSensor/HardwarePhotocell.h b/arduino/HalMultiSensor/HardwarePhotocell.h new file mode 100755 index 00000000..f6c5072f --- /dev/null +++ b/arduino/HalMultiSensor/HardwarePhotocell.h @@ -0,0 +1,4 @@ +#ifndef HARDWAREPHOTOCELL_H +#define HARDWAREPHOTOCELL_H + +#endif // HARDWAREPHOTOCELL_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/Protocol.h b/arduino/HalMultiSensor/Protocol.h new file mode 100755 index 00000000..c34487f0 --- /dev/null +++ b/arduino/HalMultiSensor/Protocol.h @@ -0,0 +1,4 @@ +#ifndef PROTOCL_H +#define PROTOCL_H + +#endif // PROTOCL_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/ProtocolNexa.cpp b/arduino/HalMultiSensor/ProtocolNexa.cpp new file mode 100755 index 00000000..8b137891 --- /dev/null +++ b/arduino/HalMultiSensor/ProtocolNexa.cpp @@ -0,0 +1 @@ + diff --git a/arduino/HalMultiSensor/ProtocolNexa.h b/arduino/HalMultiSensor/ProtocolNexa.h new file mode 100755 index 00000000..39943ffc --- /dev/null +++ b/arduino/HalMultiSensor/ProtocolNexa.h @@ -0,0 +1,4 @@ +#ifndef PROTOCOLNEXA_H +#define PROTOCOLNEXA_H + +#endif // PROTOCOLNEXA_H \ No newline at end of file diff --git a/arduino/HalMultiSensor/ProtocolOregon.cpp b/arduino/HalMultiSensor/ProtocolOregon.cpp new file mode 100755 index 00000000..e432d0b0 --- /dev/null +++ b/arduino/HalMultiSensor/ProtocolOregon.cpp @@ -0,0 +1,250 @@ +#include + +#define TX_PIN = 10; +const unsigned long TIME = 512; +#define TWOTIME TIME*2; +#define SEND_HIGH() digitalWrite(TX_PIN, HIGH) +#define SEND_LOW() digitalWrite(TX_PIN, LOW) +byte OregonMessageBuffer[9]; + + +void setup() +{ + pinMode(TX_PIN, OUTPUT); + SEND_LOW(); + byte ID[] = { 0x1A,0x2D }; //temperature/humidity sensor (THGR2228N) + setType(OregonMessageBuffer, ID); + setChannel(OregonMessageBuffer, 0x20); +} + + + +void send433(float temperature, byte humidity, byte Identitet) +{ + 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(); +} + +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; +} \ No newline at end of file diff --git a/arduino/HalMultiSensor/ProtocolOregon.h b/arduino/HalMultiSensor/ProtocolOregon.h new file mode 100755 index 00000000..23d1f3cf --- /dev/null +++ b/arduino/HalMultiSensor/ProtocolOregon.h @@ -0,0 +1,4 @@ +#ifndef PROTOCOLOREGON_H +#define PROTOCOLOREGON_H + +#endif // PROTOCOLOREGON_H \ No newline at end of file