This article illustrates how to integrate devices with the KAA IoT dashboard. We shall see an example of monitoring Temperature and Humidity on the KAA IOT dashboard over paho MQTT.
Pre-requiste: Basics of raspberry pi, Knowledge about MQTT
Introduction to KAA IOT
Kaa is a highly flexible, multi-purpose, and open-source middleware platform for implementing complete end-to-end IoT solutions. Moreover, Kaa offers a set of out-of-the-box enterprise-grade IoT features that can be easily plugged in and used to implement a large majority of the IoT use cases.
Features of KAA
- Device Management
- Data collection
- Configuration Management
- Messaging and notifications alert
- Open IOT protocol
- Data Visualization
Example of remote Monitoring of temperature and Humidity using Raspberry pi and KAA IoT dashboard
- Firstly setup the circuit that interfaces DHT11 with raspberry pi.
Pin configuration
DHT11 pins | GPIO pins of Raspberry pi |
VCC | 5v (GPIO 2) |
GND | Gnd (GPIO 6) |
S | GPIO 7 |
- Secondly, let’s Install DHT11 package and enter the below command in terminal
3. Additionally now, Let’s install Paho Mqtt package by entering the below command
However, if there are any errors found related to the paho MQTT package then you can refer Paho Python Client. Then, enter the below commands in the terminal.
4. Finally, It’s necessary to signup for a free account
5. After that, Go to Cloud and select Root account
6. After that, we shall learn how to get endpoint token. Select Device management and choose devices. In Endpoint token status click on the option create.
7. Now, select device management and add a device. Finally, enter endpoint token obtained from previous step
- Moreover, it’s necessary to copy the endpoint token and application version, paste it into the source code.
9. Edit the application configuration for the Endpoint Time Series service (EPTS). EPTS is a Kaa platform component that is responsible for transforming raw data samples into well-structured time series.
10. Run the source code and in addition to it, check if the endpoint token is active or not. Moreover, the active state of the endpoint token can be checked in the dashboard as well.
After connecting to the server
11. Furthermore, now let’s configure the dashboard
Go to solutions and choose your application.Click on dashboard and choose your widgets
12. Thus, after selecting widgets, it’s important to configure it.
Then, select edit on the widgets and enter endpoint token, application name, time series and save.
13. Finally, you can see the data in the KAA IoT dashboard.
14. Similarly, the data can be observed in device telemetry as well.
Source Code
import sys
import json
import random
import signal
import string
import time
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
pin = 7
import paho.mqtt.client as mqtt
KPC_HOST = "mqtt.cloud.kaaiot.com" # Kaa Cloud plain MQTT host
KPC_PORT = 1883 # Kaa Cloud plain MQTT port
ENDPOINT_TOKEN = "" # Paste endpoint token
APPLICATION_VERSION = "" # Paste application version
class KaaClient:
def __init__(self, client):
self.client = client
self.metadata_update_topic = f'kp1/{APPLICATION_VERSION}/epmx/{ENDPOINT_TOKEN}/update/keys'
self.data_collection_topic = f'kp1/{APPLICATION_VERSION}/dcx/{ENDPOINT_TOKEN}/json'
def connect_to_server(self):
print(f'Connecting to Kaa server at {KPC_HOST}:{KPC_PORT} using application version {APPLICATION_VERSION} and endpoint token {ENDPOINT_TOKEN}')
self.client.connect(KPC_HOST, KPC_PORT, 60)
print('Successfully connected')
def disconnect_from_server(self):
print(f'Disconnecting from Kaa server at {KPC_HOST}:{KPC_PORT}...')
self.client.loop_stop()
self.client.disconnect()
print('Successfully disconnected')
def compose_metadata(self):
return json.dumps([
{
"model": "Dht11",
"fwVersion": "v0.0.1",
"customer": "IOTEDU",
"latitude": 13.019318,
"longitude":77.595718,
}
])
def compose_data_sample(self):
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
return json.dumps([
{
"timestamp": int(round(time.time() * 1000)),
"temperature": round(temperature,2),
"humidity": int(humidity)
}
])
def on_message(client, userdata, message):
print(f'<-- Received message on topic "{message.topic}":\n{str(message.payload.decode("utf-8"))}')
def main():
# Initiate server connection
client = mqtt.Client(client_id=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)))
kaa_client = KaaClient(client)
kaa_client.connect_to_server()
client.on_message = on_message
# Start the loop
client.loop_start()
# Send data samples in loop
listener = SignalListener()
payload = kaa_client.compose_metadata()
result = kaa_client.client.publish(topic=kaa_client.metadata_update_topic, payload=payload)
print(f'--> Sent message on topic "{kaa_client.metadata_update_topic}":\n{payload}')
while listener.keepRunning:
payload = kaa_client.compose_data_sample()
result = kaa_client.client.publish(topic=kaa_client.data_collection_topic, payload=payload)
if result.rc != 0:
print('Server connection lost, attempting to reconnect')
kaa_client.connect_to_server()
else:
print(f'--> Sent message on topic "{kaa_client.data_collection_topic}":\n{payload}')
time.sleep(3)
kaa_client.disconnect_from_server()
class SignalListener:
keepRunning = True
def __init__(self):
signal.signal(signal.SIGINT, self.stop)
signal.signal(signal.SIGTERM, self.stop)
def stop(self, signum, frame):
print('Shutting down...')
self.keepRunning = False
if __name__ == '__main__':
main()
Summary of the article
Finally, the output is observed in the dashboard. Similarly, we noted that temperature and Humidity data from Python shell and Dashboard widgets are same. Moreover, the data collected, can be downloaded as a CSV file and can be used for further analytics use.