Efento Gateway integration

This tutorial will show you how to setup a simple http server with a database and configure Efento gateway to send the data to it. In this tutorial we are using Python, Flask and PostgreSQL database, but the same idea can be easily implemented in other programming languages / with different databases. Should you have any issues or questions, feel free to drop us a line at help.efento.io

Before you start

Before you start you will need to install and configure the following components: 

PostgreSQL database 

Setting up the database

After downloading and installing PostgreSQL you will need to create the first database. This in one of the steps during the PostgreSQL installation. By default, the database will be created with the following credentials:

DATABASE_HOST = ‘localhost’;
DATABASE_USER = ‘postgres’;
DATABASE_PASSWORD = ‘Your password’;
DATABASE_NAME = ‘postgres’;

If you want to, you can change the names / credentials. Write them down, as they will be needed in the next steps. If you want to check database credentials, open pgAdmin in the PostgreSQL folder. Next open Object -> Properties -> General

Creating a table

To save the measurements coming from Efento Gateway in your database, you need to create a table. In this example, we are creating a very simple table to store all the data from the sensors, no matter what the sensor type. The table will have 5 columns, all of them of “text” type. Please note that this architecture of the database is only for the demonstration purposes. Database structure should be selected according to your project requirements.

You can create the table manually, using pgAdmin’s interface or using a SQL query. In pgAdmin select your database, open Tools menu: Tools -> Query Tools. Copy the request below into the Query Editor and click Execute (▶) :

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

CREATE TABLE will create a new, initially empty table in the current database.
The table will be owned by the user issuing the command.

Python Server

Before you start

In order to make the server work, you will need:

  • Flask – Flask is a micro framework used for development of web applications. If you want to learn more about Flask check out this website. You can install and import Flask in pyCharm IDE or using pip ($ pip install -U Flask)
  • psycopg2 – one of the most popular PostgreSQL database adapter for Python. If You want to know more check out this website.

How it works?

Script we are going to write sets up http server. The server is constantly listening for data sent by Efento Gateway (gateway sends the data as JSON over REST. One message can contain multiple measurements from one sensor or measurements from multiple sensors. Once a new messages comes, server parses the data, saves it in the data base and returns “201” status code to the gateway. This means that the message has been successfully parsed and save in the database.  If anything goes wrong (e.g. database is down), server will respond with “500” status code to gateway. In that case, gateway will retry to send the same data after a while.

Python code

You can copy the code below and save as a Python script (.py). Edit the database credential and run the code to start the server and listen for the data coming from Efento Gateway.

import psycopg2
from flask import Flask, request, Response, json, g
app = Flask(__name__)
# Enter your database host, database user, database password and database name
# Making the initial connection:
conn = psycopg2.connect(
# Set up "/api/v2/measurements" endpoint, which will be receiving the data sent by Efento Gateway using POST method.
@app.route('/api/v2/measurements', methods=['POST'])
# To access incoming request data, you can use the global request object.
# Flask parses incoming request data for you and gives you access to it through that global object.
def respond():
    data = request.json
    record = []
    response_handle = []
    # iteration in list data/measurement
    for measurement in data['measurements']:
        # iteration in list data/measurement/params, creating a list of sensor parameters(measured_at,serial,battery) and measurement results
        for param in measurement['params']:
            record.extend([(measurement['measured_at'], measurement['serial'], measurement['battery'], param['type'],
    response = json.dumps(({'Y': response_handle, 'N': []}))
    measurements = "INSERT INTO measurements(measured_at, serial_number, low_battery, type, value) VALUES (%s, %s, %s, %s, %s)"
    with conn.cursor() as cur:
            # inserting a list of sensor parameters and measurement to table in PostgresSQL
            cur.executemany(measurements, record)
        except (Exception, psycopg2.DatabaseError) as error:
            return Response(status="500")
    return Response(response, status='201')
# Start the application on Your port.Default port 5000
if __name__ == "__main__":
    app.run(host='', port=5000)

Efento gateway configuration

Log in to Efento Gateway’s web panel through your web browser. Navigate to Settings -> Server settings.

In the Connection To Server field select Custom Settings. Fill in the server address (either domain or IP of the computer / server which runs the Python script) in the Server Address field. Configure the Server port to 5000 and switch TLS off. Note! If TLS is switched off, the data sent by the gateway is not encrypted. For production deployments, you should upload your server’s certificate in the CA certificate tab and use encrypted communication (https).


When you run the script, all the data coming form the gateway will be saved in the database. To view the measurements open pgAdmin 4,  select your database, then open Tools > Query Tools.

Enter the request below into the Query Editor and select Execute (▶) : 

SELECT  * FROM measurements;

Data coming from Efento Gateway is saved in the database