Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. import smbus
  2. import time
  3. import sys
  4. import Adafruit_DHT
  5. import RPi.GPIO as GPIO
  6. import pymysql
  7.  
  8. db = pymysql.connect("***","***","***","testdb")
  9. cursor = db.cursor()
  10.  
  11. LED_PIN=40
  12. GPIO.setmode(GPIO.BOARD)
  13. GPIO.setup(LED_PIN,GPIO.OUT)
  14.  
  15. I2C_ADDR  = 0x3f
  16. LCD_WIDTH = 20
  17.  
  18. LCD_CHR = 1
  19. LCD_CMD = 0
  20.  
  21. LCD_LINE_1 = 0x80
  22. LCD_LINE_2 = 0xC0
  23. LCD_LINE_3 = 0x94
  24. LCD_LINE_4 = 0xD4
  25.  
  26. LCD_BACKLIGHT  = 0x08
  27. #LCD_BACKLIGHT = 0x00
  28.  
  29. ENABLE = 0b00000100
  30.  
  31. E_PULSE = 0.0005
  32. E_DELAY = 0.0005
  33.  
  34. #bus = smbus.SMBus(0)
  35. bus = smbus.SMBus(1)
  36.  
  37. def lcd_init():
  38.   lcd_byte(0x33, LCD_CMD) # 110011 Initialise
  39.   lcd_byte(0x32, LCD_CMD) # 110010 Initialise
  40.   lcd_byte(0x06, LCD_CMD) # 000110 Cursor move direction
  41.   lcd_byte(0x0C, LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  42.   lcd_byte(0x28, LCD_CMD) # 101000 Data length, number of lines, font size
  43.   lcd_byte(0x01, LCD_CMD) # 000001 Clear display
  44.   time.sleep(E_DELAY)
  45.  
  46. def lcd_byte(bits, mode):
  47.   bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  48.   bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
  49.  
  50.   bus.write_byte(I2C_ADDR, bits_high)
  51.   lcd_toggle_enable(bits_high)
  52.  
  53.   bus.write_byte(I2C_ADDR, bits_low)
  54.   lcd_toggle_enable(bits_low)
  55.  
  56. def lcd_toggle_enable(bits):
  57.   time.sleep(E_DELAY)
  58.   bus.write_byte(I2C_ADDR, (bits | ENABLE))
  59.   time.sleep(E_PULSE)
  60.   bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  61.   time.sleep(E_DELAY)
  62.  
  63. def lcd_string(message,line):
  64.   message = message.ljust(LCD_WIDTH, " ")
  65.  
  66.   lcd_byte(line, LCD_CMD)
  67.  
  68.   for i in range(LCD_WIDTH):
  69.     lcd_byte(ord(message[i]),LCD_CHR)
  70.  
  71. def sql_commit():
  72.  try:
  73.   cursor.execute(sql)
  74.   db.commit()
  75.   print("data uploaded")
  76.  except:
  77.   db.rollback()
  78.   print("data rollback")
  79.  
  80. def led_warning():
  81.  GPIO.output(LED_PIN,GPIO.HIGH)
  82.  time.sleep(0.5)
  83.  GPIO.output(LED_PIN,GPIO.LOW)
  84.  time.sleep(0.5)
  85.  GPIO.output(LED_PIN,GPIO.HIGH)
  86.  time.sleep(0.5)
  87.  GPIO.output(LED_PIN,GPIO.LOW)
  88.  time.sleep(0.5)
  89.  GPIO.output(LED_PIN,GPIO.HIGH)
  90.  time.sleep(0.5)
  91.  GPIO.output(LED_PIN,GPIO.LOW)
  92.  time.sleep(0.5)
  93.  GPIO.output(LED_PIN,GPIO.HIGH)
  94.  time.sleep(0.5)
  95.  GPIO.output(LED_PIN,GPIO.LOW)
  96.  time.sleep(0.5)
  97.  GPIO.output(LED_PIN,GPIO.HIGH)
  98.  time.sleep(0.5)
  99.  GPIO.output(LED_PIN,GPIO.LOW)
  100.  time.sleep(0.5)
  101.  
  102. def lcd_normal():
  103.  lcd_string(t01,LCD_LINE_1)
  104.  lcd_string(t02,LCD_LINE_2)
  105.  lcd_string(time_n, LCD_LINE_3)
  106.  lcd_string("      RPiSpy I2C LCD", LCD_LINE_4)
  107.  sql_commit()
  108.  time.sleep(5)
  109.    
  110. def lcd_warning():
  111.  lcd_string(t01,LCD_LINE_1)
  112.  lcd_string(t02,LCD_LINE_2)
  113.  lcd_string(time_n, LCD_LINE_3)
  114.  lcd_string("WARNING!!!", LCD_LINE_4)
  115.  sql_commit()
  116.  led_warning()
  117.  
  118. def main():
  119.  
  120.   lcd_init()
  121.  
  122.   lcd_string("RPiSpy             <", LCD_LINE_1)
  123.   lcd_string("I2C LCD            <", LCD_LINE_2)
  124.  
  125.   time.sleep(1)
  126.  
  127.   lcd_string(">             RPiSpy", LCD_LINE_1)
  128.   lcd_string(">            I2C LCD", LCD_LINE_2)
  129.  
  130.   time.sleep(1)
  131.  
  132.   while True:
  133.    global t01,t02,time_n,sql
  134.    humidity, temperature = Adafruit_DHT.read_retry(11, 4)
  135.    t01="Temp: "+str(int(temperature))+"C"
  136.    t02="Humidity: "+str(int(humidity))+"%"
  137.    time_n=str(time.strftime("%Y-%m-%d %H:%M", time.localtime()))
  138.    sql = """INSERT INTO DHT11(rec_temp,rec_hum) VALUES(%s,%s) %("str(temperature)","str(humidity)")"""
  139.    if temperature>=30:
  140.     lcd_warning()
  141.    
  142.    else :
  143.     lcd_normal()
  144.    
  145.    
  146. if __name__ == '__main__':
  147.  
  148.   try:
  149.     main()
  150.   except KeyboardInterrupt:
  151.     pass
  152.   finally:
  153.     lcd_byte(0x01, LCD_CMD)
  154.     db.close()
  155.     GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement