Updating multiple ThingSpeak channels with python | 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
Updating multiple ThingSpeak channels with python
No permission to create posts
March 30, 2017
7:56 am
Avatar
Pvince10
New Member
Members
Forum Posts: 1
Member Since:
March 30, 2017
sp_UserOfflineSmall Offline

 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.

March 30, 2017
6:57 pm
Avatar
piajola

Gold
Members
Forum Posts: 92
Member Since:
October 12, 2015
sp_UserOfflineSmall Offline

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:
24 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:

AlienAboth, KeslarAboth, EnigmBioge, debbiepe18, MatthewmUb, cathrynww1

Administrators: Hans: 387, lee: 457