IoT Tutorial #17 [ Raspberry Pi Tutorials #5 ]
In previous post, we have seen demo for Node-Gateway IoT Architecture using Raspberry Pi 3 and NodeMCU.
Here, We will work on another IoT Architecture: Publish-Subscribe IoT Architecture.
Publish - Subscribe IoT Architecture has multiple benefits over Node-Gateway IoT Architecture:
- Faster
- Low power Consumption
Recommended Internet of Things (IOT) Courses:
- Udemy: Complete Guide to Build IOT Things from Scratch to Market
- LinkedIn: IoT Foundations: Fundamentals
- edX: Introduction to the Internet of Things (IoT)
- edureka: IoT Certification Training on Azure
- Coursera: An Introduction to Programming the Internet of Things (IOT) Specialization
- Eduonix: Internet Of Things (IOT) Bundle
Publish - Subscribe IoT Architecture consist of one Broker (Server) and multiple clients (Nodes).
The client can either be a publisher or subscriber or both.
In this demonstration, https://www.pubnub.com is acting as Broker and python scripts on Raspberry Pi will be acting as a client (publisher and Subscriber)
Getting familiar with PubNub Server:
1. Open a free account on https://www.pubnub.com/
a. Click on "Create New App" > Enter <Name for the App> > Click on "Create"
b. Click on the Newly created app
c. You can see that, Public Key and Subscribe Key
2. Open Console for Debugging purpose
a. Go to https://www.pubnub.com/docs/console
b. Copy and paste Public Key and Subscribe Key from step 1.c. to console window
c. Change the Name of the Channel to "Raspberry"
d. Click on the "SUBSCRIBE" > Conncection status will change to "CONNECTED"
e. Scroll down to (white) "message" window and write following in json format:
Click on "Send" button
you will see the same message in black colored message console.
Click on "Send" button
you will see the same message in black colored message console.
That means, it is working fine
Install pubnub libraries on Raspberry Pi 3:
3. Install punnub python library on your RPi3:
a. Open terminal on RPi3
b. type following command:
Test pubnub connection using python scrips:
4. Place python script "ps_01.py" and "ps_02.py" on RPi3
Change Following 3 things in both the scripts:
Change Following 3 things in both the scripts:
a. pubnub publish_key (from step 1.c)
b. pubnub subscibe_key (from step 1.c)
c. pubnub channel_name (from step 2.c)
5. Run these Scripts on RPi3 using "Python2" IDE or in Terminal
6. In this script, we are publishing and subscribed to the same borker.channel
NOTE:
NOTE:
a. ps_01.py and ps_02.py are exactly same python scripts with just different messages which will be sent to the broker
b. channel_name is same as topic_name
In python script ps_01.py and ps_02.py,
· we are connecting/subscribing to the Borker.channel
· then publishing the data once to the server/Broker.channel
· And then reading the received msgs in infinite loop.
Debugging using pubnub console:
7. We can debug this program using pubnub console (https://www.pubnub.com/docs/console),
a. Run this script on RPi3 (as shown in step 5)
b. Set publish_key, subscribe_key and channel_name in pubnub console as same as it is in the script
c. Publish the data from pubnub console (as shown in step 2.e)
You can observer same messages (as published in step 7.c.) received on
RPi3 Terminal (where script is running)
For Better understanding you can watch demonstration video given below:
Downloads:
Download link is given in the Description of the YouTube video shown below.
Demonstration:
ps_01.py:
ps_02.py:
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.
Thanks and Regards,
-Akshay P. Daga