In this tutorial, you are going to create a new fire alarm device type, enroll a fire alarm using the device management console, and start using the device. A sample Python agent is used to make the use case interesting.
Before you begin
Install Python.
Install pip.
Enter
pip -V
in your terminal to check the current version of pip in your machine.If it is already installed you will get a message similar to what is shown below:
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
Else you will get the following message:
The program 'pip' is currently not installed. You can install it by typing:
sudo apt install python-pip
If pip is not installed use the following command to install it.
sudo apt-get install python-pip
By default, pip is installed when you used Homebrew to install python on your machine.
Install Paho-MQTT. Paho-MQTT It is the MQTT client library for Python.
You need to have pip installed before installing Paho-MQTT.sudo pip install paho-mqtt
The above command works for Python 2.X. If you have installed Python 3.X, you need to replacepip
withpip3
.
To know the version of Python you have installed usepython -V
.sudo pip3 install paho-mqtt
If you are running on windows, remove the
sudo
prefix from the above command. Instead, you need to run the command in the command prompt as an administratorYou need to start the WSO2 IoT Serve's broker, core, and analytics profiles.
cd <IOTS_HOME>/bin ------Linux/Mac OS/Solaris ---------- ./broker.sh ./iot-server.sh ./analytics.sh -----Windows----------- broker.bat iot-server.bat analytics.bat
Let's get started!
Creating the fire alarm device type
Sign in to the WSO2 Device Management Console.
If you are an administrator, sign into the device management console using
admin
as the username andadmin
as the password. If you want to register with WSO2 IoT Server, see Registering with WSO2 IoT Server.Click
> DEVICE TYPE MANAGEMENT > CREATE DEVICE TYPE.WSO2 IoT Server has the Android, Windows, Raspberry Pi, Arduino, Android Sense and iOS sample device types by default. If you have not run the
<IOTS_HOME>/samples/device-plugins-deployer.xml
file, you will only have the Android, Android Sense, and Windows device type by default.Enter the following details to add a new device type and click Add Device Type.
Make sure to enter the values given below. Else, you are not able to complete the tutorial successfully.
Name firealarm Description This is a device used for testing. Push Notification Transport MQTT Feature name msg Feature code msg Feature description Message from the server to the device. For more information about each field, see Creating a New Device Type via the Device Management Console.
Create the event stream to gather the sensor readings or data from the device, and click Add.
Enter the following values:Make sure to enter the values given below. Else, you are not able to complete the tutorial successfully.
Transport MQTT Event name temperature Event data type INT For more information about each field, see Creating a New Device Type via the Device Management Console.
Opt to navigate to the enrollment page when prompted.
Enrolling a fire alarm
Follow the steps given below to enroll a fire alarm for the device type you just created:
- On the Enroll Device page, click on the fire alarm device type.
- Click + Create Device.
Enter the details to create the device and click Create Device.
Make sure to enter the values given below. Else, you are not able to complete the tutorial successfully.
Name Alarm1 Device Identifier 123456 Description Enrolling a device for testing. For more information about each field, see Creating a New Device Type via the Device Management Console.
The
123456.json
file downloads automatically.Download the SampleApp.zip file to a preferred location. This directory is referred to as
<SAMPLE_APP>
throughout this document. Next, unzip the SampleApp.zip.unzip <SAMPLE_APP>/SampleApp.zip
Additionally, you can find the sample app code on Git Hub too.
Rename the downloaded
123456.json
file toconfig.json
and copy it to the<SAMPLE_APP>/Configuration
directory.Navigate to the
<SAMPLE_APP>
directory via the command prompt and run the python script.cd <SAMPLE_APP> python SampleClient.py
The above command works for Python 2.X. If you have installed Python 3.X, you need to replace
python
withpython3
.
To know the version of Python you have installed usepython -V
.python3 SampleClient.py
The sample starts.
If the sample successfully connects to WSO2 IoT Server, you will see the
Connected with result code 0
message in the terminal and the app starts pushing data to WSO2 IoT Server.
Viewing the data gathered from the fire alarm
Click
> DEVICE MANAGEMENT, and click on the fire alarm device you just enrolled to view the real-time and historical data that was pushed from the fire alarm to WSO2 IoT Server.Real-time data
You see the real-time data pushed to WSO2 IoT Server as shown below:
The current temperature is 46.
Historical data
Click View Device Analytics to view the historical temperature readings of the fire alarm.
Sending a Message to the fire alarm
Follow the steps given below to send a message to the fire alarm. You configured the fire alarm device type to have the messaging feature when creating the new fire alarm device type.
- Click the msg operation.
- Enter the message you want to send, and click Send to device.
Go to the terminal where you were running the sample. The following message is shown if the message was successfully delivered to the fire alarm.
- The access token generated to access the fire alarm expires in an hour. Therefore, you are not able to use the
SimpleClient.py
script after an hour. - To overcome this, you can use the
SampleClient2.py
script. Similarly, if you want to make changes to the current script and try many things with WSO2 IoT Server.