Tim Kendal in MATLAB Answers
上次活动时间: 2021-9-24

Since submitting my query below, I have run my code overnight and found that the data is in fact being uploaded corectly. It now shows in my channel. So my main problem has gone away. The remaining problem is in these lines of code which should print the response from Thingspeak in the console: print(str(response.status) + " " + str(response.reason) + " " + str(timenow) + " " + str(mBars)) #+ " " + str(temp)) print("\n") data = response.read() print(data) conn.close() except: print("connection failed") In short, the line conn.request("POST", "/update", params, headers) works, but lines relating to the response do not. This problem did not arise in my first Channel, mentioned below, where the response in the console is like this: 200 OK 2021-09-24 10:24:25 Any suggestions or help gratefully received! Thanks for looking at this. *********************************** Original post: I have successfully created my first Thingspeak channel and uploaded Mbar data (1 variable). This worked fine. This is the upload function that works - Python3 on my Raspberry Pi. def uploadData(pressure, cTemp): #upload to Thingspeak.com #ctemp is not used mBar = pressure timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S') params = urllib.parse.urlencode({'field1': mBar, 'key':key }) headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = http.client.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() #print(mBar) print(str(response.status) + " " + str(response.reason) + " " + str(timenow) + " " + str(mBar)) #+ " " + str(temp)) #print("\n") data = response.read() conn.close() except: print("connection failed") I am now trying to upload to a new Channel I've created, with 4 variables, all from a BM688 Sensor, and get a "connection faiiled" error every time the code calls the Upload function. The reast of the code works fine, displaying all 4 variables in the Console, and also saving the data to a csv file. This is the upload function that does NOT work: def uploadData(temperature, pressure, humidity, air_quality_score): #upload to Thingspeak.com Mbars = pressure timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S') params = urllib.parse.urlencode({'field1': temperature, 'field2': Mbars,'field3': humidity,'field4': air_quality_score,'key':key }) headers = {"Content-typZZe": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = http.client.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() #print(mBar) print(str(response.status) + " " + str(response.reason) + " " + str(timenow) + " " + str(mBars)) #+ " " + str(temp)) #print("\n") data = response.read() conn.close() except: print("connection failed") I'm not certain the params line of code is correct, but it does not generate any syntax errors, so I think it should be ok. I did try changing the params line to just 1 variable, but that did not work either. The API key I'm using is correct. Can someone try to point me in the right direction to find what is wrong please! I would be very grateful! Tim Kendal

关于 ThingSpeak

The community for students, researchers, and engineers looking to use MATLAB, Simulink, and ThingSpeak for Internet of Things applications. You can find the latest ThingSpeak news, tutorials to jump-start your next IoT project, and a forum to engage in a discussion on your latest cloud-based project. You can see answers to problems other users have solved and share how you solved a problem.