Skip to content

Instantly share code, notes, and snippets.

@YordiLorenzo
Created November 23, 2014 11:57
Show Gist options
  • Save YordiLorenzo/0c1ce9dce9c1bb056987 to your computer and use it in GitHub Desktop.
Save YordiLorenzo/0c1ce9dce9c1bb056987 to your computer and use it in GitHub Desktop.
Portfolio - Simplos Home Automation
/**
* Responder Facade for returning a Facade accesor that can be used in the iOC container
*/
<?php namespace Simplos\Facades;
use Illuminate\Support\Facades\Facade;
class Responder extends Facade {
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor() { return 'responder'; }
}
/**
* Responder class for generating simple API responses and loggin them
*/
<?php namespace Simplos\Response;
use Simplos\Models\Log;
class Responder {
/**
* Responsecode of the request
* @var Int
*/
private $responseCode;
public function respond(){
$this->setResponseCode(200);
return $this->log(["title" => "hoi", "message" => "hoi"]);
}
private function setResponseCode($code){
$this->responseCode = $code;
}
private function log($log){
return Log::create($log);
}
}
/**
* Simple Arduino + Ethernet Shield client that can receive GET requests and passes them through to the corresponding hardware ie (relays, leds, sensors etc)
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 29 }; // IP
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
String relayOff = "{\"status\" : \"OFF\", \"human_status\" : \"This device is currently offline\", \"code\" : 200}";
String relayOn = "{\"status\" : \"ON\", \"human_status\" : \"This device is currently online\", \"code\" : 200}";
String deviceStatus = "{\"status\" : \"ON\", \"human_status\" : \"Simplos HUB is online\", \"IP\" : \"192.168.0.29\" ,\"code\" : 200}";
const int relayPin = 9;
const int LDRPin = 0;
int ldrResistance;
boolean relayIsOn = false;
// 950 ~ 1000 : Pikdonker
// 900 ~ 949 : Schemer
// 850 ~ 899 : Daglicht
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(relayPin, OUTPUT);
pinMode(LDRPin, INPUT);
}
String lightReadable(int resistance){
if(resistance > 850 && resistance < 900){
String response = "Daglicht";
return response;
}
if(resistance > 900 && resistance < 950){
String response = "Schemer";
return response;
}
if(resistance > 950){
String response = "Nacht";
return response;
}
}
void loop() {
ldrResistance = analogRead(LDRPin);
EthernetClient client = server.available(); // Create a client connection
if (client) { // Check if client is available
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
}
if (c == '\n') { //if HTTP request has ended
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: application/json");
client.println();
delay(1);
/**
* Determine which response is given and what action is taken
*/
if (readString.indexOf("?status") > 0||readString.indexOf("&status") >0){
String begin = "{\"climate\" : {\"light_resistance\" : \"";
begin += ldrResistance;
begin += "\"";
begin += ", \"light_human_readable\" : \"";
begin += lightReadable(ldrResistance);
begin += "\"";
begin += "}";
begin += ",\"control\":{";
begin += "\"switch_one\": \"";
begin += relayIsOn ? "ON" : "OFF";
begin += "\"";
begin += "}}";
client.println(begin);
client.println();
client.flush();
client.stop();
readString = "";
break;
}
if (readString.indexOf("?lighton") >0 || readString.indexOf("&lighton") >0){
analogWrite(relayPin, 255);
relayIsOn = true;
client.println(relayOn);
client.flush();
client.stop();
readString = "";
break;
}
if (readString.indexOf("?lightoff") > 0||readString.indexOf("&lightoff") >0){
analogWrite(relayPin, 0);
client.println(relayOff);
relayIsOn = false;
client.flush();
client.stop();
readString = "";
break;
}
if (readString.indexOf("?device") > 0||readString.indexOf("&device") >0){
client.println(deviceStatus);
client.flush();
client.stop();
readString = "";
break;
}
}
}
}
}
}
/**
* Simple Laravel service provider for adding the Facade to the iOC container of the application
*/
<?php namespace Simplos\Serviceproviders;
use Illuminate\Support\ServiceProvider;
class SimplosServiceprovider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['responder'] = $this->app->share(function($app)
{
return new \Simplos\Response\Responder;
});
// Shortcut for dynamically adding Serviceprovider to app/config/app{providers}
$this->app->booting(function()
{
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Responder', 'Simplos\Facades\Responder');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment