header photo

Maciej Dendzik's

Personal page


Wi-Fi real-time home automation using Arduino and Pusher

Home automation and monitoring has been my interest from a long time. At first, I tried to set up a http server on my Arduino using Wi-Fly shield in order to be able to communicate with sensors and other hardware. It was quite easy as there is a library for that purpose. However, this approach is not particularly appealing, because you need to do a lot of network configuration (static ip assignment for Arduino, port forwarding and firewall customization). Moreover, my ISP blocks my ports making it even harder. Then, I came across Pusher service which was exactly what I was looking for.

Pusher is a web-socket real-time service working on port 80 (or 443 for secured connections), so you don't need to worry about any configuration of your home network. Clients can subscribe to public, private and presence channels. When one of the subscribed clients sends a message, the other subscribed clients are notified instantly by the Pusher. The Pusher library for Arduino has already been implemented, so it was quite easy to start pushing. With this approach, I got the access to temperature sensor and relays from outside of my home network.



WiFly and relay shields were designed for Arduino Uno. As I am using Arduino Mega, I needed to rewire couple of pins (13->52, 12->50, 11->51, 10->53, 5->21, 4->20). I have attached thermistor with an op-amp to pin 7 and push-button to pin 31.


The library for WiFly needs to be altered slightly in order to be compatible with the Pusher library by Kevin Rohling. Triggering client-client events is possible only over private-channels and you need to to turn this on in your Pusher account (Main->Settings->Enable client events). Marco Altomonte has implemented private channels support into Arduino Pusher library and further presence channel and WiFly support was added by me. You can get all the source files here (just remember to put your Pusher credentials into config.php, PusherClient.cpp and your home network credentials into credentials.h).

Website client

In order to communicate with my Arduino in real-time, I've decided to use client-client connection in JavaScript. You can also make a Pusher server in PHP, but then you would need to refresh the page to get the current readings from the sensor. Subscription to a presence channel need to be authenticated with the hex digest of your credentials (HMAC-SHA-256). For that, you need to make authentication endpoint and configure the path for that:

Pusher.channel_auth_endpoint = 'presence_auth.php';

My PHP endpoint is very simple:


$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
$user_id = uniqid();
$presence_data = array('name' => 'Client');
echo $pusher->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user_id, $presence_data);

If you have problems with connection to Pusher, check with some debugging tools whether the authentication JSON is correct (for Chrome DevTools go to Network->presence_auth.php->preview). I spend ridiculous amount of time to debug my code and it turned out that my host (000webhost) was appending my .php files with small script for page stats (I got rid of that by adding php_value auto_append_file none to my .htaccess file). The rest of the code is pretty straight-forward. Given functions are bind to specific events. All the rest is just a layout.




Widget is loading comments...