Example server application in Python

This tutorial shows how to set up a simple HTTP server with a database and configure an Efento Gateway to send measurement data to it. In the example, we use Python, Flask, and a PostgreSQL database, but the same concept can easily be implemented in other programming languages or with different databases.

If you have any questions or run into issues, feel free to contact us at help.efento.io.

Before you start

Before beginning, ensure that you have installed and configured the following components:

  • PyCharm or any Python 3 IDE

  • PostgreSQL database

  • Efento Gateway and Efento Bluetooth Low Energy loggers

PostgreSQL Database Setup

1

Create the database

After installing PostgreSQL, you will be prompted to create your first database during setup. By default, it will be created using the following credentials:

DATABASE_HOST = 'localhost'
DATABASE_USER = 'postgres'
DATABASE_PASSWORD = 'your_password'
DATABASE_NAME = 'postgres'

You may change these values if needed. Be sure to write down your credentials, as they will be required later.

To check or modify database credentials, open pgAdmin, then navigate to: Object → Properties → General

2

Create the measurements table

To store measurements sent from the Efento Gateway, you must create a table. In this example, we create a very simple structure containing five columns, all of type text. This design is only for demonstration purposes - your production database should be structured according to your project needs.

You may create the table using pgAdmin’s interface or by running a SQL query.

In pgAdmin, select your database and navigate to: Tools → Query Tool

Insert the query below and click Execute (▶):

CREATE TABLE measurements (
    measured_at text,
    serial_number text,
    low_battery text,
    type text,
    value text
);

Running CREATE TABLE creates a new, initially empty table owned by the user who executed the command.

Python Server

The script you will create sets up a simple HTTP server. Efento Gateway sends data as JSON over REST, and a single message may contain multiple measurements.

When the server receives a request:

  1. It parses the JSON payload.

  2. It saves all measurements to the PostgreSQL database.

  3. It returns HTTP 201 to confirm that the data was successfully processed.

  4. If something goes wrong (e.g., database connection failure), the server returns HTTP 503, and the gateway will automatically retry sending the data later.

1

Install the required Python components

To run the server, you will need:

  • Flask – a micro-framework for building web applications Install it via PyCharm or using:

  • psycopg2 – a popular PostgreSQL adapter for Python Install using:

2

Run the script

Copy the code below into a .py file. Update the database credentials, run the script, and the server will begin listening for data from the Efento Gateway.

3

Configure Efento Gateway

Ethernet Gateway

  1. Log in to the Efento Gateway web panel. Navigate to: Settings → Server settings

  2. In Connection to Server, select Custom Settings.

  3. Enter the server address (your PC/server IP or domain) in the Server Address field.

  4. Set the Server Port to 5000.

  5. Switch TLS OFF for this tutorial.

Results

Once the Python script is running, all data sent from the Efento Gateway will be saved in your PostgreSQL database.

To view measurements:

  1. Open pgAdmin 4

  2. Select your database

  3. Go to Tools → Query Tool

  4. Enter and execute:

You will now see all measurements received from the Efento Gateway stored in your database.

Last updated