Thursday, January 14, 2016

IoT: Is someone there? RPi, Arduino and Firebase!

"This blog is about detecting motion and showing it on a Web page. IoT device will identify movement and publish status to a service on cloud. A web page integrated with the service will show the status on screen. Also, let's glow green LED when no motion is detected and red when there is movement."
Here is a quick demo -

Arduino with Raspberry Pi (RPi)

Using Arduino to interface with motion sensor. Code in Arduino detects motion and lights a red LED and sends high to connected Raspberry Pi pin as well. The RPi is integrated with Firebase on Cloud. Used REST API to update a flag in Firebase data source. A web page that shows default status that no moment detected (safe) will change with the flag on Firebase to notify movement (Someone's there).

Following picture depicts Arduino, RPi and motion sensor connections I have.
RPi is powering Arduino, 5v on RPi is connected to vin on Arduino. Ground to ground and RPi board pin 3/GPIO02 to Arduino pin 9. Arduino detects motion from the sensor and sends high signal to RPi on this pin.

Motion sensor has three pins. It's powered by 5v pin on Arduino. Ground to ground and pin 8 receives input from the sensor. Value will be high whenever the sensor detects motion.

In the video, got green and red LEDs indicating motion on the device. It's standard LED connections. Here is a picture.

Arduino sends high to pin 6 by default; It sends high on pin 7 and pin 9 when motion is detected. 7 lights red LED And high signal on 9 makes RPi update cloud service indicating motion. 

Code on Arduino

#define greenLed 6 #define redLed 7
#define motionSensor 8
#define rpi 9
void setup(){
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(motionSensor, INPUT);
pinMode(rpi, OUTPUT);
void loop(){
delay(500); // If High motion is detected.
if(digitalRead(motionSensor) == HIGH){
digitalWrite(rpi, HIGH);
digitalWrite(rpi, LOW);
void lightRed(){
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
void lightGreen(){
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
Following Python code on RPi updates Firebase Cloud back end that motion is detected (or vice versa). a page integrated with this data source shows the status on a web page. This code is using Firebase REST API to update status flag.

import RPi.GPIO as GPIO import time
import http.client
import json
arduinoPin = 3
GPIO.setup(arduinoPin, GPIO.IN)
while True:
if GPIO.input(arduinoPin) == GPIO.HIGH:
print('Switching ON...')
client = http.client.HTTPSConnection('')
client.request('PUT','/proximityWarning.json', '{"isClose":true}')
response = client.getresponse()
print (response.reason)
print('Switching OFF...')
client = http.client.HTTPSConnection('')
client.request('PUT','/proximityWarning.json', '{"isClose":false}')
response = client.getresponse()
print (response.reason)