Uploading data from RPi SenseHat to Thingspeak via python script | Raspberry Pi | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Uploading data from RPi SenseHat to Thingspeak via python script
No permission to create posts
June 21, 2017
1:14 pm
Avatar
embdev
New Member
Members
Forum Posts: 1
Member Since:
June 21, 2017
sp_UserOfflineSmall Offline

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)

 

 

June 22, 2017
9:24 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 209
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

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!

June 23, 2017
2:24 am
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
17 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

josephrj11, dennis55, jillhp4, tabathawg69, MarinaMa, Susandom

Administrators: Hans: 387, lee: 457