Skip to content

Instantly share code, notes, and snippets.

@archonic archonic/temp_dump.py
Last active Apr 25, 2016

Embed
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
#mongod
# Sensor array
sensors_available = W1ThermSensor.get_available_sensors([W1ThermSensor.THERM_SENSOR_DS18B20])
# DB connection
client = MongoClient()
# Check if DB connection worked
try:
server_info = client.server_info()
print("Server info: /n%s" % (server_info))
except:
print("Looks like Mongod isn't running")
sys.exit()
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.id, sensor.get_temperature()))
sensors_for_db.append( { "id": sensor.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.id, sensor.get_temperature(), datetime.datetime.utcnow() ) )
temps_for_db.append( { "id": sensor.id, "temp": sensor.get_temperature(), "time": datetime.datetime.utcnow() } )
result = temp_collection.insert_many(temps_for_db)
print("Temps written: %s" % (result.inserted_ids))
sleep(1.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.