|
Post by mikethechap on Jan 24, 2021 21:21:16 GMT -8
I'm getting an error when I am running SkyWeather2.py. It looks like the error occurs when it tries to take a picture. So I ran test testSkyCamera.py and got the same error. It looks like it boils down to:
Type
Error: Object of type bytes is not JSON serializable Here's the whole error statement:
mySkyCameraText= SkyWeather2 V001 24-Jan-2021 23:14:28 Wind Speed: 0.0MPH Wind Gust: 0.0MPH Temp: 32.0 F -------------------- SkyCam Package Sending -------------------- API Key: c35af0b6b86f7a064bebd3f44e3b88f4 ------->Sea Level 0.0 Traceback (most recent call last): File "testSkyCamera.py", line 17, in <module> SkyCamera.takeSkyPicture() File "/home/pi/SDL_Pi_SkyWeather2/SkyCamera.py", line 124, in takeSkyPicture sendSkyWeather() File "/home/pi/SDL_Pi_SkyWeather2/SkyCamera.py", line 386, in sendSkyWeather r = requests.post(url = API_ENDPOINT, json = data) File "/usr/lib/python3/dist-packages/requests/api.py", line 116, in post return request('post', url, data=data, json=json, **kwargs) File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 519, in request prep = self.prepare_request(req) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 462, in prepare_request hooks=merge_hooks(request.hooks, self.hooks), File "/usr/lib/python3/dist-packages/requests/models.py", line 316, in prepare self.prepare_body(data, files, json) File "/usr/lib/python3/dist-packages/requests/models.py", line 466, in prepare_body body = complexjson.dumps(json) File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps return _default_encoder.encode(obj) File "/usr/lib/python3.7/json/encoder.py", line 199, in encode chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode return _iterencode(o, 0) File "/usr/lib/python3.7/json/encoder.py", line 179, in default raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable
I'm guessing there's a package I'm missing somewhere. Or, maybe something else.
Any help would be appreciated.
Mike
|
|
|
Post by Jason on Jan 25, 2021 1:43:13 GMT -8
I believe the problem is the encoding of the image file. JSON can only contain string data. The base64.b64encode produces a byte string so the requests library is unable to serialize the data variable as JSON when the POST is sent.
Look for the following code snippet in SkyCamera.py:
def sendSkyWeather():
# defining the api-endpoint API_ENDPOINT = "https://skyweather.weatherstem.com/"
with open("static/skycamera.jpg", "rb") as image_file: encoded_string = base64.b64encode(image_file.read())
Add the following line immediately after the code snippet making sure to match the indent of the last line in the snippet:
encoded_string = encoded_string.decode(‘utf-8’)
Once added, try the script again.
Jason
|
|
|
Post by SDL on Jan 26, 2021 10:17:19 GMT -8
Jason,
Excellent find. The question I have is why anything works at all given that problem. Why does our test system work? Does yours?
There must be a reason for the difference in results.
BP
|
|
|
Post by Jason on Jan 26, 2021 13:55:07 GMT -8
I intentionally used the phrase "I believe..." for two reasons: I found it puzzling as well since I assumed it was working in your test systems and because I don't get my kit until tomorrow so didn't have anything on which to test it myself. I'll poke around some more tomorrow when I get a chance and hopefully provide a better answer.
Jason
|
|
|
Post by mikethechap on Jan 27, 2021 3:26:50 GMT -8
Hi, Jason! Thanks tons. I had typed out this reply day before yesterday but forgot to hit Send. Anyway, that did the trick! I appreciate you help!
I just saw the SDL reply. I have no idea why the different responses in the systems, but there it solved my problem. I was not using the dedicated SD card so I had to install everything piecemeal. AS I recall (my memory isn't working so great these days because I'm working in the hospital and everything is pretty nuts), Jason had posted some of his earlier installation steps. Those, as well, were super-helpful.
Mike
|
|
|
Post by Jason on Jan 27, 2021 4:24:42 GMT -8
I intentionally used the phrase "I believe..." for two reasons: I found it puzzling as well since I assumed it was working in your test systems and because I don't get my kit until tomorrow so didn't have anything on which to test it myself. I'll poke around some more tomorrow when I get a chance and hopefully provide a better answer. Jason Looks like USPS is still having capacity issues so the arrival of my KS upgrade kit has been delayed 😭🤬 Jason
|
|
|
Post by Jason on Jan 28, 2021 18:42:53 GMT -8
After not reporting any expected delivery date for the last few days, my KS upgrade kit showed up today! Interestingly enough, the existing code in the repo works on my Raspberry Pi :-) Building from scratch is my kinda of fun so I'm not using the pre-built SD card.
|
|
|
Post by SDL on Jan 29, 2021 10:32:23 GMT -8
Go Jason!
Glad your card works. It's all about having the modules installed correctly.
BP
|
|
|
Post by Jason on Feb 6, 2021 12:32:03 GMT -8
mikethechap what type of Raspberry Pi are you using? Jason
|
|
|
Post by mikethechap on Feb 26, 2021 11:29:10 GMT -8
Sorry I didn't see this before now, Jason. The hospital has been super busy with lots of tough cases.
I'm using a RPi 3B+. I haven't used an RPi 4 yet in any settings. I know there are sometimes issues with the RPi4.
|
|
Sopwith
Junior Member
"If it works out of the box - what fun is that?"
Posts: 69
Raspberry Pi: Yes
Other Device: Pico Pi
|
Post by Sopwith on Mar 5, 2021 12:21:48 GMT -8
re: encoded_string = encoded_string.decode('utf-8')
Can we get this fix committed to the current source base?
|
|
|
Post by SDL on Mar 7, 2021 13:36:58 GMT -8
I'll put this in V023 that we are working on now (possibly to be released in the next couple of days).
BP
|
|