Introduction: Switch-it, Automatic Outlet With Realtime Current Measurement

About: student MCT at Howest (Kortrijk Belgium)

I really like automatisation, the ability to control when something has to happen. This is what made me come up with this idea: a selfmade, automatic outlet. It can be used to plan when lights need to be turned on, when phones need to be charged or when a screen needs to be powered. In addition to all of this, you have the ability to see how much current is flowing through the outlet.

The components needed for this project are:

Supplies

Before we’ll begin these are the things you’ll definitely need to build a Switch-IT box like mine. These supplies will cost around 50 to 100 euros depending on your region and current prices.

Hardware

Electronics

  • Raspberry pi 4
  • 16gb (or more) Micro SD-Card
  • Arduino Uno
  • 5V-3.3V Level shifter
  • 5V relay module
  • ACS712 20A - current module
  • 1838 IR-Receiver 37.9 kHz module
  • IR remote (I use an Elegoo one)
  • common cathode RGB
  • 3 * 330Ω resistors
  • push button
  • RFID-RC522
  • LCD 1602A-1
  • Niko wall socket
  • 10K Ω potmeter
  • 230 to 5V - 7A power supply

Cables

  • USB-B to USB-A cable
  • USB-C power cable
  • Ethernet cable

Various

  • Male-Female Jumperwires
  • Male-Male Jumperwires
  • Solder tin
  • Shrinking tube

Case specefic parts (OPTIONAL)

  • 50 * nuts and bolds
  • metal case 40x40x5 cm with lid
  • double sided tape
  • 6 * 1cm high spacers
  • led-holder
  • Velcro tape
  • cable guides

Software

Step 1: Setup Rasbian

First of all we'll flash Rasbian onto the SD-Card. We will do that using balenaEtcher.

  1. Open balenaEtcher
  2. Choose the Rasbian Image
  3. Select your SD-Card
  4. Press Flash an wait a few minutes untill it's flashed

Adding APIPA in boot directory:
We'll be using an APIPA adress to program and configure settings on the RPI. To do this:

  1. Acces the boot directory on the SD-Card
  2. Open "cmdline.txt "
  3. Add "169.254.10.1" at the end of the document and save it
  4. Add a file called "ssh" to the boot directory (DON'T GIVE THIS FILE AN EXTENSION)
  5. After you've done this you can eject the SD Card from your PC.

Acces the RPI using PuTTy

Now we can plug in the SD card into our RPI, connect the RPI to your PC using an ethernet cable.

To connect to the RPI we'll use PuTTy with our APIPA-address.

  1. Open PuTTy
  2. Fill in our APIPA-address as host name (169.254.10.1)
  3. Make sure the port is 22 and SSH is selected
  4. Now you can open the connection
  5. The default username is: pi
  6. With default password: raspberry

raspi-config settings

Open raspi-config using:

sudo raspi-config<br>
  • Change the user password
  • In localisation options select your timezone
  • Setup WiFi using network options then Wi-Fi option where you have to fill in your SSID and Password.

Install Python 3

In this project we're using Python, therefor we'll set the default to Python3 insteat of Python2, using following commands

update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives --install /usr/bin/python python /usr/bin/python3 2<br>

Python packages

We'll need a few packages to get everything working correctly, mainly the Flask packages and one to make connection with our DataBase. We can install those packages using the following commands:

pip install Flask
pip install Flask_cors
pip install Flask_socketio
pip install Python-mysql-connecton

Database

Next up we're going to install our database management system (MariaDB) we'll do so using:

sudo apt install mariadb-server<br>

Type “Y” and Enter to continue. After a few seconds, the installation process is complete and MariaDB is almost ready to use.

To set give the database a password use the command:

sudo mysql_secure_installation<br>

Then press Enter, since the current password is empty. Then press "Y" to set a password, now you can fill in any password you want, make sure to remember it because we need it to make a correct connection between back- and front-end.

Now, press “Y” 3 times to: remove anonymous users, disallow root login remotely and remove the test database. Finally, press “Y” again to reload the privileges.

Now MariaDB and all the needed packages are succesfully installed.

Step 2: Grabbing the Code and Setting It Up

Now that we have all the packages installed we can grab the code.

The code is available on Github so you can clone it using:

git clone <a href="https://github.com/MiroVerleysen/Switch-it-front.git"> https://github.com/MiroVerleysen/Switch-it-front....</a><br>

This is the frontend

git clone <a href="https://github.com/MiroVerleysen/Switch-it-back.git"> https://github.com/MiroVerleysen/Switch-it-back.g...</a>

This is the backend

Installing the Database itself

To install the database, navigate to the .sql file wich is in the backend folder using the following commands (make sure to set it to your own path.

mysql
create database switchit
quit
mysql -u root -p switchit < Your_Path_to_Backend_Repo/switchit.sql<br>

Installing Apache

Now that we have all the code and the database set-up we can install Apache, and run our backend on it. We'll do so using:

apt-get install apache2 -y

Then replace the files in /var/ww/html with the ones in the folder from the frontend folder.

If everything went well you should be able to connect to your website using the APIPA address: 169.254.10.1 in your browser.

Step 3: Arduino Communication

To read our current sensor and our RFID sensor we're using an arduino, to do so use the arduino code given below. Upload it using an USB-A to USB-B cable and the arduino IDE software. When uploaded, the arduino part is done.

Now we have to find the serial device name on the PI. To do so make sure that in /boot/config.txt "enable_uart=1" is set right. Also make sure that "console=serial0,115200" is removed out of cmdline.txt.

Then check the ports using

ls -l /dev

Then one of the serial names should be the Arduino. Fill this name in at the arduinocom function in app.py

ser = serial.Serial('<u>/dev/ttyS0</u>',9600)

Only do that if the serial communication is not working.

Step 4: Wiring Up Switch-it

Wiring up everything is pretty straightforward although you should keep following things in mind:

  • Be carefull with the wall outlet, 230 volts can be deadly.
  • Make sure to use a level converter for communication between the RPI and Arduino.
  • Before soldering the RGB, make sure you use the Red and Green pin. Test this beforehand!
  • Test if nothing is short-circuited BEFORE powering the circuit.

Step 5: Running the Code

Now everything is connected correctly we can run our program.

To make it run automatically at startup create a file using:

sudo nano myscript.service

Then paste (make sure to use your own app.py directory:

[Unit]<br>Description=Switchit
After=network.target
[Service]
ExecStart=/usr/bin/python3 -u app.py
WorkingDirectory=//change to app.py directory//
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target

Then press "ctrl + X" and copy it to /etc/systemd/system.

Then the follwing command to make it run automatically:

sudo systemctl enable myscript.service

You can find more info about this on the RaspBerry Pi site.

Reboot and done!

Now reboot you PI using:

sudo reboot -h now

This is how you make Switch-it!

Thanks for following along, hope this was helpfull. If you have any tips or feedback, don't hesitate to leave a comment.

Step 6: (OPTIONAL) the Housing

You can build the housing entirely to your own likings. I used a metal case wich is 40x40x5 cm with a lid. Since I used a metal one I had to drill holes and use spacers to get everything off the ground plate. I chose to use a central point where the 5V, 3.3V and gnd is available. Every electrical connection is soldered with a piece of Shrinking Tube on top of it. To do the cable management I used pads with carrier straps.