Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. # _____ _____ _____ __ __ _____ _____
  2. #| | __| | | | | |
  3. #| | |__ | | |_ _| | | | |
  4. #|_____|_____|_____| |_| |_____|_____|
  5. #
  6. # Use Raspberry Pi to get temperature/humidity from DHT11 sensor
  7. # Project Tutorial Url:http://osoyoo.com/2016/12/01/use-raspberry-pi-display-temperaturehumidity-to-i2c-lcd-screen/
  8. #
  9. import smbus
  10. import time
  11. import dht11
  12. import RPi.GPIO as GPIO
  13.  
  14. #define GPIO 14 as DHT11 data pin
  15. Temp_sensor=26
  16.  
  17. # Define some device parameters
  18. I2C_ADDR = 0x27 # I2C device address, if any error, change this address to 0x27
  19. LCD_WIDTH = 16 # Maximum characters per line
  20.  
  21. # Define some device constants
  22. LCD_CHR = 1 # Mode - Sending data
  23. LCD_CMD = 0 # Mode - Sending command
  24.  
  25. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  26. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  27. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  28. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  29.  
  30. LCD_BACKLIGHT = 0x08 # On
  31. #LCD_BACKLIGHT = 0x00 # Off
  32.  
  33. ENABLE = 0b00000100 # Enable bit
  34.  
  35. # Timing constants
  36. E_PULSE = 0.0005
  37. E_DELAY = 0.0005
  38.  
  39. #Open I2C interface
  40. #bus = smbus.SMBus(0) # Rev 1 Pi uses 0
  41. bus = smbus.SMBus(1) # Rev 2 Pi uses 1
  42.  
  43. def lcd_init():
  44. # Initialise display
  45. lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  46. lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  47. lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  48. lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  49. lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  50. lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  51. time.sleep(E_DELAY)
  52.  
  53. def lcd_byte(bits, mode):
  54. # Send byte to data pins
  55. # bits = the data
  56. # mode = 1 for data
  57. # 0 for command
  58.  
  59. bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  60. bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
  61.  
  62. # High bits
  63. bus.write_byte(I2C_ADDR, bits_high)
  64. lcd_toggle_enable(bits_high)
  65.  
  66. # Low bits
  67. bus.write_byte(I2C_ADDR, bits_low)
  68. lcd_toggle_enable(bits_low)
  69.  
  70. def lcd_toggle_enable(bits):
  71. # Toggle enable
  72. time.sleep(E_DELAY)
  73. bus.write_byte(I2C_ADDR, (bits | ENABLE))
  74. time.sleep(E_PULSE)
  75. bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  76. time.sleep(E_DELAY)
  77.  
  78. def lcd_string(message,line):
  79. # Send string to display
  80.  
  81. message = message.ljust(LCD_WIDTH," ")
  82.  
  83. lcd_byte(line, LCD_CMD)
  84.  
  85. for i in range(LCD_WIDTH):
  86. lcd_byte(ord(message[i]),LCD_CHR)
  87.  
  88. def main():
  89. # Main program block
  90. GPIO.setwarnings(False)
  91. GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
  92. # Initialise display
  93. lcd_init()
  94. instance = dht11.DHT11(pin = Temp_sensor)
  95.  
  96. while True:
  97. #get DHT11 sensor value
  98. result = instance.read()
  99. # Send some test
  100.  
  101. if result.is_valid():
  102. lcd_string("Temperatura:"+str(result.temperature)+" C",LCD_LINE_1)
  103. lcd_string(" Umidita':"+str(result.humidity)+"%",LCD_LINE_2)
  104. time.sleep(3) # 3 second delay
  105. lcd_string(" -LaBucciata- ",LCD_LINE_1)
  106. lcd_string(" SmArT hOmE ",LCD_LINE_2)
  107. time.sleep(3)
  108.  
  109. if __name__ == '__main__':
  110.  
  111. try:
  112. main()
  113. except KeyboardInterrupt:
  114. pass
  115. finally:
  116. lcd_byte(0x01, LCD_CMD)
  117. exit (0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement