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

    Pybytes Google integration - removing additional characters?

    Scheduled Pinned Locked Moved Discussion
    2 Posts 1 Posters 48 Views 1 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.
    • R Offline
      Roetz Bikes
      last edited by

      I am trying to use the Pybytes-Google integration. The process went pretty smoothly, all-in-all, except that the payloads that end up on my PubSub subscription have a couple of extra characters in the beginning and end.

      I've been trying to connect directly to Google IOT, but that proved problematic, and difficult to debug.

      My publishing code looks like this:

      from time import sleep
      import utime
      import micropython
      from os import urandom
      import gc
      import pycom
      import machine
      import ujson
      
      payload = {
          "timestamp": utime.time(),
          "device_id": CONFIG.get("device_id"),
          "measurements": [{"test1": 123}, {"test2": 456}]
      }
      
      while True:
          payload["timestamp"] = utime.time()
          data = ujson.dumps(payload)
          pybytes.send_signal(0, data)
      

      The message show up nicely on Pybytes:

      {"timestamp": 1639061966, "measurements": [{"test1": 123, "test2": 456}], "device_id": "device_1"}
      

      But on my google subscription there's an additional > at the start, and a 3 at the end. Like this:

      >{"timestamp": 1639061937, "measurements": [{"test1": 123, "test2": 456}], "device_id": "device_1"}3
      

      This makes it a non-valid json object, and crashes my downstream parsing. Any idea why this is and whether/how this can be avoided?

      1 Reply Last reply Reply Quote 0
      • R Offline
        Roetz Bikes
        last edited by

        If anyone else has the same issue, my current workaround is a setting up a Cloud Function, that removes the extra chars, and republishes to another topic, using the following code:

        import base64
        import json
        from json import JSONDecodeError
        # import os
        
        from google.cloud import pubsub_v1
        
        def main(event, context):
            # Get decoded message
            decoded_message = decode_message(event, context)
            
            # Instantiates a Pub/Sub client
            publisher = pubsub_v1.PublisherClient()
            # PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT') #Returns None!
            PROJECT_ID = "YOUR-PROJECT-ID-HERE" #TODO supply as function parameter, or get the currently running project
        
            topic_name = "pybytes" #This is the topic the clean message would be published to
            topic_path = publisher.topic_path(PROJECT_ID, topic_name)
        
            # Remove first two chars
            # TODO: Make more robust (clean all until first '}' and all after last '}' - probably use regex?)
            message = decoded_message[3:][:-1]
        
            # Convert string to dict and back - otherwise problems with recognizing as json object occur
            try:
                msg_dict = json.loads(message)
            except JSONDecodeError as e:
                print("using [2:]")
                msg_dict = json.loads(decoded_message[2:][:-1])
            except JSONDecodeError as e:
                print("using [4:]")
                msg_dict = json.loads(decoded_message[4:][:-1])
            message = json.dumps(msg_dict)
        
            # Encode the message as bytes
            message_bytes = message.encode('utf-8')
        
            # Publish the clean message
            try:
                publish_future = publisher.publish(topic_path, data=message_bytes)
                publish_future.result()  # Verify the publish succeeded
                return 'Message published.'
            except Exception as e:
                print(e)
                return (e, 500)
        
        
        def decode_message(event, context):
            """Triggered from a message on a Cloud Pub/Sub topic.
            Args:
                 event (dict): Event payload.
                 context (google.cloud.functions.Context): Metadata for the event.
            """
            pubsub_message = base64.b64decode(event['data']).decode('utf-8')
            # print(pubsub_message)
            return pubsub_message
        
        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