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

    How to parse data?

    Scheduled Pinned Locked Moved LoPy
    lopyparse datapubnubandroid
    6 Posts 4 Posters 2.0k 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.
    • B Offline
      bihut
      last edited by

      Hi there!.

      Probably someone asked the same thing (or similar) before, but I didn't find any post..

      I'm using LoPy + PubNub in a bidirectional way to exchange information. The exchange of information is fine, there is no problem, but I'm not able to decode the information received in the LoPy device.

      For instance, from Android I do this to send information:
      JSONObject obj = new JSONObject();
      obj.put("port", 2);
      obj.put("dev_eui", "XXXXXXXXX");
      obj.put("payload", Base64.encodeBase64("hello".getBytes("UTF8")));
      pubnub.publish().message(obj.toString()).channel("lopy-down");

      And in my LoPy code, I do this:
      rx_pkt = soc.recv(64)

      Of course I get the information (rx_pkt!=null), but actually I do not know the format or how to decode it. I expect the json string, but I get an ¿hex? string..I tried binascii.hexlify, decode('utf-8'), ubinascii.a2b_base64(rx_pkt)..but nothing works (a2b_base64 gives an error due to the padding).

      I have tried just sending plain text ("hello"), instead a json string, but supposedly pubnub only accepts that json-structure to publish the message, because sending plain text I never get the message in the LoPy side.

      Any thoughts?.

      Thanks in advance and best regards.

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @bihut
        last edited by

        @bihut it would probably help if you posted what you currently receive.

        1 Reply Last reply Reply Quote 0
        • B Offline
          bihut
          last edited by

          @jcaron sure!

          Sending ("hello "+timestamp) in the payload field of the the json-string, in the LoPy side I have this:
          data = s.recv(64)
          print ("1",data.decode('utf-8'));
          print("2",data)
          print("3",ubinascii.hexlify(data))

          getting:
          1 <weird symbols - not readable>
          2 b'\x07f\xdc\xd5\xd7'
          3 b'0766dcd5d738'

          Thanks!

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

            @bihut said in How to parse data?:

            b'0766dcd5d738'

            Will be better if you show us what you really sending whole frame not something cutted in the middle
            show what you got, whole json and also json sended on Android side - will be helpfull

            PS. i suppose point 2 is really as below?

            b'\x07f\xdc\xd5\xd78'
            

            think about people first, then computers

            B 1 Reply Last reply Reply Quote 0
            • B Offline
              bihut @livius
              last edited by bihut

              Ok, I found the error. I was sending the payload as Base64 bytes, and should be as Base64 as String, that is,
              From:
              JSONObject obj = new JSONObject();
              obj.put("port", 2);
              obj.put("dev_eui", "XXXXXX");
              obj.put("payload", Base64.encodeBase64(msg.getBytes("UTF8")));

              To:
              JSONObject obj = new JSONObject();
              obj.put("port", 2);
              obj.put("dev_eui", "XXXXXX");
              obj.put("payload", new String(Base64.encodeBase64(msg.getBytes("UTF8"))));

              Now I get the "hello" =).

              Thanks you very much!

              1 Reply Last reply Reply Quote 0
              • O Offline
                oseto
                last edited by

                Is it possible to build reliable app with parse alternative like Back4app ?

                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