def syncTime(): from m5stack import rtc from ntptime import client n = client(host='pool.ntp.org', timezone=3) n.updateTime() rtc.setTime(*n.time[:-1]) del n
def Library_GetTime(mode): OrganizeList = ['2000', '1', '1', 'Sat', '00', '00', '00'] MonthList = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] try: if wifiCfg.wlan_sta.isconnected()==False: wifiCfg.autoConnect(lcdShow=False) ntp = ntptime.client(host='jp.pool.ntp.org', timezone=9) GetData = ntp.formatDatetime('-', ':') GetData = GetData.replace('-', ':') GetData = GetData.replace(' ', ':') TimeList = GetData.split(':') OrganizeList = [TimeList[0], MonthList[int(TimeList[1])-1], str(int(TimeList[2])),TimeList[3][0:3], TimeList[4], TimeList[5], TimeList[6]] except: None if mode == 0: return OrganizeList elif mode == 8: return ntp.getTimestamp() + 946684800 - 32400 elif len(OrganizeList) > mode - 1: return OrganizeList[mode - 1] else: return "-1"
def sync_ntp(): global g_m5obj ntp = ntptime.client(host='cn.pool.ntp.org', timezone=8)
from m5stack import * from m5ui import * from uiflow import * import wifiCfg import ntptime import i2c_bus import unit setScreenColor(0x222222) color0 = unit.get(unit.COLOR, unit.PORTA) distmm = None wifiCfg.doConnect('Name', 'Password') label2 = M5TextBox(92, 43, "D", lcd.FONT_DejaVu56, 0xFFFFFF, rotate=0) time = M5TextBox(34, 152, "00:00:00", lcd.FONT_DejaVu56, 0xFFFFFF, rotate=0) ntp = ntptime.client(host='de.pool.ntp.org', timezone=1) speaker.tone(800, 200) i2c0 = i2c_bus.easyI2C((21, 22), 0x52, freq=4000) while True: i2c0.write_u8(0x52, 0x00) distmm = (i2c0.read_data(1, i2c_bus.INT16BE))[-1] label2.setText(str((str(distmm) + str('mm')))) label0.setText(str(ntp.formatTime(':'))) wait_ms(2)
if scanfile_flg == False : # [wisun_set_r.txt]が読めないまたは異常値の場合はデフォルト値が設定される AMPERE_RED = 0.7 # デフォルト値:契約アンペア数の何割を超えたら警告 [0.1~1.0] AMPERE_LIMIT = 30 # デフォルト値:アンペア契約値(ブレーカー落ち警報目安で使用) TIMEOUT = 30 # デフォルト値:電力値更新のタイムアウト設定値(秒) この秒数以上更新無ければ電力値非表示となる return scanfile_flg # WiFi設定 import wifiCfg wifiCfg.autoConnect(lcdShow=True) print('>> WiFi init OK') # RTC設定 ntp = ntptime.client(host='jp.pool.ntp.org', timezone=9) print('>> RTC init OK') # 画面初期化 & 初期設定ファイル読込み axp.setLDO2Vol(2.7) #バックライト輝度調整(中くらい) wisun_set_filechk() if lcd.winsize() == (80,160) : # M5StickC/Plus機種判定 m5type = 0 print('>> M5Type = M5StickC') if lcd.winsize() == (136,241) : m5type = 1 print('>> M5Type = M5StickCPlus') draw_lcd()
def draw_co2(self, co2, mhz19temp): def draw_env(self, temp, hum, pres): def redraw(): # MH-Z19B control functions # see https://revspace.nl/MHZ19 class mhz19blib(object): def __init__(self): self.buff = bytearray(9) self.serial = machine.UART(1, tx=0, rx=26) self.serial.init(9600, bits=8, parity=None, stop=1) def checksum_chk(self): sum = 0 for a in self.buff[1:8]: sum = (sum + a) & 0xff c_sum = 0xff - sum + 1 if c_sum == self.buff[8]: return True else: print("c_sum un match!!") return False def ABCdisable(self): #print('send ABC disable command') self.serial.write(b'\xff\x01\x79\x00\x00\x00\x00\x00\x86') # auto caliblation off utime.sleep(0.1) self.serial.readinto(self.buff) def readSensor(self): #print('send read CO2 command') self.serial.write(b'\xff\x01\x86\x00\x00\x00\x00\x00\x79') # co2測定値リクエスト utime.sleep(0.1) len = self.serial.readinto(self.buff) #print('read '+str(len)+'bytes ', self.buff) # co2測定値リクエストの応答 if (len < 9) or (self.buff[0] != 0xff) or not self.checksum_chk() or (self.buff[0] != 0xff) or (self.buff[1] != 0x86) : print('read broken frame(' + str(len) + '): ', self.buff) len = self.serial.readinto(self.buff) print('drop broken frame(' + str(len) + '): ', self.buff) return None return [self.buff[2] * 256 + self.buff[3], self.buff[4] - 40] # メインプログラムはここから(この上はプログラム内関数) # 画面初期化 if lcd.winsize() == (80,160) : # M5StickC/Plus機種判定 m5type = 0 print('>> M5Type = M5StickC') elif lcd.winsize() == (136,241) : m5type = 1 print('>> M5Type = M5StickCPlus') # ユーザー設定ファイル読み込み co2_set_filechk() # センサー初期化 mhz19b = mhz19blib() if S_CO2HAT else None env2 = unit.get(unit.ENV2, unit.PORTA) if S_ENV2 else None # ネットワーク設定 import wifiCfg wifiCfg.autoConnect(lcdShow=True) ntp = ntptime.client(host='jp.pool.ntp.org', timezone=9) # 画面アップデート set_muteLCD(lcd_mute) draw_lcd() # 時刻表示/LED制御スレッド、ambient通知スレッド起動 _thread.start_new_thread(disp_thread, ()) _thread.start_new_thread(am_thread, ()) # ボタン検出スレッド起動 btnA.wasPressed(buttonA_wasPressed) btnB.wasPressed(buttonB_wasPressed) # ABC disable if mhz19b is not None : mhz19b.ABCdisable() # タイムカウンタ初期値設定 co2sensor_tc = utime.time() env2sensor_tc = utime.time() # メインルーチン while True : if env2 is not None : if (utime.time() - env2sensor_tc) >= env2sensor_interval : env2sensor_tc = utime.time() temp = env2.temperature hum = env2.humidity pres = env2.pressure draw_temp() #print("env2:", temp, hum, pres) if mhz19b is not None : if (utime.time() - co2sensor_tc) >= co2sensor_interval : data = mhz19b.readSensor() if data is not None : co2sensor_tc = utime.time() co2 = data[0] mzhtemp = data[1] draw_co2() #print(str(co2) + ' ppm / ' + str(temp) + 'C / ' + str(sensor_tc)) if (co2 is not None) and ((utime.time() - co2sensor_tc) >= TIMEOUT) : # co2応答が一定時間無い場合はCO2値表示のみオフ co2 = None draw_co2() utime.sleep(1) gc.collect()