Monday, November 30, 2015

Introduction to Session Managment for Smart Home Server

Great. We are on Step 0 of our smart home automation project.

For this step i am expecting that you have either Linux or Mac.
We do need an ssh connection and also if you haven't already - do set up RPi to automatically start up the ssh on boot.

Why am i saying that we are on Step 0. Because lets face it - we haven't touched ANY programming or ANY electronics so far. That will, however, change from the next tutorial.  In this one we will get the Logic of what needs to be done and why are we doing it the way we will.

For this tutorial i am using this reference.
I'm going to say it right from the start. It sucks. It sucks donkeys ass.
Still i did fix it and now i am happy to say i think it doesn't suck that much.


NOTE: Even though i do know C,C++,C#, Java, Python,... from Excellent (C/C++/C#) To Really good (Python) to i wouldmanageinitifyouthroweditatme (perl,prolog) I HAVE NOT LEARNED PHP!! So i did have to learn it since (lets face it there ain't gonna be a server without PHP).
So i did look a quick glance at the syntax and started digging in the fun stuff. (My prior expertise in a lot of other programming languages helped of course for this to go smoothly)


 CODE IDEA AND WHY CREATE A WHOLE SESSION CLASS:
So i'm going to create a whole new session class and my own session handler functions since php native session_start() is a security hazard.The idea is:
session function communicates with a smarthomedb database and in that database we have a table called sessions. And a user who is authorized to write and read to and from that table. That way we can have a really good session control with MySQL.

Creating MySQL database:
 Go to phpmyadmin page of your RPi. Log in as root and type this in:


1
CREATE DATABASE smarthomedb;

We now have the database (with no tables!) created.
For the tables we do need: members, sessions, login_attempts.
As a bare minimum for now. Later on action table will be added (as to have control which user initiated which command on the smart home) and quite possibly some others.

But for now we will do the following:
1. Go to your phpmyadmin and login as root.
2. Click on your smarthomedb and look for the field 'Create table for database ...'
3. Insert name members and number of colums 7.
4. The names and the types are as follow:

id int(11) PRIMARY KEY (check the AI field - autoincrement)
uname varchar(30)
email varchar(50)
pass char(128)
salt char(128)
acc_permissions enum('a', 'b', 'c', 'd', 'e')
phone_no varchar(15)


Now do the same for the table named sessions - no. columns 4
Names and types as follow:

id char(128) PRIMARY KEY
settime datetime
data text
skey char(128)


Great now for the login_attempts with no. columns 3
Names and types:

id int(11)
time datetime
ip varchar(45)


We now have the database and tables created. We can start the coding.

NEXT: Session Managment for Smart Home Server

Sunday, November 29, 2015

Setting up the RPI 2 Server


For this i am assuming you already have RPi 2.

Install an OS on your RPi. Now here is the catch you need a monitor.
Simply borrow the monitor from your PC plug it in turn it on and put in your FORMATED SD CARD. Also if you haven't at least download some Linux distribution on a virtual machine if you at least don't have it as your primary | secondary OS.

OK you have your RPi plugged in with the monitor, your adapter in the power, your borrowed keyboard (&mouse) also AND your Ethernet cable connecting your RPi and your router.  Since RPi is so small and doesn't require much space you can leave it connected to the router AND hide it somewhere.

Installation done, now to install LAMP server. I have followed this tutorial.
It will work on RPi also. So

sudo apt-get update
sudo apt-get install apache2 mysql-server libapache2-mod-auth-mysql php5-mysql php5 libapache2-mod-php5 php5-mcrypt


Yeah next install the database.
sudo mysql_install_db
Ok, so far so good. We have the basics. But here is the catch.
We don't want to create the login mechanics available online at all times (as they might be exploited).
So we will have to (eventually) create a C++   program that will be used for registering users (or home members) locally and more securely. For that we need to install the necessary libraries to be able to communicate with MySql.
sudo apt-get install libmysqlclient-dev

But we want to use the C++ connector (and all the advantages of OOP)

apt-get install libmysqlcppconn-dev

OK, so we do have that installed. But still we will need phpmyadmin to be able to actually communicate with mysql databases in a better fashion than a command line.

To install phpmyadmin i used this link as a reference. 
Basically you need to install it.
sudo apt-get install phpmyadmin apache2-utils
Then you need to select the apache2 server on installation , insert your mysql password, enter the password you want to log in into phpmyadmin and choose to configure it with dbconfig-common.

Now after all that we need to tell apache to include the phpmyadmin configuration file for apache.

To do this  you first need to find your apache configuration file

On RPi that will be in /etc/apache2/apache2.conf

Add a line
Include /etc/phpmyadmin/apache.conf
Great. Now to get it to work
sudo service apache2 restart

And enter
yourraspberrypi_ipaddress/phpmyadmin

Mine was
192.168.1.3/phpmyadmin


SECURITY NOTE
This is all well and fine but phpmyadmin is not famous for its security.
And we want our security. Since we don't want everyone to be able to control our home devices. We will need to eventually lock down that folder with a username/password apache protection mechanism. That will be added in a later tutorial. 


NEXT: SESSION MANAGMENT FOR OUR HOME SERVER

Friday, November 27, 2015

Introduction to Smart Home Idea

                                            HOME AUTOMATION (Part 0)

In my first post i will introduce the idea of 'Smart Home', how i started to work on it and where and how i got the components for it.


NOTE: I am not a student of Electronics, i did study physics in high school - but i do not know the 'fine tunings' behind the formulas.
What i do know is Programming.
As any programmer i have my flaws: Commenting allot, Variable naming, clean code issues.

My perks are that i try to write as good as code as i can make.


Now that we have skipped the boring introduction we can continue.

Smart Home is (surprise,surprise) the idea of home automation.
Meaning turning the lights, as well as controlling electronics  over the Internet as well as having the status of electronics in the house ON the Internet.

Meaning - if i like my shades to be closed tomorrow when the sun hits - i can tie the fine tunings with photo-receptor and a micro-controller  and a small motor and that will do the job for me.
If i want my alarm to not stop until i grab my toothbrush i can arrange for that too.

So the limit is your imagination.
Couple of issues on the fly: What, How , WTF, HOW ?
Answers are somewhat simple : Arduino , Raspberry Pi.
And no they are not the same thing.

THE LOGIC
You see Raspberry Pi (RPi) is a small sized COMPUTER that is actually well fit to connect with electronic components such as this one.

Thats a Radio Frequency (RF) module that can be connected to both - Raspberry Pi AND Arduino.
And they can communicate.
Since RPi is a credit-card sized computer it can be turned on like a server (I haven't turned mine off in like a year) and actually can be hosting a website
for your home automation.

But since your IP address is probably not static but dynamic we will have to use something like this. There is actually a script that can change the DNS settings whenever your IP changes. So - credit card sized computer , hosting a website and communicating with RF modules with other Arduinos in your home.

OK, so far so good. Now those Arduinos will be connected with other electronic components such as Relays , Other RF modules, Motion Sensors,Photo-receptors, Fire-sensors, Gas-sensors (if you use butane gas),... Those will notify and 'take orders' from the Arduino, who will communicate with RPi who will synchronize 'orders from Internet' and 'orders from Home'.


THE CATCH

All those cost money. Original Arduino  Mega costs 35$+VAT. And we need a whole bulk of it. RPi also costs around 40$.

THE SOLUTION
I'm not promoting this in any way. But on Ebay and Aliexpress you have 'fake'
Arduinos. (I would suggest if you have the money to buy 1 original Arduino to support the project - even i have one). Now we can buy a bulk of them as i did: I bought Arduino Mega's for 8$ (instead of 35$),
                      5x Arduino UNO For 15.9$ (~3$ a piece) .
                      5x High Current Relays (30A , 250V AC or 30V DC)for 14$
                      10x 2channel Relay (10A 250V DC or 30V DC) for 12$
                                                 (i actually did not receive these)
                      A whole bunch of Sensors for around 1$.

Just write "inserttypeofsensorhere module arduino" and search on ebay or aliexpress and look what pops up.

Just be vary of cameras they know to be tricky if you find them cheap.

Also get Raspberry Pi 2 - its faster, better and not a whole lot more expensive.


NEXT: SETTING UP A SERVER on RPi (Part 1)