Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import I2C_LCD_driver
- from time import *
- import board
- import adafruit_dht
- import smbus
- import time
- dhtDevice = adafruit_dht.DHT11(board.D17)
- mylcd = I2C_LCD_driver.lcd()
- RTC_ADDRESS = 0x68
- i2c = smbus.SMBus(1)
- #mylcd.lcd_display_string("Hello World!", 1)
- def read_rtc_time():
- """Read the time from the RTC module."""
- # Read data from the RTC module
- rtc_data = i2c.read_i2c_block_data(RTC_ADDRESS, 0x00, 3)
- # Extract time information
- hours = bcd_to_decimal(rtc_data[2] & 0x3F)
- minutes = bcd_to_decimal(rtc_data[1])
- seconds = bcd_to_decimal(rtc_data[0] & 0x7F)
- return hours, minutes, seconds
- def bcd_to_decimal(bcd):
- """Convert binary-coded decimal (BCD) to decimal."""
- return ((bcd // 16) * 10) + (bcd % 16)
- while True:
- try:
- hours, minutes, seconds = read_rtc_time()
- time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
- # Print the values to the serial port
- temperature = dhtDevice.temperature
- humidity = dhtDevice.humidity
- mylcd.lcd_display_string("Temp:"+str(temperature)+"C"+" Hum:" +str(humidity)+"%", 1)
- mylcd.lcd_display_string("Time:" +time_str, 2)
- print("Temp: {:.1f} C Humidity: {}% "
- .format(temperature, humidity))
- print(time_str)
- except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going
- print(error.args[0])
- sleep(2.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement