Logging and displaying you own sensor on you local "home" network via Grafana

As I am lacking a nice app to have a nice overview of my own sensor station’s data plus have my own logging to a database I created my own setup – in addition to the public sharing to PurpleAir’s map.

Here is how my dashboard looks. Work on a phone’s. browser as great as well.


A little bridging script fetches local json from my PurpleAir and dumps it every 10 minutes into a influx data base. Then a Grafana web front end to present the dash. (Ignore the kWh display, that other data from my PV system…)

2 Likes

I love it, I cannot believe there is nothing pre-built that ships with a 300 dollar gadget to accomplish this without hacking your own UI

Can you share some the code you used for the set up?

2 Likes

Would also love to hear how this was set up, I’ve used Grafana and Prometheus before

Well, putting the puzzle together:

A raspberry or any server on you home / PurpleAir network is grabbing every few minutes (up to you) the complete JSON data blob with all the data. And feeds it into a influx time series data base.

So have influxdb installed. And also grafana for displaying it.

To be precise a cron job – here is my cron tab entry:
crontab -l:

1,11,21,31,41,51 * * * * python3 /home/pi/AQDbridge/purpleaqd-get-once.py

runs my bridging python script periodically to fetch the data and feed it into influx.

Here the magic happens every few (10 in my case, on every hour and 1,11,21,31,41,51min) minutes:

Script here /home/pi/AQDbridge/purpleaqd-get-once.py :


#!/usr/bin/python3
import re
from typing import NamedTuple
import time
import requests
from influxdb import InfluxDBClient

INFLUXDB_ADDRESS = 'localhost'
INFLUXDB_USER = 'purpleguy'
INFLUXDB_PASSWORD = 'mysuperscretpasswort'
INFLUXDB_DATABASE = 'weather_stations'

PURPLE_URL = 'http://192.168.0.164/json'

influxdb_client = InfluxDBClient(INFLUXDB_ADDRESS, 8086, INFLUXDB_USER, INFLUXDB_PASSWORD, None)


def send_sensor_data_to_influxdb(sensor_data):
    json_body = [
        {
            'measurement': 'PurpleAirQuality',
            'tags': {
                'location': 'RockyPoint'
            },
            'fields': #{
                sensor_data
#                'value': sensor_data.value
            #}
        }
    ]
#    print (json_body)
    influxdb_client.write_points(json_body)


def _init_influxdb_database():
    databases = influxdb_client.get_list_database()
    if len(list(filter(lambda x: x['name'] == INFLUXDB_DATABASE, databases))) == 0:
        influxdb_client.create_database(INFLUXDB_DATABASE)
    influxdb_client.switch_database(INFLUXDB_DATABASE)

def main():
    _init_influxdb_database()

    r_json = requests.get(PURPLE_URL)
    if r_json.status_code == 200:
        send_sensor_data_to_influxdb(r_json.json())

if __name__ == '__main__':
#    print('PurpleAQD to InfluxDB bridge')
    main()

The rest is some fun grafana dash setup, all online clicking away… :wink:

-Py

PS: I put the key elements here, buy me a coffee, spend some good time to make it a nice dash :wink:

1 Like

Thank you that’s really helpful!

1 Like