Arduino Project: Control Electronics Over the Internet Using Nodejs + SQL Database & Website.

About: I am a second-year engineering student attending the University of Kent. Am passionate about doing projects in my spare time. My field of interest is broad; embedded systems (Software & Hardware), Medi...

Intro: Arduino Project: Control Electronics Over the Internet Using Nodejs + SQL Database & Website.


Date Made: 14 July 2017

Difficulty Level: Beginner with some programming knowledge.

Hardware Requirement:

- Arduino Uno, Nano, Mega (I think most MCU with the serial connection will work)

- Single LED & Current limiting resistor. Use this calculator if you are not sure: Ohms Law Calculator

- 10K Potentiometer.

Software Requirement:

- Arduino IDE

- Node.JS (this is a software on a computer, very simple to install)

- MySQL Server (easiest way I found is to use a cheap web hosting. You also can get free domain names)

Programming & Scripting language used:

Arduino (Modified C/C++), JavaScript(Nodejs), PHP, HTML & CSS

This project in a nutshell: Controlling an Arduino microcontroller from a web interface. Therefore control any electrical device from anywhere with internet. I wanted to stretch my understanding of programming and web development and what better way to do this than doing a simple but effective project. The hardware is kept at a minimal so I could focus more on software. Hence I went with a simple LED, pot setup. The pot will send data and the led will receive (PWM brightness). Using NodeJS the serial data was read (potentiometer value) and written (led brightness). The difficult part of this project was getting input data from a remote location (web server)

Step 1: Software Logic: System Architecture

Potentiometer Data:

This starts at the Arduino, read pot value is serial print. However, this time we will use Node.JS to read the value. NodeJS will open serial communication to the same port as the Arduino is connected to and read the printed pot value. NodeJS will then upload the data to a remote SQL database, this will happen every time a new pot value is printed. A webpage will connect to the SQL database set interval and retrieve the potentiometer value. This then will be displayed on the webpage.

Led Data:

For the led the PWM brightness will be set by the user on a remote webpage, so its journey starts at the opposite end of the spectrum. The input data is saved to a SQL database, every set interval the database is checked for a change in led PWM, this is done by NodeJS. If the value is different to the previous value then the new value will be sent to the Arduino via a serial bus. The Arduino changes the output PWM value of the led to change its brightness.

Ohms law calculator uses the formula's V = IR and P = IV = I²R = V²/R
For this project, I will be using a blue led. This is important because as the light frequency increases the voltage drop also increases. Since blue light has a higher frequency compared to something like a red led. This means a higher forward voltage. Depending on make, type, and size the working range will vary. For my setup, I used a 220 Ω resistor in series, negative to ground and positive to a PWM pin on an Arduino. The pot was connected to an analog pin. With 5VCC one end GND the other and the middle pin connected to an analog pin (A0 in my case).

Step 2: Step 1: Hardware Wiring

This is very simple: Just connect your current limiting resistor in series with the LED make sure you're led is correct way round. One point will go to GND while another end will go to Arduino pin. For my setup, I used pin 12 for led and A7 for Pot. I don't have a schematic since its very simple circuit. However, I did find this online (image)

Step 3: Step 2 : Arduino

First, the led and pot were checked if they were working as expected. This was down by a simple program where the pot value controls the led. I used constrain function to change pot range of 0 to 1023 to 0 to 255, but a simple /4 works as well. The pot value was smoothed out by taking the mean average from 10 consecutive readings, this is to remove spikes. (However, this smoothing caused issues with NodeJS so this was removed later on the project - more on that)

Code Arduino

Read / Write Serial
The next step is to take user input via the serial monitor windows provided by the Arduino ide to set the brightness. To do this, the serial.parseInt() is used which takes an integer value and ignores string. Also, an error checking is added to the code. The valid range of a PWM value is 0 - 255, when a user enters >255 then it assigns the value 255 and if the user enters value <0 then assigns 0. The pot value is printed on the serial monitor, however, this only happens when the current pot value is > or < +/-5 I have done this to make the reading more stable since it was fluctuation. Why this is a big problem related to SQL updating, more on that later.

Step 4: Step 3: NodeJS

I am not going to show you how to get or set up and SQL server. There are tons of tutorials out there.

There is 3 main aspect to the NodeJS program:

Read Serial Data

Write Serial Data

Update SQL Database

To make a serial connect within NodeJS, a module called serialport has to to be downloaded which can be done using npm command. Open CMD on the folder where NodeJS program will be kept, install by typing: npm install serialport Also the SQL module has to be installed to be able to connect to the sql database: npm install mysql NodeJS - Serial Port My first step with the NodeJS program was to read the printed data and send pwm brightness to the Arduino. This was done by opening serial connect at the same braudrate and port. Once the connection was established I read incoming messages and printed it onto the console window. Problem raised when I tried to write the pwm value to control the brightness.

It kept throwing errors: Port Not Open, my initial solution was to call the write function when there's incoming data. However this was a bad fix and I was quite unsatisfied with the solution, even though it worked it would only send when pot value was changed. The example code for the serial module would not work either throwing the same error. I later found out the program was trying to execute the write function without opening the port, which resulted in that error. I came around this problem by using setInterval() function

NodeJS - MySQL
The MySQL library was used (npm install MySQL) to connect to the SQL database since the server in a remote location the IP address of the server was used instead of localhost.

var con holds the connection information in JSON format, once the connection has been made successfully, the database can bed queried. 2 functions were created one for updating the table other selecting with parameters taking in the SQL query. The update table is called when a new pot value is received and brightness check query will run periodically.

NodeJSCode Link

Step 5: Step 4: the Web Interface

Web Interface

The main webpage was written in PHP since I already had some had some experience from my CO323 Databases and the Web module at university. Html table & form was used to display the sql data.

PHP SQL Access Code: Link Scroll to web interface section.

HTML & CSS * Javascript Webapage Code: Link Scroll to the bottom

Final Webpage

Step 6: Result



    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Audio Contest 2018

      Audio Contest 2018
    • Plastics Contest

      Plastics Contest

    2 Discussions