How to parse data?



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



  • 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!



  • @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'
    


  • @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!



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


Log in to reply
 

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