Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

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

    Scheduled Pinned Locked Moved MicroPython
    micropythonlopymqttlipo battery
    6 Posts 3 Posters 2.2k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      sjp770
      last edited by sjp770

      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?

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @sjp770
        last edited by

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

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

        think about people first, then computers

        S 2 Replies Last reply Reply Quote 0
        • S Offline
          sjp770 @livius
          last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • S Offline
            sjp770 @livius
            last edited by sjp770

            @livius

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

            Returns:
            IP Address is: 192.168.1.136

            Cheers :)

            1 Reply Last reply Reply Quote 0
            • ledbelly2142L Offline
              ledbelly2142
              last edited by

              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

              Friends don't let friends run with scissors

              S 1 Reply Last reply Reply Quote 0
              • S Offline
                sjp770 @ledbelly2142
                last edited by

                @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).

                1 Reply Last reply Reply Quote 0

                Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                With your input, this post could be even better 💗

                Register Login
                • 1 / 1
                • First post
                  Last post

                Pycom on Twitter

                Pycom