Phpiwire: a PHP extension (written using Zephir) for controlling the Raspberry Pi GPIO

So it’s been a while since my lightening talk on Zephir and I realized I hadn’t really done anything to try to learn it a little more in-depth.  Coincidentally, I was recently going through a drawer and realized that one of my Raspberry Pi’s was in there just begging to be put to some use (seriously, I could practically hear it weep).  So I thought that the only thing to do was to attempt to write an extension using Zephir so that I could control the GPIO via PHP.  I mean, isn’t it obvious?

I really didn’t want to write the heavy duty, nitty-gritty stuff myself but thankfully I didn’t have to!  There’s a great C library called wiringPi by Gordon Henderson that does all of the hard stuff which made it possible for me to just tinker around with Zephir and see what could be done. And with that, here’s introducing…

Phpiwire

Phpiwire is really just a wrapper for wiringPi, but it allows you to create a PHP extension in a very easy to understand way.  It’s very early stages for the extension – I really haven’t even tested most of the methods I have in there, but it does blink an LED if nothing else! 😉 And here’s the code required to do just that:

<?php

namespace Phpiwire;

set_time_limit(0);

$pi = new Board();
$p = $pi->getPin(0)->mode(Pin::OUTPUT);

while (true) {
    $p->write(Pin::HIGH);
    sleep(1);
    $p->write(Pin::LOW);
    sleep(1);
}

So if you’re interested in seeing something simple using zephir, or controlling the Raspberry Pi GPIO via PHP, then hop on over and fork on GitHub, and please feel free to give me any feedback.

 

Did you like this? Share it:

Leave a Reply