Introduction: 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:
- 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
- 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
- LDR (Light Depend Resistor)
Step 2: Installing the DHT11 Sensor at RPi
- First, get the Library from Github:
- 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
- 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:
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 Instructable.
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". Bellow 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:
For more projects, please visit my blog: MJRoBot.org
Saludos from the south of the world!
Runner Up in the
Internet of Things Contest 2016
We have a be nice policy.
Please be positive and constructive.