efranzwa / WuPicoWeather

:partly_sunny: Raspberry Pi Pico weather station using BME280 sensor that uploads data to a weather service
MIT License
0 stars 0 forks source link

A character "b" is being inserted before ID #5

Closed moz2186 closed 1 year ago

moz2186 commented 1 year ago

https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php? b'ID=myid&baromin=29.998&PASSWORD=mypass&action=updateraw&dateutc=now&humidity=49.645&dewptf=50.902&tempf=69.902'

commenting out: postdata = postdata.encode('ascii') removes the "b'" and single apostrophe on the end but is still not sending updates or joining correctly.

`postdata = urlencode.urlencode(values) postdata2 = postdata.encode("ascii") req = requests.post(urlmod, data=postdata)

print("Sending update disabled for testing/debugging\n")

print(urlmod + postdata)`

will output a valid (clickable) url in Thonny, but it doesn't seem to send itself.

efranzwa commented 1 year ago

Below are the urlmod, postdata and req from a weather station that is running. The 'b' is added when you encode the string using the encode function and should be used this way. I don't think that you will end up with a 'clickable' link after doing the encoding.

Try to look at the error being returned from the website. Use the urllib.error.HTTPError and urllib.error.URLError functions to see what the response error is to help debug.

Do you already have an account at WeatherUnderground? Did you replace 'myid' and 'mypass' with your credentials?

urlmod: https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php? postdata: b'action=updateraw&ID=****&PASSWORD=****&dateutc=now&tempf=54.374&baromin=30.042&humidity=83.186&dewptf=49.377' req: <urllib.request.Request object at 0xb6786280>

moz2186 commented 1 year ago

I added a couple print statements to view output, looked like string is correct. Yes I have account and id/pass just didn't want public. I'm able to directly paste into browser and get a success return but always code 400 in app. Haven't been able to figure out why

On Thu, Jun 8, 2023, 7:38 AM efranzwa @.***> wrote:

Below are the urlmod, postdata and req from a weather station that is running. The 'b' is added when you encode the string using the encode function and should be used this way. I don't think that you will end up with a 'clickable' link after doing the encoding.

Try to look at the error being returned from the website. Use the urllib.error.HTTPError and urllib.error.URLError functions to see what the response error is to help debug.

Do you already have an account at WeatherUnderground? Did you replace 'myid' and 'mypass' with your credentials?

urlmod: https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php ? postdata: b'action=updateraw&ID=&PASSWORD= &dateutc=now&tempf=54.374&baromin=30.042&humidity=83.186&dewptf=49.377' req: <urllib.request.Request object at 0xb6786280>

— Reply to this email directly, view it on GitHub https://github.com/efranzwa/WuPicoWeather/issues/5#issuecomment-1582509238, or unsubscribe https://github.com/notifications/unsubscribe-auth/BALESMITTBRN5EYNPAWFRDDXKHBURANCNFSM6AAAAAAY3WW2VE . You are receiving this because you authored the thread.Message ID: @.***>

moz2186 commented 1 year ago

After uncommenting the debugging lines and fresh from git clone, changing my wifi and WU credentials:

MPY: soft reboot starting wifi connected ip = MYLOCALIP

Temp = 70.659 Pres = 28.782 Humi = 49.503 Dewp = 50.852

Server status code: 400

{'Connection': 'close', 'Content-Type': 'text/plain; charset=utf-8', 'Date': 'Thu, 08 Jun 2023 13:42:26 GMT', 'X-Content-Type-Options': 'nosniff', 'Content-Length': '12'} utf-8 bad request

Traceback (most recent call last): File "main.py", line 138, in File "main.py", line 113, in main File "main.py", line 54, in sendDataWU File "urequests.py", line 33, in json ValueError: syntax error in JSON MicroPython v1.20.0-68-g3229791b6 on 2023-05-11; Raspberry Pi Pico W with RP2040 Type "help()" for more information.

On Thu, Jun 8, 2023 at 8:31 AM Nicholas Mosloski @.***> wrote:

I added a couple print statements to view output, looked like string is correct. Yes I have account and id/pass just didn't want public. I'm able to directly paste into browser and get a success return but always code 400 in app. Haven't been able to figure out why

On Thu, Jun 8, 2023, 7:38 AM efranzwa @.***> wrote:

Below are the urlmod, postdata and req from a weather station that is running. The 'b' is added when you encode the string using the encode function and should be used this way. I don't think that you will end up with a 'clickable' link after doing the encoding.

Try to look at the error being returned from the website. Use the urllib.error.HTTPError and urllib.error.URLError functions to see what the response error is to help debug.

Do you already have an account at WeatherUnderground? Did you replace 'myid' and 'mypass' with your credentials?

urlmod: https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php ? postdata: b'action=updateraw&ID=&PASSWORD= &dateutc=now&tempf=54.374&baromin=30.042&humidity=83.186&dewptf=49.377' req: <urllib.request.Request object at 0xb6786280>

— Reply to this email directly, view it on GitHub https://github.com/efranzwa/WuPicoWeather/issues/5#issuecomment-1582509238, or unsubscribe https://github.com/notifications/unsubscribe-auth/BALESMITTBRN5EYNPAWFRDDXKHBURANCNFSM6AAAAAAY3WW2VE . You are receiving this because you authored the thread.Message ID: @.***>

efranzwa commented 1 year ago

Great, this shows that you are able to query sensor, connect to wifi and create the http request. Code 400 also means the request was received by the server, so practically everything is working ae expected.

I think this might be related to the MicroPython version and possibly the module versions being used. I developed this using MicroPython v1.19.1 (see top of main.py file), you have v1.20.0. There is also a dependency on urequests module which is used to create the http request which you seem to have issue with.

Let me look into this hopefully over the weekend as I should keep the code current with the latest MicroPython release. Thanks for the feedback to help keep this code up to date!

moz2186 commented 1 year ago

I'm glad to help! I am quite new with Python and programming in general. I started by (trying) to verify that the url being generated was correct and partially succeeded.

I want to mention I had an 'unstable' version of MicroPython, I also loaded up the 'stable' version and had the same result.

Thanks for taking the time to look into the issue!

On Fri, Jun 9, 2023 at 7:13 AM efranzwa @.***> wrote:

Great, this shows that you are able to query sensor, connect to wifi and create the http request. Code 400 also means the request was received by the server, so practically everything is working ae expected.

I think this might be related to the MicroPython version and possibly the module versions being used. I developed this using MicroPython v1.19.1 (see top of main.py file), you have v1.20.0. There is also a dependency on urequests module which is used to create the http request which you seem to have issue with.

Let me look into this hopefully over the weekend as I should keep the code current with the latest MicroPython release. Thanks for the feedback to help keep this code up to date!

— Reply to this email directly, view it on GitHub https://github.com/efranzwa/WuPicoWeather/issues/5#issuecomment-1584478516, or unsubscribe https://github.com/notifications/unsubscribe-auth/BALESMJT7ILG3T2QIZ7ASWLXKMHOLANCNFSM6AAAAAAY3WW2VE . You are receiving this because you authored the thread.Message ID: @.***>

efranzwa commented 1 year ago

This should be fixed now. Turns out that MicroPython (with urequests) and standard Python (with urllib.request) handle http post requests differently. The fix now creates the entire url in one string so your original idea to remove the extra encoding is part of the solution. I don't think this is the most 'correct' method as I am leaving out the 'data' argument of the requests.post function. Now the response code is 200 so the data should be correctly sent to the server. Please pull the updated main.py to try it out and let me know. Cheers!

''' build url request string '''
urlmod = url + "?"
postdata = urlencode.urlencode(values)
urlsum = urlmod + postdata
req = requests.post(urlsum)
moz2186 commented 1 year ago

The Pico kept crashing after about 4 updates; some further diagnosis shows that memory was running out. The fix was to add req.close() as below.

''' build url request string ''' urlmod = url + "?" postdata = urlencode.urlencode(values) urlsum = urlmod + postdata req = requests.post(urlsum) req.close()

Edit: I don't know why it doesn't show on new lines in the code box.

efranzwa commented 1 year ago

Hi Moz, I added the req.close() per your suggestion and ran code for 10 updates and seems stable now. Thanks for your contributions! This motivates me to finally clean up the README. I wrote that based on what I 'think' are the steps to setup and check everything (but did not actually try it yet on a new RPi Pico).