Skip to content

mqtt client and flask backend for routing traffic from various sensors with a raspberry pi

Notifications You must be signed in to change notification settings

michaelpappas/van_mqtt_client

Repository files navigation

Van Systems MQTT Client

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.

Table of Contents

Manual Installation

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

Development Environment

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

Running the Script

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 Structure

\                           # 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

Further Improvements

  1. Complete routing for MQTT messages in client to trigger correct response
  2. Write test for flask endpoint, models, and mqtt client
  3. Code cleanup and refactor

About

mqtt client and flask backend for routing traffic from various sensors with a raspberry pi

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages