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')
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
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!