Wi-Fi real-time home automation using Arduino and Pusher
WiringWiFly 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.
ArduinoThe 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).
My PHP endpoint is very simple:
Pusher.channel_auth_endpoint = 'presence_auth.php';
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.
$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);