Skip to content

Instantly share code, notes, and snippets.



Last active Apr 25, 2016
What would you like to do?
Dumps temperature data to Mongo, theoretically. I'm running a weird version of Mongo (packaged with a fork of meteor) so this hasn't worked yet.
import os, glob, sched, time, sys
from w1thermsensor import W1ThermSensor
from pymongo import MongoClient
from datetime import datetime
# Run mongo if it's not already running
# Sensor array
sensors_available = W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18B20])
# DB connection
client = MongoClient()
# Check if DB connection worked
server_info = client.server_info()
print("Server info: /n%s" % (server_info))
print("Looks like Mongod isn't running")
db = client.test
sensors_collection = db.sensors
temp_collection = db.temps
# Populate sensors in DB so they can be associated with vessels
print("Sensors detected: %s" % (sensors_available))
sensors_for_db = []
# write sensors to DB
for index, sensor in enumerate(sensors_available):
print("Sensor %s has temperature %.2f" % (, sensor.get_temperature()))
sensors_for_db.append( { "id": } )
result = sensors_collection.insert_many(sensors_for_db)
print("Sensors written: %s" % (result.inserted_ids))
# continuously write temps to DB
while True:
temps_for_db = []
for index, sensor in enumerate(sensors_available):
print( "Sensor %s has temperature %.2f at %s" % (, sensor.get_temperature(), datetime.datetime.utcnow() ) )
temps_for_db.append( { "id":, "temp": sensor.get_temperature(), "time": datetime.datetime.utcnow() } )
result = temp_collection.insert_many(temps_for_db)
print("Temps written: %s" % (result.inserted_ids))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment