Lumen, the Smart Lamp

by AtomicPaladin in Circuits > Raspberry Pi

147 Views, 1 Favorites, 0 Comments

Lumen, the Smart Lamp

IMG_20220620_134340-removebg-preview.png

Lumen, The Smart Lamp


I started this project as an assignment for school, during the project it became clear I liked making this a lot and it became more then just a school project. I poured a lot of sweat and tears into this.

What I made is a smart lamp. But what exactly is a smart lamp, it contains three sensors:

  • Light sensor, this measures the outside light and changes the lamp's brightness accordingly.
  • Motion sensor, scans for motion and turns the lamp on. If the sensor doesn't detect light for a while it turns off again.
  • Sound sensor, this one listens to pulses (in this case 2 pulses in a 2 second interval) and turns the lamp on or off.

These sensors do not work all at the same time, it is possible to choose to control everything manual, or switch into an automatic mode (this means you can choose one of the three sensor at the same time).

Besides these three sensors, it is also possible to program the lamp to activate at a certain time. You could compare it to a clock in the morning. Let's say you want to wake up at 8:00 AM, you could set an alarm on the website and wake up to a flashing light.

Some other small functionality is, the lamp changes color to your own likings. On the website there are some pre defined colors you can choose from or a color wheel to pick your own color, besides the color you can also change the brightness.

Supplies

Main Computing Unit

  • Raspberry pi 4, model B
  • This will run all the programs and databases

Power

  • 5V usb c connector for the Raspberry pi
  • 9V connector for the breadboard powersupply

Electronics and wiring

  • KY-038
  • This is the sound sensor used, others will work (digital pinout)
  • PIR hc-sr501
  • Motion sensor used
  • GL5516 LDR
  • Measures light intensity (analog
  • Jumper wires
  • Connect all the components with these wires
  • BreadBoard
  • Lll the components will fit on this board, easy to connect with the jumper wires
  • MCP3008 (or MCP3004)
  • This is used to convert analogue signals to digital
  • Breadboard power supply
  • We have some components that require 5V which our can't deliver.
  • Unicorn HAT, pimoroni
  • My choice of LED Matrix, you could use neo pixel ring or other LED (you'll have to program them using the right library)
  • T-Cobler + flatcable (optional)
  • Easier connection to the breadboard from the Raspberry
  • LCD Display
  • This is used to show some extra information
  • Potentio Meter
  • This is used to adjust the contrast of the LCD display

Casing

  • Cable Tray
  • Wood base
  • This is what I used. Feel free to use whatever you want or prefer.

Tools

  • Soldering iron, Soldering
  • Screwdriver
  • Wood glue
  • White paint
  • Miter box
  • Saw
  • ...

Electrical Engineering

The first step to completing this project will be building the electrical circuit. We'll start with constructing this on a breadboard. Why you ask? This will make it if we have any problems with the circuit you can still change it and don't have to worry about soldering anything yet.

Before you power on anything, double or even triple check the circuit. Make sure you have no shorts, also make sure there is not direct connection between the raspberry and the breadboard power supply. The raspberry will be fried with direct 5v connection.

After checking everything you can power on the raspberry and the breadboard powersupply.

Configuration of the Raspberry Pi

I'll provide the image for the operating system. This should have everything configured correctly:

  • Apache webserver, Frond-end hosting
  • MariaDB, this will hold the database server
  • Pre-installed python libraries for the code to run

This custom image can be downloaded here.

Extra information on how to instal this image can be found on my github.

Configuration of the Database

ERD.png

Here in this step I will explain to you how I set up the database. The only thing you will have to do is import the database dumb, how to do this is explained in the readme file on github where you can also find the database dumb.


Before we have a look at the database you have to make sure all the right packages are installed on your pi, this should all be done if you installed the image correctly. If not you can find extra explanation on my github.


Table 1:

  • Device
  • Here are all the devices stored with their name and deviceID being the most important

Table 2:

  • Action
  • Here are all the actions that can be performed during the usage of the smart lamp.

Table 3:

  • History
  • Here is al the data stored, with the value, deviceID and actionID
  • This makes it so we can see what data is being stored by which device

Table 4:

  • Alarm
  • On the website you can add a new alarm. These get stored in here with the date, time and Profile

Table 5:

  • Profile
  • These are the profiles used for the lamp. I might upgrade these later so you can make your own profiles. At this point in time they are fixt.

Back-end

For this part of the process you have to clone/download the file from my github. This is where most of the magic happens.

Before we start unpacking this

If you installed the image provided in this document you'll have the basic package to run the program, if they app.py file doesn't run. It's probably cause you haven't got all the right libraries. Extra information on what to do can be found on my github.


NOTE: If you chose a different password for the installation of your database, make sure to change this in the config.py


Backend/helpers:

  • In this folder you'll find helper classes for all the different sensors.
  • Also found in this folder is a test file for specific sensor. Here you can test some things without having to change the whole project.


App.py:

  • This is the main file the project. Here you'll find all the code for the connection with the database, connection with the front-end and last but not least the reading of the sensors.
  • Here you can change some timings if you would like. (for example, timeout timer for the movement detection)
  • All the sensors operate on a different thread. If you want to add more sensors, no problem but add them in a new thread


Running app.py on startup:

If everything seems to run as you like, then it's time to configure the startup service for the app.py. This file will be included in the github repo, but you'll have to change some things:

  • You'll have to change the path to where your app.py file is located.
  • When this is done copy the file AS ROOT to /etc/systemd/system with this command
sudo cp lumen.service /etc/systemd/system/lumen.service
  • To test this service run this command:
sudo systemctl start lumen.service
  • If this works correctly stop the service again with:
sudo systemctl stop lumen.service
  • And last but not least, run the service on startup with:
sudo systemctl enable lumen.service


If you've done everything correctly, your back-end should run automatically when you start your pi.

Front-End

frond-end-02.png
frond-end-01.png
frond-end-03.png

Alright, this is the step where we set up the frond-end website. I designed this website to be practical. I like it to some extent but it's not my finest of works. If you would want to change anything design wise on this website you can. Just go to the style folder and edit the main.css do not edit the normalized.css in any way this is just a file to normalize all this styling.

NOTE: The design I made has a mobile first look to it. Most user of lamp would use it as an app. So keep this in mind when editing the design.

NOTE: When editing the html files for design changes make sure you don't edit the classes beginning wiht 'js-' these are the classes solely used for javascript.

If you want to make sure the website loads when surfing to the IP-adress of the raspberry. Don't forget to edit the apache2 server on your pi. More information on how to do this can be found on my github.



Putting Things Together

IMG_20220620_134340-removebg-preview.png

This is the creative part of the build.

For my housing i used some wood and cable tray. Honestly this wasn't the best idea I've ever had. I struggled a lot with getting all the eletronics in, but it is possible. If I ever have to build this again I would probably chose to either 3D print a housing or laser cut one out of wood.

Just be sure all the components are tucked away and secured properly so they don't come lose. (and obviously don't make open wires connect => fried Raspberry)