Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import smbus
  4. import time
  5. # Define some constants from the datasheet
  6.  
  7. DEVICE = 0x23 # Default device I2C address
  8.  
  9. POWER_DOWN = 0x00 # No active state
  10. POWER_ON = 0x01 # Power on
  11. RESET = 0x07 # Reset data register value
  12.  
  13. # Start measurement at 4lx resolution. Time typically 16ms.
  14. CONTINUOUS_LOW_RES_MODE = 0x13
  15. # Start measurement at 1lx resolution. Time typically 120ms
  16. CONTINUOUS_HIGH_RES_MODE_1 = 0x10
  17. # Start measurement at 0.5lx resolution. Time typically 120ms
  18. CONTINUOUS_HIGH_RES_MODE_2 = 0x11
  19. # Start measurement at 1lx resolution. Time typically 120ms
  20. # Device is automatically set to Power Down after measurement.
  21. ONE_TIME_HIGH_RES_MODE_1 = 0x20
  22. # Start measurement at 0.5lx resolution. Time typically 120ms
  23. # Device is automatically set to Power Down after measurement.
  24. ONE_TIME_HIGH_RES_MODE_2 = 0x21
  25. # Start measurement at 1lx resolution. Time typically 120ms
  26. # Device is automatically set to Power Down after measurement.
  27. ONE_TIME_LOW_RES_MODE = 0x23
  28.  
  29. bus = smbus.SMBus(1) # Rev 2 Pi uses 1
  30.  
  31. import time
  32. import datetime
  33. #print datetime.datetime.now().replace(microsecond=0, second=0)
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. # Temperature Sensor x2
  42. # Download and intialise drivers for I2C
  43. import MySQLdb
  44. import os
  45. import time
  46. import datetime
  47. os.system('modprobe w1-gpio')
  48. os.system('modprobe w1-therm')
  49.  
  50. sensors = ['28-800000081656','28-800000081973','28-011592a904ff']
  51. ##sensors = ['28-0000074a84a4','28-0000074b05a6','28-0000074b7551','28-0000074ab5e5','28-0000074b122e']
  52.  
  53.  
  54.  
  55. def get_temp(num):
  56.  
  57. try:
  58. f = open('/sys/bus/w1/devices/%s/w1_slave' % sensors[num-1], 'r')
  59. lines = f.readlines()
  60. f.close()
  61. while lines[0].strip()[-3:] != "YES":
  62. time.sleep(0.2)
  63. f = open('/sys/bus/w1/devices/%s/w1_slave' % sensors[num-1], 'r')
  64. lines = f.readlines()
  65. f.close()
  66.  
  67. temp_raw = lines[1].find('t=')
  68. if temp_raw != -1:
  69. temp_string = lines[1].strip()[temp_raw+2:]
  70. temp = int(temp_string) / 1000.0
  71.  
  72. return temp
  73. except:
  74. return 'Err '
  75.  
  76.  
  77. import MySQLdb
  78.  
  79. while True:
  80. ## 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)]
  81. ## 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)]
  82. temperatures = [get_temp(1),round(get_temp(2),1),get_temp(3)]
  83.  
  84. for i in range(0,1):
  85. if not isinstance(temperatures[i],basestring):
  86. temperatures[i] = round(temperatures[i],1)
  87.  
  88. measurementTime = datetime.datetime.now()
  89. # print (measurementTime, "xx")
  90.  
  91. ## These two lines are for testing the colours on the website, creating ofsets
  92. temperatures[1] = temperatures[1] + 10
  93. temperatures[2] = temperatures[2] + 25
  94. #print [temperatures[0]]
  95. # print [temperatures[1]]
  96. # print [temperatures[2]]
  97.  
  98. sql = "INSERT INTO temperatures (temp1,temp2,temp3,time) VALUES ('%s','%s','%s','%s')" % (temperatures[0],temperatures[1],temperatures[2],measurementTime)
  99. ## sql = "INSERT INTO raspberrypi (temp1,temp2,time,aux1) VALUES ('%s','%s','%s','%s')" % (temperatures[0],temperatures[1],temperatures[2],temperatures[3],temperatures[4],measurementTime()))
  100.  
  101. # print 'Connecting to database'
  102.  
  103.  
  104. try:
  105. db = MySQLdb.connect(host="", user="",passwd="",db="")
  106. cur = db.cursor()
  107.  
  108. # print 'Connected'
  109.  
  110.  
  111. cur.execute(sql)
  112. db.commit()
  113.  
  114. # print 'SQL executed'
  115. cur.close()
  116. db.close()
  117.  
  118. except MySQLdb.Error as e:
  119. print e
  120.  
  121. # print 'Loop complete'
  122.  
  123. for x in range(0,59):
  124.  
  125.  
  126. time.sleep (1)
  127.  
  128. ##time.sleep (59)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement