esp.I2C(sda_pin, scl_pin)
I found a Ds3231 clock board in my box so I have tested it using that. I also ordered an I2C light meter to give a try.
Code: Select all
class Ds3231:
DS3231_DEVICE_ADDR = 0x68
DS3231_ADDR_TIME = 0x00
def __init__(self, i2c):
self.i2c = i2c
@staticmethod
def http_time_to_ds_array(aa):
md = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
wdays = {'Sun': 1, 'Mon': 2, 'Tue': 3, 'Wed': 4, 'Thu': 5, 'Fri': 6, 'Sat': 7}
bb = re.match('(.*), ([0-9]+) (...) ([0-9]+) ([0-2][0-9]):([0-5][0-9]):([0-5][0-9])', aa)
data = [int(bb.group(7)), # Second
int(bb.group(6)), # Minute
int(bb.group(5)), # Hour
wdays[bb.group(1)], # Weekday number
int(bb.group(2)), # Day
md[bb.group(3)], # Month
int(bb.group(4)) - 1900] # YY
return data
@staticmethod
def decToBcd(dec):
return ((dec // 10) * 16) + (dec % 10)
@staticmethod
def bcdToDec(bcd):
return ((bcd // 16) * 10) + (bcd % 16)
@staticmethod
def format_da(aa):
return "%02d/%02d/%02d %02d:%02d:%02d" % (aa[6] + 1900, aa[5], aa[4], aa[2], aa[1], aa[0])
def set_date(self, date):
print("date '%s'" % date)
st = [self.DS3231_ADDR_TIME] + list(map(self.decToBcd, self.http_time_to_ds_array(date)))
self.i2c.write(st, self.DS3231_DEVICE_ADDR)
def date(self):
i2c.write([self.DS3231_ADDR_TIME], self.DS3231_DEVICE_ADDR)
return self.format_da(list(map(self.bcdToDec, i2c.read(7, self.DS3231_DEVICE_ADDR))))
i2c = esp.I2C()
ds = Ds3231(i2c)
Code: Select all
def pp(timer):
print("\033[2J")
print(ds.date())
Code: Select all
esp.os_timer(pp, period=500)
and getting the date from the header. No fancy NTP for me. The whole example is here:
https://raw.githubusercontent.com/miano ... /ds3221.py
I am pulling master into my branch once every few days.
What I want to do next is load modules from a remote web site. I am just deciding if I load them into 'irom0' like my own esp_frozen system or try and parse them as the callback brings in blocks of data.