Wifi RSSI and mqtt subscribe
-
I'm trying to change the RGB led through a simple subscribe and callback but it doesn't seem to be processing the subscribed message.
I also wanted to publish the current signal strength at boot - is there a way to do this, even if its from a scan before wifi connect?
Any ideas? Here is my main.py so far:
from mqtt import MQTTClient from machine import Pin import machine import time from network import WLAN import pycom pycom.heartbeat(False) def trig_reset(arg): machine.reset() def sub_cb(topic, msg): print(msg) wlan = WLAN(mode=WLAN.STA) wlan.connect("SSID_Name", auth=(WLAN.WPA2, "wifipass"), timeout=5000) while not wlan.isconnected(): machine.idle() print("Connected to Wifi\n") wifiaddress = ((str(wlan.ifconfig()[:1]))[2:])[:-3] client = MQTTClient("LoPy", "192.168.1.15",user="username", password="pass", port=1883) client.connect() client.set_callback(sub_cb) client.subscribe(topic="lopy/rgb") client.publish(topic="lopy/ip", msg=wifiaddress) button = Pin('P10', mode=Pin.IN, pull=Pin.PULL_UP) button.callback(Pin.IRQ_RISING, trig_reset)
-
@this.wiederkehr Thanks, I will try that later tonight.
-
You have to call either
client.wait_msg()
orclient.check_msg()
in order to receive messages. The former is blocking whereas the latter is non blocking and has therefore to be called in a loop. See the comments on the mentioned methods in the following file:
https://github.com/pycom/pycom-libraries/blob/master/examples/mqtt/mqtt.py