Skip to content

The system allows to monitor the pressure of a car's tyre in order to alert the driver in case a tyre puncture occurs, or just to report low tyre pressure.

Notifications You must be signed in to change notification settings

micheledellipaoli/TyrePressureDetection

Repository files navigation

SCIoT Project

Project name: Tyre Pressure Detection
Student name: Michele Delli Paoli
Student ID: 0522500797

Description

1) Application Goal

The system allows to monitor the pressure of a car's tyre in order to alert the driver:

  • in case a tyre puncture occurs;
  • just to report low tyre pressure.

2) Scenario

The system elaborates data recorded by a simulated sensor and sends to the console a specific message, which will be:

  1. "Low tyre pressure registered!": if the recorded pressure is lower than the standard one;
  2. "Tyre pressure restored!": if the sensor records a pressure greater or equals than the standard one, while the previously recorded pressure was lower than the standard one.
  3. "Optimal tyre pressure!": if the sensor records a pressure greater or equals than the standard one, and the previously recorded pressure was already greater or equals than the standard one.
  4. "Warning, possible tyre puncture!": if the sensor records a continuous drop in pressure during a small period of time.
    More specifically, to simulate a tyre puncture, we will suppose to record 5 tyre pressure decreasing values in a period of time of 1 minute.

3) Architecture

The system is composed by:

  • several functions in Node.js;
  • two loggers;
  • a MySQL Database.

Functions
  • Simulate Low Tyre Pressure: it simulates a drop in tyre pressure by sending a message to the topic "iot/tyre/pressure" with a pressure value lower than the standard one.

  • Simulate Tyre Puncture: it simulates a puncture by sending 5 decreasing pressure messages to the topic "iot/tyre/pressure".
    Note: each of the last 4 pressure values is lower than the recorded previous one, and always lower than the standard value one.

  • Restore Tyre Pressure: it simulates the act of inflating the tyre by sending a message to the topic "iot/tyre/pressure" with a pressure value greater or equals than the standard one.

  • Consume Tyre Pressure: it is triggered by an incoming message on the topic "iot/tyre/pressure" and insert into a Relational Database a record composed by the pressure value and its relative timestamp.

  • Interpreter: it retrieves the last 5 records from the Database and interprets the data.
    More specifically:

    • if timestamps fit into a period of time of 1 minute and the pressure values are sorted in decreasing order by the timestamps, then the function will send a message to the topic "iot/console" reporting: "Warning, possible tyre puncture!";

    • if timestamps don't fit into a period of time of 1 minute and the last pressure value is lower than the standard one, then the function will send a message to the topic "iot/console" reporting: "Low tyre pressure registered!";

    • if pressure values are not sorted in decreasing order by the timestamps, and the last pressure value is lower than the standard one, then the function will send a message to the topic "iot/console" reporting: "Low tyre pressure registered!";

    • if the second-last pressure value is lower than the standard one, while the last pressure value is greater or equals to the standard one, the function will send a message to topic "iot/console" reporting: "Tyre pressure restored!".

    • if the second-last pressure value is greater or equals to the standard one, and the last pressure value is greater or equals to the standard one as well, the function will send a message to topic "iot/console" reporting: "Tyre pressure optimal!".


Loggers
  • logger.js: it logs incoming messages on the topic "iot/tyre/pressure".

  • loggerConsole.js: it logs incoming messages on the topic "iot/console".


MySQL Database:
  • MySQL Instance: a single Table which is composed by three columns: id, pressure, timestamp.

Figure 1: System Architecture.



4) Setup

To run the system, you must install the following components:

  • Docker: it is a Containerization platform which allows you to package, deploy and run portable applications with ease.
    To install Docker, execute the following commands:
    sudo apt-get update
    sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) \ stable"
    sudo apt-get update
    sudo apt-get install docker-ce
    sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

  • Node.js: it is a JavaScript runtime useful to execute back-end code.
    To install Node.js, execute the following command:
    sudo apt install nodejs

  • Nuclio: it enables you to run a function when an event is triggered.
    It will be executed on a Docker Container.

  • RabbitMQ: it is a message broker useful to post messages on topics, by using either MQTT and AMQP protocol.
    It will be executed on a Docker Container.

  • MySQL: it provides a Relational Database instance in which we will store our recorded pressure values with their relative timestamp, in order to gather and process them.
    It will be executed on a Docker Container.



5) Build in Docker

To build the system on Docker Containers, you must execute the folliwng steps.

  1. Run MySQL on a Docker Container named "mysqldb" by executing the following command:

    docker run --name mysqldb -p 33060:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql:latest
  2. Enter the MySQL shell by executing the following command:

    docker exec -it mysqldb mysql -u root -p
  3. You will be asked to enter the password associated with the MySQL Root Account which you've typed in the previous "docker run" command, so type 1234 and press enter.

  4. Create a Database called "sciot_project" by executing the following command:

    mysql> CREATE DATABASE sciot_project;
  5. Create a Table called "tyre_pressure_detection" inside the "sciot_project" Database, by executing the following commands:

    mysql> USE sciot_project;
    mysql> CREATE TABLE tyre_pressure(id int(11) NOT NULL AUTO_INCREMENT, pressure varchar(45) NOT NULL, timestamp varchar(100) NOT NULL,  PRIMARY KEY (id));
    
  6. In order to avoid a MySQL error version, re-configure the password for the MySQL Root Account by executing the following commands:

    mysql> ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY '1234';
    mysql> FLUSH PRIVILEGES;
  7. Exit the MySQL shell by executing the following command:

    mysql> exit

  1. Run Nuclio into a Docker Container by executing the following command:

    docker run --name nuclio-dashboard -p 8070:8070 -d -v /var/run/docker.sock:/var/run/docker.sock -v /tmp:/tmp nuclio/dashboard:stable-amd64
  2. Open your Browser, type into the Address bar the following URL and press enter to visualize the Nuclio Dashboard:

    localhost:8070
  3. Create a Docker network named "project-network" and add the "mysqldb" Container and the "nuclio-dashboard" Container, in order to enable the communication.

    docker network create project-network
    docker network connect project-network mysqldb
    docker network connect project-network nuclio-dashboard

  1. Run RabbitMQ into a Docker Container by executing the following command:
    docker run --name rabbit-dashboard -p 9000:15672 -d -p 1883:1883 -p 5672:5672 cyrilix/rabbitmq-mqtt

  1. Check your local IP Address by executing the following command (it works only on Linux-based systems):
    hostname -I

  1. Open the Nuclio Dashboard and click on the "Create project" button.
  2. Fill the form to create a New Project, then click on the "Create" button.
  3. Click on the Project you've just created to open it.

  1. Click on the "Create Function" button to create a new function.

  2. Click on the "Import" option above, and then on the "Import" button below.

  3. Select the mqtt_consume_tyre_pressure YAML file, which is located in the project directory, to import the "Consume Tyre Pressure" function.

  4. Click on the "Create Function" button.

  5. Edit the "Consume Tyre Pressure" function by replacing the [localIpAddress] parameter at line 6 with the String value of your local IP Address you've checked in the previous step:

    6   var DATABASE_HOST = "[localIpAddress]";
  6. Edit the "Consume Tyre Pressure" function by replacing the [localIpAddress] parameter at line 18 with your local IP Address:

    18       amqp.connect('amqp://guest:guest@[localIpAddress]:5672').then(function(conn) {
  7. Go to the "Triggers" section above, click on the '>' symbol to open the "myMqttTrigger" trigger form, then edit the "URL" element by replacing the [localIpAddress] parameter with your local IP Address:

    URL
    guest:guest@[localIpAddress]:1883
  8. Click the "Deploy" button to deploy the function.


  1. Click on the "Create Function" button to create a new function.

  2. Click on the "Import" option above, and then on the "Import" button below.

  3. Select the mqtt_interpreter YAML file to import the "Interpreter" function.

  4. Click on the "Create Function" button.

  5. Edit the "Interpreter" function by replacing the [localIpAddress] parameter at line 6 with the String value of your local IP Address:

    6   var DATABASE_HOST = "[localIpAddress]";
  6. Edit the "Interpreter" function by replacing the [localIpAddress] parameter with your local IP Address in the 128-th line of code:

    128       amqp.connect('amqp://guest:guest@[localIpAddress]:5672').then(function(conn) {
  7. Go to the "Triggers" section above, click on the '>' symbol to open the "myMqttTrigger" trigger form, then edit the "URL" element by replacing the [localIpAddress] parameter with your local IP Address:

    URL
    guest:guest@[localIpAddress]:1883
  8. Click the "Deploy" button to deploy the function.


  1. Click on the "Create Function" button to create a new function.

  2. Click on the "Import" option above, and then on the "Import" button below.

  3. Select the mqtt_simulate_low_tyre_pressure YAML file to import the "Simulate Low Tyre Pressure" function.

  4. Click on the "Create Function" button.

  5. Edit the "Simulate Low Tyre Pressure" function by replacing the [localIpAddress] parameter at line 2 with your local IP Address:

    2   var mqtt_url = url.parse(process.env.CLOUDAMQP_MQTT_URL || 'mqtt://guest:guest@[localIpAddress]:1883');
  6. Click the "Deploy" button to deploy the function.


  1. Click on the "Create Function" button to create a new function.

  2. Click on the "Import" option above, and then on the "Import" button below.

  3. Select the mqtt_simulate_tyre_puncture YAML file to import the "Simulate Tyre Puncture" function.

  4. Click on the "Create Function" button.

  5. Edit the "Simulate Tyre Puncture" function by replacing the [localIpAddress] parameter at line 2 with your local IP Address:

    2   var mqtt_url = url.parse(process.env.CLOUDAMQP_MQTT_URL || 'mqtt://guest:guest@[localIpAddress]:1883');
  6. Click the "Deploy" button to deploy the function.


  1. Click on the "Create Function" button to create a new function.

  2. Click on the "Import" option above, and then on the "Import" button below.

  3. Select the mqtt_restore_tyre_pressure YAML file to import the "Restore Tyre Pressure" function.

  4. Click on the "Create Function" button.

  5. Edit the "Restore Tyre Pressure" function by replacing the [localIpAddress] parameter at line 2 with your local IP Address:

    2   var mqtt_url = url.parse(process.env.CLOUDAMQP_MQTT_URL || 'mqtt://guest:guest@[localIpAddress]:1883');
  6. Click the "Deploy" button to deploy the function.



6) Run the system

Run the system by following the next steps.

  1. Go to the project folder, open the terminal and then run the logger.js by executing the following command:

    node logger.js
  2. Go to the project folder, open another terminal and then run the loggerConsole.js by executing the following command:

    node loggerConsole.js

  1. Go to the "Simulate Low Tyre Pressure" function into the Nuclio Dashboard, then click on the "Test" button on the right.

  2. The "Simulate Low Tyre Pressure" function will generate a random pressure value which is lower than the standard one (32 psi), and it will post a message on the "iot/tyre/pressure" topic.
    The "Consume Tyre Pressure" function will be triggered by the incoming message on the "iot/tyre/pressure" topic and will execute a query in order to insert the pressure value and its relative timestamp into the Database.
    The "Interpreter" function will be triggered too by the incoming message on the "iot/tyre/pressure" topic, it will execute a query in order to get the last 5 records from the Database and it will interpret the data by posting a message on the "iot/console" topic.

  3. Go to the terminal in which you've executed the logger.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node logger.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 29-Mon Dec 05 2022 18:12:56 GMT+0000 (UTC)'
    
    
  4. Go to the terminal in which you've executed the loggerConsole.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node loggerConsole.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Low Tyre Pressure registered!'
    
    

  1. Go to the "Simulate Tyre Puncture" function into the Nuclio Dashboard, then click on the "Test" button on the right.

  2. The "Simulate Tyre Puncture" function will generate 5 random pressure values in decreasing order which are all lower than the standard one (32 psi), and it will post 5 consecutive messages on the "iot/tyre/pressure" topic.
    The "Consume Tyre Pressure" function will be triggered by each of the incoming messages on the "iot/tyre/pressure" topic and will execute a query for each message in order to insert each pressure value and its corresponding timestamp into the Database.
    The "Interpreter" function will be triggered too by each incoming message on the "iot/tyre/pressure" topic, and for each incoming messagge it will execute a query in order to get the last 5 records from the Database and it will interpret the data by posting a message on the "iot/console" topic.

  3. Go to the terminal in which you've executed the logger.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node logger.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 30-Mon Dec 05 2022 18:59:21 GMT+0000 (UTC)'
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 26-Mon Dec 05 2022 18:59:24 GMT+0000 (UTC)'
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 23-Mon Dec 05 2022 18:56:30 GMT+0000 (UTC)'
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 21-Mon Dec 05 2022 18:59:33 GMT+0000 (UTC)'
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 18-Mon Dec 05 2022 18:59:35 GMT+0000 (UTC)'
    
    
  4. Go to the terminal in which you've executed the loggerConsole.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node loggerConsole.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Warning, possible tyre puncture!'
    [x] Received 'Warning, possible tyre puncture!'
    [x] Received 'Warning, possible tyre puncture!'
    [x] Received 'Warning, possible tyre puncture!'
    [x] Received 'Warning, possible tyre puncture!'
    
    

  1. Go to the "Restore Tyre Pressure" function into the Nuclio Dashboard, then click on the "Test" button on the right.

  2. The "Restore Tyre Pressure" function will generate a random pressure value which is greater or equals than the standard one (32 psi), and it will post a message on the "iot/tyre/pressure" topic.
    The "Consume Tyre Pressure" function will be triggered by the incoming message on the "iot/tyre/pressure" topic and will execute a query in order to insert the pressure value and its corresponding timestamp into the Database.
    The "Interpreter" function will be triggered too by the incoming message on the "iot/tyre/pressure" topic, and it will execute a query in order to get the last 5 records from the Database and it will interpret the data by posting a message on the "iot/console" topic.

  3. Go to the terminal in which you've executed the logger.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node logger.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Function 'mqtt_consume_tyre_pressure' has been triggered by the following message received on topic 'iot/tyre/pressure': 33-Mon Dec 05 2022 19:09:15 GMT+0000 (UTC)'
    
    
  4. Go to the terminal in which you've executed the loggerConsole.js, and you will see something like this:

    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node loggerConsole.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Tyre Pressure restored!'
    
    

  1. Now, if you go back the "Restore Tyre Pressure" function into the Nuclio Dashboard, and then click on the "Test" button on the right, you will have something like this in the terminal in which you've executed the loggerConsole.js:
    C:\Users\Michele Delli Paoli\Desktop\TyrePressureDetection>node loggerConsole.js
    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Tyre Pressure restored!'
    [x] Received 'Tyre Pressure optimal!'
    
    

About

The system allows to monitor the pressure of a car's tyre in order to alert the driver in case a tyre puncture occurs, or just to report low tyre pressure.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages