MQTT birth and last will - how to send IP and battery



  • I have my LoPy connecting to wifi and my local MQTT server but im having issues sending the IP as a publish. Ideally I would like to send the IP on wifi connect, and beacon the battery health / send warnings when its low.

    This is in my main.py (when the publish is set to plain text in quotes it works fine)

    wifiaddress = "Wifi Address is:", (wlan.ifconfig()[:1])
    client.set_callback(sub_cb)
    client.connect()
    client.publish(topic="LoPy", msg=wifiaddress)
    

    This is what i get:

    Traceback (most recent call last):
      File "main.py", line 21, in <module>
      File "mqtt.py", line 117, in publish
    TypeError: object with buffer protocol required
    

    Ideally i'd like a cleaner way to find the IP as that run at repl returns

    print(wlan.ifconfig()[:1])
    ('192.168.1.136',)
    

    Also is there a slightly longer explanation of the Atom plugin pymakr.conf setup for syncing files.. I think mine is right but im obviously missing something. For now im running Ampy to copy files over.

    Is there support for last will mqtt message?



  • @ledbelly2142 Not yet, I did read somewhere here that the battery voltage is connected to ADC pin 16 so it should be possible. Hoping for an example that can report it as a Battery percentage.

    I'd like to have the following Topic structure on my LoPy's MQTT topic:
    lopy/ip
    lopy/probes/1
    ...
    lopy/probes/4
    lopy/battery
    lopy/rgb
    lopy/oled/1
    ...
    lopy/oled/4
    lopy/ble

    Cant think of any more yet. Still not sure how i'll go with my plan of 4x SPI Type K temp probes and 4x 128x32 OLEDs as they are SPI as well and require more than just a CS per screen.

    Reporting on the Battery topic would be things like percentage when it hits internal warning levels.. but with the view to using deep sleep it would probably report on wake up. Also a notification when its charging if at all possible would be nice.

    I changed the IP topic and paylod to this btw

    client.publish(topic="lopy/ip", msg=wifiaddress)
    

    And on the far end I use Node-red to append to the front:

    "LoPy Booted with IP: " + msg.payload; 
    

    *node red function code - not micropython

    And send the string to Pushover for a push notification on my phone. I've also mapped the on board button to do a machine.reset() so I can carry it around, hit reset then get a notification on my phone if it connects to WiFi (good for range testing).



  • Did you get the battery value posting to your MQTT broker?

    I had a question about how to do this. With MQTT I am sending a string from the LoRa device to a NanoGateway that takes the MQTT string, e.g. sensors/sensor1/temp/22.5 and publishing it to the mqtt broker.

    This way may require many transmissions from a single sensor 'pack'/device that measure multiple things, such as temp, pressure, humidity, altitude, etc... Are you sending multiple values in a single transmission or multiple?

    Thanks



  • @livius

    wifiaddress = "IP Address is: " + ((str(wlan.ifconfig()[:1]))[2:])[:-3]
    

    Returns:
    IP Address is: 192.168.1.136

    Cheers :)



  • @livius said in MQTT birth and last will - how to send IP and battery:

    wifiaddress = "Wifi Address is:" + str(wlan.ifconfig()[:1])

    Excellent! That works.
    Wifi Address is:('192.168.1.136',)

    I should be able to do remove the first 2 and last 3 characters to clean it up now that its a string.. Might try that now.



  • @sjp770
    change this line
    wifiaddress = "Wifi Address is:", (wlan.ifconfig()[:1])
    to

    wifiaddress = "Wifi Address is:" + str(wlan.ifconfig()[:1])
    


Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.