Sparkcore Smart Outlet Control

I wanted to build a “smart” web controlled outlet using the Sparkcore. The final idea for this would be to have all the electronics fit into a normal outlet box but to demo it I would just build a small platform that would hold a plug in socket that could be turned on and off via the REST interface of the Sparkcore. This project is potentially dangerous (120VAC) so make sure that you know what out are doing and double check everything before plugging anything in.

The normal wall outlet as shown has 3 poles and they tie into the 3 color-coded wires on a normal PC power cable. I chose the PC power cable because it has 3 poles and would be properly color-coded. It looks like this when you cut a PC power cable open:

The black wire is the “hot” one and can be very dangerous if not properly handled (be very careful). The wire connections are as follows:

Black wire (hot) – > common connection on relay 4 (middle pole)
NC (normally closed) on relay 4 – > brass screw poles (hot) on outlet
White wire (neutral) – > silver screw poles on outlet
Green wire (ground) – > green screw ground on outlet

As seen here (white and ground poles):

and here (hot wire and relay):

showing both sides:

For my example here I have both outlets connected so they turn on and off together, but you could easily separate the sockets and use 2 relays to control them independently. After I had it safely wired the socket and relay into the circuit I uploaded the following code to the Sparkcore (you should always deploy the code to the Sparkcore while it is not in the circuit first, to prevent pins being setup from previous deployments causing issues):

You can control the relays using cURL by executing the following in a terminal window (if you have cURL installed)

// all relays on
curl{DEVICEID}/relay -d access_token={ACCESSTOKEN} -d "params=1,1,2,1,3,1,4,1"

// all relays off
curl{DEVICEID}/relay -d access_token={ACCESSTOKEN}-d "params=1,0,2,0,3,0,4,0"


int RELAY1 = D0;
int RELAY2 = D1;
int RELAY3 = D2;
int RELAY4 = D3;

void setup()
//Initilize the relay control pins as output
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
// Initialize all relays to an OFF state
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY4, LOW);

//register the Spark function
Spark.function("relay", relayControl);

void loop()
// This loops for ever

int tokenise(char * command, char * tokens[], int max_tokens) {
int n = 0;
char * p = command;
while (n=20 || token_count == 0) return(-1);

while (n<token_count) {
if (tokens[n][0] == 'R' || tokens[n][0] == 'r') relayNumber = tokens[n][1] - '0';
else relayNumber = tokens[n][0] - '0';

if (strncmp(tokens[n],"HIGH", 4) == 0 || strncmp(tokens[n], "1", 1) ==0 ) relayState = 1;
else if (strncmp(tokens[n],"LOW",3) == 0 || strncmp(tokens[n], "0", 1) ==0 ) relayState = 0;


// do a sanity check
if (relayNumber 4 || relayState==-1) continue;

digitalWrite(relayNumber-1, relayState);


return 1;

To verify that the connections we set correctly I bought the outlet tester. The outlet tester shows 2 orange lights only when the outlet is properly connected and powered.

The future version of this will probably not include the relay shield because of the size. I plan on wiring directly to just 2 relays (instead of having 4) so that I can control each of the sockets independently and will try and fit all of the hardware inside the normal socket box so that a “smart” socket can replace a normal socket without needing additional space.

Feel free to leave me comments or questions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s