A Python script that starts an MQTT client to monitor traffic coming from various microcontrollers that read data from systems in a camper van. Depending on those types of messages it will store data from remote sensors in the database, grab values from the database to transmit to sensors when they connect, or reset values when the water tank is refilled. Currently this will have a water sensor with a GR-301 hall effect flow meter connected to a Raspberry Pi Zero W and an ESP-32 that is reading serial communication from a JBD BMS for a DIY LiFePO4 battery. A flask app is also included to allow for an endpoint that resets water consumption from a link in the Grafana Dashboard.
Clone the repo:
git clone https://github.com/michaelpappas/van_mqtt_client
cd van_mqtt_clint
Set the environment variables:
touch .env
# open .env and modify the environment variables
or
cp .env.example .env
# open .env and modify the environment variables
SECRET_KEY - Choose any string
DATABASE_URL - Replace the {postgres username} and {postgres password} with your personal postgres username and password.
More info regarding configuring postgres on a raspberry pi can be found here
In the cloned directory create a virtual environment
python3 -m venv venv
Activate that venv
source venv/bin/activate
Install the requirements
pip3 install -r requirements.txt
Once all the requirements are installed and everything is configured you can start the MQTT client with
python3 mqttClient.py
and run the Flask server with
flask run -p (desired port)
Once all of this is working correctly it is suggested that you have both of these scripts begin on boot using cron.
\ # project directory
|--.env.example # example environment variables
|--mqttClient.py # main MQTT Client
|--models.py # models for battery and water tank data
|--app.py # flask app for water consumption reset endpoint
|--requirements.txt # script requirements
- Complete routing for MQTT messages in client to trigger correct response
- Write test for flask endpoint, models, and mqtt client
- Code cleanup and refactor