1:14 pm

June 21, 2017

Dear fellow coders, i'm trying to upload temperature data from my RPi sensehat -> Thingspeak channel. I copied the scripts from the internet and tried to integrate them together. I executed the Python script on Putty terminal, the printed temperature looks ok, hovering between 29-30 degrees C but when displayed on thingspeak, it shows up as werid numbers like 4 or 6 degrees C.
Greatly appreciate your advice! Thank you!
#!/usr/bin/env python
import httplib, urllib
import time
sleep = 3
key = '...'
def thermometer():
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
tmax = 40
tmin = tmax - 8
while True:
temp = sense.get_temperature()
#-----rd off temp to 2 d.p.-----
temp = round(temp,2)
print(temp)
temp = int(temp) - tmin
for x in range(0, 8):
for y in range(0, temp):
sense.set_pixel(x, y, 255, 0, 0)
for y in range(temp, 8):
sense.set_pixel(x, y, 0, 0, 0)
params = urllib.urlencode({'field1': temp, 'key':key })
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 temp
print response.status, response.reason
data = response.read()
conn.close()
except:
print "connection failed"
#break
#sleep for desired amount of time
if __name__ == "__main__":
while True:
thermometer()
time.sleep(sleep)
9:24 am


Moderators
March 7, 2017

I think I see part of the problem.
Following these lines in your code:
tmax = 40
tmin = tmax - 8
Then tmin = 32.
temp = int(temp) - tmin #.....or temp=30 - 32
For temps such as 29-30, you would get a temp of -2 to -3 when you post to ThingSpeak. This doesn't explain the values of 4 or 6, except if your variable type for temp is unsigned, but it explains some of the difference (rounding could explain more). Generally ThingSpeak is pretty literal about what it does with your data, so I wouldn't usually look there if I see a wrong format of my data, as long as you get a success response after your post.
Good luck with your project!
2:24 am


Top
January 29, 2014

I suspect the previous poster is correct- you are messing up the value of temp before you send it to thingspeak. Either save a copy of the variable or move the section where you send to thingspeak before you update the pixels.
If that doesnt work you could also try printing out the value of params to make sure you are sending what you think you are sending
print temp
print params
print response.status, response.reason
Most Users Ever Online: 166
Currently Online:
23 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
rw950431: 272
Vinod: 240
piajola: 95
turgo: 70
vespapierre: 63
Adarsh_Murthy: 62
Member Stats:
Guest Posters: 1
Members: 8665
Moderators: 1
Admins: 2
Forum Stats:
Groups: 3
Forums: 14
Topics: 1600
Posts: 5760
Newest Members:
bogart, estebanmateo93, mikewrite, onlinebusiness, winzy99, Zark_zeuganModerators: cstapels: 460
Administrators: Hans: 405, lee: 457