0% found this document useful (0 votes)
13 views6 pages

IoT P4

This document outlines a practical project for controlling a Raspberry Pi using WhatsApp through Twilio. It includes steps for hardware setup, creating a Flask application, and connecting to Twilio's WhatsApp sandbox. The project allows users to control LEDs connected to the Raspberry Pi by sending commands via WhatsApp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

IoT P4

This document outlines a practical project for controlling a Raspberry Pi using WhatsApp through Twilio. It includes steps for hardware setup, creating a Flask application, and connecting to Twilio's WhatsApp sandbox. The project allows users to control LEDs connected to the Raspberry Pi by sending commands via WhatsApp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Pritee Badgujar Internet of Things

(IoT)

Practical - 4
Aim: Controlling Raspberry Pi with WhatsApp.

Materials Required:
 Raspberry Pi (e.g., Raspberry Pi 3 B+)
 Jumper Wires
 LED Board

Step 1: Hardware Setup (Wiring)

S.No Rsp Pi GPIO Rsp Pi PIN Board Pin


number number Name

1 GPIO 2 PIN 3 D0 (1)

2 GPIO 3 PIN 5 D1 (2)

3 GPIO 4 PIN 7 D2 (3)

4 GPIO 17 PIN 11 D3 (4)

5 GPIO 27 PIN 13 D4 (5)

6 GPIO 22 PIN 15 D5 (6)

7 GPIO 10 PIN 19 D6 (7)

8 GPIO 9 PIN 21 D7 (8)

Chalk and Duster


Pritee Badgujar Internet of Things
(IoT)

9 GND PIN 6 GND (0)

Step 2: Create a Twilio Account


Go to: https://www.twilio.com/whatsapp
Get a Twilio trial account
Set up a WhatsApp sandbox
Note the sandbox number and join code

Step 3: Set Up Raspberry Pi


pip3 install flask twilio

Optional:
1) Create virtual environment
python3 -m venv twilio-env
2) Activate Virtual environment
source twilio-env/bin/activate
2) Install
pip install twilio flask RPi.GPIO

Step 4: Write a Flask App. Create Python file


“Practical-4.py” and write the following code:

from flask import Flask, request


from twilio.twiml.messaging_response import MessagingResponse
import RPi.GPIO as GPIO

Chalk and Duster


Pritee Badgujar Internet of Things
(IoT)

# Update with your 8 GPIO pins


LED_PINS = {
"led1": 2,
"led2": 3,
"led3": 4,
"led4": 17,
"led5": 27,
"led6": 22,
"led7": 10,
"led8": 9
}

# Setup GPIO
GPIO.setmode(GPIO.BCM)
for pin in LED_PINS.values():
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)

app = Flask(__name__)

@app.route("/whatsapp", methods=["POST"])
def whatsapp():
msg = request.form.get('Body').lower().strip()
response = MessagingResponse()

if "on" in msg or "off" in msg:


found = False
for led, pin in LED_PINS.items():
if led in msg:

Chalk and Duster


Pritee Badgujar Internet of Things
(IoT)

state = GPIO.HIGH if "on" in msg else GPIO.LOW


GPIO.output(pin, state)
response.message(f"{led.upper()} turned {'ON' if state else
'OFF'}")
found = True
break
if not found:
if "all on" in msg:
for pin in LED_PINS.values():
GPIO.output(pin, GPIO.HIGH)
response.message("All LEDs turned ON")
elif "all off" in msg:
for pin in LED_PINS.values():
GPIO.output(pin, GPIO.LOW)
response.message("All LEDs turned OFF")
else:
response.message("Invalid LED command.")
else:
response.message("Try: led1 on, led3 off, all on, all off")

return str(response)

@app.route("/")
def home:
return "Welcome to the LED control app."

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)

Chalk and Duster


Pritee Badgujar Internet of Things
(IoT)

Step 5: Create ngrok account


 Create a free account at https://ngrok.com/
 In the ngrok dashboard, you’ll find your AuthToken.

Step 6: Install pyngrok


pip3 install pyngrok
OR
Step 6: Download and install pyngrok
1) Download Ngrok
wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-
arm.zip
2) Unzip it
unzip ngrok-v3-stable-linux-arm.zip
3) Move it to a location in your PATH
sudo mv ngrok /usr/local/bin
4) Connect ngrok to your account
ngrok config add-authtoken YOUR_AUTH_TOKEN
Replace YOUR_AUTH_TOKEN with your AuthToken
5) ngrok version
6) Run ngrok
ngrok http 5000
7) Forwarding https://abcd1234.ngrok.io

Check venv is active and run the file.


python practical4.py

Step 6: Create twilio account


 Go to https://www.twilio.com/try-twilio.
 Sign up (you’ll need a valid phone number for verification).
 After logging in, you’ll be in the Twilio Console.

Chalk and Duster


Pritee Badgujar Internet of Things
(IoT)

 In the Twilio Console search bar, type “WhatsApp”.


 Click “Messaging / Try it Out / WhatsApp
Sandbox” or Send a WhatsApp message (or search for
“WhatsApp Sandbox” in the search bar).
 Scan the given code and open link. Open WhatsApp on your
phone.
 Send the join code (e.g., join bright-owl) to the Twilio sandbox
number.
 You’ll get a confirmation message:
"You are now connected to the Twilio Sandbox for WhatsApp!"

Step 7: Connect Your Flask App to the Sandbox


 In the “Sandbox Configuration” page,
o WHEN A MESSAGE COMES IN:
https://abcd1234.ngrok.io/whatsapp
Paste your publicly accessible Flask endpoint (from Raspberry Pi)
here.

Step 8: Test on WhatsApp


Send these messages from your verified number to the sandbox
number:
 all on
 all off

Activity: 1) update the code to add blinking.

Chalk and Duster

You might also like