Skip to content

IoT project for a smart house using mqtt and Coap sensor. The sensor is implemented with real board and tested on Cooja. Is used also a sql database and Grafana to visualize element

Notifications You must be signed in to change notification settings

DomenicoArmillotta/IOT_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IOT_project

Smart House

Library to install to run project:

$ pip3 install tabulate

$ pip3 install pypiserver

$ pip3 install databases

$ pip3 install logging

$ pip3 install nrfutil

link to fix bug on nrfutil = links

Terminal to run for Cooja emulation:

Terminal to run Cooja

contikier
cd tools/cooja
ant run

Terminal for Mosquitto:

sudo mosquitto -c /etc/mosquitto/mosquitto.conf

Terminal for Border router:

cd contiki-ng/IoTProject/sensors/rpl-border-router
make TARGET=cooja connect-router-cooja

Terminal for Python Server:

cd contiki-ng/IoTProject/collector/
python3 provaServer.py

How to flash Dongle card:

Flash Mqtt:

cd contiki-ng/IoTProject/sensors/mqtt-sensor
make TARGET=nrf52840 BOARD=dongle mqtt-client.dfu-upload PORT=/dev/ttyACM0
make TARGET=nrf52840 BOARD=dongle login PORT=/dev/ttyACM0

Flash Border router & run:

cd contiki-ng/IoTProject/sensors/rpl-border-router
make TARGET=nrf52840 BOARD=dongle border-router.dfu-upload PORT=/dev/ttyACM0
make TARGET=nrf52840 BOARD=dongle connect-router PORT=/dev/ttyACM0

Flash Coap:

cd contiki-ng/IoTProject/sensors/coap-sensor
make TARGET=nrf52840 BOARD=dongle coap_sensor.dfu-upload PORT=/dev/ttyACM0

Command for MySql

Sql terminal command:

sudo mysql -u root -p

use name_db;

show tables;

DROP TABLE name_table;

CREATE TABLE name_table (valeìue FLOAT NOT NULL,...);

Project Overview:

Our project aims to create a home monitoring system , environmental factors and an intrusion detection system. All the data are then saved in a database. "mqtt" sensors take care of the monitoring part of gas , lighting , temperature and humidity. "Coap" sensors deal with the intrusion system , as soon as the motion sensors sense the intruder , the alarm is triggered and gradually increases in intensity. To manually stop the alarm , just press the button of one of the alarms

MQTT implementation

The mqtt sensors take care of monitoring some parameters including gas, light,temperature and humidity in the room.

As soon as they started, they connect to the border router deployed on a physical sensor, while the message broker used was deployed on the Vm,Mosquittos broker was chosen.

If the sensor connects to the border router , the LEDs are turned on for a short time to signal that the connection has been made.

Once the connection is made, the data is sent in json format,on the topic "info". Not having the actual sensors, the data are randomly generated with the rand()function.

hidit= d() %
umyran

This is the message sent to the broker:

sprintf(app_buffer,"{"temp":%d,"humidity":%d,"light":%d,"gas":%d}”,temp,humidity, light, gas);

As soon as the app receives the data via json (onMessage()sensors , it extracts the data and makes a query to the SQL Database to insert it into the table "mqttsensors" , with an associated timestamp , useful for graph visualization.

sql = "INSERT INTO "mqttsensors'("temperature","humidity","Light",'gas') VALUES (%s, %s, %s , %s)"

A thread is created for the mqtt client.

Testing on cooja, you can see that messages are sent and received correctly:

01:14.995 ID:2 Check connected
01:14.995 ID:2 Message: {"temp":17,"humidity":95, "light":2,"gas":23}
01:44.995 ID:2 Message: {"temp":9, "humidity":52, "light":2,"gas":70}
02:14.995 ID:2 Message:{"temp":32, "humidity":61, "light":1, "gas":5}
Filter:

And they are also correctly entered on the DB:

timestamp light temperature humidity gas
2022-07-23 09:22:56 DARK 17 - 95 232022-07-23 09:23:08 DARK 9 52 702022-07-23 09:23:11 RELAX 32 61 5

Coap implementation

"Coap" sensors take care of the alarm system; we have the motion sensors placed in various parts of the house, which detect movement and start an alarm when movement is detected. In order to do that, we have two resources, a motion resource and an alert resource. The motion resource randomly generates motion data with a timer, having no physical sensors.

The sensor client was done by dividing the threads for recording and detecting the state change, so as to avoid problems caused by synchronization of threads,detected in the testing phase.

When the sensors register to the Python Server, they are instantiated with the resource class, and the observation of the sensor "Motion" begins , which detects any change in state.

When the sensors connect to the border router, the LEDs areturned on for a short time to signal that the connection has been made.

As soon as the state of the resource changes, a query is made to the MySQL database of the sensor , and a POST is made to the alarm to turn the alarm on or off.

response = self.client.post( lf.actuator_resource,$^ { \prime \prime } O F F ^ { \prime \prime } )$

sql = "INSERT INTO 'coapsensorsmotion' ('value`) VALUES (%s)

When an intruder is detected, the alarm is activated. It emits a variable intensity sound, in fact the intensity is increased according to the time of the intruder detection.

char val2 = ísActive == 1 ? 'T': 'N';

strcpy( Dest: msg, Source: "{"info":"");

strncat(Dest:msg,Source: &val2, Count: 1);

Communication via json was also used for these sensor types, here is an example of creating the GET message of the alarm:

//strcat(msg,"""");

strcat(Dest: msg, Source: "", "intensity":"");char intensity_str[400];

sprintf(intensity_str,"%d", intensity);

//printf("intensity: %s\n",intensity_str);

strcat( Dest: msg, Source: intensity_str);

strcat( Dest:msg,Source: ""}");

printf("MSG: %s\n",msg);

It is saved in the Database , the signal intensity and actual alarm activation combined with a timestamp, useful data for graphing in Graph.

59L="INSERT INTO 'coapsensorsalarm'( value 'intensity) VALUES (%s, %s)

To disable/enable the alarm and motion detection in manual mode, you can press the button of any sensor.

After the connection and registration phase , the sensor emits a light signal, to signal that the sensor is actually activated.

Testing on cooja:

1.we see the motion sensor changing state:

02:58.167 ID:2 Trigger Motion
02:58.167 ID:2 MSG res.c invio : {"isDetected":"T"}

2.The server receives the status change and activates the alarm sensors:

Callback calledresource arrived
,{"isDetected":"T", "info":"T", "intensity":"10"}{"isDetected": "T", "info":"T", "intensity": "10"}Detection value motion node :['T'][ '10']

3.The server makes a query to the database with"iDtctd=1",the intensity of the alarm and the timestamp:

value alarm in tensity timestamp
0N 10 2022-09-08 11:37:09
1 T 10 2022-09-0811:37:18
1 T 10 2022-09-0811:37:19
0 N 20 2022-09-08 11:38:18
0 N 20 2022-09-08 11:38:24

4. The alarm is activated with an intensity that will vary over time:

01:46.890 ID:4 Trigger Motion
01:46.890 ID:4 MSG res_motion send : {"isDetected":"N", "info":"N","intensity":"20"}
01:46.950 ID:4 entered in POST function!
01:46.950 ID:4 [DBG:motion sensorl POST/PUT Request Sent
01:46.950 ID:4 Post handler called
01:47.258 ID:5 Trigger Motion
01:47.258 ID:5 MSG res motion send :{"isDetected":"N", "info":"N" "intensity":"20"}
01:47.441 ID:3 Message:{"temp":5, "humidity":71,"light":0,"gas":17},
01:49.443 ID:5 entered in POST function!
01:49.443 ID:5 [DBG: motion sensor] POST/PUT Request Sent
01:49.443 ID:5 Post handler called

MySql Server

The Python library "pymysql" was used to create the SQL server with the following access data:

$h o s t = ^ { \prime } \log t h o s t ^ { \prime } ,$

$u s e r = ^ { \prime } r o o t ^ { \prime } ,$

$p a s s w o r d = P A S S W O R D ^ { \prime } ,$

databa$b a s e = ^ { \prime } \cot t o r ^ { \prime } .$

With 2 tables inside:

mqttsensors=for temperature, gas, light, humidity info of mqtt sensors.

coapsensorsmotion = for tracking detected movements and associated alarms over time.

Grafana

We used Grafana for visualization of the data collected from the sensors and saved in a SQL database, coupling them with timestamp.

Recommended use of Chrome.

We update the graphs every 30 seconds.

“Mqtt”dashboard:

1. Chart for gas in the room

2.Chart for Temperature

3.Chart for the luminosity of the room

4.Chart for the humidity

“Coap”dashboard:

1. Chart for alarm % activation over the time

2.Chart for the detection of the intrusion

  1. Dashboard for the intensity of the alarm updated real time value have a range 10-100.

About

IoT project for a smart house using mqtt and Coap sensor. The sensor is implemented with real board and tested on Cooja. Is used also a sql database and Grafana to visualize element

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published