Hi!
I accomplish to do what I wanted thanks to the help of you all, so I'm sharing the results
I ended up with this very simple MySensorGeneric.ino that is able to manage a DHT and a BH1750
/**
* Name: MySensorGeneric.ino
* Created: 20-Aug-16 22:47:51
* Author: Daniel
*
*******************************
*
* REVISION HISTORY
* Version 0.1 - Development
*
*/
#include "MySensorConfig.h"
#include <MySensors.h>
#include "MySensorWrapperDHT.h"
#include "MySensorWrapperBH1750.h"
#define CHILD_ID_LIGHT 2
#define CHILD_ID_TEMP 1
#define CHILD_ID_HUM 0
#define HUMIDITY_SENSOR_DIGITAL_PIN 3
MySensorWrapperBH1750 SensorBH1750(CHILD_ID_LIGHT);
MySensorWrapperDHT SensorDHT(CHILD_ID_TEMP, CHILD_ID_HUM, HUMIDITY_SENSOR_DIGITAL_PIN);
void setup()
{
SensorBH1750.Setup();
SensorDHT.Setup();
}
void presentation() {
sendSketchInfo("My Generic Sensor", "0.11");
SensorBH1750.Present();
SensorDHT.Present();
}
void loop()
{
SensorBH1750.UpdateSensorValue();
SensorDHT.UpdateSensorValue();
wait(SLEEP_TIME);
}
I've created a class to manage the BH1750:
MySensorWrapperBH1750.h
// MySensorWrapperBH1750.h
#ifndef _MYSENSORWRAPPERBH1750_h
#define _MYSENSORWRAPPERBH1750_h
//Need to add to project Version.h and MyEepromAdresses.h
#include "MySensorsCore.h"
#include <BH1750.h>
class MySensorWrapperBH1750
{
public:
MySensorWrapperBH1750(uint8_t sensor_id);
~MySensorWrapperBH1750();
void Setup();
void Present();
void UpdateSensorValue();
private:
uint8_t _sensor_id;
uint16_t _lastValue;
BH1750 _sensor;
MyMessage _msg;
};
#endif
MySensorWrapperBH1750.cpp
/**
* Name: MySensorWrapperBH1750
* Created: 20-Aug-16 22:47:51
* Author: Daniel
* Version 0.1 - Draft
*
*******************************
*
* MySensors Wrapper for BH1750 sensor.
* Wiring (I2C):
* Sensor -> Arduino
* GND -> GND
* ADD -> NC
* SDA -> A4
* SCL -> A5
* VCC -> +5V
*
*/
#include "MySensorWrapperBH1750.h"
MySensorWrapperBH1750::MySensorWrapperBH1750(uint8_t sensor_id)
{
_sensor_id = sensor_id;
MyMessage _msg(_sensor_id, V_LEVEL);
}
MySensorWrapperBH1750::~MySensorWrapperBH1750()
{
}
void MySensorWrapperBH1750::Setup()
{
// TODO: Set initialization mode
// Default mode:
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
// #define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10
// 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
_sensor.begin();
}
void MySensorWrapperBH1750::Present()
{
present(_sensor_id, S_LIGHT_LEVEL);
}
void MySensorWrapperBH1750::UpdateSensorValue()
{
// Fetch light in Lux from BH1750 sensor
uint16_t newValue = _sensor.readLightLevel();// Get Lux value
if (isnan(newValue)) {
#ifdef MY_DEBUG
Serial.println("Failed reading Light from BH1750");
#endif
}
else if (newValue != _lastValue) {
#ifdef MY_DEBUG
Serial.print("Lux: ");
Serial.println(newValue);
#endif
send(_msg.set(newValue));
_lastValue = newValue;
}
}
And for the DHT I've created the following class:
MySensorWrapperDHT.h
// MySensorWrapperDHT.h
#ifndef _MYSENSORWRAPPERDHT_h
#define _MYSENSORWRAPPERDHT_h
//Need to add to project Version.h and MyEepromAdresses.h
#include "MySensorsCore.h"
#include "DHT.h"
class MySensorWrapperDHT
{
public:
MySensorWrapperDHT(uint8_t sensor_temp_id, uint8_t sensor_humd_id, uint8_t sensor_pin = 3);
~MySensorWrapperDHT();
void Setup();
void Present();
void UpdateSensorValue();
private:
uint8_t _sensorTempId;
uint8_t _sensorHumdId;
uint8_t _sensorPin;
DHT _sensor;
MyMessage _msgTemp;
MyMessage _msgHumd;
float _lastTempValue;
float _lastHumdValue;
};
#endif
MySensorWrapperDHT.cpp
/**
* Name: MySensorWrapperDHT
* Created: 20-Aug-16 22:47:51
* Author: Daniel
* Version 0.1 - Draft
*
*******************************
*
* MySensors Wrapper for DHT sensor.
* Wiring ():
* Sensor -> Arduino
* GND -> GND
* OUT -> D3
* VCC -> +5V
*
* NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
* to 3.3V instead of 5V!
* Connect pin 2 of the sensor to whatever your DHTPIN is
* Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
* https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino
*/
#include "MySensorWrapperDHT.h"
MySensorWrapperDHT::MySensorWrapperDHT(uint8_t sensor_temp_id, uint8_t sensor_humd_id, uint8_t sensor_pin)
{
_sensorTempId = sensor_temp_id;
_sensorHumdId = sensor_humd_id;
_sensorPin = sensor_pin;
MyMessage _msgTemp(_sensorTempId, V_TEMP);
MyMessage _msgHumd(_sensorHumdId, V_HUM);
}
MySensorWrapperDHT::~MySensorWrapperDHT()
{
}
void MySensorWrapperDHT::Setup()
{
_sensor.setup(_sensorPin, DHT::DHT22);
}
void MySensorWrapperDHT::Present()
{
present(_sensorTempId, S_TEMP);
present(_sensorHumdId, S_HUM);
}
void MySensorWrapperDHT::UpdateSensorValue()
{
// TODO: really needed?
wait(_sensor.getMinimumSamplingPeriod());
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float currentTempValue = _sensor.getTemperature();
float currentHumdValue = _sensor.getHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(currentTempValue) || isnan(currentHumdValue) ){
Serial.println("Failed to read from DHT sensor!");
}
else {
// Update temperature from DHT sensor
if (currentTempValue != _lastTempValue) {
_lastTempValue = currentTempValue;
send(_msgTemp.set(currentTempValue, true));
#ifdef MY_DEBUG
Serial.print("Temp: ");
Serial.println(currentTempValue);
#endif
}
// Update humidity from DHT sensor
if (currentHumdValue != _lastHumdValue) {
_lastHumdValue = currentHumdValue;
send(_msgHumd.set(currentHumdValue, 1));
#ifdef MY_DEBUG
Serial.print("Hum: ");
Serial.println(currentHumdValue);
#endif
}
}
}
And to my surprise the footprint after compiling is even smaller, 18,374 bytes (59%) of program storage space and 913 bytes (44%) of dynamic memory with a single .ino versus 17,748 bytes (58%) of program storage space and 857 bytes (42%) of dynamic memory the above project in VisualMicro. Both results with MY_DEBUG defined.
Next step is to create classes to manage switches, relays and LEDs.
Once again thank you all for the help and fell free to share your opinions about the way I'm trying to do things.
Best regards
@tbowmo more out of curiosity, and from someone that just got started on electronics, so please sorry if this sounds like very noob, but, would it not be safer to plug the V_TARGET LED to the +3.3v pin? How do you ensure that the resistor on the V_TARGET will have constant voltage and that will not blow from over voltage if the VCCio pin is depended on the the Motherboard power?
Thank You and nice work
What I didn't quite figure out was how it operates independently as a regular light switch.
What I find most interesting is that this is 8 years old. There have been some incredible advances in the IoT world in that time. I was about to do a similar thing which morphed into an ESP-12F then added a touch screen. I call it the Universal Light Switch
Imagine this: All the switches are identical. Any switch can easily be configured to control up to five devices ... and then changed, on-the-fly, to control a different set of devices.
My design is part of a system that would require a controller (eg. Home Assistant), an MQTT broker, and receiving modules in the devices being switched. It's WiFI which assumes an access point.
The DIY ULS is under $20 and the off-the-shelf receiving modules are under $10. (About the cost of a non-networked dimmer switch) If one doesn't have the controller and MQTT broker, they can run on an old (5 years?) computer (which is cheaper than an old RasberryPi).
This project is a good one!
OSD