Created
August 22, 2018 20:33
-
-
Save tomrandle/ac835fb26b7676652a51a6b427956109 to your computer and use it in GitHub Desktop.
Log sensor readings to Geckoboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import geckoboard, datetime, bme680 | |
# Set up Pimroni BME680 sensors | |
# https://github.com/pimoroni/bme680-python/blob/master/examples/read-all.py | |
sensor = bme680.BME680() | |
sensor.set_humidity_oversample(bme680.OS_2X) | |
sensor.set_pressure_oversample(bme680.OS_4X) | |
sensor.set_temperature_oversample(bme680.OS_8X) | |
sensor.set_filter(bme680.FILTER_SIZE_3) | |
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) | |
# Set up Geckoboard connection (This is where you need to put your API key). | |
client = geckoboard.client("YOUR-API-KEY-GOES-HERE") | |
try: | |
client.ping() | |
print "Authentication successful" | |
except: | |
print "Incorrect API Key" | |
# Look for a dataset with the following name and if it doesn't exist create it. | |
# Here we tell Geckoboard what schema to expect. | |
dataset = client.datasets.find_or_create( | |
'environmentreadings', { | |
'temperature' : {'type':'number','name':'Temperature','optional':False}, | |
'pressure' : {'type':'number','name':'Pressure','optional':False}, | |
'humidity' : {'type':'number','name':'Humidity','optional':False}, | |
'air-quality' : {'type':'number','name':'Air Quality','optional':True}, | |
'timestamp': { 'type': 'datetime', 'name': 'Time' }}, | |
['timestamp']) # This sets timestamp as the primary key | |
print "Dataset successfully created" | |
# Get the current time | |
reading_time = datetime.datetime.now().isoformat() | |
# Read the sensor values and post them to Geckoboard | |
dataset.post([ | |
{ 'timestamp': reading_time, | |
'temperature': sensor.data.temperature, | |
'pressure': sensor.data.pressure, | |
'humidity': sensor.data.humidity, | |
'air-quality': sensor.data.gas_resistance | |
}], 'timestamp') | |
# Print the values in the console | |
output = "{0:.2f} C,{1:.2f} hPa,{2:.2f} %RH".format(sensor.data.temperature, sensor.data.pressure, sensor.data.humidity) | |
print "Data successfully appended" | |
print output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment