Introduction: UCL-IIoT-Security System
This is an educational project building on our previous project found at https://www.instructables.com/id/EAL-Embedded-Security-System/ aiming to get familiar with webservers, databases and communication between those and an arduino board.
Step 1: Overview
In this instructable, using our previous project (linked to at the top), we will aim to show how to set up a webserver and a database to handle logins for the security system. This time we are going to switch our Arduino Mega out with an Arduino UNO board from Wemos. This board has a built in ESP8266 WiFi module, which we are going to use to communicate with the webserver. We have also updated the Android application to send a username as well as a password, opening for the possibility of distinguishing between who logs into the system. Had the system been implemented on multiple entities you could also administrate what users would have security access to what entities.
The webserver is being implemented using Node-Red (a block programming tool for building webservers, using Javascrip and Node-JS). Using the Arduino boards WiFi connection the webserver gets logins, looks them up in the database and communicates back to the Arduino bard whether or not the login should be accepted. Furthermore you can administrate the users in the database through a graphical user interface on the webserver, as well as view which users logged in at what times.
The database is being built using MySQL and consists of two tables: one table containing a list of usernames and their corresponding passwords and another containing a log of successful logins with usernames and timestamps.
Step 2: Video
Step 3: Parts List
- Wemos D1 R2 (Arduino UNO)
- Arduino KY-003 Hall magnetic sensor module
- HC-05 Integrated Bluetooth Module Wireless Serial Port Module 6 pin New for Arduino
- Breadboard 830 point solderless PCB breadboard mb-102
- 3 Color RGB LED KY-016 FZ0455
- Strong magnet
Step 4: Wiring
Step 5: Arduino Program
On top of the code from the previous project, we have added the capability to connect to a WiFi network and send and receive packets over UDP. Thus we can send a username and password received over Bluetooth from an Android phone to a webserver, have the webserver check those against a database and send back whether the login checks out, to be received by the Arduino program.
In the setup we connect to the WiFi network:
//Connecting to a WiFi network and printing the progress and recieved IP to serial.
Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());
At the same time we print the progress to serial, so its possible to monitor whether or not the connection is made, as well as get the IP that's been given to the Arduino board.
We also start the UDP protocol:
//Start UDP.
udp.begin(localport);
When a username and password has been received from a phone, the received data is send over the WiFi network using UDP to the webserver:
//Sends the recieved username and password from the phone to the webserver.
//Starting a packet on UDP. udp.beginPacket(broadcast,localport); //Writing the packet. udp.write(code); //Closing the packet. udp.endPacket();
When the webserver sends a response, the response is received and given that this response is 'true' (indicating the username and password is correct), the login bool, in, is set to true and thus the user is logged in. If the response isn't 'true', in is set to false and the code buffer is cleared, to make ready for another login attempt:
//Receives true/false packet from the webserver depending on whether the username and password was found in the database.
int packetSize = udp.parsePacket(); //Checks whether data is comming and that the code buffer isnt empty. if(packetSize && strcmp(code,"")!=0) { //Reads the incomming packet. int len = udp.read(packetBuffer, 255); //Puts a 0 at the end of the packet. if (len > 0) { packetBuffer[len] = 0; } //Checks whether the packet is true and sets the passed bool. passed = strcmp(packetBuffer,"true")==0; //Sets checked to indicate the packet has been checked. checked = true; }
//Sets the login bool to true, if the password had been varified by the webserver. if(passed) { in=true; //Sets the built in LED for debugging purposses. digitalWrite(LED_BUILTIN, true); //Resets passed and checked to false. passed=false; checked=false; } //Else it sets the login bool to false. else if(checked) { in=false; //Delete the content of the code string of chars. for(i=0;i<254;i++){ code[i]=0; } //Resets the built in LED to false, for debugging purposses. digitalWrite(LED_BUILTIN, false); checked=false; }
Attachments
Step 6: Android Application
In this project we used all the function from our old project “Embedded security system”. The only change we made was to put another textbox in for the username and when the log_in button is pressed the username and password is put together with a join function, which only separates the two texts with a ",". Other than that, the app still does the same when pressed where it sends a string to the Arduino.
Step 7: Webserver
We used node red to access, manage and update the database. We also used node red to create a webserver with an easy useable userinterface where we were able to manage the two tables in the database we had. In the first table we had the users with their password where we could insert users and remove users. The other table were for logging where we could see the date and time a user would log in.
The database was made with mysql. Mysql databases can be accessed in node red by downloading the mysql palette. This gives you a block in node red where you can connect the database by adding a mysqldatabase where you type the IP-address of the host, the port you use, username for the database and password if it is used, and the database’s name. To get the information from the database shown on the interface we had to create a dashboard table with a template we then added an inject input with a sql-query to keep updating the database on the interface.
To manage the database, we made two buttons on the dashboard. One named register that would insert the text from the two text fields, user and password, into the log in table in the database.
global.set('user',msg.payload)
global.set('password',msg.payload)
msg.topic = "INSERT INTO login (user,password) VALUES ('"+global.get('user')+"', '"+global.get('password')+"')"; return msg;
The other button, delete, would delete the user which was written in the Delete user text field.
global.set('deleteUser',msg.payload)
msg.topic = "delete from login where user ='"+global.get('deleteUser')+"' "; return msg;
We also used node red to check the database when a log in request came from the Arduino. To receive a message through WiFi from the Arduino we had to listen for udp messages that way we could receive the log in string that came from the Arduino. The string that we would receive was “username,password” which we would have to separate with a function so we would have the username in one variable and the password in another.
var data = msg.payload.split(","); var user = data[0]; global.set('user1',user) global.set('pass',data[1]);
That way we could check the database if the user was registered and if the password was correct.
msg.topic = "SELECT password FROM login WHERE user = '"+user+"'"; return msg;
We would then get TRUE or FALSE which would be send back to the Arduino in a udp message.
try { msg.payload = msg.payload[0].password==global.get('pass'); } catch(err) { msg.payload=false; } return msg;
We also implemented that if the user and password was correct the user and a timestamp would be inserted into the log table, that way we could keep a logging track of who logs in.
if (msg.payload) { msg.topic = "INSERT INTO log (user,timestamp) VALUES ('"+global.get('user1')+"', '"+global.get('timestamp')+"')"; } return msg;
To get the date and time would put a date/time formatter in a variable which we would keep injecting every second to get the exact time.
global.set('timestamp',msg.topic) return msg;
Attachments
Step 8: Database
The database i made using MySQL. We built two tables: one for storing login (named 'login') information for users, namely usernames and passwords and one for storing a log of logins (named 'log'), with usernames and a timestamp for the moment a user successfully logged into the security system.
The 'login' table is using usernames as a unique key and its primary key, ensuring that no two users can be committed with the same username.
The 'log' table is using usernames as a foreign key, referencing the usernames from the 'login' table, thus ensuring that only existing usernames can be logged.
The database is written with SQL queries and likewise anytime we insert, update, delete or select data from the database it is being done by sending SQL queries.
Installation and setup instructions on Ubuntu
Installing the MySQL server on Ubuntu is easiest using the command line package manager:
sudo apt install mysql-server
To allow communication from a remote client the port has to be enabled through the firewall:
sudo ufw allow mysql
Last the server has to be started and its ready for use:
sudo service start mysql
Setting up the database on the server you first have to log in. By default a root user will be registered with your Ubuntu password:
sudo mysql -u root -p > Enter password: ubuntuPassword
To create and select a database you can execute the following SQL queries:
CREATE DATABASE dbname;
USE dbname;
To create the tables in this project you can use the following SQL queries:
CREATE TABLE login (user TEXT UNIQUE PRIMARY KEY, password TEXT);
CREATE TABLE log (user TEXT, timestamp TEXT, FOREIGN KEY (user) REFERENCES login(user));
The database is now ready for use with this project.
Step 9: Connectivity
The Arduino board, database host and node red host are all connected through WiFi. To connect through WiFi you either must be on the same network or port forward the router. We went on the same network. You also need to enable the port forward on the firewall or just disable it like we did.
In node red we used UDP input and output to receive the information we wanted from the Arduino. In this case we received a string from the Arduino and sent back a bool value. To receive and send data with UDP you just must listen on the same port as the information is being sent on. You also need to specify the Ip-address you are sending it to. The Arduino or node red then catches the information sent on that port.
To connect the database to node red we had to be on the same network. That means the computer with the database should be on the same network as the computer with node red trying to connect to the database. In node red we used a mysql storage node to connect to the database with the following parameters: Host Ip-address, Port, Username and password, and the database name. when the database is selected and deployed in node red and the database is running on the host computer it should connect automatically.
Step 10: I/O List
Bluetooth HC-05
- RXD
- TXD
Digital inputs
Hall magnetic sensor
- Hall = D3
Digital outputs
RGB LED
- Blue = D5
- Red = D6
- Green = D7
Step 11: Evaluation
Discussion
Comparing to the previous project, the hardware implementation is still pretty barebones. However a lot of backend features have been implemented. It is now possible to have different users with different passwords and manage said users from a userfriendly graphical user interface on a webserver.
Further Development
Given further work on the project we would still like to add the functionality to receive a push notification on your phone over the internet, if the alarm goes off. Unfortunately this didn't fit within the scope of the educational specifications for the project this time.