comparison net.py @ 1:5a93179ccae9

7key code
author drewp@bigasterisk.com
date Thu, 11 May 2023 15:07:10 -0700
parents
children
comparison
equal deleted inserted replaced
0:b0c0a808af2b 1:5a93179ccae9
1 import network
2 import secrets
3 from umqttsimple import MQTTClient
4
5
6 class Net:
7 def connect_wifi(self, status):
8 status(1, "wifi: ")
9 station = network.WLAN(network.STA_IF)
10 station.active(True) # activate the interface
11 status(1, "wifi: on")
12
13 station.connect(secrets.ssid, secrets.wifi_pass)
14 status(1, "wifi: on/connect")
15
16 while station.isconnected() == False:
17 pass
18
19 status(1, "wifi: on/connect/ok")
20
21 def connect_mqtt(self, status):
22 client_id = '7key'
23 status(2, "mqtt: ")
24 self.client = MQTTClient(client_id, secrets.mqtt_broker)
25
26 self.client.set_callback(self.on_subscribed_msg)
27 status(2, "mqtt: connect")
28 self.client.connect()
29 status(2, "mqtt: connect/ok")
30
31 def subscribe(self, topic, cb):
32 self.client.subscribe(topic)
33
34 def on_subscribed_msg(self, topic, msg):
35 print('mqtt ->', (topic, msg))
36 if topic == b'notification' and msg == b'received':
37 print('ESP received hello message')
38
39 def mqtt_poll(self):
40 self.client.check_msg()
41
42 def send(self, topic, msg):
43 self.client.publish(topic.encode('ascii'), msg.encode('ascii'))