Project tutorial
IoT: Connecting The RPi, Arduino And The World!

IoT: Connecting The RPi, Arduino And The World! © GPL3+

In this project, we will program a RPi and an Arduino, to work together to capture data from sensors, sending them to the Web.

  • 7,717 views
  • 1 comment
  • 25 respects

Components and supplies

Apps and online services

About this project

This tutorial was one of the winners of INTERNET of THINGS 2016 Contest. Thanks a lot! In this project, we will program a RPi and an Arduino, to work together to capture data from sensors, sending them to the Web.

The functions will be split be split between the two devices:

RPI:

  • Capture Temperature and Humidity data via an digital sensor (DHT-11)
  • Capture light information (simulating a simple ADC)
  • Communicate with Web, sending all captured data

Arduino:

  • Capture analog data with accuracy

The video bellow, shows data being captured by RPi and sent to ThingSpeak.com website:

This project was part of my tutorial: Mobile Station prototype for Environmental Data Capture ("a Mars Rover emulator") that will be expand here for better understanding. The "Mars Rover" project was also featured on MagPi magazine.

Step 1: Bill of Material

For this project, we will need:

  • Rapsberry Pi2
  • WiFi dongle
  • DHT-11 Temperature and Humidity Sensor
  • Arduino Nano
  • Capacitor
  • Resistors
  • LDR (Light Depend Resistor)

Step 2: Installing the DHT11 sensor at RPi

First, get the Library from Github:

git clone https://github.com/adafruit/Adafruit_Python_DHT.gi... 

Installing the Library:

sudo apt-get update
sudo apt-get install build-essential python-dev python-opensslcd /Home/Pi/Adafruit_Python_DHT
sudo python setup.py install 

Test the sensor running the file AdafruitDHT.py at monitor. Enter as parameters: 11 (Sensor DHT11) and 4 (GPIO where the sensor is connected):

sudo python /Home/Pi/Adafruit_Python_DHT/examples/AdafruitDHT.py 11 4 
  • The result should be the temperature and humidity read by the sensor.

Step 3: Sending Data to Web

For the basic settings of the DH-11 sensor with the RPi, and sending data to internet, a great help was get from this tutorial:

Plotting DHT11 sensor data at ThingSpeak.com using Raspberry Pi

From what was learned, the important is

  • Setting a Channel at ThingSpeak.com:
  • Run the Python Code bellow for tests

Step 4: Adding a "digital" light sensor

The general idea for this step of the project was learned from:

A LDR and a Capacitor was connected to GPIO24. If there is light, the GPIO24 will return HIGH and w/o light “LOW”.

The Python Code used in this test:

Step 5: Adding a anagog Light Intensity sensor

The next step was to get "light intensity data". To add the LDR to RPi the best is to convert the analog signal from the sensor to a digital value using an external ADC (Analog to Digital Converter). The RPi does not have an internal ADC as the Arduino. If you do not have an ADC, a good approximation is to use a capacitor charging/discharging technic. The "Raspberry Pi Cookbook" gives the solution (note that Instead the Potentiometer, a LDR could be used):

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
a_pin = 25
b_pin = 23
def discharge():
    GPIO.setup(a_pin, GPIO.IN)
    GPIO.setup(b_pin, GPIO.OUT)
    GPIO.output(b_pin, False)
    time.sleep(0.005)
def charge_time():
    GPIO.setup(b_pin, GPIO.IN)
    GPIO.setup(a_pin, GPIO.OUT)
    count = 0
    GPIO.output(a_pin, True)
    while not GPIO.input(b_pin):
        count = count + 1
    return count
def analog_read():
    discharge()
    return charge_time()
while True:
    print(analog_read())
    time.sleep(1)

The best is to use the Arduino to capture this kind of info and send it to RPi. The result will be more accurate.

Step 6: Sending all data to the web

The previous Python Code was updated, including the new sensors:

Step 7: Sending an alarm tweet

A simple solution to send alarms, is t send a Tweet directly from the website. In this case the ”React” feature of ThingSpeak.com can be used. Please see the details how to define an React in the video at introduction of this tutorial.

Step 8: Connecting the RPi and the Arduino using Serial Communication

The Arduino used was the NANO that is so powerful as the UNO but on a "small form factor".

For tests purposes, A potentiometer was connected to Arduino analog port A0 and the value transmitted via Serial to the RPi. In the test, RPi will be reading a Keyboard (connect to it or thru VNC) and depending on the command, the Arduino LED will be toggle "ON/OFF". Below the Arduino and Python code used on the tests:

Step 9: Sending Arduino data to the Web

Once the Arduino is connected with RPi, additional data captured by Arduino can also be sent to the Web, together with other data captured by the DH11 and LDR.

The Python code used to send data to the Website was changed to also included the data captured by Arduino (potentiometer value).

Step 10: That's all folks

As always, I hope this project can help others to find their way in the exciting world of electronics and IoT!

The updated code for this project can be found at GITHUB: https://github.com/Mjrovai/MJRoBot-IoT-RPi-Arduino

For more projects, please visit my blog: MJRoBot.org

Saludos from the south of the world!

Thank you

Marcelo

Code

Github
https://github.com/adafruit/Adafruit_Python_DHT.git
Github
https://github.com/Mjrovai/MJRoBot-IoT-RPi-Arduino

Schematics

arduino_Rpi
xPEDYkyCpR8AA4Jm19PW.fzz

Comments

Similar projects you might like

IoT Made Easy w/ UNO, ESP-01, ThingSpeak & MIT App Inventor

Project tutorial by MJRoBot

  • 17,891 views
  • 15 comments
  • 35 respects

The "Mars Rover" Emulator

Project tutorial by MJRoBot

  • 10,121 views
  • 3 comments
  • 48 respects

How To Make An Mini IOT Weather Station

Project tutorial by Austin Detzel

  • 4,507 views
  • 4 comments
  • 16 respects

OH HAI! on Windows 10 IoT Core

Project in progress by BuddyC

  • 12,530 views
  • 3 comments
  • 48 respects

ArduFarmBot - Part 2: "Remote Station" an IoT Implementation

Project tutorial by MJRoBot

  • 6,477 views
  • 8 comments
  • 41 respects

IoT Blink - Getting started with IoT

Project showcase by AppShed Support

  • 2,507 views
  • 1 comment
  • 16 respects
Add projectSign up / Login