Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 80 lines (65 sloc) 2.359 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
#include <PusherClient.h>

const char mySSID[] = "my$ssid"; //use $ to represent spaces in your ssid or passphrase
const char myPassword[] = "my$pass$phrase";
const char pusherKey[] = "abc123";

PusherClient client;

SoftwareSerial wifiSerial(2,3); //we will use these pins for debug output

int poweroutlet = 12; //pinout for the powerswitch

int led = 13; //status indicator led

WiFly wifly;

void setup() {
  //initialize our digital pinouts
  pinMode(poweroutlet, OUTPUT);
  pinMode(led, OUTPUT);

  //turn off the poweroutlet and led starting out
  digitalWrite(poweroutlet, LOW);
  digitalWrite(led, LOW);
  wifiSerial.begin(57600);

  Serial.begin(9600);
  if (!wifly.begin(&Serial, &wifiSerial)) { //start up the serial connection to our wifi, tell the WiFlyHQ lib to use our softwareserial for debug
    wifiSerial.println(F("Failed to start wifly"));
    wifly.terminal(); //if the wifly fails to start, give us access to a direct serial terminal to the RN-XV
  }

  if (!wifly.isAssociated()) { //check to see if we are already associated with the network before connecting
    wifiSerial.println(F("Joining network"));
    if (wifly.join(mySSID, myPassword, true)) { //using the true flag at the end of wifly.join indicates that we are using WPA
      wifly.save();
      wifiSerial.println(F("Joined wifi network"));
    }
    else {
      wifiSerial.println(F("Failed to join wifi network"));
      wifly.terminal();
    }
  }
  else { //if we are already associated with the network
    wifiSerial.println(F("Already joined network"));
  }
  client.setClient(wifly); //initialize the pusher client with our
  if(client.connect(pusherKey)) { //connect to our pusher account
    client.bind("powersms", powerSwitch); //bind the powersms event to the powerSwitch callback function
    client.subscribe("robot_channel"); //subscribe to our pusher channel
  }
  else { //if we fail to connect, just loop
    while(1) {
    }
  }
}

void loop() {
  if (client.connected()) {
    digitalWrite(led,HIGH);
    client.monitor();
  }

}

void powerSwitch(String data) {
  String cmd = client.parseMessageMember("command", data);
  wifiSerial.print("Command: ");
  wifiSerial.println(cmd);

  if (cmd == "on") {
    digitalWrite(poweroutlet,HIGH);
  }
  else {
    digitalWrite(poweroutlet,LOW);
  }

}
Something went wrong with that request. Please try again.