3:49 am

March 14, 2018

Hi guys
I have built a circuit with 5 DHT22s there data pins are connected to IO 21 20 16 26 12 respectively and power directly from the psu.
A total of 10 fields 2 each (Temp and humidity) with a max of 8 fields per channel I had to split the channels
unfortunately I have very little coding skills - using the below code I managed to get one sensor to work correctly.
(https://www.hackster.io/adamgarbo/raspberry-pi-2-iot-thingspeak-dht22-sensor-b208f4)
<span class="hljs-string">""" </span><span class="hljs-string">dht22.py </span><span class="hljs-string">Temperature/Humidity monitor using Raspberry Pi and DHT22. </span><span class="hljs-string">Data is displayed at thingspeak.com </span><span class="hljs-string">Original author: Mahesh Venkitachalam at electronut.in </span><span class="hljs-string">Modified by Adam Garbo on December 1, 2016 </span><span class="hljs-string">"""</span> <span class="hljs-keyword">import</span> sys <span class="hljs-keyword">import</span> RPi.GPIO <span class="hljs-keyword">as</span> GPIO <span class="hljs-keyword">from</span> time <span class="hljs-keyword">import</span> sleep <span class="hljs-keyword">import</span> Adafruit_DHT <span class="hljs-keyword">import</span> urllib2 myAPI = <span class="hljs-string">"<your API code here>"</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getSensorData</span><span class="hljs-params">()</span>:</span> RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, <span class="hljs-number">21</span>) <span class="hljs-keyword">return</span> (str(RH), str(T)) <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span> <span class="hljs-keyword">print</span> <span class="hljs-string">'starting...'</span> baseURL = <span class="hljs-string">'https://api.thingspeak.com/update?api_key=%s'</span> % myAPI <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>: <span class="hljs-keyword">try</span>: RH, T = getSensorData() f = urllib2.urlopen(baseURL + <span class="hljs-string">"&field1=%s&field2=%s"</span> % (RH, T)) <span class="hljs-keyword">print</span> f.read() f.close() sleep(<span class="hljs-number">300</span>) <span class="hljs-comment">#uploads DHT22 sensor values every 5 minutes </span> <span class="hljs-keyword">except</span>: <span class="hljs-keyword">print</span> <span class="hljs-string">'exiting.'</span> <span class="hljs-keyword">break</span> <span class="hljs-comment"># call main </span> <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>: main()
I have no idea how to get it more readings to send, and also the fact that I need to send to more than one channel might be tricky..
Any help appreciated, thanks!
3:57 pm

March 14, 2018

""" baseURL = 'https://api.thingspeak.com/update?api_key=%s'
dht22.py
Temperature/Humidity monitor using Raspberry Pi and DHT22.
Data is displayed at thingspeak.com
Original author: Mahesh Venkitachalam at electronut.in
Modified by Adam Garbo on December 1, 2016
"""
import sys
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT
import urllib2
count = 0
myAPI = "JDYURK8TAGWRZL29"
def getSensorData21():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 21)
return (str(RH), str(T))
def getSensorData16():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 16)
return (str(RH), str(T))
def getSensorData26():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 26)
return (str(RH), str(T))
def getSensorData12():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 12)
return (str(RH), str(T))
def getSensorData20():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 20)
return (str(RH), str(T))
print 'starting...'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % "JDY####"
RH, T = getSensorData21()
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (RH, T))
print f.read()
f.close()
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % "CYJ####"
RH, T = getSensorData16()
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (RH, T))
print f.read()
f.close()
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % "CYJ####"
RH, T = getSensorData26()
f = urllib2.urlopen(baseURL +
"&field3=%s&field4=%s" % (RH, T))
print f.read()
f.close()
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % "I9Q####"
RH, T = getSensorData12()
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (RH, T))
print f.read()
f.close()
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % "I9Q####"
RH, T = getSensorData20()
f = urllib2.urlopen(baseURL +
"&field3=%s&field4=%s" % (RH, T))
print f.read()
f.close()
print 'end'
sleep(30) #uploads DHT22 sensor values every 5 minutes
I have had some luck with the above code above, but the channel fields 3 and 4 of the second sensor do not upload 🙁
1:00 pm

December 12, 2018

HI Guys,
I was trying almost the same with two DHT sensors. I used a standard script which I extended with some lines as described above. Innitially also field 3 and 4 did not upload but when I added an "sleep" command between the two read commands it worked fine for me. So maybe the scipt below does help you guys.
"""
dht22.py
Temperature/Humidity monitor using Raspberry Pi and DHT22.
Data is displayed at thingspeak.com
Original author: Mahesh Venkitachalam at electronut.in
Modified by Adam Garbo on December 1, 2016
"""
import sys
import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT
import urllib2
myAPI = ""
def getSensorData1():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
return (str(RH), str(T))
def getSensorData2():
RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 22)
return (str(RH), str(T))
def main():
print 'starting...'
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
while True:
try:
RH, T = getSensorData1()
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s" % (RH, T))
print f.read()
f.close()
sleep(30) #uploads DHT22 sensor values every 30 secconds
RH, T = getSensorData2()
f = urllib2.urlopen(baseURL +
"&field3=%s&field4=%s" % (RH, T))
print f.read()
f.close()
sleep(30) #uploads DHT22 sensor values every 30 secconds
except:
print 'exiting.'
break
# call main
if __name__ == '__main__':
main()
I'm not an experienced programmer but nevertheless I hope this helps you guys.
Regards, PIeter
2:07 pm


Moderators
March 7, 2017

The most efficient way would be to update all four fields at once. You can concatenate up to 8 fiels on the end of the api command
"&field1=%s&field2=%s&field3=%s&field4=%s...
Then you need to wait at least 15 seconds between updates for a free account, but you will be able to update all four at once.
https://api.thingspeak.com/update.json?api_key=&field1=123&field2=45&field3=56&field4=67 etc
see https://www.mathworks.com/help/thingspeak/writedata.html for more info.
Most Users Ever Online: 166
Currently Online:
32 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