Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT version with additional syslog logging #1

Merged
merged 3 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.pio
.pioenvs
.piolibdeps
.vscode/
.vscode/
secret_config.ini
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Environment Monitor with Logging to InfluxDB (TICK stack)
# Environment Monitor with Logging to MQTT

This device measures environmental conditions such as temperature, humidity, relative pressure and rainfall and reports that data to InfluxDB via the Telegraf endpoint when deployed as part of the TICK stack (https://www.influxdata.com/time-series-platform/). It also includes a battery voltage monitor to enable alerting when the device needs to be recharged. The current timestamp is retrieved via NTP each time the board wakes from deep sleep mode.

## TICK stack deployment

I used a modified version of the Docker Compose script found [here](https://medium.com/lucjuggery/the-tick-stack-as-a-docker-application-package-1d0d6b869211) that adds persistent volumes for storage and configuration of Chronograf dashboards and Kapacitor alerting as well as exposing additional endpoints such as InfluxDB itself. The YAML for this deployment can be found in the tick-stack directory. I deployed this onto an ESXi box I had with spare capacity and added a DNS entry with fixed IP using my LAN's [pi-hole](https://pi-hole.net/) DHCP/DNS server.
## MQTT deployment

I used the eclipse/mosquitto image deployed into a Kubernetes cluster and connected it to HomeAssistant.
## Components Required

Please note that all links are via Amazon UK. This is solely because that's where I purchased them. I did not receive any consideration from Amazon for linking to their site. While these are the specific components that I used, there are many similar pin-compatible items available as alternatives.
Expand All @@ -30,9 +29,6 @@ This diagram shows the connector layout. Note that I'm using pins 27 and 32 in
## The Code

The microcontroller code was written using [PlatformIO](https://platformio.org/) in VSCode. Hopefully this is reasonably self-explanatory. I've added some configuration macros at the top for details of specific rooms. The RAIN_MONITOR macro should be defined when creating the outdoor version and commented out for the indoor model.

The Pi code is written using [Go](https://golang.org/) and makes use of the [PayPal GATT](https://github.com/paypal/gatt) library which is no longer maintains so may cease to function if significant changes are made to the Linux BlueZ stack. There are more recent forks of this library but I found issues with them.

## Construction

I'll leave that as an exercise for the user since I'm not 100% happy with the assembly I've done and you're likely to use a different enclosure to me anyway. Here's a photo of what it looks like when put together:
Expand Down
2 changes: 0 additions & 2 deletions ble-central/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions ble-central/Makefile

This file was deleted.

5 changes: 0 additions & 5 deletions ble-central/copy.sh

This file was deleted.

8 changes: 0 additions & 8 deletions ble-central/go.sh

This file was deleted.

141 changes: 0 additions & 141 deletions ble-central/src/main/main.go

This file was deleted.

14 changes: 9 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
extra_configs = secret_config.ini

[env:lolin_d32]
platform = espressif32
board = lolin32
framework = arduino
upload_port = /dev/cu.SLAB_USBtoUART
upload_port = /dev/cu.usbserial-0001
upload_speed = 921600
monitor_port = /dev/cu.SLAB_USBtoUART
monitor_port = /dev/cu.usbserial-0001
monitor_speed = 115200
lib_deps =
1554
551
1841
enviromonitor/BME280_Light @ 0.0.0-alpha+sha.600667f3a6
arduino-libraries/NTPClient @ ^3.2.1
ottowinter/AsyncMqttClient-esphome @ ^0.8.6
arcao/Syslog @ ^2.0.0
13 changes: 13 additions & 0 deletions secret_config.ini.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[env:lolin_d32]
src_build_flags=
'-D WIFI_SSID="my-ssid"'
'-D WIFI_PASSPHRASE="my-passphrase"'
'-D MQTT_SERVER="server.domain"'
'-D MQTT_PORT=1883'
'-D SYSLOG_ENABLED=1'
'-D SYSLOG_HOST="loghost"'
'-D SYSLOG_PORT=514'
'-D ROOM="garden"'
'-D ALTITUDE=80'
'-D RAIN_MONITOR=0'
'-D BATTERY_MONITOR=0'
75 changes: 75 additions & 0 deletions src/battery_sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "battery_sensor.h"
#include "logging.h"

#define MAX_VOLTAGE 4.4 * 5.0

BatteryHomeSensor::BatteryHomeSensor(int data_pin, int pwr_pin) :
data_pin(data_pin),
pwr_pin(pwr_pin),
data_sent(false),
is_on(false)
{
if (pwr_pin > 0)
{
pinMode(pwr_pin, OUTPUT);
}
pinMode(data_pin, INPUT);
}

String BatteryHomeSensor::name()
{
return "Battery";
}

void BatteryHomeSensor::setup()
{
}

void BatteryHomeSensor::submitReading(ReadingSubmitter &submitter)
{
logln("Getting Battery sensors");

uint16_t analogueReading = analogRead(data_pin);
double actual = ((double)analogueReading / 4095.0) * MAX_VOLTAGE;

submitter.sendReading("battery", actual);
data_sent = true;
}

bool BatteryHomeSensor::ready()
{
return true;
}

bool BatteryHomeSensor::failed()
{
return false;
}

bool BatteryHomeSensor::sent()
{
return data_sent;
}

bool BatteryHomeSensor::isOn()
{
return is_on;
}

void BatteryHomeSensor::switchOn()
{
if (pwr_pin > 0)
{
digitalWrite(pwr_pin, HIGH);
}
is_on = true;
}

void BatteryHomeSensor::switchOff()
{
if (pwr_pin > 0)
{
digitalWrite(pwr_pin, LOW);
}
is_on = false;
}
22 changes: 22 additions & 0 deletions src/battery_sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <Wire.h>
#include "home_sensor.h"

class BatteryHomeSensor: public HomeSensor {
public:
BatteryHomeSensor(int data_pin, int pwr_pin);
virtual void submitReading(ReadingSubmitter &submitter);
virtual void setup();
virtual bool ready();
virtual bool failed();
virtual void switchOn();
virtual void switchOff();
virtual bool isOn();
virtual bool sent();
virtual String name();
private:
int data_pin;
int pwr_pin;
bool data_sent;
bool is_on;
};
Loading