Let me introduce myself. I'm a student who is interested in electronics, I know that I threw a pretty vague term there but lemme explain. When I was very young, maybe in 6th std (excuse me I don't know the exact year) my sister gave me a light bulb, one with a filament on it. From there my interest in electronics started and later aligned with the field of computers... cause
COMPUTERS == APPLICATION_OF_ELECTRONICS
Back in the year 2k21, this was the year after I started college. I used to watch build videos of electronic projects like LED cube matrix or POV displays but in the end, something I wanted to do out of my **curiosity **, of knowing how those devices would work, and that stuff really fascinated me.
After getting loaded with a bunch of electronic project ideas, there was one project which stuck on my mind for a very long time. In that project, I wanted to control my home light bulbs and fans via my smartphone. Then my research journey began...
Let the journey begin
I was not sure how to achieve that result, thus I started Dorking Google. I saw the most common method to achieve my result, and that was using services like Arduino IoT cloud or Blynk, etc. At that time I had only one board which was Raspberry pi 3. Because of the following factors, i could not use those appealing services :
I wasn't aware of the platform which supports Raspberry pi and
I had no dev board other than raspberry pi cause
I was poor
Communication between things
In this particular project, the things were two, and they were :
Raspberry pi + LED connected to it
web app to fire commands
I was looking for some kinda glue to stick them both together so that they could start communicating via messages. That was the very basic plan but before hitting google I thought to myself, what if we could use the HTTP protocol to enable the communication? I was familiar with some of the networking stuff, so I picked up my raspberry pi and installed an apache server, and coded a Lil
.py script to control the led on the pi connected via GPIO.
The idea was to deploy my web app onto the raspberry pi, on doing so I could write a script onto the backend of the web app to directly control the LED via commands coming from the frontend.
I ended up not applying the HTTP protocol and began research. Then I found the MQTT protocol and this time I'm using it.
So I wrote the
.py script on two different files. One for the
publish.py and the other
subscribe.py. I ran both scripts using two separate terminals. the
publish.py file was responsible to publish a msg into the broker and
subscribe.py was to subscribe. The broker is the central MQTT server which is stateless.
As I published a msg from one terminal, immediately i could see the msg appearing on the other terminal. I was so happy that I took that laptop that day and showed it to my whole family.
Way to fire commands
Here comes the juicy stuff. MQTT demo was working flawlessly, now there was the challenge to code
UI and get command from there and
POST to backend. I chose
Express+Node setup at the backend with no database involved. The plan was to make it less complicated. On the frontend side, i used simple
The web app is ready, time was to add MQTT flavor to the backend to communicate via a broker. Thus i used the npm package called also
mqtt. Glued everything together and landed with this beast...
On the PI I modified the
subscribe.py file and interfaced it with a GPIO pin for controlling the LED attached to it.
import paho.mqtt.client as mqtt import time import RPi.GPIO as GPIO #setmode GPIO.setmode(GPIO.BCM) #pin_setup for led. GPIO.setup(4,GPIO.OUT) def on_message(client, userdata, message): msg = str(message.payload.decode("utf-8")) if(msg == "on" or msg=="ON" or msg=="On"): GPIO.output(4, GPIO.HIGH) elif(msg == "off" or msg=="OFF" or msg=="Off"): GPIO.output(4, GPIO.LOW) print("received message: " ,str(message.payload.decode("utf-8"))) mqttBroker ="broker.hivemq.com" client = mqtt.Client("internet") client.connect(mqttBroker) #Loop_Start client.loop_start() client.subscribe("internet") client.on_message=on_message time.sleep(90000) #Loop_End client.loop_stop()
Zoom out a Lil bit
I just finished the three major parts of the project :
If we tryna understand what's really going on under the hood then we need to step back zoom out a Lil bit and see the bigger picture. I won't explain you in the text but on looking at the below image you'll get what i wanna explain.
GitHubrepo : Github
I made a video
documentaryon this project : Documentary