Page 2 - Always-code/Analytical-dashboard GitHub Wiki
Step 1 - Server side implementation
This section explains 4 python codes required to complete step 1.
sensorDataHandler.py
- This python code is written in Visual Studio Code IDE.
- This code creates a new data table in PostgreSQL database.
- In this code, following steps are implemented:
- Import psycopg2 python library. This library is the PostgreSQL database adaptor for python programming language.
- Connect to PostgreSQL database with sslmode = 'require'. Visit https://www.postgresql.org/docs/current/ssl-tcp.html for more information on Secured Sockets Layer (SSL) modes in PostgreSQL.
- Create a data table and specify its column names and data types based on your requirement.
NOTE: For more information on classes psycopg2.connect and psycopg2.cursor, visit https://www.psycopg.org/docs/index.html
publisher.py
- This python code is written in Geany, which is accessed using VNC viewer.
- This code reads temperature, pressure, humidity, roll, pitch and yaw values from Sense-Hat module.
- In this code, following steps are implemented:
- Import paho.mqtt.client, random, time, datetime, json and sense_hat python libraries. For more information on paho, visit https://www.eclipse.org/paho/. For more information on Sense-Hat, visit https://pythonhosted.org/sense-hat/
- Define MQTT broker.
- Define MQTT client. This is the client on the publisher side.
- Connect MQTT client to MQTT broker.
- Read temperature, pressure, humidity, roll, pitch and yaw values from Sense-Hat module as a python object.
- Covert the python object into json string and publish data to MQTT broker.
NOTE: In this demo, Quality of Service (qos) level in MQTT is equal to 0. You can select this value as 0, 1 or 2 based on your requirement. For more information on qos levels, visit https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels/
subscriber.py
- This python code is written in Visual Studio Code IDE.
- This code takes temperature, pressure, humidity, roll, pitch and yaw values from MQTT broker and sends the values to data table in PostgreSQL database.
- In this code, following steps are implemented:
- Import paho.mqtt.client, random, time, datetime, json and sense_hat python libraries. For more information on paho, visit https://www.eclipse.org/paho/. For more information on Sense-Hat, visit https://pythonhosted.org/sense-hat/
- Define MQTT broker.
- Define MQTT client. This is the client on the subscriber side.
- Connect MQTT client to MQTT broker.
- Implement 3 callback functions (on_connect, on_log, on_message). For more information on these functions, refer section 'Functions and Classes' in this document.
- Implement client.loop_start method. (This method starts the loop by calling the built-in loop() function which reads the receive and send buffers.)
- Subscribe to MQTT topic.
- Implement client.loop_stop method. (This method stops the loop.)
NOTE: For more information on MQTT callbacks, visit https://www.eclipse.org/paho/index.php?page=clients/python/docs/index.php
apiHandler.py
- This python code is written in Visual Studio Code IDE.
- This code defines 3 API resources.
- In this code, following steps are implemented:
- Import flask, flask_restful, psycopg2, pandas, paho.mqtt.client python libraries.
- Connect to PostgreSQL database using psycopg2.connect function. For more information on this function, visit https://www.psycopg.org/docs/connection.html
- Define Flask application.
- Define 3 classes (AllResourceEndpoints, MachineData, MachineDataLast) inherited from parent class 'Resource'. For more information on these classes, refer section 'Functions and Classes' in this document.
- Add resources to API.
NOTE: AllResourceEndpoints, MachineData, MachineDataLast are class names. You can select your own class names.