Author: Yoga Perdana Putra
Last Update: February 2018
Author's Note:
This project was created when the author was still an electronics student a few years ago, therefore the source code is no longer updated. Also for the first time learning to use the Python programming language on this project.
The interface views (application and web page) are presented in Bahasa Indonesia, except for system-generated error messages. This documentation is intentionally written in English and doesn't provide a complete detailed explanation of entire project. Please kindly contact the author if you have any questions.
This is an IoT (Internet of Things) project using multiple light sensors connected by Arduino Nano (compatible) board and send detected values wirelessly to PC via radio telemetry device. PC will collect the data stream, store it into database and presenting to a simple web-based analytical interface that can be made accessible through the network.
Picture above: Entire system block diagram
The concept is that each light sensor will be placed in a separate room. Any light detected by the sensor in the room will be considered the room active or "ON". Otherwise the room will be considered inactive or "OFF". All sensor nodes will be connected to each other and placed for example on the ceiling of the building. Data is transmitted wirelessly so that the devices that record the data (in this case it's a PC) can be placed more flexible.
- Arduino Nano
- 433MHz 3DR-based USB Radio Telemetry Kit (example product)
- Plain PCB (printed circuit board)
- LDR (light-dependent resistor) with 10k-ohm resistor as voltage divider
- Additional housing pins, cables, read more on another parts of this documentation.
Current working environments:
Curcuit & Board Development
- EAGLE 6.3
- Arduino IDE 1.8.5
Middleware, Database & Web Interface
- Microsoft Windows 10
It's possible to run within Linux kernel or MacOS. Not fully tested yet. - Python 3.6.4 with installed packages:
- PySerial 3.4
- PyMySQL 0.8.0
- PyInstaller 3.3.1 for executable application builder (optional, Windows only)
- Windows SDK (optional, Windows only)
- XAMPP 7.1.1, bundled with:
- Apache 2.4.25
- PHP 7.1.1
- MariaDB 10.1.21 (MySQL database compatible)
This project consist of 3 elements:
- Main microcontroller board
Arduino Nano controls the entire physical devices group based on given command program. - Telemetry device
Send data from board to PC and vice versa. - Sensor circuit
Small circuit unit, each one contains a light sensor.
Two types of circuit boards are made. Arduino and telemetry device can be assembled into one package. Sensor board made separately as many according on demand, up to 8 pcs based on the availability of Arduino Nano input pins.
Picture above: The controller board's appearance (final assembly)
Picture above: The sensor unit boards' appearance (final assembly)
The telemetry device consist of two separate units: (1) Pin-based unit, and (2) USB-port unit. Each can act as transmitter or receiver based on the process cycle. The first unit is paired with Arduino and the USB unit is plugged into the PC.
USB radio telemetry hardware currently tested running smoothly on 115200 baudrate (can be different if using another compatible clone device)
Picture above: Circuit schematic diagram
Each Arduino analog pin is connected to an LDR component. A voltage divider circuit is formed by adding a 10k-ohm resistor in parallel. Different types of LDR may require different voltage divider resistance.
All components connected to Arduino are given a voltage of 5V. Arduino Nano (rev 3.0) has 8 analog pins (A0-A7) that can be used all or partially according on demand.
Sensors and Arduino will be farly separated and connected by a long cables.
Telemetry device connected to Arduino's receiver-transmitter pins which are allocated on pin D0 for RX and D1 for TX.
Picture above: Board design for the physical unit (top: for controller, bottom: for sensor)
I've provided source files that can be accessed using EAGLE software. Files have been created using EAGLE version 6.3. Print-ready document is also provided.
Navigate to device/board/
directory to download.
Please refer to The Official Documentation of Arduino Nano for complete installation instructions.
Make sure the Arduino device is successfully connected into PC via USB port. Check the port address from device manager (e.g. COM3 on Windows).
Load the sketch file device/arduino/nano.ino
into Arduino IDE. Select the correct board type and port. Then, upload and run the sketch file.
Adjust the configuration variables before uploading to microcontroller if you have assembled with a different setup.
Data from all sensors will be collected at the same time as a row stream with following format:
[<s0_data>|<s1_data>|...|<s7_data>]
For example:
[1023|1023|1023|1023|1023|1023|1023|1023]
Unused or disconnected pin will return a value of zero. Brighter light detection will return a higher integer value up to 1023.
Reading value refers to 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage (5V) into integer values between 0 and 1023.
-
Install Python
Download from Python's official download page and follow their documentation for installation instructions.
If you want to keep up with the original working environment of this project, you can choose Python version 3.6.4. -
Navigate to the middleware directory
Run the following command:cd middleware
-
Install the
virtualenv
package
Thevirtualenv
package is required to create virtual environments. You can install it with pip:pip install virtualenv
-
Create the virtual environment
Specify a local directory name for the environtment, e.g.room-app-env
, type the following:virtualenv room-app-env
-
Activate the virtual environment
Run the following command:- Linux/MacOS:
source room-app-env/bin/activate
- Windows:
room-app-env\Scripts\activate
- Linux/MacOS:
-
Install the requirement tools
Run the following command:pip install -r requirements.txt
-
Run the application script
Run the following command:python app.py
If you want to run applications directly through Windows OS without installing Python, you can directly execute the file: middleware/app.exe
. Tested on Windows 10.
Rest assured, the provided executable file is completely free of viruses or malicious injection because it is directly compiled from the Python scripts. But it can cause some bugs such as the python.exe process that won't exit when the application is closed.
The script can be compiled into an executable file for portable use on Windows environment. You must successfully installed these packages first before performing compile:
- PyInstaller
- Windows SDK (Software Development Kit)
Windows 10 SDK contains UCRT DLL files like api-ms-win-*.dll
that required to build Python's Tkinter window interfaces.
Run Command Prompt or PowerShell as Administrator. Use command below to start compiling:
pyinstaller app.py -p '<path-to-sdk-directory>' -w -F -y --clean
For example:
pyinstaller app.py -p 'C:\Program Files (x86)\Windows Kits\10\Redist\ucrt\DLLs\x86' -w -F -y --clean
For debugging, remove -w paramater to show terminal window when program is running.
This will create two new directories: build
and dist
. The app.exe
file is in the dist
directory.
Picture above: Screenshot of middleware application
Select active port of radio telemetry on list and press Select button. The app will receiving data from radio telemetry continously unless you press Stop button. Received data will be displayed on data history box.
On the first run, maybe you will get incorrect interval data transmissions. Change the transmission interval to any seconds. I recommend you to give value about 5 seconds or greater.
You must specify the cutoff sensor value with your own calibration by looking on the detection area. Set how much light amount is detected to set as ON or OFF state.
To store the data into database, fill MySQL hostname, username and password that assigned to database. Then press Connect button. If anything goes wrong, error messages will be displayed on the database monitor box. You can change database interval anytime.
Make sure you have successfuly created the correct database and specified tables before connecting middleware app to MySQL server. Read next documentation for more instructions.
If you want to keep up with the original working environment of this project, I use XAMPP which contains packages such as Apache web server to serve PHP web language and MariaDB (MySQL database compatible) to storing data.
Install XAMPP first (see their documentation for more instructions) and make sure the Apache and MySQL services are successfuly started. Don't forget to configure MySQL's authentication.
Alternatively if you wish to serve the web and database server separately from the middleware, I recommend using Linux server distro and installing Apache-PHP & MySQL server separately. Please adjust the configuration by youself.
In order to store received data from sensors, a database and tables must be created first. Run the batch SQL script with following command:
mysql -h <host> -u <user> -p < database/initial.sql
The command will ask you for the SQL server password. Please adjust the hostname and username with your SQL server configuration. For example with default authentication:
mysql -h localhost -u root -p < database/initial.sql
If you're using XAMPP, script can be executed via phpMyAdmin. Login first to phpMyAdmin page. You can copy the contents of the script and paste it on SQL tab or import the SQL file through import tab.
The script will create a new database and two new table: sensor_config
and sensor_data
with the specified structure.
Make sure the database and tables is successfully created before continue to next step.
For some reason, if you want to clear the sensor data from database, you can truncate with following SQL command:
TRUNCATE TABLE sensor_data
Copy our webroot
directory to htdocs
folder inside XAMPP's installation directory. Navigate to http://localhost/webroot/ from your browser to access the webpage. Please adjust if you've changed the directory name.
Library dependencies:
Picture above: Screenshot of analytical data page (sample data)
This project might still contain several bugs and code might be deprecated. The project is working with basic requirements mentioned above.
Some ideas that can be done next:
- Upgrade telemetry device with better specifications. Several tests have been carried out at different distances but found unsatisfactory results.
- Replace with multiple telemetry nodes to represent each sensor and flexible location. This project need a very long cables that causes some little voltage loss and affected to detection data.
- Do detection with another type of sensors. For example: phototransistor types, photodiode types, FIR (far infrared) thermal sensor, etc.
- Serve web-based application with Python web framework. Could be packaged together with the middleware application or separated microservices with integration.
Anyone can make a replication and/or modifications of all contents of this project with any purpose. Please do a cite the author and this repository if any project is made and published based on source code or included asset from this project.