Updating multiple ThingSpeak channels with python | 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
Updating multiple ThingSpeak channels with python
Avatar
Pvince10
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
March 30, 2017 - 7:56 am
sp_Permalink sp_Print

 Hi everyone.

I'm currently working on a raspberry pi based solution to monitor a greenhouse. I've got all my sensors (namely temperature and humididty sensors) to work and would like to display the readings i get from them on my website as graphs using thingspeak. I have successfully updated one channel but never managed to update the two i need at the same time. Here's my code :

#!/usr/bin/python3

import smbus
import time
import http.client, urllib.parse

# Get I2C bus
bus = smbus.SMBus(1)

# SHT31 address, 0x44(68)
# Send measurement command, 0x2C(44)
#        0x06(06)    High repeatability measurement
bus.write_i2c_block_data(0x44, 0x2C, [0x06])

time.sleep(0.5)

# SHT31 address, 0x44(68)
# Read data back from 0x00(00), 6 bytes
# Temp MSB, Temp LSB, Temp CRC, Humididty MSB, Humidity LSB, Humidity CRC
data = bus.read_i2c_block_data(0x44, 0x00, 6)

# Convert the data
temp = data[0] * 256 + data[1]
cTemp = -45 + (175 * temp / 65535.0)
fTemp = -49 + (315 * temp / 65535.0)
humidity = 100 * (data[3] * 256 + data[4]) / 65535.0

#Send humidity
params = urllib.parse.urlencode({'field3': humidity, 'key':'XXXXXXXX'})
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 (humidity)
        print (response.status, response.reason)
        data = response.read()
        conn.close()
except:
        print ("connection failed")

#Send temperature
params = urllib.parse.urlencode({'field1': cTemp, 'key':'XXXXXXXXXXX'})
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 (cTemp)
        print (response.status, response.reason)
        data = response.read()
        conn.close()
except:
        print ("connection failed")

# Output data to screen
print ("Temperature in Celsius is : %.2f C" %cTemp)
print ("Temperature in Fahrenheit is : %.2f F" %fTemp)
print( "Relative Humidity is : %.2f %%RH" %humidity)

 

I'd like to know what I'm doing wrong and any advice or help is greatly appreciated.

Avatar
piajola

Gold
Forum Posts: 62
sp_UserOfflineSmall Offline
2
March 30, 2017 - 6:57 pm
sp_Permalink sp_Print

Pvince10,

Welcome to the forum and TS

You can read your SHT31

Now read this link

http://www.raspberrypi-spy.co.uk/?s=thingspeak

(just tried my "code cloning" and it is working fine in thingspeak ...)

"mix and stir" your SHT31 code with his code and it will work for sure

Then analyze the mix and you can understand "a way" to use python in raspi. It is python 2 but does not matter

Good luck ... it is easySmile

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
12 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 224

Vinod: 134

vespapierre: 63

piajola: 62

chrisjmears: 54

turgo: 54

Newest Members:

eli

sally

Metabolicz

RodrigoSos

tjheikki

manimb54

Forum Stats:

Groups: 4

Forums: 17

Topics: 1124

Posts: 3994

 

Member Stats:

Guest Posters: 1

Members: 5293

Moderators: 0

Admins: 2

Administrators: Hans, lee