Hey there buddies! I hope you are doing well. Today I will be taking things a little bit further than what we did previously. If you guys haven’t read the previous blog yet, click here and go through it. It will give you a broad idea about how to create a two-way communication between your pi-node and the web app.
Last time, the project was very simple in which a simple channel for Raspberry Pi was created that submitted values to the cloud app hosted on Cloudways. These values could come from any source, whether a sensor or the CPU itself. For the purpose of the app, I pushed CPU usage values to the cloud app.
Today, I will be doing something similar but the flow of data will be reversed. I will demonstrate how to send instructions to the Raspberry Pi from the Cloudways-hosted app. To keep things simple, I will show how to light up a simple LED. Although the demonstration is very simple, it is enough to create a foundation of a system that could operate (switch on and off) a host of devices. This allows you to implement a smart system of remote-controlling appliances.
So let’s get started. There are some prerequisites, so just grab your things and get ready.
- Raspberry Pi B+ (you can use any version. Just be sure about pin allocation)
- Simple PHP Cloudways App. If you do not have one, click here to launch one!
- Jumper Wires
- Red, Blue, and Green LEDs
What Is Happening on the Web Page and Raspberry Pi
On the web page, I will use a simple form with 6 buttons to control LEDs connected to the Raspberry Pi. I will make a REST API in our RPi made on Python-Flask. Then, I will call the API routes to perform certain actions. With every route I have appended a GPIO action, which will turn the LEDs on and off. Sounds too much? Just hold on and I will explain.
Before starting to code, let’s check some of the Raspberry Pi settings.
1- The Raspberry Pi should be accessible remotely via an IP. If not, there might be several issues that could be easily fixed. Checkout this link
2- Flask framework must be installed on the Raspberry Pi. Check this documentation for installation
Python Program on Raspberry Pi
- – SSH into your Raspberry Pi with your credentials.
- – Make a new directory and move into it.
mkdir iot && cd iot vim remoteLed.py
import RPi.GPIO as GPIO //Initialising GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17, GPIO.OUT) //Initialising GPIO pins as output GPIO.setup(18, GPIO.OUT) GPIO.setup(27, GPIO.OUT) from flask import Flask // Importing Flask app = Flask(__name__) @ app.route('/') //Simple Hello World Route def hello_world(): return 'Hello, World!' @ app.route('/redledon') //Route for Turning RedLed On def redledon(): GPIO.output(17, GPIO.HIGH) //Turning Pin 17 --> Red Led HIGH return "Red LED on" @ app.route('/redledoff') //Route for Turning RedLed Off def redledoff(): GPIO.output(17, GPIO.LOW) return "Red LED off" //Turning Pin 17 --> Red Led LOW @ app.route('/greenledon') def greenledon(): GPIO.output(18, GPIO.HIGH) return "Green LED on" @ app.route('/greenledoff') def greenledoff(): GPIO.output(18, GPIO.LOW) return "Green LED off" @ app.route('/blueledon') def blueledon(): GPIO.output(19, GPIO.HIGH) return "Blue LED on" @ app.route('/blueledoff') def blueledoff(): GPIO.output(19, GPIO.LOW) return "Blue LED off" if __name__ == "__main__": app.run()
Running the Python Program
– Save and Exit VIM
– Run your program using the following command
The circuit consists of the Pi, LEDs, and a resistor to limit the current that can flow through the circuit.
All the ground are connected to the (GND) pin of the Rpi that basically acts like the negative or 0 volts of a battery. On the other hand, he GPIO Pins 17 , 18 and 19 act like the positive terminal of a battery. When the signal is HIGH, the LED will light up and when the signal is LOW, the LED will stop glowing.
Now take a look at the circuit diagram below:
Code for Cloudways App
You have to change your R-Pi IP-address in the buttons’ actions. This will give a layout of buttons which you can use to turn your circuitry on and off.
<html> <body> <title>Raspberry PI Remote Control</title> <h1>Raspberry PI Remote Control</h1> <form action="http://<your_raspberry_ip>:5000/redledon"> <input type="submit" value="Red LED On"> </form> <form action="http://<your_raspberry_ip>:5000/redledoff"> <input type="submit" value="Red LED Off"> </form> <form action="http://<your_raspberry_ip>:5000/blueledon"> <input type="submit" value="Blue LED On"> </form> <form action="http://<your_raspberry_ip>:5000/blueledoff"> <input type="submit" value="Blue LED Off"> </form> <form action="http://<your_raspberry_ip>:5000/greenledon"> <input type="submit" value="Green LED On"> </form> <form action="http://<your_raspberry_ip>:5000/greenledoff"> <input type="submit" value="Green LED Off"> </form> </body> </html>
Now visit the app. You will see something like this:
Click on any action and you will see the result on the Raspberry Pi.
This is it! A tiny setup which enables you to control appliances from a remote app.