This commit is contained in:
Jack 2021-08-07 18:24:13 +01:00
parent c4e26212bd
commit c26332c94b
3 changed files with 980 additions and 0 deletions

854
Dashboard.json Normal file
View File

@ -0,0 +1,854 @@
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": "-- Grafana --",
"enable": true,
"hide": true,
"iconColor": "rgba(0, 211, 255, 1)",
"name": "Annotations & Alerts",
"target": {
"limit": 100,
"matchAny": false,
"tags": [],
"type": "dashboard"
},
"type": "dashboard"
}
]
},
"description": "A simple DHT11 based temp reading based on python",
"editable": true,
"gnetId": null,
"graphTooltip": 0,
"id": 24,
"links": [
{
"icon": "external link",
"tags": [],
"type": "dashboards"
}
],
"panels": [
{
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"mappings": [],
"max": 38,
"noValue": "Offline",
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "dark-green",
"value": null
},
{
"color": "#EAB839",
"value": 30
},
{
"color": "red",
"value": 35
}
]
},
"unit": "celsius"
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 6,
"x": 0,
"y": 0
},
"id": 4,
"options": {
"reduceOptions": {
"calcs": [
"mean"
],
"fields": "",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": true,
"text": {}
},
"pluginVersion": "8.1.0",
"targets": [
{
"alias": "C",
"groupBy": [
{
"params": [
"20s"
],
"type": "time"
},
{
"params": [
"previous"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"temperature_c"
],
"type": "field"
},
{
"params": [],
"type": "mean"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Current Temp °C",
"transparent": true,
"type": "gauge"
},
{
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"mappings": [],
"max": 98,
"noValue": "Offline",
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "dark-green",
"value": null
},
{
"color": "#EAB839",
"value": 89
},
{
"color": "red",
"value": 95
}
]
},
"unit": "fahrenheit"
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 6,
"x": 6,
"y": 0
},
"id": 7,
"options": {
"reduceOptions": {
"calcs": [
"mean"
],
"fields": "",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": true,
"text": {}
},
"pluginVersion": "8.1.0",
"targets": [
{
"alias": "C",
"groupBy": [
{
"params": [
"20s"
],
"type": "time"
},
{
"params": [
"previous"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"temperature_f"
],
"type": "field"
},
{
"params": [],
"type": "mean"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Current Temp °F",
"transparent": true,
"type": "gauge"
},
{
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"mappings": [],
"max": 100,
"noValue": "Offline",
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "#EAB839",
"value": 45
},
{
"color": "red",
"value": 60
}
]
},
"unit": "humidity"
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 6,
"x": 12,
"y": 0
},
"id": 8,
"options": {
"reduceOptions": {
"calcs": [
"mean"
],
"fields": "",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": true,
"text": {}
},
"pluginVersion": "8.1.0",
"targets": [
{
"alias": "",
"groupBy": [
{
"params": [
"20s"
],
"type": "time"
},
{
"params": [
"previous"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "table",
"select": [
[
{
"params": [
"humidity"
],
"type": "field"
},
{
"params": [],
"type": "mean"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Current Humidity %",
"transparent": true,
"type": "gauge"
},
{
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"mappings": [],
"max": 35,
"min": 15,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "#EAB839",
"value": 30
},
{
"color": "red",
"value": 35
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 9,
"w": 6,
"x": 18,
"y": 0
},
"id": 10,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
"calcs": [
"max"
],
"fields": "",
"values": false
},
"text": {},
"textMode": "auto"
},
"pluginVersion": "8.1.0",
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"previous"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "B",
"resultFormat": "table",
"select": [
[
{
"params": [
"temperature_c"
],
"type": "field"
},
{
"params": [],
"type": "max"
},
{
"params": [
"Max Temp °C"
],
"type": "alias"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Max Temp °C",
"transparent": true,
"type": "stat"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB (Room Temp Local)",
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 13,
"w": 18,
"x": 0,
"y": 9
},
"hiddenSeries": false,
"id": 6,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "8.1.0",
"pointradius": 0.5,
"points": true,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"groupBy": [
{
"params": [
"20s"
],
"type": "time"
},
{
"params": [
"none"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "table",
"select": [
[
{
"params": [
"temperature_c"
],
"type": "field"
},
{
"params": [],
"type": "mean"
},
{
"params": [
"Temp C"
],
"type": "alias"
}
],
[
{
"params": [
"temperature_f"
],
"type": "field"
},
{
"params": [],
"type": "mean"
},
{
"params": [
"Temp F"
],
"type": "alias"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Temps",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"transparent": true,
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:1374",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"$$hashKey": "object:1375",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
},
{
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"mappings": [],
"max": 95,
"min": 20,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "#EAB839",
"value": 82
},
{
"color": "red",
"value": 95
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 7,
"w": 6,
"x": 18,
"y": 9
},
"id": 11,
"options": {
"colorMode": "value",
"graphMode": "area",
"justifyMode": "auto",
"orientation": "auto",
"reduceOptions": {
"calcs": [
"max"
],
"fields": "",
"values": false
},
"text": {},
"textMode": "auto"
},
"pluginVersion": "8.1.0",
"targets": [
{
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"previous"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "B",
"resultFormat": "table",
"select": [
[
{
"params": [
"temperature_f"
],
"type": "field"
},
{
"params": [
"7"
],
"type": "top"
},
{
"params": [
"Max Temp °F"
],
"type": "alias"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"timeFrom": null,
"timeShift": null,
"title": "Max Temp °F ",
"transparent": true,
"type": "stat"
},
{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "InfluxDB (Room Temp Local)",
"fieldConfig": {
"defaults": {
"unit": "humidity"
},
"overrides": []
},
"fill": 1,
"fillGradient": 0,
"gridPos": {
"h": 13,
"w": 18,
"x": 0,
"y": 22
},
"hiddenSeries": false,
"id": 12,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"nullPointMode": "null",
"options": {
"alertThreshold": true
},
"percentage": false,
"pluginVersion": "8.1.0",
"pointradius": 0.5,
"points": true,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"groupBy": [
{
"params": [
"20s"
],
"type": "time"
},
{
"params": [
"none"
],
"type": "fill"
}
],
"measurement": "dht11",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "table",
"select": [
[
{
"params": [
"humidity"
],
"type": "field"
},
{
"params": [],
"type": "mean"
},
{
"params": [
"Humidity"
],
"type": "alias"
}
]
],
"tags": [
{
"key": "location",
"operator": "=",
"value": "Jacks Room"
}
]
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Humidity",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"transparent": true,
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"$$hashKey": "object:1374",
"format": "humidity",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"$$hashKey": "object:1375",
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}
],
"refresh": "30s",
"schemaVersion": 30,
"style": "dark",
"tags": [],
"templating": {
"list": []
},
"time": {
"from": "now-2d",
"to": "now"
},
"timepicker": {},
"timezone": "",
"title": "5. Rack Temp",
"uid": "8sniS6Rgz",
"version": 24
}

13
dht11.service Normal file
View File

@ -0,0 +1,13 @@
[Unit]
Description=Python DHT11 Grafana Script
After=network.target
[Service]
Type=simple
Restart=on-failure
User=ubuntu
ExecStart=/usr/bin/python3 /home/ubuntu/dht11/sensor-iot.py
WorkingDirectory=/home/ubuntu/dht11
[Install]
WantedBy=multi-user.target

113
sensor-iot.py Normal file
View File

@ -0,0 +1,113 @@
import time
import requests
import configparser
import Adafruit_DHT
from influxdb import InfluxDBClient
# Configuration File
CONFIG_FILE = "settings.conf"
def get_reading(config):
# InfluxDB connection info
host = "192.168.5.16"
port = "8086"
user = "jack"
password = "Jack32145"
dbname = "roomtemp"
# Create the InfluxDB client object
client = InfluxDBClient(host, port, user, password, dbname)
# Sensor details
#sensor = str(config['sensor_settings']['sensor'])
sensor = Adafruit_DHT.DHT11
sensor_gpio = "4"
measurement = "dht11"
location = "Jacks Room"
humidity, celcius = Adafruit_DHT.read_retry(sensor, sensor_gpio)
# Add Farhenheit for us 'Mericans
farhenheit = celcius * 9 / 5 + 32
# Structure Timestamp to UTC
current_time = time.gmtime()
timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', current_time)
# Structure the data for write
data = [
{
"measurement": measurement,
"tags": {
"location": location,
},
"time": timestamp,
"fields": {
"temperature_c": celcius,
"temperature_f": farhenheit,
"humidity": humidity
}
}
]
# Write it!
client.write_points(data)
# Return the temperature value.
return float(data[0]['fields']['temperature_f'])
#def post_alert(config, trigger, value):
# # IFTTT Webhook Info
# ifttt_url = "https://maker.ifttt.com/trigger/{}/with/key/"
# ifttt_key = config['alerting_settings']['ifttt_key']
# 'value' will be sent to IFTTT and can be included in alert
# data = {"value1": value}
# Pass in event name to trigger appropriately
# ifttt_event_url = ifttt_url.format(trigger) + ifttt_key
# Post it!
# requests.post(ifttt_event_url, json=data)
def read_config():
cfg = configparser.ConfigParser()#
# Read the config
cfg.read(CONFIG_FILE)
# Read the Values from the config
config = {section: {k: v for k, v in cfg.items(section)} for section in cfg.sections()}
# Return the config
# return config
def main():
# Initial threshold counter.
# threshold_counter = []
# Read the config
config = read_config()
while True:
# Get the reading and send to Influx
current_temperature = get_reading(config)
# If temp is higher than threshold, Append to counter
# if current_temperature > float(config['alerting_settings']['temperature_threshold_high']) or current_temperature < float(config['alerting_settings']['temperature_threshold_low']):
# threshold_counter.append(current_temperature)
# If we hit threshold_count, alert and reset.
# This prevents us from alerting every temperature check
# if len(threshold_counter) == int(config['alerting_settings']['threshold_count']):
# temperature_alert = config['alerting_settings']['ifttt_event_name']
# post_alert(config, temperature_alert, current_temperature)
# Reset counter
# threshold_counter = []
# Sleep the interval
time.sleep(int(2))
if __name__ == '__main__':
main()