We're sorry but this page doesn't work properly without JavaScript enabled. Please enable it to continue.
Feedback

What Symfony Has To Do With My Garage - Home Automation With PHP

00:00

Formal Metadata

Title
What Symfony Has To Do With My Garage - Home Automation With PHP
Title of Series
Part Number
16
Number of Parts
59
Author
License
CC Attribution - NonCommercial 2.0 Germany:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor.
Identifiers
Publisher
Release Date
Language

Content Metadata

Subject Area
Genre
Abstract
What Symfony Has To Do With My Garage - Home Automation With PHP Raspberry Pi is a perfect mini computer to experiment with, small, inexpensive, low energy consumption and easy to expand. This talk, using the example of a Symfony app as a remote control for a garage door drive, shows that it's possible to write hardware drivers in PHP and how to easily use the Raspberry Pi along with the PiFace Digital Expansion Board for home automation. ······························ Speaker: Jan Unger Event: FrOSCon 2014 by the Free and Open Source Software Conference (FrOSCon) e.V.
Keywords
3
Thumbnail
53:13
53
Thumbnail
54:04
54
57
Thumbnail
1:04:46
Open sourceFreewareOpen setSoftwareMUDComputer hardwareDigital photographyGoodness of fitEvent horizonRevision controlContext awarenessClosed setPoint (geometry)Moment (mathematics)Mechanism designField (computer science)Range (statistics)Motion captureMereologySoftwareMultiplication signCuboidCoefficient of determinationIntegrated development environmentEndliche ModelltheorieNumberCurvatureForm (programming)CoprocessorWebsiteMikrocomputerTable (information)AreaFunction (mathematics)outputOperating systemWhiteboardSmartphoneSound effectBoss CorporationInheritance (object-oriented programming)Thermal expansionGame controllerComputerPower (physics)ChainBasis <Mathematik>Volume (thermodynamics)Contrast (vision)Physical systemBranch (computer science)Object (grammar)Remote administrationCoordinate systemOpen setGroup actionNear-ringMetropolitan area networkBitReading (process)ArmComputing platformPlastikkarteSerial portPersonal identification number1 (number)Web applicationInterface (computing)PeripheralComputer hardwareBus (computing)WordApproximationAssembly languageView (database)XMLUMLLecture/Conference
SoftwareComputer hardwareRAIDConvex hullExecution unitAnnulus (mathematics)8 (number)Web pageLevel (video gaming)Cartesian coordinate systemInternetworkingGame controllerInterface (computing)Group actionSubject indexingSoftwareModule (mathematics)State of matterComponent-based software engineeringPatch (Unix)Digital photographySingle-precision floating-point formatEndliche ModelltheorieSocial classVideo gameService (economics)WeightContext awarenessPrice indexAbstractionComputer-assisted translationProjective planeDependent and independent variablesSpeicheradressePersonal identification numberLibrary (computing)WindowMereologyPhase transitionSmartphoneBitAddress spaceCodeBus (computing)Functional (mathematics)Natural numberData structureInstance (computer science)Order (biology)Moment (mathematics)PiMultiplication signPhysical systemRepresentation (politics)Function (mathematics)outputWeb pageComputer hardwareProxy serverCASE <Informatik>Point (geometry)EmailOpen setFocus (optics)Mobile appLink (knot theory)Whiteboard2 (number)Video game consoleWebcamLecture/Conference
Convex hullAnnulus (mathematics)Numeral (linguistics)GUI widgetAndroid (robot)Control flowLink (knot theory)Metropolitan area networkConnected spaceGame controller2 (number)Open setComputer hardwareQuicksortFocus (optics)Link (knot theory)Medical imagingWeb pagePulse (signal processing)Cartesian coordinate systemWebcamLogicLibrary (computing)Point (geometry)Group actionSoftware developerReduction of orderRight angleDifferent (Kate Ryan album)Rule of inferenceView (database)Component-based software engineeringLoop (music)Server (computing)Process (computing)Range (statistics)Multiplication signState of matterModule (mathematics)DemonReading (process)Execution unitDialectBitRadical (chemistry)Hash functionExterior algebraForm (programming)TransponderQueue (abstract data type)WindowVideo game consoleSmartphoneMobile appMaterialization (paranormal)Android (robot)AbstractionEmailReal numberOcean currentTunisVideo gameVideoconferencingPrice indexSingle-precision floating-point formatGUI widgetWeb 2.0Stack (abstract data type)Machine codeWeb applicationDigital photographyStructural loadLecture/Conference
WaveProduct (business)Slide ruleWeb 2.0Control flowGUI widgetTwitterEmailVibrationConfiguration spacePoint (geometry)Thermal expansionSlide ruleWebcamProduct (business)Computer hardwareStreaming mediaSpacetimeClosed setMechanism designGame controllerEmailCrash (computing)Exterior algebraKinematicsLink (knot theory)State of matterOpen setWordNeighbourhood (graph theory)Sheaf (mathematics)WhiteboardTheory of relativityGreatest elementDigital photographyCASE <Informatik>Proof theoryFood energyVariety (linguistics)Computing platformProjective planeVideo gameInsertion lossDegree (graph theory)Metropolitan area networkRange (statistics)AreaContent (media)Physical lawArithmetic meanPublic domainGraph (mathematics)Wireless LANSummierbarkeitComponent-based software engineering1 (number)Presentation of a groupRiflingCoefficient of determinationLecture/Conference
Computer animation
Transcript: English(auto-generated)
So, good morning everybody. I hope you all slept well. I wasn't here at the social event yesterday but I saw photos of the PHP table outside in the social area and I guess there was some hard stuff going on as far as the photos told me.
Okay, what Symphony has to do with my garage. I'm going to tell you a little story of what I did in the last month. The big headline is home automation with PHP.
A few words about me. My name is Jan Umer. I'm a freelancer living in the Rhein-Mein area near Frankfurt. I'm doing web applications mostly with PHP and JavaScript. PHP since 14 years now. Back in 2000 I was young and needed the money. Nowadays I'm old and still need the money.
If you have any questions don't hesitate. Just shout out. Try to answer them instantly. The object. What I'm going to talk about is this garage. My garage at home.
It's a sectional door. The electric drive is already in place. The view from inside is like this. As you see up here we have a box with a motor in it and inside this metal beam there is a chain quite similar to a bicycle chain you might know. Attached to this chain at the front is another small metal beam and this metal beam is attached to the door.
The motor drives this chain and thereby pulls the door back and forth. This was already in place and my intention was to get this thing accessible via anything like HTTP.
There already was a radio remote control. This thing here attached to my car key. This remote control was one of the main reasons why I started all this. I used to have this remote control in my pocket and from time to time without noticing I accidentally pushed the button.
The door goes open and I didn't notice. The problem is, as you see in the photo, this here is the door where you can go upstairs to our flat. There is a hole in the wall between the garage and the stairs and when the door is open you can go straight away up to the flat.
Which is obviously not what I want. This was my motivation number one. The other thing I had from time to time, I drove away in the morning deep in thoughts and two corners later I thought,
have I closed the fucking door? I don't remember. Yeah, and this is the motivation for all. You have another thing in place. This is a button at the wall which you can just press. It's wired up to the motor. Press the button and the motor goes up.
The hardware. You might all know Raspberry Pi which is the base for my little thing I built. Who knows Raspberry Pi? For the others it's a microcomputer approximately the size of a banking card.
It's a platform based on an ARM processor, quite similar to what you might have in your pocket as a smartphone. The big thing about this is it's quite cheap, about 30 euros and it's easy to expand.
This here is the Raspberry and it has an expansion board GPIO, General Purpose Input Output. Five of these pins implement something that is called SPI, a Serial Peripheral Interface Bus. It's a de facto standard invented by Motorola for a synchronous serial in and out.
To make this GPIO port accessible for me, I'm not that deep in electronics. I used another thing, this expansion board. It's called PiPhase.
It costs another 30 euros and it utilizes this SPI bus that comes out here and brings us eight input ports and eight output ports where you just can plug in two wires. If the wires are connected, the electronic can tell the input port is connected. If they are apart, the input port is open. The other way around is just the same.
If you tell the software close this port, then those two pins get connected. If you tell the software open the port, the two pins get apart. Another nice thing which I didn't use but which makes it quite usable for home automation,
is that you can stack up to four of these expansion boards onto one Raspberry Pi. So you have 32 inputs and 32 outputs with a quite affordable amount of money.
The software base is on the Raspberry. The operating system is called Raspbian. It's an ARM version of Debian Easy. So if you know Debian, you're good to go. It's a fully operable computer which you have. It has HDMI output, it has two USB ports, it has a network interface, audio in an output
and the persistence is done by an SD card plugged to the board. And this is how it looks when it's assembled.
This Pi face is just stacked upon the Raspberry and it's quite as big as a cigarette box, approximately. If you want to have a look on it, I have one with me. I have it here.
If you're interested, just come to me after the talk and take a look at it. Stealing it has no point, it's broken. I somehow managed to break it. Okay, the sensors. I want to tell if the door is open or closed or anything in between.
And to reach this I have two things that are called read contacts. A read contact is basically a small capsule. Inside this capsule there are two metal plates and if they get in the range of a magnetic field,
they close to each other and provide an electrical contact. Which means we have a magnet which I'm going to attach to the door. This magnet is going to move back and forth with the door and somewhere we have those sensors and if the magnet approaches these sensors, I get a contact at these two wires
and just plug them into my Raspberry and so I can tell where the door actually is. Okay, so far. As I said, any questions just shout out. Okay, this is the installation. Still looking a bit like a prototype, which it is actually.
The Raspberry mounted in some common installation box. We have an Ebonet uplink here and we have power voltage and we have some wires to the sensors and the actor.
The actor is the thing mounted on the wall. You remember the switch? I just attached two additional wires to this switch coming out of the relays here. Which means I can tell the software closed the relay. It's as if I pushed the button and opened the relay, releasing the button.
This is the situation in front at the door where the drive approaches the door and here is where I mounted this one reed contact. You get a bit closer, it looks like this. This is the capsule with this reed contact.
This is the magnet attached to this metal beam moving back and forth with the door. And on the other side, backwards, there's the same, this is the reed contact and the magnet comes up here and stops right in front of this sensor.
Another thing I have in place, because I'm a control freak, is a webcam, which was in fact the first step in this little project, where I could tell if the door is open or not. The thing is, the camera won't tell me, hey, the door is going up, have a look what's happening here.
This is the view down from the camera and as you can see, we have a direct link here from the garage up to the stairway up to a flat. The software is a small single page application which can easily be accessed via a smartphone.
It has a status indicator which tells me the door is closed, the door is open or the door is somewhere in between, supposedly moving at the moment.
We have a live photo from the garage and we have a button where we can tell the drive, go. This is the basic structure of the application. The container is a symphony app, mostly overkill, but another intention which I had in mind was to tell if a Raspberry is suitable for running symphony applications.
In fact it is. It's a bit slow but still usable, as long as you don't have too many users at the same time. I'm the only user and it's quite okay. We have four functional components.
We have a door state indicator, which we saw here, which is able to tell whether the contacts are closed or open. We have a motor trigger which takes post requests from the single page app and the motor go ahead. We have the camera proxy delivering the photo from the webcam.
Another thing in place is a watchdog, which is a console command, running endlessly, which reads the contacts every second. If the state changes, which means the door is up to move, it's going to send me an email.
This was another solution I would like to have, to be informed when the door opens without me doing anything about it. The camera proxy was necessary because the camera itself has an HTTP interface
but no SSL in place. Sending credentials over the internet is probably not the best idea, at least in plain HTTP. This is what this camera proxy is for. It pulls a photo and sends it out via another channel. I didn't invent the wheel myself, as you might guess.
There were two main components in place which helped me to solve my problems. The first one is this component, PHP-SPI. It is a PHP module implemented in C. Some guy, much smarter than me, did this.
It makes it possible to access the hardware via PHP. This is where the magic happens. It provides a PHP interface, a class called SPI, which models this hardware access.
The second thing that made it much easier to access the hardware is this project from a guy from Norway. It's an abstraction layer above this SPI module where you can simply access a method that reads and writes values
to the single pins on the Raspberry. This actually is a PHP part of the original library from the vendor of Pyface. It's that one here, and this one is done in Python. You might say, okay, you could also have used the original Python library,
but this is not what I wanted to do. My point was to get to know if it works with PHP, and it worked out quite well, as we're going to see. Okay, let's have a look at the code that we have here. First and foremost, there is this driver class.
As you can see, there's many stuff going on here with hexadecimal addresses. I didn't dig in that deep, I must admit. I just was happy that all of that works. If you like, it's all on GitHub, and you can read afterwards what's happening here.
The thing is, we have an API that allows us to read bits from the SPI bus and to write values to other bits at this SPI bus.
My hardware uses this. Just a second, where have I? There it is. This is the topmost abstraction layer of the Pyface board, which I've built.
When it creates an instance, it gets itself eight representations of the input pins and eight representations of the output pins. All what these pins do, they know I'm attached to this board and to this pin number,
and they can tell the value of the actual pin, and they can write the value in case of an output pin to the appropriate memory address. The application itself is rather simple.
Do you all know Symfony? At least at a basic level, who knows Symfony? The basic thing is, you have a front controller dispatching requests to specialized controllers in the application.
In my application, we have four of these controllers. We have an index action, index controller, which delivers the static HTML for the single-page application. We have another controller in place for the door state action,
which gets a service from the service container and is able to tell whether the door is open, the door is closed, or the door is moving. It sends back a JSON response, which the single-page application is able to process. The next controller is this trigger action, which gets a post request
and tells the door, the motor, go ahead, move. The last controller we have in place is the snapshot action for the webcam, which pulls an actual image from the camera and sends it out to the requesting application.
As you remember, we had another thing in place, this watchdog. It's this one here. It's a console command. Symfony provides console commands, which you can run on the server. This one here is simply running a while loop endlessly,
where it executes a so-called watchdog component. The watchdog component reads the sensors, tells the state, and if the state changes, it sends out a mail. This mail is queued up. Symfony has a mail queue. After executing the watchdog, this mail queue is flushed and the mail actually is sent out.
From time to time, because PHP and long-running process is not that good idea, this thing terminates itself every 24 hours and gets restarted by a supervisor daemon, which takes care that this process is started if it terminates for whatever reason.
I originally intended to do a live demonstration with this thing here, but as you know, it's broken.
Obviously, I can't bring my garage here and I can't take you to my garage to show you how this works in real life. I did a little video where you can see what happens.
This is the garage. When I go downstairs in the garage, I open my smartphone, push the button, the relay triggers, the motor starts moving.
You see the magnet leaving the read sensor, the door opens. The read sensor on the other end, the magnet reaches the other end, the read sensor at the other end,
pushing the button again, the relay triggers, the motor starts moving, the magnet reaches the sensor,
and my application tells me the door is closed, everything's fine.
Okay, this is the current state as it's currently running in my garage. I have some things in mind I want to try out with this. First of all, some performance tuning. As I said before, Raspberry is not the real performance hardware.
Running a full LAMP stack on it with a Symfony application is okay. It's not perfect. I'm going to try out a few alternatives, throwing out the Symfony stuff and using plain PHP, using Nginx and live HTTP as a web server, which might help a little.
Let's see, I'm just throwing out a few things. The other thing I have in mind is a feature, automatic closing, which allows me to go to the garage, open the door,
drive the car out and don't care how the door closes, it will close by itself. I could do this just by telling okay, the door is open for 15 seconds now, the car should be out. Let's close the door. Might work. Might be too fast, who knows.
I already have some hardware in place to solve this. I have a photo sensor at the height approximately of the car bumper. And if this photo sensor, if the light beam gets disconnected, we have again the situation where we have two wires coming out of this.
If the light beam is connected, the sensor is closed. If the light beam is disconnected, which means the car goes through, the sensor is disconnected. And thereby I can tell okay, the light beam has been disconnected for five seconds. It has been connected again. Okay, I think the car is out, we can close the door.
It depends, I'm going to do a few, a little research on that, how long it's disconnected when the car goes through, compared to how long it's disconnected when a person walks through. I'm not quite sure about that, I haven't tried it out yet, but I'm going to.
It's a point I had in mind and I'm not sure if it works, let's see. Another thing I have in mind is trying out WebSockets. WebSockets currently, my single page application, pops every two seconds, this data indicator.
The image gets updated every ten seconds, which causes quite a bit load on the Raspberry. And by having WebSockets, I would be, this problem would be solved, I would have an open connection to the server and the server would be able to push a notification as soon as something happens,
which might be faster than the actual polling solution. Another thing I'm going to try out is an Android widget instead of the web application. I've never done Android development before, that might be a good point to start.
A native application that talks to my door, let's see how it works. Voice control is a point, I've seen a video of a guy in the US who has done quite a similar thing. The main difference is he hasn't built a web application, but he talks to Siri.
He tells Siri, open the door, and magically the garage door starts moving. I'm going to try this also. And the last thing I'd like to try out is iBeacon, which means having a Bluetooth transponder in the range of the garage.
And if the smartphone comes in range of this Bluetooth transponder, the Android app might fire up and say, hey, we're in the garage, shall I open the door? You see, it's a nice playground for big boys.
OK, I collected some links, if you want to dig in deeper. First of all, this is my application, which I built. This garage door control is the Symfony application, it's on GitHub. There's also the library which abstracts the SPI bus, the PHP module for accessing the SPI bus,
and some materials for further reading. This is the documentation of the window of this Pi Faceboard, and some more stuff if you want to dig in deeper in Raspberry Pi and this GPIO port.
I've put up the slides at SlideShare, I'm going to tweet the link afterwards. If you would like to read, go ahead and grab it. There are quite some alternatives out there. Raspberry Pi is not the only thing to use if you want to do a home automation. There is stuff called Z-Wave and a related project, Raspberry, I don't know how to pronounce it correctly.
It's similar, but it's wireless, which means you have an expansion board on the Raspberry with some kind of wireless controller talking to hardware over the air. One wire, as far as I know, is something similar, I don't know much about that.
A friend of mine played around with Z-Wave and he did a garage drive lately, which seems to work quite well. Another alternative might be Arduino.
I don't know much about Arduino, but we have a talk today, as far as I remember, in the evening, with Thomas Wienert. Oh, it was yesterday, sorry. But he may be still around if you'd like to have some words about Arduino, talk to him.
Of course, we have a big variety of commercial products for home automation, I don't know much about them, but I would guess they might be more expensive than this do-it-yourself solution. Short recap, the electric door drive was already present, a common one, like you can buy it at the hardware store.
Things I bought myself were these two reed switches for about 12 euro each, some cables, the Raspberry, which is about 30 euros, the expansion board, 30 euros. So with 90 euros I have my own home automation project, which I can play around with.
Home automation can be easily done as a build-yourself project, at least for me this was proof enough. The Raspberry Pi works well as a platform for that. It's compact, it's inexpensive, has a small energy footprint, and it's easy to extend.
Okay, and now I'm happy to take your questions, if there are any left. The question was if I considered to use an expansion camera for the Raspberry Pi, attached directly at the Raspberry Pi.
In fact, yes I did, but in my case the camera was in place long before the Raspberry, because as a first quick solution to tell whether the door is open or not, I just wanted to have access to a photo. So I had this web camera already and just put it up there.
The Raspberry is mounted at the wall, but not at a place where it can look at the door. This is the point why I didn't consider, but this might be a point also, yes, using a camera attached to the Raspberry.
I also have a question for the hardware configuration. If you could talk correctly, the standard telling if it's open or closed, it's more like vibrating. You can only say open or closed, right? If it's half open or half closed, or almost closed, it's impossible.
For other applications, in particular in my tradition, the kids put something in the garage, and they put it not far enough in the garage, then the door comes closed, and then there's an internal mechanism which is stopping, and then it's half closed, or big closed.
This is, from my perspective, a very critical situation. So you have seen the door closing down, but then there's a space in between, and everybody gets peaceful. Maybe in winter, with snow and ice, you have another situation.
Have you thought about something to solve this problem? I'll just repeat the question for the stream. The question was the sensors, if they are able to tell whether, if they currently, put the answer right into it. Currently, the sensors are just able to tell whether the door is closed, completely closed, or completely opened,
as we have a punctual sensor, which can tell the magnet is in range, or it is not. I cannot tell where the door is when it's moving in between. I might get into the same situation as you described it, that something is in the way of the door.
The door crashes into some kind, and it stops. The electrical drive is designed to stop if it reaches something in its way. In my case, it's not that big a problem, as I get an email as soon as the door opens,
and I get another email as soon as the door closes. Closing means closing completely. If it stops underways, the state of the door is currently moving. The sensors can't tell if it's actually moving, but they know it's not completely opened, and it's not completely closed.
And that's enough for me in that case. About 15 centimetres, 10 centimetres. It might be enough to leave some centimetres of the door open at the bottom, in edge cases, yes.
I haven't had that case yet, but it would be not that much of a problem, as those 5, 10 centimetres won't be enough for someone to break into my house.
And this was my main point, which we didn't see here. The kinematic of the door is so that the door reaches the bottom of the garage long before this metal beam arrives here.
It's hard to describe. It's a sectional door, and the topmost section gets tilted on the last 20 centimetres of this way. On the last 20 centimetres of this way, the bottommost section is already on the ground of the garage.
So what could happen if it stopped here, say, that the top of this door stays open for, say, 10 centimetres. Could be a problem if you have a neighbourhood who is willing to throw something in, but in my case it's not the point.
OK, some more questions. 3, 2, 1. Thank you.