Skip to content
Always-code edited this page Jul 2, 2021 · 7 revisions

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:
  1. Import psycopg2 python library. This library is the PostgreSQL database adaptor for python programming language.
  2. 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.
  3. 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:
  1. 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/
  2. Define MQTT broker.
  3. Define MQTT client. This is the client on the publisher side.
  4. Connect MQTT client to MQTT broker.
  5. Read temperature, pressure, humidity, roll, pitch and yaw values from Sense-Hat module as a python object.
  6. 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:
  1. 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/
  2. Define MQTT broker.
  3. Define MQTT client. This is the client on the subscriber side.
  4. Connect MQTT client to MQTT broker.
  5. Implement 3 callback functions (on_connect, on_log, on_message). For more information on these functions, refer section 'Functions and Classes' in this document.
  6. Implement client.loop_start method. (This method starts the loop by calling the built-in loop() function which reads the receive and send buffers.)
  7. Subscribe to MQTT topic.
  8. 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:
  1. Import flask, flask_restful, psycopg2, pandas, paho.mqtt.client python libraries.
  2. Connect to PostgreSQL database using psycopg2.connect function. For more information on this function, visit https://www.psycopg.org/docs/connection.html
  3. Define Flask application.
  4. 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.
  5. Add resources to API.

NOTE: AllResourceEndpoints, MachineData, MachineDataLast are class names. You can select your own class names.

Clone this wiki locally