Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code for Homie multisensor using TSL2561 luminosity and BME280 triple sensors.
#include <Homie.h>
#include <SparkFunTSL2561.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <SPI.h>
SFE_TSL2561 light;
BME280I2C bme;
boolean gain; // Gain setting, 0 = X1, 1 = X16;
unsigned int ms; // Integration ("shutter") time in milliseconds
unsigned long lastDataSent = 0;
// data nodes
HomieNode luxNode("lux", "lux");
HomieNode tempNode("temperature", "degrees");
HomieNode humidityNode("humidity", "relative");
HomieNode pressureNode("pressure", "hectopascals");
// custom setting for frequency of update
HomieSetting<long> intervalSetting("interval", "How often should this device send data (in seconds)");
void setupHandler() {
luxNode.setProperty("unit").send("lux");
tempNode.setProperty("unit").send("c");
humidityNode.setProperty("unit").send("%");
pressureNode.setProperty("unit").send("hPa");
light.begin();
if (!bme.begin()) {
Homie.getLogger() << "Could not find a valid BME280 sensor, check wiring!" << endl;
}
gain = 0;
unsigned char time = 1;
Homie.getLogger() << "Setting up light sensor" << endl;
light.setTiming(gain,time,ms);
light.setPowerUp();
}
void sendLux() {
unsigned int data0, data1;
if (light.getData(data0,data1))
{
double lux; // Resulting lux value
boolean good; // True if neither sensor is saturated
// Perform lux calculation:
good = light.getLux(gain,ms,data0,data1,lux);
Homie.getLogger() << "Lux: " << lux << " lux" << endl;
luxNode.setProperty("lux").send(String(lux));
}
else
{
// getData() returned false because of an I2C error, inform the user.
byte error = light.getError();
printError(error);
}
}
void sendTemp() {
float temp(NAN), hum(NAN), pres(NAN);
uint8_t pressureUnit(1);
bme.read(pres, temp, hum, true, pressureUnit);
Homie.getLogger() << "Temp: " << temp << "°C" << endl;
tempNode.setProperty("degrees").send(String(temp));
Homie.getLogger() << "Humidity: " << hum << "% RH" << endl;
humidityNode.setProperty("relative").send(String(hum));
Homie.getLogger() << "Pressure: " << pres << "hPa" << endl;
pressureNode.setProperty("hectopascals").send(String(pres));
}
void loopHandler() {
if (millis() - lastDataSent >= intervalSetting.get() * 1000UL || lastDataSent == 0) {
sendLux();
sendTemp();
lastDataSent = millis();
}
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
intervalSetting.setDefaultValue(10).setValidator([] (long candidate) {
return (candidate >= 1) && (candidate <= 3600);
});
Homie_setFirmware("awesome-multisensor", "1.0.0");
Homie.setSetupFunction(setupHandler).setLoopFunction(loopHandler);
luxNode.advertise("unit");
luxNode.advertise("lux");
tempNode.advertise("unit");
tempNode.advertise("degrees");
humidityNode.advertise("unit");
humidityNode.advertise("relative");
pressureNode.advertise("unit");
pressureNode.advertise("hectopascals");
Homie.setup();
}
void loop() {
Homie.loop();
}
void printError(byte error)
// If there's an I2C error, this function will
// print out an explanation.
{
Homie.getLogger() << "I2C error: " << error << ", ";
switch(error)
{
case 0:
Homie.getLogger() << "success";
break;
case 1:
Homie.getLogger() << "data too long for transmit buffer";
break;
case 2:
Homie.getLogger() << "received NACK on address (disconnected?)";
break;
case 3:
Homie.getLogger() << "received NACK on data";
break;
case 4:
Homie.getLogger() << "other error";
break;
default:
Homie.getLogger() << "unknown error";
}
Homie.getLogger() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment