2

I am using PyCamera module of Raspberry Pi to capture an image and store as .jpg. first encoding the image using base64.encodestring(). but while sending encoded string to PubNub server, I get error on my_publish_callback as

('ERROR: ', 'Expecting value: line 1 column 1 (char 0)') ('ERROR: ', JSONDecodeError('Expecting value: line 1 column 1 (char 0)',))

I have tried using base64.b64encode() but still get the same errors. I have tried the script in python 2 and 3;

def my_publish_callback(envelope, status):
    if not status.is_error():
       pass  # Message successfully published to specified channel.
    else:
       #print("recv: ", envelope)
       print("ERROR: ", status.error_data.information)
       print("ERROR: ", status.error_data.exception)

def publish(channel, msg):
    pubnub.publish().channel(channel).message(msg).async(my_publish_callback)

def captureAndSendImage():  
  camera.start_preview()
  time.sleep(2)
  camera.capture("/home/pi/Desktop/image.jpg")
  camera.stop_preview()

  with open("/home/pi/Desktop/image.jpg", "rb") as f:
        encoded = base64.encodestring(f.read())
        publish(myChannel, str(encoded))

I am not able to find or print full error traceback so that I can get some more clues about where the error is occurring. But it looks like PubNub is trying to parse the data in JSON, and its failing.

Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54
  • Enable logging in the PubNub Python SDK and send the full log file to [PubNub Support](https://support.pubnub.com) (attach log file, please). By the way, you should consider [this file sharing solution with PubNub](https://support.pubnub.com/support/solutions/articles/14000043783-can-i-send-large-messages-with-pubnub-). – Craig Conover Jul 14 '18 at 23:24
  • Have you tried sending a JSON object as message, instead of a plain string `pubnub.publish().channel(channel).message({'image': msg})` like shown in this example https://www.pubnub.com/docs/python/data-streams-publish-and-subscribe ? – derpirscher Jul 15 '18 at 05:37
  • The plain string will (should) work. In fact, when you provide a cipher key in the PubNub init, the message gets encrypted which results in a string. – Craig Conover Jul 15 '18 at 18:30
  • Anum Sheraz - looking forward to the PubNub SDK logs, but can you verify that there is actually bits being read? The error message got me thinking that there is no file bits being read (bad path?) and when I google the error, I found this SO post: https://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0 - there are three answers and they all might be your root cause. Please report back and let us know. – Craig Conover Jul 15 '18 at 18:38
  • 1
    I realized the .jpg file size is 154KB, whereas PubNub max packet size is 32KB, so that should clearly say it all. @Craig Thanks for referring to that link , Its useful though https://support.pubnub.com/support/discussions/topics/14000006326 – Anum Sheraz Jul 16 '18 at 21:07
  • @derpirscher , yes I tried that way too, didn't worked. But sending simple string works aswell . – Anum Sheraz Jul 16 '18 at 21:09
  • @AnumSheraz - if you add your solutions as official answer, you will earn more SO rep/points. – Craig Conover Jul 20 '18 at 00:22

1 Answers1

1

I realized the .jpg file size is 154KB, whereas PubNub max packet size is 32KB, so that should clearly say it all. PubNub recommends to send large messages by splitting them and re-arranging them in subscriber-end. Thanks @Craig for referring to that link, Its useful though support.pubnub.com/support/discussions/topics/14000006326

Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54