Salwan Damman
Published

IoT Power Plug Controlled by Alexa/Google Assistant

A Particle Photon is used to make an IoT power plug that can be controlled using voice assistants to power appliances.

BeginnerFull instructions provided5 hours55
IoT Power Plug Controlled by Alexa/Google Assistant

Things used in this project

Hardware components

Photon
Particle Photon
×1
Electrical Handy Box
×1
Handy Box Duplex Receptacle Cover
×1
15 Amp Duplex Outlet
×1
3 ft. 14/3 SPT A/C Extension Cord - Beige
×1
3/8 in. Non-Metallic (NM) Twin-Screw Cable Clamp Connectors
×1
Resistor 10k ohm
Resistor 10k ohm
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Relay 5V
×1
5V micro USB charger
×1
small breadboards
×1

Hand tools and fabrication machines

Wire Stripper
Screwdriver
Multi-meter
for debugging

Story

Read more

Schematics

IoT power outlet schematic

This is the schematic of the circuit including the wiring of the relay

Code

Particle Photon code for the power plug

Arduino
The code declares a function and an output pin to control the relay. The function can be called through the Particle app, Particle io or it can be used as part of IFTTT routine. This code is what I am using currently for my room. It's not automated like the other code. It only follows voice commands.
int trigger = D0; // or your choice of pins

void setup() {

  pinMode(trigger, OUTPUT);
  
  Particle.function("plug",plugTrigger);
  digitalWrite(trigger, HIGH);
}


void loop() {
}

int plugTrigger(String command) {

    if (command=="on") {
        digitalWrite(trigger, HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(trigger, LOW);
        return 0;
    }
    else {
        return -1;
    }
}

Particle Photon code for Automated power plug AND voice command

Arduino
This code is what I used first where the decoration lights would turn on and off automatically according to a set time. I also added plug function for voice command/app along with default option, so the lights would be either turned controlled through voice command, or automated according to the time.
#include <SparkTime.h>

int trigger = D0; //Assign the pin connected to the relay signal
unsigned long currentTime;
unsigned long lastTime = 0UL;
int total_min; //storing the total minutes
int trigger_st;

SparkTime rtc; 
UDP UDPClient;

void setup() {
  rtc.begin(&UDPClient, "north-america.pool.ntp.org");
  rtc.setTimeZone(-8); // this sets the timezone
  Particle.function("plug",plugTrigger); //declaring the plug function
  pinMode(trigger, OUTPUT);
}

void loop() {
    currentTime = rtc.now();
    if ((currentTime != lastTime) && (trigger_st==0)) 
    {
        int minutes = rtc.minute(currentTime); // getting the current times (minutes and hours seperatly)
        int hours = rtc.hour(currentTime);
        total_min = minutes + hours*60; 
        // 1050 is the time I set for the plug to turn on. 1050 = 5:30 pm. Following 24 hour form, 5:30 is 17:30. So, 17*60+30 = 1050.
        // follow the same calculation to set your own time.
        delay(100);
        if (total_min >= 1050)
        {
            digitalWrite(trigger, HIGH);
        }
        else
        {
            digitalWrite(trigger, LOW);
        }
    }
}

int plugTrigger(String command) {

    if (command=="on") {
        digitalWrite(trigger, HIGH);
        trigger_st=1;
        return 1;
    }
    else if (command=="off") {
        digitalWrite(trigger, LOW);
        trigger_st=1;
        return 0;
    }
    else if (command=="default") {
        trigger_st=0;
        return 2;        
    }
    else {
        return -1;
    }
}

Credits

Salwan Damman

Salwan Damman

1 project • 0 followers
Thanks to Circuit Basics .

Comments