Mercurial > code > home > repos > 7key
diff net.py @ 1:5a93179ccae9
7key code
author | drewp@bigasterisk.com |
---|---|
date | Thu, 11 May 2023 15:07:10 -0700 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net.py Thu May 11 15:07:10 2023 -0700 @@ -0,0 +1,43 @@ +import network +import secrets +from umqttsimple import MQTTClient + + +class Net: + def connect_wifi(self, status): + status(1, "wifi: ") + station = network.WLAN(network.STA_IF) + station.active(True) # activate the interface + status(1, "wifi: on") + + station.connect(secrets.ssid, secrets.wifi_pass) + status(1, "wifi: on/connect") + + while station.isconnected() == False: + pass + + status(1, "wifi: on/connect/ok") + + def connect_mqtt(self, status): + client_id = '7key' + status(2, "mqtt: ") + self.client = MQTTClient(client_id, secrets.mqtt_broker) + + self.client.set_callback(self.on_subscribed_msg) + status(2, "mqtt: connect") + self.client.connect() + status(2, "mqtt: connect/ok") + + def subscribe(self, topic, cb): + self.client.subscribe(topic) + + def on_subscribed_msg(self, topic, msg): + print('mqtt ->', (topic, msg)) + if topic == b'notification' and msg == b'received': + print('ESP received hello message') + + def mqtt_poll(self): + self.client.check_msg() + + def send(self, topic, msg): + self.client.publish(topic.encode('ascii'), msg.encode('ascii'))