Beargardian

Introduction: Beargardian

About: I'm a student in NMCT at howest and I'm interested in web & app development. I speak Dutch, English and French.

Hey guys for school I needed an idea for a project. So I was thinking, it has to be a project with the raspberry pi and it's local. Suddenly I had an great idea and don't ask me how I get that idea but I thought about an upgrade for a baby monitor. Just think a second about that idea, the most baby monitors just have the function to listen to the baby's room.

The features

  • A little light show with adjustable colors
  • A camera that shows you live images
  • A speaker to play music
  • Sensors to capture the baby's moving
  • All that showing on a website

Short information

Letme explain this in a short version. So we need a website and for this project I'm using Flask, we also need a database and I'm using mysql, also a script that runs the hardware and this is with python(3) and as last we need a server setup that would be nginx on the PI.

What do we need

  • The Raspberry Pi 3
  • The stepmotor 28BYJ
  • The stepmotor driverchip ULN2003 stepper module
  • A rgb led with 3 resistors 330Ohm
  • The Pi NoIR camera V2
  • The ultrasonic sensor HC-SR04
  • The micro module from ardiuno
  • The MAX98357A
  • A speaker 8Ohm
  • And don't forget to buy a bear

Setup raspberry pi ---------------------------------------------------------------------------------------------------------------------------

At first we need to setup the Pi. Start already to login via putty, if you don't have putty I recommend you to download this, simply type your static ip of the Pi with ssh and you go with it. If you have to install your Raspberry Pi then I got bad news, I'm not explaining this in this project.

Install packages

sudo apt update
sudo apt install -y python3-venv python3-pip python3-mysqldb mysql-server uwsgi nginx uwsgi-plugin-python3

Virtual environment

python3 -m pip install --upgrade pip setuptools wheel virtualenv
mkdir {your project foldername} && cd {your project foldername}
python3 -m venv --system-site-packages env
source env/bin/activate
python -m pip install mysql-connector-python argon2-cffi Flask Flask-HTTPAuth Flask-MySQL mysql-connector-python passlib

Now you have to clone the git repository in your project folder

https://github.com/NMCT-S2-Project-I/Project-I.git

If you look in your project folder you have to see 5 folders

  • conf
  • env
  • sensor
  • sql
  • web

Database

sudo systemctl status mysql
ss -lt | grep mysql
sudo mysql

create a user in the database with all privileges and make your database

create user 'user'@'localhost' identified by 'password';
create database yourdatabasename;
grant all privileges on yourdatabasename.* to 'user'@'localhost' with grant option;

Conf files for server

In the uwsgi-flask.ini you change 'module = ...' to 'module= web:app' and the path to your virtualenv that you created. In the other files you need to change the paths to the actual absolute paths of your directory.

Once you figured that out you can set the files in the right place.

sudo cp conf/project1-*.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start project1-*
sudo systemctl status project1-*

now we have to set this available

sudo cp conf/nginx /etc/nginx/sites-available/project1
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled/project1
sudo systemctl restart nginx.service
sudo nginx -t

If everything did go well you shoud have hello world with this command

wget -qO - localhost

Done ! Well that's for the part to let run your system on...

Step 1: Wiring the Hardware to the Pi

using BCM

audio MAX98357A

  • BCK to GPIO 18
  • Data to GPIO 21
  • LRCK to GPIO 19

light

  • red to GPIO 17
  • green to GPIO 27
  • blue to GPIO 22

motor module ULN2003

  • pin 1 to GPIO 5
  • pin 2 to GPIO 6
  • pin 3 to GPIO 13
  • pin 4 to GPIO 26

micro

  • D0 to GPIO 21

ultrasonic sensor

  • trig to GPIO 16
  • echo to GPIO 20

Step 2: Coding the Main Programs

I'm not getting into details right here but you can checkout my code in github.

To begin with I made my html and css, a index, login, register, homescreen, music, addmusic, addbear, light, camera, camerasettings, sensor, dashboard page. The html files has to be in the templates and the css files in static/css folder. You can fully customize the css like you wish.

If you done this part you need to setup your flask. Flask is easy to use just an example of the hello world

# import flask at first
from flask import *

@app.route('/')
def index():
	return render_template('index.html')

Now in my code this is already filled in, the only thing you need to do is change the database user and password to that from you and ofcourse make the same database that you also can find in github.

Step 3: Creating the Database

For the real fans I'm going to tell you how to create the same database.

So first we need to create the database if you didn't in step one.

create database beargardian;

Once you did this you create the tables in mysql workbench or phpadmin

user table has

  • userID
  • firstname
  • lastname
  • email
  • babyname
  • password with sha1
  • userfolder
  • playmusic (int)
  • playlight (int)
  • playrecording (int)

music table has

  • musicID
  • song
  • path
  • userfolder
  • status
  • volume

recording table has

  • recordingID
  • path
  • userfolder
  • time
  • day

color table has

  • colorID
  • red
  • green
  • blue
  • brightness
  • userID

bear table has

  • bearID (decimal(8))
  • userID default null
  • bearname

sensor table has

  • sensorID
  • distance
  • micro
  • bearID
  • time
  • day
  • sleeptime

So now you've created the database succesfully, let's go to the hardware.

Step 4: Hardware Coding

I'll be showning a little bit of code and tell you why I did it that way.

To begin with I used threading, what a absolute must is in this project. What is threading, hmmm good question ! Well threating in python is to run multiple programs at once. So if you for example change the color you can also record. It's easy to use don't worry.

import _thread
def function_name(something,something_else): code to run
_thread.start_new_thread(function_name,tuple_with_the_functions_variables)

If you looked at my program you saw logger.info('...'). This is the print function but much better, because on the Pi you can't print stuff out so I make a file and print it in there. Yoe can set the log file with this code.

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO) # create a file handler handler = logging.FileHandler('logger.log') handler.setLevel(logging.INFO)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.info('start up hardware\n---------------------------------------')

further in the code itself I explain everything.

Step 5: Great Job

That's it ! You did it !

Share

    Recommendations

    • Tiny Home Contest

      Tiny Home Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Metalworking Contest

      Metalworking Contest

    Discussions