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

    SiPy, Micropython and Google IoT Core MQTT

    Scheduled Pinned Locked Moved Discussion
    iotgooglesipymicropythonmqtt
    7 Posts 6 Posters 3.0k Views 6 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.
    • J Offline
      jonnyboy
      last edited by

      Hi, new to pycom but do have experience with MQTT and Google IoT Core.

      Has anyone managed to connect a device running micropython to the Google MQTT IoT Core broker service?

      If so could they please help me with the initial connection code.

      My code:

      #example here: https://docs.pycom.io/chapter/tutorials/all/mqtt.html
      
      from umqtt import MQTTClient
      from network import WLAN
      import machine
      import time
      
      def sub_cb(topic, msg):
        print(msg)
      
      wlan = WLAN(mode=WLAN.STA)
      wlan.connect("SSID", auth=(WLAN.WPA2, "WIFIPWD"), timeout=5000)
      
      while not wlan.isconnected():
          machine.idle()
      print("Connected to Wifi\n")
      
      client = MQTTClient("device_id", "mqtt.googleapis.com", user="USERNAME", password="APIKEY", port=8883)
      
      client.set_callback(sub_cb)
      client.connect()
      client.subscribe(topic="projects/mongoose-iota/topics/telemetry-topic")
      
      while True:
          print("Sending ON")
          client.publish(topic="projects/mongoose-iota/topics/telemetry-topic", msg="ON")
          time.sleep(1)
          print("Sending OFF")
          client.publish(topic="projects/mongoose-iota/topics/telemetry-topic", msg="OFF")
          client.check_msg()
      time.sleep(1)
      

      I keep getting this error:

      Traceback (most recent call last):
      File "main.py", line 22, in <module>
      File "/flash/lib/umqtt.py", line 97, in connect
      AssertionError:
      MicroPython v1.8.6-849-86da809 on 2018-01-17; SiPy with ESP32

      Line 97 is this, mqtt taken from this source https://pypi.python.org/pypi/micropython-umqtt.simple/1.3.4

      assert resp[0] == 0x20 and resp[1] == 0x02
      

      Any help would be very much appreciated.
      Thank you in advance!
      Jon

      1 Reply Last reply Reply Quote 0
      • J Offline
        jonnyboy
        last edited by

        Hi, bumping this on.....anyone please.

        1 Reply Last reply Reply Quote 0
        • sebS Offline
          seb
          last edited by

          @jonnyboy said in SiPy, Micropython and Google IoT Core MQTT:

          https://pypi.python.org/pypi/micropython-umqtt.simple/1.3.4

          It is my understanding that google's mqtt requires you to use JWT to authenticate, unfortunately the cryptography necessary to create JWT tokens is not something our modules can support with the current firmware. You would have to use an external server to generate the JWT tokens which when using MQTT are valid for 1h.

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

            @seb
            JWT tokens use HMAC
            and this lib can be used for it
            https://github.com/micropython/micropython-lib/tree/master/hmac

            think about people first, then computers

            1 Reply Last reply Reply Quote 0
            • S Offline
              Simon90
              last edited by

              @jonnyboy I've been trying to get our pycom device talking to google IoT Core as well. Did you have any luck making it work or finding a micro-python compatible JWT Library?

              1 Reply Last reply Reply Quote 0
              • J Offline
                johanbrichau
                last edited by

                I started to get it working but with the http bridge and not with MQTT (for now).

                On https://github.com/jbrichau/TrackingPrototype I am hacking together a gps tracker prototype that sends location info to a google iot project.

                First off, you need RSA instead of HMAC, as Google requires signing with a private key in the JWT instead of a hash. I hacked some changes into an existing RSA library to make it work on micropython.

                Next, I had to divert to using http because the umqtt library did not work with Google iot server. It did work well with the adafruit server though.

                This is very much work in progress and a lot of hacking, but at least it shows there is a path.

                J 1 Reply Last reply Reply Quote 0
                • J Offline
                  joelrobert @johanbrichau
                  last edited by

                  @johanbrichau thanks for your example project! I am having trouble getting this to work based on your project. are you able to share an example of you config.py file? How do you load the rsa file and generate it?

                  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