using raspberry py 4 with python 3.9

JosenMargret on 2 Nov 2022 (Edited on 2 Nov 2022)
Latest activity Reply by Jos Spronk on 14 Nov 2022

Hello
I am Jos
i have made a program for upload data to mine channels in pytython 2.7, now I am forced to go to 3.9 and I cannot get the program running
code for 2.7
import httplib
import urllib
def WriteData():
while True:
params = urllib.urlencode({'key': 'xxxxxxxxxxxxxxxx', 'field1': '0.021'})
headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print (response.status, response.reason)
data = response.read()
conn.close()
except:
print ("connection failed")
break
can someone help me with this code?
JosenMargret
JosenMargret on 6 Nov 2022
Hello Christopher
is there an example for writing multiple channels in python 3.9?
now I can write one channel but I can not find a way for multiple channels
this is the code
baseURL = 'http://api.thingspeak.com/update?api_key=C2MDJO6H1F4JPTG4&field1='
f = urllib.request.urlopen(baseURL +str(cpu_percent))
f.read()
f.close
sleep(0.5)
please help me out
Christopher Stapels
Christopher Stapels on 7 Nov 2022 (Edited on 7 Nov 2022)
Do you mean multiple channels or multiple fields in the same channel?
For multiple channels, just repeat the call with the appropriate API keys and channel ID's.
For multiple fields, append &fieldx=number to the end of the url.
Jos Spronk
Jos Spronk on 14 Nov 2022
Hello Christopher
thanks to Ashwin Pajankar I found the right format for multiple fields
baseURL + '&field1=%s&field2=%setc' % (value1, value2,etc)
so I can now write 8 fields
Jos
Christopher Stapels
Christopher Stapels on 2 Nov 2022
What is the output when you use it in 3.9? What error do you see?
Jos Spronk
Jos Spronk on 4 Nov 2022
hello Christoper
thanks for your answer.
python 3.9 is using different librarys
so I get a lot of errors.
the question is.
how to use the correct librarys in 3.9
for example
in 3.9 you call http.client in stead of httplib
I got errors about not loading urlibb or errors around urlencode.
so what is the correct code to load same data to thingspeak in 3.9
I have searched internet for the proper code but did not find the correct answer.
I am using thingspeak for a terrarium.
I made a temperature control which data is send into thingspeak
I hope someone can help me with the proper code.
So I can use python 3.9 instead 2.7
Jos

Tags

No tags entered yet.