Adam Garbo
Published © GPL3+

Raspberry Pi 2 IoT: Thingspeak & DHT22 Sensor

Display temperature and humidity data in real-time using a Raspberry Pi 2, a DHT22 sensor and the ThingSpeak IoT platform.

EasyFull instructions provided2 hours37,040
Raspberry Pi 2 IoT: Thingspeak & DHT22 Sensor

Things used in this project

Story

Read more

Schematics

DHT22 Wiring Diagram

Xx6my1zpzgaqjdgc9pft

Raspberry Pi 2 & DHT22 Sensor Wiring Diagram

N5px8e6humfhzor4kvyf

Code

dht22.py

Python
""" 
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 = "<your API code here>" 

def getSensorData(): 
   RH, T = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 23) #23 refers to GPIO 23, or pin 16 on the RPi
   return (str(RH), str(T)) 

def main(): 
   print 'starting...' 
   baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI 

   while True: 
       try: 
           RH, T = getSensorData() 
           f = urllib2.urlopen(baseURL + 
                               "&field1=%s&field2=%s" % (RH, T)) 
           print f.read() 
           f.close() 
           sleep(300) #uploads DHT22 sensor values every 5 minutes 
       except: 
           print 'exiting.' 
           break 

# call main 
if __name__ == '__main__': 
   main() 

ipcheck.sh

SH
#! /bin/sh
sleep 20

SUBJ="Warning: Raspberry Pi - new IP Address"
SUBJ2="Warning: Raspberry Pi was restarted"
EMAIL="<email>@gmail.com"

ip1=""
ip2=""

read ip1 < /home/pi/ipcheck/ip.txt

ip2=$(/sbin/ip -o -4 addr list wlan0 | awk '{print $4}' | cut -d/ -f1)

if [ "$ip1" = "$ip2" ]
        then
        echo "Your Raspberry was restarted, the IP Address is the same as before: $ip2"| mail -s "$SUBJ2" $EMAIL;
else
        echo "$ip2" > /home/pi/ipcheck/ip.txt;
        echo "New IP Address was assigned to your Raspberry Pi: $ip2" | mail -s "$SUBJ" $EMAIL;
        exit;
fi

ipcheck2.sh

SH
#! /bin/sh

SUBJ="Warning: Raspberry Pi - new IP Address"
EMAIL="<email>@gmail.com"

ip1=""
ip2=""

read ip1 < /home/pi/ipcheck/ip.txt

ip2=$(/sbin/ip -o -4 addr list wlan0 | awk '{print $4}' | cut -d/ -f1)

if [ "$ip1" = "$ip2" ]
        then
  exit
else
        echo "$ip2" > /home/pi/ipcheck/ip.txt;
        echo "New IP Address was assigned to your Raspberry Pi: $ip2" | mail -s "$SUBJ" $EMAIL;
        exit
fi

wpa_supplicant.conf

Python
country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

#Enter the details of your wifi here
network={
    ssid="SSID"
    psk="password"
}

#This will allow your Raspberry Pi to roam and connect to open networks
network={
    key_mgmt=NONE
    priority=-999
}

crontab

Python
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command


@reboot /home/pi/ipcheck/ipcheck.sh

*/5 * * * * sh /home/pi/ThingSpeak/script_check.sh &>/dev/null

*/15 * * * * sh /home/pi/ipcheck/ipcheck2.sh &>/dev/null

ssmtp.conf

Python
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=


# The full hostname
hostname=raspberrypi

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

AuthUser=<email>@gmail.com
AuthPass=<password>
UseSTARTTLS=YES

revaliases

Python
# sSMTP aliases
#
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.

root:<email>@gmail.com:smtp.gmail.com:587

script_check.sh

SH
Script that will start the dht22.py script in the background.
pgrep -f dht22.py || sudo nohup python /home/pi/ThingSpeak/dht22.py /dev/null &

Credits

Adam Garbo

Adam Garbo

1 project • 2 followers

Comments