Trouble when calling urlopen() twice in a row

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Trouble when calling urlopen() twice in a row

Post by oserror » Sun Dec 08, 2019 8:16 pm

I am having a problem sending two requests to Thingspeak in a row. Below is my code:

Code: Select all

def SendToThingspeak(host, api_key, result):
    from urllib.urequest import urlopen
    # from time import sleep
    # import urequests as ureq

    def start_url():
        return 'http://%s/update?api_key=%s' % (host, api_key)

    data_url = (start_url() +
            ''.join(['&field%d=%.2f' % (x +1, result[x]) for x in range(8)]))
            
    pressure = result[10].replace(' ', '%20')
    forecast = result[9].replace(' ', '%20')
    
    status_url = (start_url() + 
                 '&status=Pressure%%20%s%%20Forecast%%20%s%%20Accuracy%%20%s%%20percent'
                 % (pressure, forecast, result[8]))
    
    try:
        print('data_url: %s' % data_url)
        res1 = urlopen(data_url)
        # res1 = ureq.request('GET', data_url, headers={'Connection':'close'})
        print('Response 1: %s\nData sent to Thingspeak' % res1.read())
        # print('Response 1: %s\nData sent to Thingspeak' % res1.text())
        res1.close()

        print('status_url: %s' % status_url)
        res2 = urlopen(status_url)
        # res2 = ureq.request('GET', status_url, headers={'Connection':'close'})
        print('Response 2: %s\nStatus sent to Thingspeak' % res2.read())
        # print('Response 2: %s\nStatus sent to Thingspeak' % res2.text())
        res2.close()
    except:
        print('Send to Thingspeak failed')
Here is the result of that code:

Code: Select all

Free mem when leaving weather_station: 24192                                                                                                  
Free mem after W.S. unloaded in main(): 24320
data_url: http://api.thingspeak.com/update?api_key=snip&field1=71.54&field2=32.13&field3=41.68&field4=29.86&field5=71.54&field6=30
.24&field7=30.29&field8=3.94                                                                                                                  
Response 1: b'5929'                                                                                                                           
Data sent to Thingspeak                                                                                                                       
status_url: http://api.thingspeak.com/update?api_key=snip&status=Pressure%20Falling%20slowly%20Forecast%20Fairly%20Fine,%20Showers
%20Later%20Accuracy%2078%20percent                                                                                                            
Response 2: b'0'                                                                                                                              
Status sent to Thingspeak
A response of b'0' is a failure.

Changing the order of the URL requests gives opposite results, i.e. the first request works and the second one fails. Adding a 5 second sleep() between requests has no effect.

I have also tried concatenating the urls into one url, which should work, and does work in the WebREPL, but fails when called normally in my program (when the power comes on). I think I should have enough free memory as you can see in the top two lines (from the WebREPL).

Am I closing the sockets/requests properly? I also tried with one variable, res (not res1 and res2), and this didn't work.

Any help would be greatly appreciated!

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: Trouble when calling urlopen() twice in a row EDIT: also with a single long URL

Post by oserror » Mon Dec 09, 2019 6:59 pm

Okay, it seems that updates can only be sent every 15 seconds or they get rejected. That won't work for me as I'm concerned about battery usage.

It still doesn't explain why the full request (values + status -- the entire post/get in one url) only seems to work from the REPL and not when the Wemos D1 Pro Mini is normally operating. Does anyone have an idea why that is the case?

BTW, I did some basic substitutions for the space and comma characters but I've seen this work without the url encoded, i.e. just a string of text after the status keyword, like '&status=Pressure: Falling Fast Forecast: Head for the hills! Accuracy: 94 percent'.

Thanks!

Post Reply