Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save manuelkiessling/6ec728703b07a8a90f651ec4df136cd5 to your computer and use it in GitHub Desktop.

Select an option

Save manuelkiessling/6ec728703b07a8a90f651ec4df136cd5 to your computer and use it in GitHub Desktop.
// Due to buggy behaviour in the Blockly -> Arduino source code transformation on https://blockly.sensebox.de,
// I didn't manage to "click together" a sketch that successfully published the data from
// 6 different sensors (Temperature & Humidity, Visible Light & UV, Pressure & Height Above Sea Level)
// into 6 different MQTT topics.
// The problem was that even with multiple "MQTT - Publish to Feed/Topic" blocks, the resulting
// Arduino code still only contained one Adafruit_MQTT_Publish state variable, and thus, all
// data ended up in one topic.
// This handcrafted code contains a correct implementation with 6 separated topics.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h> // http://librarymanager/All#Adafruit_GFX_Library
#include <Adafruit_SSD1306.h> // http://librarymanager/All#Adafruit_SSD1306
#include <WiFi.h>
#include <Adafruit_MQTT.h> //http://librarymanager/All#Adafruit_MQTT_Library"
#include <Adafruit_MQTT_Client.h>
#include <Adafruit_HDC1000.h> // http://librarymanager/All#Adafruit_HDC1000_Library
#include <LTR329.h>
#include <VEML6070.h>
#include <Adafruit_DPS310.h> // http://librarymanager/All#Adafruit_DPS310
char ssid[] = "CHANGEME";
char pass[] = "CHANGEME";
const long intervalInterval14 = 5000;
long time_startInterval14 = 0;
long time_actualInterval14 = 0;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define SERVER "CHANGEME"
#define SERVERPORT 1883
#define USERNAME "CHANGEME"
#define PASS "CHANGEME"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, SERVER, SERVERPORT, USERNAME, PASS);
// HDC1080, Temperature & Humidity
Adafruit_HDC1000 hdc = Adafruit_HDC1000();
// Light & UV
bool lightsensortype = 0; //0 for tsl - 1 for ltr
LTR329 LTR;
unsigned char gain = 1;
unsigned char integrationTime = 0;
unsigned char measurementRate = 3;
VEML6070 veml;
// DPS310, Air Pressure & Height Above Sea Level
Adafruit_DPS310 dps;
sensors_event_t temp_event, pressure_event;
Adafruit_MQTT_Publish stateTemperatur = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/temperature/state");
Adafruit_MQTT_Publish stateLuftfeuchte = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/humidity/state");
Adafruit_MQTT_Publish stateHelligkeit = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/brightness/state");
Adafruit_MQTT_Publish stateUv = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/uv/state");
Adafruit_MQTT_Publish stateLuftdruck = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/airpressure/state");
Adafruit_MQTT_Publish stateHoehenn = Adafruit_MQTT_Publish(&mqtt, "homeassistant/device/CHANGEME/heightabovesealevel/state");
void printOnDisplay(String title1, String measurement1, String unit1, String title2, String measurement2, String unit2) {
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE, BLACK);
display.println(title1);
display.setCursor(0, 10);
display.setTextSize(2);
display.print(measurement1);
display.print(" ");
display.setTextSize(1);
display.println(unit1);
display.setCursor(0, 30);
display.setTextSize(1);
display.println(title2);
display.setCursor(0, 40);
display.setTextSize(2);
display.print(measurement2);
display.print(" ");
display.setTextSize(1);
display.println(unit2);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care of connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
display.display();
delay(100);
display.clearDisplay();
WiFi.begin(ssid, pass);
if(WiFi.status() == WL_NO_SHIELD){
while(true);
}
if(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass);
delay(5000);
}
hdc.begin();
Lightsensor_begin();
dps.begin_I2C(0x76);
dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);
}
void loop() {
time_startInterval14 = millis();
MQTT_connect();
dps.getEvents(&temp_event, &pressure_event);
if (time_startInterval14 > time_actualInterval14 + intervalInterval14) {
time_actualInterval14 = millis();
printOnDisplay("Temperatur", String(hdc.readTemperature()), "C", "Luftfeuchtigkeit", String(hdc.readHumidity()), "%");
display.display();
stateTemperatur.publish(hdc.readTemperature());
stateLuftfeuchte.publish(hdc.readHumidity());
stateHelligkeit.publish(Lightsensor_getIlluminance());
stateUv.publish(veml.getUV());
stateLuftdruck.publish(pressure_event.pressure);
stateHoehenn.publish(dps.readAltitude(1013));
}
}
// Light Sensor **************************************
int read_reg(byte address, uint8_t reg)
{
int i = 0;
Wire.beginTransmission(address);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)address, (uint8_t)1);
delay(1);
if(Wire.available())
i = Wire.read();
return i;
}
void write_reg(byte address, uint8_t reg, uint8_t val)
{
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
void Lightsensor_begin()
{
unsigned int u = 0;
u = read_reg(0x29, 0x80 | 0x0A); //id register
if ((u & 0xF0) == 0xA0) // TSL45315
{
write_reg(0x29, 0x80 | 0x00, 0x03); //control: power on
write_reg(0x29, 0x80 | 0x01, 0x02); //config: M=4 T=100ms
delay(120);
lightsensortype = 0; //TSL45315
}
else
{
LTR.begin();
LTR.setControl(gain, false, false);
LTR.setMeasurementRate(integrationTime, measurementRate);
LTR.setPowerUp(); //power on with default settings
delay(10); //Wait 10 ms (max) - wakeup time from standby
lightsensortype = 1; //
}
}
uint32_t Lightsensor_getIlluminance()
{
unsigned int lux = 0;
if (lightsensortype == 0) // TSL45315
{
unsigned int u = (read_reg(0x29, 0x80 | 0x04) << 0); //data low
u |= (read_reg(0x29, 0x80 | 0x05) << 8); //data high
lux = u * 4; // calc lux with M=4 and T=100ms
}
else if (lightsensortype == 1) //LTR-329ALS-01
{
delay(100);
unsigned int data0, data1;
for (int i = 0; i < 5; i++) {
if (LTR.getData(data0, data1)) {
if(LTR.getLux(gain, integrationTime, data0, data1, lux));
if(lux > 0) break;
else delay(10);
}
else {
byte error = LTR.getError();
}
}
}
return lux;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment