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

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
Uploading data from RPi SenseHat to Thingspeak via python script
Avatar
embdev
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
June 21, 2017 - 1:14 pm
sp_Permalink sp_Print sp_EditHistory

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)

 

 

Avatar
cstapels

Gold
Forum Posts: 54
sp_UserOfflineSmall Offline
2
June 22, 2017 - 9:24 am
sp_Permalink sp_Print

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!

Avatar
rw950431

Forum Posts: 250
sp_UserOfflineSmall Offline
3
June 23, 2017 - 2:24 am
sp_Permalink sp_Print

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:
22 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 250

Vinod: 140

piajola: 73

vespapierre: 63

Adarsh_Murthy: 59

cstapels: 54

Newest Members:

Duck

darsana123

decx

ginkgo88

angy1551

wayari

Forum Stats:

Groups: 4

Forums: 17

Topics: 1239

Posts: 4308

 

Member Stats:

Guest Posters: 1

Members: 5537

Moderators: 0

Admins: 2

Administrators: Hans, lee