Help converting bytes to int [resolved]
Posted: Mon Jan 17, 2022 8:15 pm
I'm developing a project using micropython on a Raspberry Pi Pico with an RTC clock, temp sensor, and displaying on a 1602 LCD. It's working thanks libraries and tutorials written and shared by others like David Hyland. Thank you!
I'm stuck trying to convert then compare the bytes returned from the RTC with integers. For example I would like to code:
I can't figure out how to cast or convert the bytes returned from the RTC, so right now I have a simple counter in an "if" statement. My code and some photos follow.
Thanks for any help.
Jake
I'm stuck trying to convert then compare the bytes returned from the RTC with integers. For example I would like to code:
Code: Select all
if (minute == 30) and (second == 0):
do_something()
Thanks for any help.
Jake
Code: Select all
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from machine import I2C
from machine import Pin
from tmp102 import Tmp102
import time
# 1602 LCD 0x27
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# DS3231 RTC 0x68
RTC_ADDR = 0x68
RTC_REGISTER = 0x00
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
def ds3231ReadTime():
return i2c.readfrom_mem(int(RTC_ADDR),int(RTC_REGISTER),7);
# tmp102 temperature sensor
TMP102 = Tmp102(i2c, 0x48)
tempC=0
tempF=0
if __name__ == "__main__":
lcd.move_to(0,0)
lcd.putstr("* Raspberry Pi *")
lcd.move_to(0,1)
lcd.putstr("* PICO *")
time.sleep(2)
lcd.clear()
count = 0
while True:
t = ds3231ReadTime()
second = t[0]&0x7F #sec
minute = t[1]&0x7F #min
hour = t[2]&0x3F #hour
week = t[3]&0x07 #week
day = t[4]&0x3F #day
month = t[5]&0x1F #month
year = t[6]&0x3F #year
lcd.move_to(0,0)
lcd.putstr("20%x-%02x-%02x %02x:%02x" % (year,month,day,hour,minute))
lcd.move_to(0,1)
lcd.putstr("* Raspberry Pi *")
print("20%x-%02x-%02x %02x:%02x:%02x" % (year,month,day,hour,minute,second))
time.sleep(1)
if count == 14:
tempC = TMP102.temperature
tempF = (tempC * 9/5 + 32)
print("tempC {0:.1f} tempF {0:.1f}".format(tempC, tempF))
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("Temp C {0:.1f}".format(tempC))
lcd.move_to(0,1)
lcd.putstr("Temp F {0:.1f}".format(tempF))
time.sleep(3)
lcd.clear()
count = 0
count += 1