Advertisement
safwan092

Untitled

Jan 27th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import I2C_LCD_driver
  2. from time import *
  3. import board
  4. import adafruit_dht
  5. import smbus
  6. import time
  7.  
  8. dhtDevice = adafruit_dht.DHT11(board.D17)
  9. mylcd = I2C_LCD_driver.lcd()
  10.  
  11. RTC_ADDRESS = 0x68
  12. i2c = smbus.SMBus(1)
  13.  
  14. #mylcd.lcd_display_string("Hello World!", 1)
  15.  
  16. def read_rtc_time():
  17. """Read the time from the RTC module."""
  18. # Read data from the RTC module
  19. rtc_data = i2c.read_i2c_block_data(RTC_ADDRESS, 0x00, 3)
  20.  
  21. # Extract time information
  22. hours = bcd_to_decimal(rtc_data[2] & 0x3F)
  23. minutes = bcd_to_decimal(rtc_data[1])
  24. seconds = bcd_to_decimal(rtc_data[0] & 0x7F)
  25.  
  26. return hours, minutes, seconds
  27.  
  28. def bcd_to_decimal(bcd):
  29. """Convert binary-coded decimal (BCD) to decimal."""
  30. return ((bcd // 16) * 10) + (bcd % 16)
  31. while True:
  32. try:
  33. hours, minutes, seconds = read_rtc_time()
  34. time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
  35.  
  36. # Print the values to the serial port
  37. temperature = dhtDevice.temperature
  38. humidity = dhtDevice.humidity
  39. mylcd.lcd_display_string("Temp:"+str(temperature)+"C"+" Hum:" +str(humidity)+"%", 1)
  40. mylcd.lcd_display_string("Time:" +time_str, 2)
  41. print("Temp: {:.1f} C Humidity: {}% "
  42. .format(temperature, humidity))
  43. print(time_str)
  44.  
  45. except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going
  46. print(error.args[0])
  47. sleep(2.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement