Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import smbus
- import time
- # Define some constants from the datasheet
- DEVICE = 0x23 # Default device I2C address
- POWER_DOWN = 0x00 # No active state
- POWER_ON = 0x01 # Power on
- RESET = 0x07 # Reset data register value
- # Start measurement at 4lx resolution. Time typically 16ms.
- CONTINUOUS_LOW_RES_MODE = 0x13
- # Start measurement at 1lx resolution. Time typically 120ms
- CONTINUOUS_HIGH_RES_MODE_1 = 0x10
- # Start measurement at 0.5lx resolution. Time typically 120ms
- CONTINUOUS_HIGH_RES_MODE_2 = 0x11
- # Start measurement at 1lx resolution. Time typically 120ms
- # Device is automatically set to Power Down after measurement.
- ONE_TIME_HIGH_RES_MODE_1 = 0x20
- # Start measurement at 0.5lx resolution. Time typically 120ms
- # Device is automatically set to Power Down after measurement.
- ONE_TIME_HIGH_RES_MODE_2 = 0x21
- # Start measurement at 1lx resolution. Time typically 120ms
- # Device is automatically set to Power Down after measurement.
- ONE_TIME_LOW_RES_MODE = 0x23
- bus = smbus.SMBus(1) # Rev 2 Pi uses 1
- import time
- import datetime
- #print datetime.datetime.now().replace(microsecond=0, second=0)
- # Temperature Sensor x2
- # Download and intialise drivers for I2C
- import MySQLdb
- import os
- import time
- import datetime
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- sensors = ['28-800000081656','28-800000081973','28-011592a904ff']
- ##sensors = ['28-0000074a84a4','28-0000074b05a6','28-0000074b7551','28-0000074ab5e5','28-0000074b122e']
- def get_temp(num):
- try:
- f = open('/sys/bus/w1/devices/%s/w1_slave' % sensors[num-1], 'r')
- lines = f.readlines()
- f.close()
- while lines[0].strip()[-3:] != "YES":
- time.sleep(0.2)
- f = open('/sys/bus/w1/devices/%s/w1_slave' % sensors[num-1], 'r')
- lines = f.readlines()
- f.close()
- temp_raw = lines[1].find('t=')
- if temp_raw != -1:
- temp_string = lines[1].strip()[temp_raw+2:]
- temp = int(temp_string) / 1000.0
- return temp
- except:
- return 'Err '
- import MySQLdb
- while True:
- ## temperatures = [round(get_temp(1),1),round(get_temp(2),1),round(get_temp(3),1),round(get_temp(4),1),round(get_temp(5),1),round(get_temp(6),1)]
- ## temperatures = [round(get_temp(1),1),round(get_temp(2),1),round(get_temp(3),1),round(get_temp(4),1),round(get_temp(5),1)]
- temperatures = [get_temp(1),round(get_temp(2),1),get_temp(3)]
- for i in range(0,1):
- if not isinstance(temperatures[i],basestring):
- temperatures[i] = round(temperatures[i],1)
- measurementTime = datetime.datetime.now()
- # print (measurementTime, "xx")
- ## These two lines are for testing the colours on the website, creating ofsets
- temperatures[1] = temperatures[1] + 10
- temperatures[2] = temperatures[2] + 25
- #print [temperatures[0]]
- # print [temperatures[1]]
- # print [temperatures[2]]
- sql = "INSERT INTO temperatures (temp1,temp2,temp3,time) VALUES ('%s','%s','%s','%s')" % (temperatures[0],temperatures[1],temperatures[2],measurementTime)
- ## sql = "INSERT INTO raspberrypi (temp1,temp2,time,aux1) VALUES ('%s','%s','%s','%s')" % (temperatures[0],temperatures[1],temperatures[2],temperatures[3],temperatures[4],measurementTime()))
- # print 'Connecting to database'
- try:
- db = MySQLdb.connect(host="", user="",passwd="",db="")
- cur = db.cursor()
- # print 'Connected'
- cur.execute(sql)
- db.commit()
- # print 'SQL executed'
- cur.close()
- db.close()
- except MySQLdb.Error as e:
- print e
- # print 'Loop complete'
- for x in range(0,59):
- time.sleep (1)
- ##time.sleep (59)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement