Introduction: IoT: Connecting the RPi, Arduino and the World!

Picture of IoT: Connecting the RPi, Arduino and the World!

This instructable 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 Instructable: 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

Picture of Installing the DHT11 Sensor at RPi
  1. First, get the Library from Github:
  2. Installing the Library:
    • sudo apt-get update
    • sudo apt-get install build-essential python-dev python-openssl
    • cd /Home/Pi/Adafruit_Python_DHT
    • sudo python setup.py install
  3. 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
  4. The result should be the temperature and humidity read by the sensor

Step 3: Sending Data to Web

Picture of 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

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

Step 4: Adding a "digital" Light Sensor

Picture of 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

Picture of 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

Picture of Sending All Data to the Web

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

Step 7: Sending an Alarm Tweet

Picture of 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 Instructable.

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

Picture of 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". Bellow the Arduino and Python code used on the tests:

Step 9: Sending Arduino Data to the Web

Picture of 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

Picture of 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

Comments

AmitD58 (author)2017-05-05

Sir, cant we use only RPI for this project?

mjrovai (author)AmitD582017-05-06

This project was developed to show how to connect the RPI and an Arduino. But each one perform specific tasks. For example, to connect the camera, sensors, connect with ThingSpeak.com you only need the RPI.

DiegoD68 (author)2016-05-31

Hola, muy buenos días, excelente proyecto,consulta tienes algun comando que al momento de reboot de la RPI ,el programa en Python pueda iniciarse solo?

Saludos

About This Instructable

9,893views

175favorites

License:

Bio: Engineer, writer and forever student. Passionate to share knowledge of electronics with focus on IoT and robotics.
More by mjrovai:ESP32 and OLED Display: Internet Clock - DHT22 IOT Made Simple: Playing With the ESP32 on Arduino IDEIoT Made Simple: Monitoring Multiple Sensors
Add instructable to: