Some refactoring
This commit is contained in:
parent
78317d6c4f
commit
1273bcae49
11 changed files with 182 additions and 234 deletions
|
|
@ -4,17 +4,18 @@
|
||||||
#define ENABLE_DEBUG // comment out to disable debug
|
#define ENABLE_DEBUG // comment out to disable debug
|
||||||
|
|
||||||
#define TIMER_MILLISECOND 60*1000 // poling in minutes
|
#define TIMER_MILLISECOND 60*1000 // poling in minutes
|
||||||
|
#define INDICATOR_PIN 13 // diod
|
||||||
|
|
||||||
// POWER CONSUMPTION SENSOR
|
// POWER CONSUMPTION SENSOR
|
||||||
#define POWERCON_ENABLED // comment out to disable sensor
|
#define POWERCON_ENABLED // comment out to disable sensor
|
||||||
#define POWERCON_SENSOR SensorBH1750()
|
#define POWERCON_SENSOR SensorBH1750()
|
||||||
#define POWERCON_PROTOCOL ProtocolOregon(118)
|
#define POWERCON_PROTOCOL ProtocolOregon(11, 118)
|
||||||
#define POWER_TIMER_MULTIPLIER 1
|
#define POWER_TIMER_MULTIPLIER 1
|
||||||
|
|
||||||
// TEMPERATURE SENSOR
|
// TEMPERATURE SENSOR
|
||||||
#define TEMPERATURE_ENABLED // comment out to disable sensor
|
#define TEMPERATURE_ENABLED // comment out to disable sensor
|
||||||
#define TEMPERATURE_SENSOR SensorDHT11(5)
|
#define TEMPERATURE_SENSOR SensorDHT11(10)
|
||||||
#define TEMPERATURE_PROTOCOL ProtocolOregon(100)
|
#define TEMPERATURE_PROTOCOL ProtocolOregon(11, 100)
|
||||||
#define TEMPERATURE_TIMER_MULTIPLIER 1
|
#define TEMPERATURE_TIMER_MULTIPLIER 1
|
||||||
|
|
||||||
// LIGHT SENSOR
|
// LIGHT SENSOR
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,11 @@
|
||||||
|
|
||||||
#include "HalConfiguration.h"
|
#include "HalConfiguration.h"
|
||||||
|
|
||||||
|
// Utility functions
|
||||||
|
|
||||||
#ifdef ENABLE_DEBUG
|
#ifdef ENABLE_DEBUG
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define DEBUG(msg) \
|
#define DEBUG(msg) \
|
||||||
Serial.println(msg); \
|
Serial.println(msg); \
|
||||||
Serial.flush();
|
Serial.flush();
|
||||||
|
|
@ -18,60 +22,81 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
inline void pulse(short pin, short count)
|
||||||
|
{
|
||||||
|
while (--count >= 0)
|
||||||
|
{
|
||||||
|
digitalWrite(INDICATOR_PIN, HIGH);
|
||||||
|
delay(150);
|
||||||
|
digitalWrite(INDICATOR_PIN, LOW);
|
||||||
|
if (count != 0) delay(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
// INTERFACES
|
||||||
|
|
||||||
class Sensor
|
class Sensor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setup() = 0;
|
virtual void setup() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SensorPowerConsumption : public Sensor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// returns number of pulses from power meter
|
|
||||||
virtual unsigned int getConsumption() = 0;
|
|
||||||
virtual void reset() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SensorTemperature : public Sensor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual int getTemperature() = 0;
|
|
||||||
virtual int getHumidity() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SensorLight : public Sensor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual void setup() = 0;
|
|
||||||
virtual unsigned int getLuminosity() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Protocol
|
class Protocol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setup() = 0;
|
virtual void setup() = 0;
|
||||||
virtual void send() = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct PowerData
|
||||||
|
{
|
||||||
|
unsigned int consumption;
|
||||||
|
};
|
||||||
|
class SensorPowerConsumption : public Sensor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// returns number of pulses from power meter
|
||||||
|
virtual void read(PowerData& data) = 0;
|
||||||
|
};
|
||||||
class ProtocolPowerConsumption : public Protocol
|
class ProtocolPowerConsumption : public Protocol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setConsumption(unsigned int cons) = 0;
|
virtual void send(const PowerData& data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct TemperatureData
|
||||||
|
{
|
||||||
|
float temperature;
|
||||||
|
short humidity;
|
||||||
|
};
|
||||||
|
class SensorTemperature : public Sensor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void read(TemperatureData& data) = 0;
|
||||||
|
};
|
||||||
class ProtocolTemperature : public Protocol
|
class ProtocolTemperature : public Protocol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setTemperature(float temp) = 0;
|
virtual void send(const TemperatureData& data) = 0;
|
||||||
virtual void setHumidity(unsigned char humidity) = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct LightData
|
||||||
|
{
|
||||||
|
unsigned int lumen;
|
||||||
|
};
|
||||||
|
class SensorLight : public Sensor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual void read(LightData& data) = 0;
|
||||||
|
};
|
||||||
class ProtocolLight : public Protocol
|
class ProtocolLight : public Protocol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setLuminosity(int lumen) = 0;
|
virtual void send(const LightData& data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // HALINTERFACES_H
|
#endif // HALINTERFACES_H
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ A interrupt based sensor device that reads multiple sensors and transmits
|
||||||
the data to a central location.
|
the data to a central location.
|
||||||
*/
|
*/
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
#include "HalConfiguration.h"
|
#include "HalConfiguration.h"
|
||||||
#include "HalInterfaces.h"
|
#include "HalInterfaces.h"
|
||||||
|
|
@ -34,6 +33,7 @@ void setup()
|
||||||
#ifdef ENABLE_DEBUG
|
#ifdef ENABLE_DEBUG
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
#endif
|
#endif
|
||||||
|
pinMode(INDICATOR_PIN, OUTPUT);
|
||||||
|
|
||||||
// Setup Sensors and protocols
|
// Setup Sensors and protocols
|
||||||
#ifdef POWERCON_ENABLED
|
#ifdef POWERCON_ENABLED
|
||||||
|
|
@ -64,6 +64,8 @@ void setup()
|
||||||
Interrupt::setCallback(timerInterruptFunc);
|
Interrupt::setCallback(timerInterruptFunc);
|
||||||
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
|
Interrupt::setupWatchDogInterrupt(TIMER_MILLISECOND); // one minute scheduled interrupt
|
||||||
|
|
||||||
|
|
||||||
|
pulse(INDICATOR_PIN, 3);
|
||||||
DEBUG("Ready");
|
DEBUG("Ready");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,17 +79,17 @@ void timerInterruptFunc()
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
digitalWrite(INDICATOR_PIN, HIGH);
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
|
|
||||||
// Send power consumption
|
// Send power consumption
|
||||||
#ifdef POWERCON_ENABLED
|
#ifdef POWERCON_ENABLED
|
||||||
if(timerMultiplier == POWER_TIMER_MULTIPLIER)
|
if(timerMultiplier == POWER_TIMER_MULTIPLIER)
|
||||||
{
|
{
|
||||||
unsigned int consumption = powerSensor->getConsumption();
|
static PowerData powerData;
|
||||||
DEBUGF("Read POWERCON_SENSOR= consumption:%d", consumption);
|
powerSensor->read(powerData); // not needed, only here for future use
|
||||||
powerSensor->reset();
|
DEBUGF("Read POWERCON_SENSOR= consumption:%d", powerData.consumption);
|
||||||
powerProtocol->setConsumption(consumption);
|
powerProtocol->send(powerData);
|
||||||
powerProtocol->send();
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -95,12 +97,10 @@ void loop()
|
||||||
#ifdef TEMPERATURE_ENABLED
|
#ifdef TEMPERATURE_ENABLED
|
||||||
if(timerMultiplier == TEMPERATURE_TIMER_MULTIPLIER)
|
if(timerMultiplier == TEMPERATURE_TIMER_MULTIPLIER)
|
||||||
{
|
{
|
||||||
unsigned int temperature = tempSensor->getTemperature();
|
static TemperatureData tempData;
|
||||||
unsigned int humidity = tempSensor->getHumidity();
|
tempSensor->read(tempData);
|
||||||
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", temperature, humidity);
|
DEBUGF("Read TEMPERATURE_SENSOR= temperature:%d, humidity:%d", tempData.temperature, tempData.humidity);
|
||||||
tempProtocol->setTemperature(temperature);
|
tempProtocol->send(tempData);
|
||||||
tempProtocol->setHumidity(humidity);
|
|
||||||
tempProtocol->send();
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -108,14 +108,15 @@ void loop()
|
||||||
#ifdef LIGHT_ENABLED
|
#ifdef LIGHT_ENABLED
|
||||||
if(timerMultiplier == LIGHT_TIMER_MULTIPLIER)
|
if(timerMultiplier == LIGHT_TIMER_MULTIPLIER)
|
||||||
{
|
{
|
||||||
unsigned int lumen = lightSensor->getLuminosity();
|
static LightData lightData;
|
||||||
DEBUG("Read LIGHT_SENSOR= lumen:%d", lumen);
|
lightSensor->read(lightData);
|
||||||
lightProtocol->setLuminosity(lumen);
|
DEBUG("Read LIGHT_SENSOR= lumen:%d", lightData.lumen);
|
||||||
lightProtocol->send();
|
lightProtocol->send(lightData);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
interrupts();
|
interrupts();
|
||||||
|
digitalWrite(INDICATOR_PIN, LOW);
|
||||||
|
|
||||||
DEBUG("Sleeping");
|
DEBUG("Sleeping");
|
||||||
Interrupt::sleep();
|
Interrupt::sleep();
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,29 @@
|
||||||
#include "ProtocolOregon.h"
|
#include "ProtocolOregon.h"
|
||||||
|
|
||||||
#define RF_TX_PIN 10
|
|
||||||
#define RF_DELAY 512
|
#define RF_DELAY 512
|
||||||
#define RF_DELAY_LONG RF_DELAY*2
|
#define RF_DELAY_LONG RF_DELAY*2
|
||||||
#define RF_SEND_HIGH() digitalWrite(RF_TX_PIN, HIGH)
|
#define RF_SEND_HIGH() digitalWrite(txPin, HIGH)
|
||||||
#define RF_SEND_LOW() digitalWrite(RF_TX_PIN, LOW)
|
#define RF_SEND_LOW() digitalWrite(txPin, LOW)
|
||||||
|
|
||||||
|
|
||||||
void ProtocolOregon::setup()
|
void ProtocolOregon::setup()
|
||||||
{
|
{
|
||||||
pinMode(RF_TX_PIN, OUTPUT);
|
pinMode(txPin, OUTPUT);
|
||||||
RF_SEND_LOW();
|
RF_SEND_LOW();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProtocolOregon::setTemperature(float temp)
|
|
||||||
|
void ProtocolOregon::send(const PowerData& data)
|
||||||
{
|
{
|
||||||
this->temperature = temp;
|
send(data.consumption, 0);
|
||||||
}
|
|
||||||
void ProtocolOregon::setHumidity(unsigned char humidity)
|
|
||||||
{
|
|
||||||
this->humidity = humidity;
|
|
||||||
}
|
|
||||||
void ProtocolOregon::setConsumption(unsigned int cons)
|
|
||||||
{
|
|
||||||
this->temperature = cons;
|
|
||||||
this->humidity = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ProtocolOregon::send(const TemperatureData& data)
|
||||||
|
{
|
||||||
|
send(data.temperature, data.humidity);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProtocolOregon::send(float temperature, short humidity)
|
||||||
void ProtocolOregon::send()
|
|
||||||
{
|
{
|
||||||
byte buffer[9];
|
byte buffer[9];
|
||||||
setType(buffer, 0x1A,0x2D); //temperature/humidity sensor (THGR2228N)
|
setType(buffer, 0x1A,0x2D); //temperature/humidity sensor (THGR2228N)
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,17 @@
|
||||||
class ProtocolOregon : public ProtocolTemperature, public ProtocolPowerConsumption
|
class ProtocolOregon : public ProtocolTemperature, public ProtocolPowerConsumption
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ProtocolOregon(unsigned char address) : address(address){};
|
ProtocolOregon(short pin, unsigned char address) : txPin(pin), address(address){};
|
||||||
|
|
||||||
virtual void setup();
|
virtual void setup();
|
||||||
virtual void setTemperature(float temp);
|
virtual void send(const TemperatureData& data);
|
||||||
virtual void setHumidity(unsigned char humidity);
|
virtual void send(const PowerData& data);
|
||||||
virtual void setConsumption(unsigned int cons); //Power
|
|
||||||
virtual void send();
|
|
||||||
private:
|
|
||||||
unsigned char address;
|
|
||||||
float temperature;
|
|
||||||
unsigned char humidity;
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
short txPin;
|
||||||
|
unsigned char address;
|
||||||
|
|
||||||
|
void send(float temperature, short humidity);
|
||||||
void setType(byte *data, byte b1, byte b2);
|
void setType(byte *data, byte b1, byte b2);
|
||||||
void setChannel(byte *data, byte channel);
|
void setChannel(byte *data, byte channel);
|
||||||
void setId(byte *data, byte id);
|
void setId(byte *data, byte id);
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,40 @@ based on Christopher Laws, March, 2013 code.
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void SensorBH1750::setup() {
|
void SensorBH1750::setup() {
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
configure(BH1750_CONTINUOUS_HIGH_RES_MODE);
|
configure(BH1750_CONTINUOUS_HIGH_RES_MODE);
|
||||||
|
|
@ -30,69 +64,34 @@ void SensorBH1750::configure(uint8_t mode) {
|
||||||
case BH1750_ONE_TIME_HIGH_RES_MODE_2:
|
case BH1750_ONE_TIME_HIGH_RES_MODE_2:
|
||||||
case BH1750_ONE_TIME_LOW_RES_MODE:
|
case BH1750_ONE_TIME_LOW_RES_MODE:
|
||||||
// apply a valid mode change
|
// apply a valid mode change
|
||||||
write8(mode);
|
Wire.beginTransmission(BH1750_I2CADDR);
|
||||||
|
Wire.write(mode);
|
||||||
|
Wire.endTransmission();
|
||||||
_delay_ms(10);
|
_delay_ms(10);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Invalid measurement mode
|
// Invalid measurement mode
|
||||||
#if BH1750_DEBUG == 1
|
DEBUG("Invalid measurement mode");
|
||||||
Serial.println("Invalid measurement mode");
|
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int SensorBH1750::getConsumption()
|
void SensorBH1750::read(PowerData& data)
|
||||||
{
|
|
||||||
return pulses;
|
|
||||||
}
|
|
||||||
void SensorBH1750::reset()
|
|
||||||
{
|
{
|
||||||
|
data.consumption = pulses;
|
||||||
pulses = 0;
|
pulses = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int SensorBH1750::getLuminosity(void) {
|
void SensorBH1750::read(LightData& data) {
|
||||||
uint16_t level;
|
uint16_t level;
|
||||||
|
|
||||||
Wire.beginTransmission(BH1750_I2CADDR);
|
Wire.beginTransmission(BH1750_I2CADDR);
|
||||||
Wire.requestFrom(BH1750_I2CADDR, 2);
|
Wire.requestFrom(BH1750_I2CADDR, 2);
|
||||||
#if (ARDUINO >= 100)
|
|
||||||
level = Wire.read();
|
level = Wire.read();
|
||||||
level <<= 8;
|
level <<= 8;
|
||||||
level |= Wire.read();
|
level |= Wire.read();
|
||||||
#else
|
|
||||||
level = Wire.receive();
|
|
||||||
level <<= 8;
|
|
||||||
level |= Wire.receive();
|
|
||||||
#endif
|
|
||||||
Wire.endTransmission();
|
Wire.endTransmission();
|
||||||
|
|
||||||
#if BH1750_DEBUG == 1
|
data.lumen = level/1.2; // convert to lux
|
||||||
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 SensorBH1750::write8(uint8_t d) {
|
|
||||||
Wire.beginTransmission(BH1750_I2CADDR);
|
|
||||||
#if (ARDUINO >= 100)
|
|
||||||
Wire.write(d);
|
|
||||||
#else
|
|
||||||
Wire.send(d);
|
|
||||||
#endif
|
|
||||||
Wire.endTransmission();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,3 @@
|
||||||
/*
|
|
||||||
|
|
||||||
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 SensorBH1750_H
|
#ifndef SensorBH1750_H
|
||||||
#define SensorBH1750_H
|
#define SensorBH1750_H
|
||||||
|
|
||||||
|
|
@ -19,52 +5,16 @@ based on Christopher Laws, March, 2013 code.
|
||||||
#include "HalInterfaces.h"
|
#include "HalInterfaces.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 SensorBH1750 : public SensorPowerConsumption, public SensorLight{
|
class SensorBH1750 : public SensorPowerConsumption, public SensorLight{
|
||||||
public:
|
public:
|
||||||
virtual void setup();
|
virtual void setup();
|
||||||
virtual unsigned int getLuminosity();
|
virtual void read(PowerData& data);
|
||||||
virtual unsigned int getConsumption();
|
virtual void read(LightData& data);
|
||||||
virtual void reset();
|
|
||||||
|
|
||||||
void configure(uint8_t mode);
|
|
||||||
private:
|
private:
|
||||||
unsigned int pulses;
|
unsigned int pulses;
|
||||||
void write8(uint8_t data);
|
|
||||||
|
void configure(uint8_t mode);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,40 +13,22 @@
|
||||||
// + added comments
|
// + added comments
|
||||||
// + removed all non DHT11 specific code
|
// + removed all non DHT11 specific code
|
||||||
// + added references
|
// + added references
|
||||||
|
// Refactored by Ziver Koc
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "SensorDHT11.h"
|
#include "SensorDHT11.h"
|
||||||
|
|
||||||
|
|
||||||
SensorDHT11::SensorDHT11(int pin)
|
|
||||||
{
|
|
||||||
this->pin = pin;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SensorDHT11::setup(){}
|
void SensorDHT11::setup(){}
|
||||||
int SensorDHT11::getTemperature()
|
|
||||||
{
|
|
||||||
return temperature;
|
|
||||||
}
|
|
||||||
int SensorDHT11::getHumidity()
|
|
||||||
{
|
|
||||||
return humidity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returnvalues:
|
|
||||||
// 0 : OK
|
void SensorDHT11::read(TemperatureData& data)
|
||||||
// -1 : checksum error
|
|
||||||
// -2 : timeout
|
|
||||||
int SensorDHT11::read()
|
|
||||||
{
|
{
|
||||||
// BUFFER TO RECEIVE
|
// BUFFER TO RECEIVE
|
||||||
uint8_t bits[5];
|
uint8_t bits[5] = {0};
|
||||||
uint8_t cnt = 7;
|
uint8_t cnt = 7;
|
||||||
uint8_t idx = 0;
|
uint8_t idx = 0;
|
||||||
|
|
||||||
// EMPTY BUFFER
|
|
||||||
for (int i=0; i< 5; i++) bits[i] = 0;
|
|
||||||
|
|
||||||
// REQUEST SAMPLE
|
// REQUEST SAMPLE
|
||||||
pinMode(pin, OUTPUT);
|
pinMode(pin, OUTPUT);
|
||||||
digitalWrite(pin, LOW);
|
digitalWrite(pin, LOW);
|
||||||
|
|
@ -58,24 +40,40 @@ int SensorDHT11::read()
|
||||||
// ACKNOWLEDGE or TIMEOUT
|
// ACKNOWLEDGE or TIMEOUT
|
||||||
unsigned int loopCnt = 10000;
|
unsigned int loopCnt = 10000;
|
||||||
while(digitalRead(pin) == LOW)
|
while(digitalRead(pin) == LOW)
|
||||||
if (loopCnt-- == 0) return -2;
|
if (loopCnt-- == 0)
|
||||||
|
{
|
||||||
|
DEBUG("DHT11 timeout");
|
||||||
|
return;;
|
||||||
|
}
|
||||||
|
|
||||||
loopCnt = 10000;
|
loopCnt = 10000;
|
||||||
while(digitalRead(pin) == HIGH)
|
while(digitalRead(pin) == HIGH)
|
||||||
if (loopCnt-- == 0) return -2;
|
if (loopCnt-- == 0)
|
||||||
|
{
|
||||||
|
DEBUG("DHT11 timeout");
|
||||||
|
return;;
|
||||||
|
}
|
||||||
|
|
||||||
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
|
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
|
||||||
for (int i=0; i<40; i++)
|
for (int i=0; i<40; i++)
|
||||||
{
|
{
|
||||||
loopCnt = 10000;
|
loopCnt = 10000;
|
||||||
while(digitalRead(pin) == LOW)
|
while(digitalRead(pin) == LOW)
|
||||||
if (loopCnt-- == 0) return -2;
|
if (loopCnt-- == 0)
|
||||||
|
{
|
||||||
|
DEBUG("DHT11 timeout");
|
||||||
|
return;;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long t = micros();
|
unsigned long t = micros();
|
||||||
|
|
||||||
loopCnt = 10000;
|
loopCnt = 10000;
|
||||||
while(digitalRead(pin) == HIGH)
|
while(digitalRead(pin) == HIGH)
|
||||||
if (loopCnt-- == 0) return -2;
|
if (loopCnt-- == 0)
|
||||||
|
{
|
||||||
|
DEBUG("DHT11 timeout");
|
||||||
|
return;;
|
||||||
|
}
|
||||||
|
|
||||||
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
|
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
|
||||||
if (cnt == 0) // next byte?
|
if (cnt == 0) // next byte?
|
||||||
|
|
@ -86,14 +84,14 @@ int SensorDHT11::read()
|
||||||
else cnt--;
|
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];
|
uint8_t sum = bits[0] + bits[2];
|
||||||
|
if (bits[4] != sum)
|
||||||
|
DEBUG("DHT11 checksum error");
|
||||||
|
|
||||||
if (bits[4] != sum) return -1;
|
// WRITE TO RIGHT VARS
|
||||||
return 0;
|
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
|
||||||
|
data.temperature = bits[2];
|
||||||
|
data.humidity = bits[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,37 +1,19 @@
|
||||||
//
|
#ifndef SensorDHT11_h
|
||||||
// FILE: dht11.h
|
#define SensorDHT11_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
|
|
||||||
#ifndef dht11_h
|
|
||||||
#define dht11_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "HalInterfaces.h"
|
#include "HalInterfaces.h"
|
||||||
|
|
||||||
#define DHT11LIB_VERSION "0.3.2"
|
|
||||||
|
|
||||||
class SensorDHT11 : public SensorTemperature
|
class SensorDHT11 : public SensorTemperature
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
SensorDHT11(int pin);
|
SensorDHT11(short pin) : pin(pin) {};
|
||||||
|
|
||||||
virtual void setup();
|
virtual void setup();
|
||||||
virtual int getTemperature();
|
virtual void read(TemperatureData& data);
|
||||||
virtual int getHumidity();
|
|
||||||
int read();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int pin;
|
short pin;
|
||||||
float temperature;
|
|
||||||
unsigned char humidity;
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@ void SensorPhotocell::setup()
|
||||||
Interrupt::setupPinInterrupt(PC2); //PC3
|
Interrupt::setupPinInterrupt(PC2); //PC3
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int SensorPhotocell::getConsumption()
|
void SensorPhotocell::read(PowerData& data)
|
||||||
{
|
{
|
||||||
return pulse;
|
data.consumption = pulse;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SensorPhotocell::reset()
|
void SensorPhotocell::reset()
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,10 @@ class SensorPhotocell : public SensorPowerConsumption
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void setup();
|
virtual void setup();
|
||||||
virtual unsigned int getConsumption();
|
virtual void read(PowerData& data);
|
||||||
virtual void reset();
|
virtual void reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Interrupt* interrupt;
|
|
||||||
unsigned int pulse;
|
unsigned int pulse;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue