We have generated data and stored it in SQL. Now we need to do an analysis and generate a report based on this data. We shall describe how to do this.

We have done the following in different chapters of this website.

https://www.hemelix.com/plc/structured-text-programming/ (Programming in Structured Text)

https://www.hemelix.com/scada-hmi/twincat-hmi/animation-in-twincat-hmi/ (Animation of image)

https://www.hemelix.com/scada-hmi/twincat-hmi/data-grid-view/ (How to display data from HMI to SQL and vice versa) 

https://www.hemelix.com/automation/raspberry-pi/ (How to program in Raspberry Pi)

https://www.hemelix.com/sql/  (How to work with HMI and database)

https://www.hemelix.com/plc/structured-text-mqtt/ (What is MQTT and how to use it)

https://www.hemelix.com/automation/structured-text-mqtt-node-red-mssql/ (What is Node-RED and saving data to SQL)

On this page, we shall use all this knowledge to generate a report.

Still updating, follow us!

Figure 01:  Fitting reporting architecture in the Beckhoff automation system. 

Figure 02:  Sample HMI allows saving a few properties to DB and sending a report automatically (under development

We shall use a DC motor and it will be run by PLC by using Modbus. The motor can be started or stopped manually. The motor can reach maximum speed for a certain time and it can change the speed during operation. It will record the maximum speed and how long it was running at that speed. All information can be included in a report which can be sent to our client by email.

Figure 03: Saving data to cloud, architecture provided by Beckhoff

Supabase Edge Functions (Connecting remote PG)

Supabase Edge Functions to connect to the remote database.

Supabase → Edge Function → Remote Database

import { Client } from "pg";
const client = new Client({
  host: "remote-host",
  user: "user",
  password: "password",
  database: "db",
  port: 5432
});
await client.connect();

Here is a simple Node.js example that connects to a remote PostgreSQL database and retrieves data. The standard library used is pg (node-postgres).

 

1. Install the PostgreSQL client

Run:

npm install pg

2. Example Node.js script

import pkg from "pg";
const { Client } = pkg;
async function fetchData() {
  const client = new Client({
    host: "REMOTE_DB_HOST",      // e.g. 192.168.1.10 or db.example.com
    port: 5432,
    user: "REMOTE_DB_USER",
    password: "REMOTE_DB_PASSWORD",
    database: "REMOTE_DB_NAME",
    ssl: false // change to true if your database requires SSL
  });
  try {
    // connect to database
    await client.connect();
    console.log("Connected to remote PostgreSQL");
    // run a query
    const result = await client.query(
      "SELECT id, name, email FROM users LIMIT 5"
    );
    // print rows
    console.log("Query result:");
    result.rows.forEach(row => {
      console.log(row);
    });
  } catch (error) {
    console.error("Error:", error);
  } finally {
    // close connection
    await client.end();
    console.log("Connection closed");
  }
}
fetchData();

3. Example output

Connected to remote PostgreSQL

Query result:

{ id: 1, name: 'Alice', email: 'alice@email.com' }
{ id: 2, name: 'Bob', email: 'bob@email.com' }
Connection closed

 

 

Mendix

=> Reporting tool provided by Siemens

=> Put login ON/OFF   Security | Security level

References:

What Next

Now learn how to send email by using C# and JS, 

Browse  https://www.hemelix.com/automation/send-email-by-js-and-csharp/

Ask questions related to Hemelix sample code and design at Google group https://groups.google.com/g/hemelix