-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingspeak.py
executable file
·104 lines (84 loc) · 3.34 KB
/
thingspeak.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
import bme680
import time
import paho.mqtt.client as mqtt
from urllib.parse import urlparse
import sys
import logging
from dotenv import dotenv_values
#load MQTT configuration values from .env file
config = dotenv_values(".env")
#configure Logging
logging.basicConfig(level=logging.INFO)
# Define event callbacks for MQTT
def on_connect(client, userdata, flags, rc):
logging.info("Connection Result: " + str(rc))
def on_publish(client, obj, mid):
logging.info("Message Sent ID: " + str(mid))
mqttc = mqtt.Client(client_id=config["clientId"])
# Assign event callbacks
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
# parse mqtt url for connection details
url_str = sys.argv[1]
print(url_str)
url = urlparse(url_str)
base_topic = url.path[1:]
# Configure MQTT client with user name and password
mqttc.username_pw_set(config["username"], config["password"])
# Load CA certificate for Transport Layer Security
mqttc.tls_set("./broker.thingspeak.crt")
#Connect to MQTT Broker
mqttc.connect(url.hostname, url.port)
mqttc.loop_start()
#Set Thingspeak Channel to publish to
topic = "channels/"+config["channelId"]+"/publish"
#create an instance of the sensor
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except (RuntimeError, IOError):
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the sensor data.
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
#filter protects against transient changes in conditions
sensor.set_filter(bme680.FILTER_SIZE_3)
#enable and change gas measurement settings
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
#publish data to thingspeak using MQQT protocol
print('\n\nPublishing:')
try:
while True:
if sensor.get_sensor_data():
# get temp, humidity and pressure from sensor
temperature = round(sensor.data.temperature,2)
humidity = round(sensor.data.humidity,2)
pressure = round(sensor.data.pressure,2)
# add data to payload
payload=f"field1={temperature}&field2={humidity}&field3={pressure}"
# if the sensor heat is stable
if sensor.data.heat_stable:
#get gas resistance reading
gas_resistance = round(sensor.data.gas_resistance,2)
#get air quality reading from text file - will take last reading if more than one in file
with open('airquality.txt') as f:
for line in f:
pass
air_quality = line
#add data to payload
payload=f"field1={temperature}&field2={humidity}&field3={pressure}&field4={gas_resistance}&field5={air_quality}"
#publish data to thingspeak
mqttc.publish(topic, payload)
else:
#publish data to thingpeak without gas resistance and air quality if sensor heat is not stable
mqttc.publish(topic, payload)
#pause for amount of time specified in .env file
time.sleep(int(config["transmissionInterval"]))
except KeyboardInterrupt:
pass