Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Commit

Permalink
Fixed communication between webserver and raspberry pi. Updated the s…
Browse files Browse the repository at this point in the history
…ave function at the user panel and placed the form in the header and footer so you dont have to included it each page. All our pages are forms anyway...
  • Loading branch information
Luuk van Houdt committed Jan 28, 2016
1 parent 7834c59 commit 86f47d4
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 60 deletions.
13 changes: 2 additions & 11 deletions html/admin.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
<div class="row border thick">
<h1>Beheerders paneel</h1>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
<button name="logout" type="submit">Uitloggen</button>
</form>
<button name="logout" type="submit">Uitloggen</button>
</div>
<div class="block border thick">
<h2>Domotica systemen</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input name="city" type="text" placeholder="Stad" />
<input name="street" type="text" placeholder="Straat" />
<input name="house" type="text" placeholder="Huisnummer" />
<button name="search" type="submit">Zoeken</button>
</form>
<?php
if (isset($_POST['search'])) {
$q = $pdo->prepare('SELECT *
FROM systems
WHERE city LIKE :city AND
street LIKE :street AND
house LIKE :house
');
$q = $pdo->prepare('SELECT * FROM systems WHERE city LIKE :city AND street LIKE :street AND house LIKE :house');
$q->execute(array(
':city' => '%'.$_POST['city'].'%',
':street' => '%'.$_POST['street'].'%',
Expand Down
18 changes: 6 additions & 12 deletions html/login.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<div class="block border thick">
<h1>Inloggen</h1>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
<p>
<input type="text" name="username" placeholder="Gebruikersnaam" />
</p>
<p>
<input type="password" name="password" placeholder="Wachtwoord" />
</p>
<p>
<button type="reset">reset</button>
<button name="login" type="submit">login</button>
</p>
</form>
<input type="text" name="username" placeholder="Gebruikersnaam" />
<input type="password" name="password" placeholder="Wachtwoord" />
<p>
<button type="reset">reset</button>
<button name="login" type="submit">login</button>
</p>
</div>
73 changes: 37 additions & 36 deletions html/user.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
<div class="block border thick">
<div class="heading border thick">
<h1>Gebruikerspaneel</h1>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
<section class="dotted">
<ul>
<li><strong>Woonplaats:</strong><span><?php print $_SESSION['system']['city']; ?></span></li>
<li><strong>Straatnaam:</strong><span><?php print $_SESSION['system']['street']; ?></span></li>
<li><strong>Huisnummer:</strong><span><?php print $_SESSION['system']['house']; ?></span></li>
</ul>
</section>
<section class="lights">
<h2>Verlichting</h2>
<?php
$lights = $_SESSION['system']['lights'];
$lights_class = ($lights==1) ? ' active' : '';
?>
<div class="switch<?php print $lights_class; ?>">
<a class="off">uit</a>
<a class="on">aan</a>
<input name="lights" type="hidden" value="<?php print $lights; ?>" />
</div>
</section>
<section class="camera">
<h2>Camera</h2>
<?php
$camera = $_SESSION['system']['camera'];
$camera_class = ($camera==1) ? ' active' : '';
?>
<div class="switch<?php print $camera_class; ?>">
<a class="off">uit</a>
<a class="on">aan</a>
<input name="camera" type="hidden" value="<?php print $camera; ?>" />
</div>
</section>
<button name="logout" type="submit">Uitloggen</button>
<button name="save" type="submit">Opslaan</button>
</form>
<button name="logout" type="submit">Uitloggen</button>
</div>
<div class="block border thick">
<h2>Systeem</h2>
<section class="dotted">
<ul>
<li><strong>Woonplaats:</strong><span><?php print $_SESSION['system']['city']; ?></span></li>
<li><strong>Straatnaam:</strong><span><?php print $_SESSION['system']['street']; ?></span></li>
<li><strong>Huisnummer:</strong><span><?php print $_SESSION['system']['house']; ?></span></li>
</ul>
</section>
<section class="lights">
<h2>Verlichting</h2>
<?php
$lights = $_SESSION['system']['lights'];
$lights_class = ($lights==1) ? ' active' : '';
?>
<div class="switch<?php print $lights_class; ?>">
<a class="off">uit</a>
<a class="on">aan</a>
<input name="lights" type="hidden" value="<?php print $lights; ?>" />
</div>
</section>
<section class="camera">
<h2>Camera</h2>
<?php
$camera = $_SESSION['system']['camera'];
$camera_class = ($camera==1) ? ' active' : '';
?>
<div class="switch<?php print $camera_class; ?>">
<a class="off">uit</a>
<a class="on">aan</a>
<input name="camera" type="hidden" value="<?php print $camera; ?>" />
</div>
</section>
<button name="save" type="submit">Opslaan</button>
</div>
1 change: 1 addition & 0 deletions inc/footer.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
</form>
</body>
</html>
76 changes: 75 additions & 1 deletion inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,79 @@
}

if (isset($_POST['save'])) {
var_dump($_POST);
// Get the new value
$lights = (isset($_POST['lights'])) ? $_POST['lights'] : 0;
$camera = (isset($_POST['camera'])) ? $_POST['camera'] : 0;

// Update the database with the new value
$q = $pdo->prepare('UPDATE systems SET lights = :lights, camera = :camera WHERE user_id = :uid');
$q->execute(array(
':lights' => $_POST['lights'],
':camera' => $_POST['camera'],
':uid' => $_SESSION['user']['user_id'],
));

// Update the session with the new value
$_SESSION['system']['lights'] = $lights;
$_SESSION['system']['camera'] = $camera;

// Get the user's password (hash)
$q = $pdo->prepare('SELECT password FROM users WHERE user_id = :uid');
$q->execute(array(
':uid' => $_SESSION['user']['user_id'],
));
$r = $q->fetch();

// Inform the related system with a POST request
$ip = rtrim($_SESSION['system']['ip']);
$port = rtrim($_SESSION['system']['port']);
$url = 'http://'.$ip.':'.$port.'/execute';
$data = array(
'user' => urlencode($_SESSION['user']['username']),
'pass' => urlencode($r['password']),
'lights' => urlencode($_SESSION['system']['lights']),
'camera' => urlencode($_SESSION['system']['camera']),
);
$data_string = '';
foreach($data as $key=>$value) { $data_string .= $key.'='.$value.'&'; }
rtrim($data_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
$result = curl_exec($ch);

// Redirect back to the form
header('Location:'.$_SERVER['REQUEST_URI']);
}

if (isset($_POST['api']) and !empty($_POST['user']) and !empty($_POST['pass'])) {
$q = $pdo->prepare('SELECT user_id FROM users WHERE username = :user AND password = :pass');
$q->execute(array(
':user' => $_POST['user'],
':pass' => md5($_POST['pass']),
));
$current_user = $q->fetch();

// Handle the on boot api call
if ($_POST['api'] == 'boot' and $current_user) {
$q = $pdo->prepare('UPDATE systems SET ip = :ip, port = :port WHERE user_id = :uid');
$q->execute(array(
':ip' => $_POST['ip'],
':port' => $_POST['port'],
':uid' => $current_user['user_id'],
));
print('The system is ready to interact.');
}

// Handle messages send via the api
if ($_POST['api'] == 'message' and $current_user) {
$q = $pdo->prepare('INSERT INTO messages (user_id) VALUES (:uid)');
$q->execute(array(
':uid' => $current_user['user_id'],
));
$result = $q->fetch();
print('Message was successfully sent.');
}
die();
}
8 changes: 8 additions & 0 deletions inc/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
<html>
<head>
<title>Alpha Domotica</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/main.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<?php if (isset($_SESSION['status'])): ?>
<div class="status <?php print $_SESSION['status']['type']; ?>">
<?php print $_SESSION['status']['message']; ?>
</div>
<?php endif; ?>

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">

16 changes: 16 additions & 0 deletions py/boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from config import *
import urllib2, urllib, os

# Data send to the webserver
sendBoot = urllib.urlencode([
('api', 'boot'),
('user', username),
('pass', password),
('ip', ip),
('port', port),
])

# Send te boot message
boot = urllib2.Request(host, sendBoot)
boot.add_header('Content-type', 'application/x-www-form-urlencoded')
print(urllib2.urlopen(boot).read())
8 changes: 8 additions & 0 deletions py/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

# Configuration
username = 'guest'
password = 'guest'
host = 'http://192.168.178.23/repos/alpha-domotica/index.php'
ip = os.popen('ifconfig eth0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1').read().rstrip()
port = 4000
4 changes: 4 additions & 0 deletions py/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from config import *
from boot import *
from speaker import *
from listener import *
38 changes: 38 additions & 0 deletions py/listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from config import *
from bottle import request, post, run
from RPi import GPIO as GPIO
import hashlib

# Setup the hardware
theLight = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(theLight, GPIO.OUT)
GPIO.output(theLight, GPIO.LOW)

# Setup the webserver so we can receive data
@post('/execute')
def do_execute():
# Check if the login is valid
output = ""
if request.POST.get('user') == username and request.POST.get('pass') == hashlib.md5(password).hexdigest():
if int(request.POST.get('lights')):
GPIO.output(theLight, GPIO.HIGH)
output += "Lights ON"
else:
GPIO.output(theLight, GPIO.LOW)
output += "Lights OFF"

output += " and "

if int(request.POST.get('camera')):
output += "Camera ON"
else:
output += "Camera OFF"
else:
output = "Invalid login credentials."

print(output)

run(host=ip, port=port)

GPIO.cleanup()
41 changes: 41 additions & 0 deletions py/speaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from config import *
from time import sleep as sleep
from RPi import GPIO as GPIO
import urllib2, urllib

try:
# What will be send when the switch is triggered
sendMessage = urllib.urlencode([
('api', 'message'),
('user', username),
('pass', password),
])

# Use the numbering scheme
GPIO.setmode(GPIO.BCM)

# Define the PIN's
switchAlertA = 5
switchAlertB = 6

# Setup the GPIO's
GPIO.setup(switchAlertA, GPIO.IN)
GPIO.setup(switchAlertB, GPIO.IN)


while True:

# When the switch is triggered send a message
if GPIO.input(switchAlertA) or GPIO.input(switchAlertB):
message = urllib2.Request(host, sendMessage)
message.add_header('Content-type', 'application/x-www-form-urlencoded')
print(urllib2.urlopen(message).read())
sleep(5)

# Do not use all the CPU
sleep(0.1)

# Code execution is interrupted
except KeyboardInterrupt:
# Reset GPIO all settings
GPIO.cleanup()
Empty file removed py/webserver.py
Empty file.

0 comments on commit 86f47d4

Please sign in to comment.