def processNumber(thing): global initPhase if (thing["parameters"]["source"] == "temperature"): temp = temperature.read() res = client.send(thing["parameters"]["stateTopic"], str(temp))
# get list of sensors and their ids from the db sql_show_all = "SELECT id, sernum, name FROM sensors WHERE active != 0" dbcur.execute(sql_show_all) dbsensors = dbcur.fetchall() # get current date/time curr_date = temperature.current_date() curr_time = temperature.current_time() # for each sensor, measure temperature for sensor_row in dbsensors: sensor_id = sensor_row[0] sensor_serial = sensor_row[1] sensor_name = sensor_row[2] result = temperature.read(sensor_serial) good_reading = result[0] curr_temp = result[1] if good_reading == 'YES': # insert new record with sensor_id, date, time, temperature sql_insert = "INSERT INTO templog (sensor_id, date, time, temperature) VALUES (" + str(sensor_id) + ", '" + str(curr_date) + "', '" + str(curr_time) + "', " + str(curr_temp) + ")" try: dbcur.execute(sql_insert) dbconn.commit() print "Successful record:", sensor_id, curr_date, curr_time, curr_temp except: dbconn.rollback() print "Rolling back ..." elif good_reading == 'NO': print "Wrong checksum!", sensor_id, curr_date, curr_time, curr_temp
def mylogger(): lock.acquire() global ptn global next_call global rate global datafolder global datafile global strrate global index # immediately set schedule of next sample. next_call += rate threading.Timer(next_call - time.time(), mylogger).start() #open datafile f = open(datafolder + datafile, 'rb') #set the first sample time stamp stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # Set space between sample rate and start of samples space = ' ' f.readline() if f.tell() == 0: f = open(datafolder + datafile, 'ab') samplerdata = ''.join(readadc.read()) data = str(stamp) + ' ' + temperature.read() + ' ' + strrate + space + \ str(samplerdata) f.write(data) f.close() elif f.tell() == 512: ptn += 1 datafile = hex(ptn).split('x')[1].upper().zfill( 4) # change filenumber to hex if datafile == 'FFFE': try: pidfile = open(config.get('paths', 'pidfile'), 'r') pid = int(pidfile.read()) pidfile.close() except IOError as e: logger.critical("%s %s", "Unable to assign pid to pro.pid capture.py", e) else: try: os.remove(str(config.get('paths', 'pidfile'))) except OSError as e: logger.critical("%s %s", "Unable to remove pid file fatal error", e) else: try: os.kill(pid, signal.SIGTERM) except OSError as e: logger.critical( "%s %s", "Unable to kill process logger/sampler", e) f = open(datafolder + datafile, 'wb') samplerdata = ''.join(readadc.read()) data = str(stamp) + ' ' + temperature.read() + ' ' + strrate + space + \ str(samplerdata) f.write(data) f.close() else: f = open(datafolder + datafile, 'ab') samplerdata = ''.join(readadc.read()) data = str(samplerdata) f.write(data) f.close() logger.debug("%s %s %s %s", str(index).rjust(6, ' '), str(stamp), str(temperature.read()), str(samplerdata)) index += 1 lock.release()
fmttime = splitdatetimelst[1].split(':') # split time field on colon # create date time object lasttimestamp = datetime.datetime(int(fmtdate[0]), int(fmtdate[1]), int(fmtdate[2]), int(fmttime[0]), int(fmttime[1]), int(fmttime[2])) ################################################# # increase lasttimestamp by (sample rate * 40) # There could be issues doing this as below as I'm not sure what would happen in the case of the day rolling over xtime = int(config.get('capture', 'rate').lstrip("0")) * 40 nexttimestamp = lasttimestamp + datetime.timedelta(0, xtime) # get current time and check that's in excess of the nexttimestamp currentdatetime = datetime.datetime.now() while currentdatetime < nexttimestamp: time.sleep(xtime) else: stamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") f = open(config.get("paths", "datafolder") + datafile, 'wb') data = str(stamp) + ' ' + temperature.read() + ' ' + strrate + ' ' f.write(data) f.close() mylogger()
import datetime import time from gpiozero import LED s = temperature.init() furnace = LED(4) hysteresis = 0.5 desired = 0 actual = 100 while 1: if (actual - desired) > hysteresis: # Turn off furnace # Note: This may result in over-heating, since the # furnace does not turn off immidiately, but keeps # the blower on for a few minutes to cool down furnace.off() print("Turning furnace off...") elif (desired - actual) > hysteresis: # Turn on furnace furnace.on() print("Turning furnace on...") # Sleep for several seconds time.sleep(3) actual = temperature.read(s) print(datetime.datetime.now().time(), "current temp is", actual) try: desired = schedule.get() except: print("Google Calendar timed out.")
# temperature, double # ============================================================================= devices_path = '/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves' dbconn = MySQLdb.connect(host="localhost", user="******", passwd="temperature", db="thermodb") dbcur = dbconn.cursor() if __name__ == "__main__": # read list of devices from /sys/... sensors_list = temperature.read_lines(devices_path) # for each device for sensor in sensors_list: # read from /sys/.../28-000.../w1_slave result = temperature.read(sensor) print result # check if this sensor is in database sqlquery = "SELECT * FROM sensors WHERE sernum = '" + sensor + "'" sensrow = dbcur.execute(sqlquery) # if crc_good == YES if result[0] == "YES": # if sensor exists in db if sensrow != 0: # update sensor's active = true sql_update = "UPDATE sensors SET active = 1 WHERE sernum = '" + sensor + "'" try: dbcur.execute(sql_update) dbconn.commit() except: dbconn.rollback()