Pi Switch

Control an LED over the internet from your mobile phone with Raspberry Pi.

Inspired from the Internet Switch tutorial by Talha which was based on ESP8266, I thought of doing the same on Raspberry Pi instead.

So that is what we are going to do in this tutorial. We are going to use Raspberry Pi and Grandeur Cloud to build an internet switch. In other words, we will be controlling an LED connected to a GPIO pin of the RPi from our web app which we’ll build with Grandeur Cloud as our back-end. Our web app will send ON/OFF command to the Cloud which our RPi will listen to and will consequently update the voltage on its LED pin.

Why Grandeur Cloud?

If we do not use a managed backend, we would have to integrate an MQTT server and client API between our app and our hardware device (RPi) to let the realtime communications happen between them. And if we have multiple RPis and we want to signal some of them to turn ON their LEDs and others to stay OFF, MQTT’s pub/sub would do it for us.

But if we need to store logs too, let’s say when was RPi1’s LED turned ON or OFF and who did it, we’d need to integrate a database (mySQL or mongoDB) with our system too. Similarly adding file storage, OTA, and other such features would keep increasing the number of integrations we need to do and so the complexity of our system.

Grandeur Cloud gives all of these things in one single integration, and gives us a single API that is available in python, javascript, and C language for RPis, ESPs, and all Arduinos. That’s why it is usually preferred as a backend in IoT, as the single integration for most IoT use cases.

Let us dive into our project now.

Setting up our Raspberry Pi OS

First of all, let’s set up our Raspberry Pi with OS. You can follow these steps to set up your new RPi:

  • Download Raspberry Pi Imager from here and install it.
  • Attach your SD card with your PC and run the RPi imager.
  • Choose the OS you want to run your RPi on. I wanted to use RPi in headless mode without its desktop GUI to keep things as light as possible and not attaching a screen to the RPi. So I installed Raspbian Lite.
  • Choose your SD card and click Write.
  • Once the writing is complete, remove and insert the SD card back, if on mac, or leave it be on windows.
  • Open the SD card (named boot) from explorer, create a new file named wpa_supplicant.conf and place the following in it and replace the angle brackets with corresponding parameters (Read this for details):
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdevupdate_config=1country=<Insert 2 letter ISO 3166-1 country code here>network={ ssid="<Name of your wireless LAN>" psk="<Password for your wireless LAN>"}
  • Create an empty file named ssh in boot. This will allow you to connect to your RPi’s terminal from your PC through ssh (secure shell), without needing a separate screen.
  • Eject the SD card and remove it from your PC. Insert it into the RPi and power it up. The RPi will connect with your WiFi by using the WiFi configurations you provided in the wpa_supplicant.conf file.
  • In your PC, open terminal and run the ping command to check if the RPi has come online using the SSID and PSK you provided in wpa_supplicant.conf file:
ping raspberrypi.local
  • If pinging is successful, it means RPi is connected to your router’s access point. You can now ssh into your RPi. pi is the default user account in Raspbian which you can log into it by running:
ssh [email protected]

If it asks for password, use “raspberry”.

  • Now that you are connected to Raspbian’s terminal, update its repositories and python version you want use.
sudo apt-get updatesudo apt-get install python3 python3-env

Step 1: Getting Started

We’ll follow the same steps as Talha did in his Internet Switch tutorial. For reference, here is a checklist of things you have to do to set up your project on Grandeur Cloud:

Visit settings page, get your project’s API key, and generate the access credentials as shown below. We will be in need of the API key and access credentials while building our app.

Create a new user account from the accounts tab. We will later sign into our app with the email and password we create now. This is another cool feature of Grandeur Cloud: multiple users can use your app by signing in with their emails and passwords. Each user can pair and interact with its own devices. Authentication is built at the core of Grandeur Cloud. User creation is illustrated below:

Now create a new device through devices page. This operation generates a new device ID which we’ll use while writing code for our hardware. So technically, each device that communicates with the cloud needs to have a unique ID. Your device is like your digital asset with a unique ID and you will use this ID whenever you communicate to your device from your app. Device registration also generates an access token. Don’t forget to copy it for it is required in hardware code to establish source authenticity of device requests. It won’t be accessible after closing the dialog. Device creation is illustrated below:

A user cannot interact with a device unless it’s paired with it. Pairing a device makes the user the admin of the device — which means the user can fetch/update its paired devices variables. There are two ways you can pair your device: 1) using the Cloud Dashboard, or 2) through the web app by using pairDevice() function of the device API. This second way has a very strong significance if you look at it from production point of view. Like you can now ship your hardware products, and your users can sign in to your app and claim ownership of a hardware device they buy from your store by pairing it. Here’s how you can pair your device with a user using the Cloud Dashboard:

This is it. You can follow the hello world tutorial of Grandeur Cloud to get more details about each step.

Step 2: Hardware Program

Since we defined a state variable in parms while creating our device model from Cloud Dashboard, we’ll ask the Cloud to send us any updates in our device parms on our RPi-end which consequently will be giving us update to state. For this purpose, we’ll use the Py SDK of Grandeur Cloud. The objective is when our program is run, it listens for any change in the state variable. If it’s value changes to 1, this program turns our LED on GPIO17 ON, otherwise OFF. Let’s do it.

After doing ssh into our RPi, let’s create a virtual environment first for the device program of our project to keep its packages and libraries isolated from the global ones:

python3 -m venv "Pi Switch"

We install RPi’s GPIO package to help control RPi’s GPIO pins and grandeurcloud package for communication with the Cloud.

python -m pip install RPI.GPIO grandeurcloud

Then we create a new hardware.py file and write our device program in it.

Source: Pi Switch

Scroll to Top
Read previous post:

IBASE Technology, a global provider of Industrial motherboards and embedded computing solutions, reveals the IB919 3.5-inch single board computer based on 8th...