How to build a bidirectional app for Internet of Things/Chat with Python
What is Internet of Things (IoT) “In simple english”?
Nowadays, a large number of objects we use in everyday activities can connect to the internet such as smartwatches, cars, smart lock doors, thermostat, sporting equipment, home appliances (refrigerators, oven, etc), and even shoes. This connectivity allows these objects to have their own identity as well as the ability to receive and communicate valuable information, making them “Smart”. This is known as “Internet of Things”.
For illustration, a smartwatch can collect data such as how many steps you take, your body temperature, heart rate, etc; which can be viewed from other internet connected devices like a smartphone or tablet. All the data collected by the smartwatch can then be analyzed to tell you how many calories you have burned, give you a personalized fitness advice and other related health information.
The Internet of Things isn’t limited to consumer products. There are many more examples extending to health care, manufacture, agriculture, and infrastructure (sensors on bridges and buildings that check for stress or damage to their structure; city trash cans equipped with sensors that send an alert when they need to be emptied).
There are privacy and security concerns that come with IoT. For example, a hacker might be able to unlock and start your car remotely. All these devices collect a large amount of data, capturing your actions and location throughout the day, we often don’t really know all the data that is collected or how they are being used. One thing we know is that The internet of things is going to grow at a faster rate over the next few years, bringing with it new benefits and challenges.
In this article, we are going to explore how to publish and subscribe IoT Architecture using PubNub server.
PubNub is a global Data Stream Network and real-time infrastructure-as-a-service company that provides a real-time data stream network for web, mobile, and IoT developers. The architecture of pubnub is very similar to MQTT protocol in which users subscribe to a topic and listen for data that are published on that topic by another user.
For the purpose of this demo, we are going to be using the pubnub python SDK to build bidirectional chat app you will need to have python 3.65 or greater and pubnub install on your computer.
Now, we’ll just go ahead and follow the setup on https://www.pubnub.com/docs/python/pubnub-python-sdk.
The simplest way to get started is to install PubNub Python SDK via pypi. Python 3.6 comes with pip3 if you did upgrade pip to pip3 make sure you run your command with python3 and pip3 instead of python and pip which will refer to python 2. version that comes in a mac by default.
I copied and modified the “hello world” python file added more comment to better explain what each section and line of code does:
Open your editor (I’m using visual code)and create a python file to which you will copy and paste the code above. I named mine “yann.py”, you can name yours as you wish.
Pubnub provides an online console on which you can be used to test your application. We’ll use the default demo app set on the pubnub console by specifying the channel name: “awesomeChannel”, publish and subscribe key value: “demo”.
Now, depending on which the editor you are using, build it or simply open terminal or command prompt; cd into your file directory and run the following command line:
I’m using Visual Studio Code editor. After successfully running your python file, send a message from the pubnub console and see the result on your terminal or command prompt and vise-versa:
I used python in this case because I created an alias command on my .bash_profile that refer python command to python3 and pip to pip3 for future use:
Now, create another python file for the second user. I named mine bob.py, copy the same code from the previous file and assign new uuid name for the second user:
# let assign each user a basic user id for the basic chat app
pnconfig.uuid = 'User_2_name'
Run both files and you have a chat app between user 1 and 2:
For more details about the code, check out my GitHub repo.
If you enjoyed this article, you might also like “Raspberry Pi 3 — Shell Scripting — Door Monitor (an IoT Device)”