NightLight

by Lotte Keyngnaert in Circuits > Raspberry Pi

168 Views, 0 Favorites, 0 Comments

NightLight

thumbnail_IMG_3323.jpg

You know that feeling when you are lying in bed and suddenly really need to pee, but the light switch is way too far, so you decide not to go because it is too dark? Yes? Then this project is an amazing solution for you! No? Well, this project could still come in handy and makes your room look cool!

This was on the back of my mind for a while and because I had to make a project for school, I decided to make it.

In this instructable I am going to explain how I made this project so you can make it yourself.

Supplies

Raspberry Pi 4B & 16GB SD Card

All code runs on my Raspberry Pi 4B with a configured image.

LCD Display 16x2

The LCD is used to show the IP address of my web interface and the set timer.

Datasheet: https://asset.conrad.com/media10/add/160267/c1/-/...

MCP3008

To read the data from my LDR and Sound Sensor. The MCP3008 is an 8 channel, 10-bit, analog to digital converter with SPI interface.

Datasheet: https://cdn-shop.adafruit.com/datasheets/MCP3008....

LDR

= Light Decreasing Resistance, or light-dependent resistor. Also known as a photoresistor and detects the light intensity in the room. Because of this I can see if its dark or not.

Datasheet: https://components101.com/asset/sites/default/fil...

PIR Motion Sensor

The PIR sensor allows me to detect whether a human has moved in or out of the sensors range.

Datasheet: https://asset.conrad.com/media10/add/160267/c1/-/...

SparkFun Sound Detector

The Sound Detector not only provides an audio output, but also a binary indication of the presence of sound, and an analog representation of its amplitude. The 3 outputs are simultaneous and independent, so you can use as many or as few as you want at once. I only use the envelope output because it allows me to easily read amplitude of sound by simply measuring the analog voltage.

Datasheet: https://learn.sparkfun.com/tutorials/sound-detect...

WS2812B – Neopixel Ledstrip

This is a ledstrip with 60 RGB neopixels but I also have one with 120 RGB neopixels (it all depends on where you are going to place it). This is connected to my raspberry pi, but it can also be connected to other devices like Arduino Uno.

Datasheet: https://cdn-shop.adafruit.com/datasheets/WS2812B....

Push button

2 buttons are used. 1 is used to change the screen on the LCD and the other is used to turn on the ledstrip manually.

External power supply

I use a Breadboard external power supply, but u can also use batteries. Either way both 3.3V and 5V are necessary.

Others

To connect everything correctly I used a GPIO T-Part, a small breadboard, some jumper wires and a few resistors. I’ve put every single component that I used in a BOM (= Bill Of Materials) but I recommend buying everything double in case something goes wrong by accident.

Assembling the Circuit & Raspberry Pi

FritzingBreadboard_KeyngnaertLotte.png
FritzingSchematic_KeyngnaertLotte.png

Above you can find the breadboard and electronic schematics as images but under here you will find the fritzing files.

I needed the analog data of the LDR and the Sound detector, but Raspberry pi only reads digital data. I used the mcp3008 to convert the analog data to digital. All three are powered with 3.3V.

The LCD, Motion Sensor and the ledstrip are powered with 5V but because both the ledstrip and the LCD are very heavy its recommended to connect the LCD to the 5V power on the Raspberry pi.

On the positive side of both buttons there is a 470 Ohm resistor. The LDR has a 10k Ohm resistor.

Setting up the Raspberry Pi

The first thing that needs to happen is installing a Raspbian Image. On this page you will find the instructions on how to install operating system images.

Up next is making sure that the Raspberry pi is connected to the Wi-Fi.

  • Start with “sudo -I” to get administrator rights.
  • Type “wpa_passphrase >> /etc/wpa_supplicant/wpa_supplicant.conf” but instead of you write the name of your own Wi-Fi and instead of you write the password of your Wi-Fi.
  • “wpa_cli -i wlan0 reconfigure” is to reload your card.
  • Now we will check if your connected by typing “wget www.google.com”

After connecting to your wifi is upgrading. “apt update” is to show which updates are available and “apt upgrade” will install those. If it stops and you get a question, then just type “Y”.

Now we will install Apache: “apt install apache2 -y”. This is the webserver.

You will also need to install MariaDB and on this site everything is explained perfectly how to do that.

Install these packages to make sure its possible to run the python file:

  • pip3 install flask-cors
  • pip3 install flask-socketio
  • pip3 install mysql-connector-python
  • pip3 install gevent
  • pip3 install gevent-websocket

Creating the Case

Page1_Case.jpg
Page2_Case.jpg

Technically building the case itself is interpretable but I put some pictures up if you want some inspiration or if you want to replicate it exactly. I really wanted to 3D print my case but after trying for while I figured that my measurements were too big. So I decided to make it in foamboard but I’m sure its also possible in other materials.

Side note: I am belgian so I wrote the sides in dutch but you can always message me if you can't figure out which is which side.

Creating a Website and Database

198889310_503338450990592_4988086115391165549_n.png

You'll need two programs in order to actually use my code. The first one is Visual Studio code and the seconde one is MySQLWorkbench.


Setting up Visual Studio:

  • Start Visual Studio Code
  • Install the “Remote-SSH” extension
  • Press F1 and type “SSH”. Chose option: “Remote-SSH: Add New SSH Host”
  • Type “ssh YourUsername@192.168.168.168 -A”
  • Chose the option to save the file
  • Press F1 and type “SSH”. Chose option: “Remote-SSH: Connect To Host”
  • Chose 192.168.168.168
  • A new window will be opened and ask you to write down your RPI password so do that
  • Now VS will open your connection (this can take a while)

Creating a Database (:

  • Open MySQLWorkbench
  • Create a new connection
  • Chose the option: “Standard TCP/IP over SSH” at Connection Method
  • SSH Hostname: “YourRPIIpAddress”
  • SSH Username: “YourRPIUsername”
  • SSH Password: “YourRPIPassword” and save it
  • MySQL Hostname: 127.0.0.1
  • MySQL Server Port: 3306
  • Username: “YourRPIUsername”
  • Password: “YourRPIPassword” and save it
  • Now open your connection
  • Go to navigator and click on Data Import/Restore
  • Click on Import from Self-Contained File and search for your dumpfile

Open Website:

After setting up you should be able to click on the green triangle in the file "App.py". This wil make sure the backend is running. Now u can open the webpage by surfing to 192.168.168.168.

If everything goes correctly you'll see the same thing you see in the picture.

Automation

We still need to do a few things to make sure that the back end automatically starts every time you start up.

Go to the terminal of your pi in Visual Studio and enter following command:

Sudo nano /etc/system/system/NightLight.service

Save with Ctrl + O and exit with Ctrl + X

Copy the text below and paste it into the terminal.

[Unit]

Description=ProjectOne NightLight

After=network.target

[Service]

ExecStart=/usr/bin/python3 -u /home/student/2020-2021-projectone-KeyngnaertLotte/Code/Backend/app.py

WorkingDirectory=/home/student/2020-2021-projectone-KeyngnaertLotte/Code/Backend

StandardOutput=inherit

StandardError=inherit

Restart=always

User=student

[Install]

WantedBy=multi-user.target


Now enter these commands:

  • Sudo systemctl daemon-reload
  • Sudo systemctl enable NightLight.service
  • Sudo systemctl start NightLight.service
  • Sudo systemctl status NightLight.service
  • Sudo reboot

Wait for a few minutes and the service will start.

Ending

Hopefully you were able to recreate my project without any problems. If you do have problems you can always comment below. I will try my best to respond as quickly as possible.

Also if you have any suggestions, feel free to leave a them in the comments.

Kind regards,

Lotte Keyngnaert