Skip to content

Instantly share code, notes, and snippets.

@kskilling
Last active February 16, 2023 16:52
Show Gist options
  • Select an option

  • Save kskilling/2c8943aa1358adbd9ca35616b8fb6abd to your computer and use it in GitHub Desktop.

Select an option

Save kskilling/2c8943aa1358adbd9ca35616b8fb6abd to your computer and use it in GitHub Desktop.
Python code for a Raspberry Pi to collect data from multiple sensors and upload them to different dashboards
import serial, time
from time import gmtime, strftime
from datetime import datetime
import bme280
import smbus2
from Adafruit_IO import Client
ser = serial.Serial('/dev/ttyUSB0')
from wavefront_sdk import WavefrontProxyClient
wavefront_sender = WavefrontProxyClient(
host="<Wavefront proxy fqdn>",
metrics_port=2878,
distribution_port=2878,
tracing_port=30000,
)
aio = Client ('<adafruit username>', '<adafruit secret key>')
port = 1
address = 0x77
bus = smbus2.SMBus(port)
bme280.load_calibration_params(bus,address)
while True:
now = datetime.now()
timestamp_aq = datetime.timestamp(now)
print("timestamp =", timestamp_aq)
data = []
for index in range(0,10):
datum = ser.read()
data.append(datum)
pmtwofive = int.from_bytes(b''.join(data[2:4]), byteorder='little') / 10
pmten = int.from_bytes(b''.join(data[4:6]), byteorder='little') /10
print(pmtwofive)
wavefront_sender.send_metric(name="<wavefront dashboard name>", value=pmtwofive, timestamp=timestamp_aq, tags={"country":"uk"}, source
="<wavefront user alias>")
print(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
bme280_data = bme280.sample(bus,address)
temperature = bme280_data.temperature
pressure = bme280_data.pressure
humidity = bme280_data.humidity
aio.send('<adafruit temperature data feed name>',temperature)
aio.send('adafruit air pressure data feed name>', pressure)
aio.send('adafruit humidity data feed name>', humidity)
aio.send('adafruit air quality pmtwofive data feed name>',pmtwofive)
aio.send('adafruit air quality pmten data feed name>',pmten)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment