Skip to content

Instantly share code, notes, and snippets.

@jahir
Last active June 1, 2019 11:26
Show Gist options
  • Save jahir/3bb8a9b7de91ccf48ac37964f5b9ce9e to your computer and use it in GitHub Desktop.
Save jahir/3bb8a9b7de91ccf48ac37964f5b9ce9e to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# vim: ts=2
# -*- coding: utf-8 -*-
#
from time import strftime, sleep
from urllib.request import urlopen,Request,URLopener
# install with `pip3 install adafruit-circuitpython-bme280`
import busio
import board
import adafruit_bme280
###########
VZ_URL = "https://deine.middleware.example.com/api/data/%s.json?value=%.1f"
UUID = [
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # temperature
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # humidity
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', # pressure
]
I2C_ADDR = 0x76
###########
def log(s):
print(strftime("%F %T"), s)
def vzpost(uuid, val):
url = VZ_URL % (uuid, val)
try:
urlopen(Request(url, headers={'User-Agent': 'bme2vz '+URLopener.version}), data=b'', timeout=20)
except OSError as e:
log("vz Error: %s" % e)
# main #
try:
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=I2C_ADDR)
data = (bme280.temperature, bme280.humidity, bme280.pressure)
log('%5.2f °C | %4.1f %%rH | %6.1f mbar' % data)
for i, val in enumerate(data):
vzpost(UUID[i], val)
except OSError as e:
log("Error: %s" % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment