def mqtt(self, state): self.oled.write_lines( 'WIFI:', ('%8d' % wifi.rssi()) if wifi.is_connected() else ('%8s' % '--'), 'MQTT:', '%8s' % ('OK' if state.mqtt else '--'), )
def __init__(self, state): led = None if reset_cause() == DEEPSLEEP_RESET else StatusLED(config.LED) wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD, led=led) if wifi.is_connected(): print(wifi.mac(), wifi.ip(), wifi.rssi()) mqtt = MQTT(config.NAME, secrets, uid = UID, led=led) opt = dict(exp_aft = config.FREQ * 2.5) mqtt.add('Temp', Temperature, **opt).set_state(state['temp']) mqtt.add('Humid', Humidity, **opt).set_state(state['humid']) mqtt.set_attr("freq", config.FREQ) mqtt.do_pub_cfg = reset_cause() != DEEPSLEEP_RESET or ticks_us() % 10 == 0 mqtt.connect() sleep(1) mqtt.pub_state() sleep(5) mqtt.discon() sleep(5)
def update(self, state, changed): self.oled.rst() self.oled.txt('WIFI:%3d' % (wifi.rssi() if wifi.is_connected() else 0)) self.oled.txt( 'MQTT:%3s' % ('OK' if hasattr(state, 'mqtt') and state.mqtt else '--')) self.oled.txt('SOLR:%3d' % state.solar_temp) self.oled.txt('TANK:%3d' % state.tank_temp) self.oled.txt('TARG:%3d' % state.tank_target_temp) self.oled.txt('%-4s:%3s' % (state.mode.upper(), 'ON' if state.pump else 'OFF'))
def pub(_): self._sched = False if config.COMPNT != None: if hub.light_cmd: hub.light_cmd = False try: self.mqtt.publish(light.cmd_tpc(), light.ON if hub.light else light.OFF, retain = True) except: pass light.set_state(hub.light) if config.COMPNT == LIGHT and config.BRIGHTNESS: light.set_bri(hub.brightness) if config.MOTN: motion.set_state(hub.motion) enable.set_state(hub.enable) if config.COMPNT != None: auto.set_state(hub.auto) if config.BATT: battery.set_state(hub.battery) self.mqtt.set_attr(BATT, hub.battery) if wifi.is_connected(): self.mqtt.set_attr('rssi', wifi.rssi()) self.mqtt.pub_state()
def start(self, hub): if hub.internet: status_led.fast_blink() wifi.connect(secrets.WIFI_NAME, secrets.WIFI_PASSWORD) status_led.off() sleep(2) if wifi.is_connected(): status_led.fast_blink() self.mqtt = MQTT(config.NAME, secrets, uid=UID) self.mqtt.add('Water', Moisture, prim=True, key='water', off_dly=config.NORMAL_SLEEP).set_state(hub.water) enable = self.mqtt.add('Enable', Switch, icon="mdi:bell") def enable_rx(msg): hub.set(enable=msg == enable.ON) self.mqtt.sub(enable.cmd_tpc(), enable_rx) self.mqtt.add('Battery', Battery, key='bat').set_state(hub.battery) self.mqtt.set_attr('battery', hub.battery) self.mqtt.set_attr('rssi', wifi.rssi()) self.mqtt.do_pub_cfg = self.do_pub_cfg self.mqtt.connect() sleep(1) self.mqtt.pub_state() sleep(1) status_led.off()
def set_wifi_attr(self): self.set_attr("ip", wifi.ip()) self.set_attr("mac", self.mac) self.set_attr("rssi", wifi.rssi())