def init_oled(): lcd.init_i2c(22, 21, 128, 64) lcd.set_font(font.font16, 16) lcd.set_font(font.font32, 32) lcd.text_cn('系统启动中', 0, 25, 16) lcd.text('...', 80, 25, 16) lcd.show()
def connect_wifi(): ok = tziot.connect_wifi(WIFI_SSID, WIFI_PWD) if ok is False: print('connect wifi failed') lcd.clear() lcd.text_cn('连接', 0, 25, 16) lcd.text('wifi', 32, 25, 16) lcd.text_cn('失败', 64, 25, 16) lcd.show() machine.reset() print('connect wifi success') lcd.clear() lcd.text_cn('连接', 0, 25, 16) lcd.text('wifi', 32, 25, 16) lcd.text_cn('成功', 64, 25, 16) lcd.show()
def app(): """业务程序.每分钟获取ntp时间和温度值""" global pipe # 连接物联网 fail_num = 0 while True: if fail_num >= 10: print('connect too many.reset!') machine.reset() if not tziot.is_conn(): fail_num += 1 time.sleep(10) continue break print('connect ok') lcd.clear() lcd.text('connect ok', 0, 20, 16) lcd.show() # 周期业务 fail_num = 0 run_time = 0 while True: if fail_num >= 10: print('fail too many.reset!') machine.reset() lcd.clear() # 获取ntp网络时间 resp, err = tziot.call(pipe, 0x2141000000000004, RID_GET_TIME, 5000, bytearray()) if err != 0: fail_num += 1 continue lcd.text(tziot.bytearray_to_str(resp), 0, 0, 8) # 获取南京室外温度 resp, err = tziot.call(pipe, 0x2141000000010001, RID_GET_TEMP, 5000, bytearray()) if err != 0 or len(resp) != 2: fail_num += 1 continue fail_num = 0 temp = (resp[0] << 8) + resp[1] if temp >= 0x8000: temp = 0x10000 - temp temp = temp / 10 lcd.text('%.1f' % temp, 30, 20, 32) lcd.show() time.sleep(60) # 每半小时定时复位 run_time += 60 if run_time >= 1800: print('reset system!') machine.reset()
def detect_key(): global key, rx_text, red_led, blue_led, last_push_time, date_text, outdoor_temp # 2s之内重复按键无效 t = utime.time() if t - last_push_time <= 2: return last_push_time = t value = -1 for i in range(6): if key[i].value() == 0: value = i break if value == -1: return print('key push:', value) # 5号按键是确认键 if value == 5: rx_text = '' red_led.value(1) blue_led.value(1) beep.value(0) lcd.clear() lcd.text(date_text, 0, 0, 8) lcd.text('%.1f' % outdoor_temp, 30, 20, 32) lcd.show() return red_led.value(1) blue_led.value(0) beep.value(0) rx_text = '' lcd.clear() lcd.text_cn('发送中', 0, 25, 16) lcd.text('...', 48, 25, 16) lcd.show() resp, err = tziot.call(pipe, DST_IA, RID_KEY, 5000, bytearray([value])) blue_led.value(1) lcd.clear() if err != 0: lcd.text_cn('发送失败', 0, 25, 16) else: lcd.text_cn('发送成功', 0, 25, 16) lcd.show()
def init_oled(): lcd.init_i2c(22, 21, 128, 64) lcd.text('start system', 0, 0, 16) lcd.text('please wait...', 0, 20, 16) lcd.show()
def app(): """业务程序.每分钟获取ntp时间""" global pipe, red_led, blue_led, beep, is_rx_msg, rx_text, date_text, outdoor_temp # 连接物联网 fail_num = 0 while True: if fail_num >= 10: print('connect too many.reset!') machine.reset() if not tziot.is_conn(): fail_num += 1 time.sleep(10) continue break print('connect ok') # 周期业务 fail_num = 0 run_time = 0 last_get_time = 0 while True: if fail_num >= 10: print('fail too many.reset!') machine.reset() if is_rx_msg: is_rx_msg = False # 亮灯,蜂鸣器鸣叫 red_led.value(0) beep.value(1) time.sleep(0.5) beep.value(0) time.sleep(0.5) beep.value(1) time.sleep(0.5) beep.value(0) # 显示文字 lcd.clear() lcd.text(date_text, 0, 0, 8) lcd.text_cn(rx_text, 0, 32, 16) lcd.show() beep.value(0) t = utime.time() if t - last_get_time > 60 and rx_text == '': last_get_time = t # 获取ntp网络时间 resp, err = tziot.call(pipe, 0x2141000000000004, RID_GET_TIME, 5000, bytearray()) if err != 0: fail_num += 1 continue date_text = tziot.bytearray_to_str(resp) lcd.clear() lcd.text(date_text, 0, 0, 8) # 获取南京室外温度 resp, err = tziot.call(pipe, 0x2141000000010001, RID_GET_TEMP, 5000, bytearray()) if err != 0 or len(resp) != 2: fail_num += 1 continue fail_num = 0 outdoor_temp = (resp[0] << 8) + resp[1] if outdoor_temp >= 0x8000: outdoor_temp = 0x10000 - outdoor_temp outdoor_temp = outdoor_temp / 10 lcd.text('%.1f' % outdoor_temp, 30, 20, 32) lcd.show() red_led.value(1) blue_led.value(1) # 检测按键是否按下 detect_key() time.sleep(1) # 每半小时定时复位 run_time += 1 if run_time >= 3600: print('reset system!') machine.reset()