Relay IoT

 

        This project allows you to control a relay from a mobile App. This is equivalent to an ON/OFF switch.

 

    Complexity

Project  Relay Control
Time to build  1 hour
Cost $30
Arduino Code download
Cloud Device Code N/A
Mobile App Code download

 

    Hardware

        Here you have the list of the components required to build the hardware. We assume you have a computer, a router and a network cable. If you purchase the Arduino board from the specified source it comes with the USB cable. Otherwise you might need to provide a USB cable too. You can find better options for hardware. Be careful what components you buy. For Ethernet boards there are many versions with different chipsets but only the standard Arduino Shield or equivalent works with the software provided here. The prices are just a guide. These are the values we found when we created this project.

  
Picture Name Price Source

Arduino Uno

 $13 Link
Arduino Ethernet Shield $10 Link
Relay $3 Link
Single Port Female to Female Jumper Wires $3 Link
Single Port Male to Male Jumper Wires $2 Link

 

        We recommend to buy an Arduino board with a smaller USB connector (mini or micro USB) because the Ethernet Shield will not insert properly most of the time if you do not use original Arduino boards.

        Here you have a picture with the components we used:

 

 

     Specifications

       This section shows the pinout of the components or provide a link to the specifications of the component.

 

Component Name Pins
Arduino Uno See: http://arduino.cc/en/Main/arduinoBoardUno
Arduino Ethernet See: http://arduino.cc/en/Main/ArduinoEthernetShield
Relay
Pin Description
Gnd Ground
Signal Pin to control the state of the relay.
Vcc 5V

 

     Wiring

        This section shows you how to connect the components together. First you have a pin-out connection and below there are pictures showing how it's done on the real boards.

 

Relay Pin ArduinoUno
GND GND
Vcc +5V
Signal D8

 

Arduino Ethernet ArduinoUno
Just insert the Ethernet Shield into Arduino Uno
 

       Here you have pictures with the wiring

 

 

 

 

        The schematic is

 

 

    Software

        To run the system you need two software components. One is the software to run the hardware (Arduino Software) and the other one is the mobile App (Mobile App).

 

     Arduino Software

        To run the software you need the Arduino development environment.You have to load the software provided here and configure it. There is only one value you need to change in the code.

 

Field Description
device_id

This is a unique id you have to create for your hardware device to identify it from others in the cloud. Use this www.guidgenerator.com to generate GUID (Global Unique ID)

String devId      = "b5ebf7a7-e63a-4660-8f83-cf1e5d39cf5b";
auth

This is the authentication string requred for basic authentication of the http requests. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"

String auth    = "dXNlcjpwYXNzd29yZA==";

 

       And here you have the sketch (download)

 

#include <SPI.h>
#include <Ethernet.h>


// Network settings
byte mac[]     = { 0xDE, 0xED, 0xDD, 0xDD, 0xFE, 0xED };
EthernetClient eth;

char server[]  = "www.ic2pro.com";                          // Wiring Cloud host name
int port       = 80;
String devId   = "111-222-333";                             // Device ID. CREATE YOUR OWN GUID; Use this http://www.guidgenerator.com/
String auth    = "dXNlcjpwYXNzd29yZA==";                    // Authentication credentials Create a string from <email_address>:<API_Password> and encode it base64
                                                            // The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password"

int lastState;       // last known state of the relay
int relayPin = 8;    // pin used to read the PIR sensor

void setup() {
  Serial.begin(115200);        // start serial monitoring
  pinMode(relayPin, OUTPUT);   // set-up relay pin
  Ethernet.begin(mac);         // start network communication
  delay(1000);
  
  Serial.println("Start...");

  lastState = -1;              // force relay to take the first command
  digitalWrite(relayPin, lastState);
}

void loop() {
  String state = readRelay();
  Serial.println("[" + state + "]");
  if((state.indexOf("RELAY,ON") > 0) && (lastState != 1)) {        // turn ON relay
    lastState = 1;
    digitalWrite(relayPin, lastState);
  }
  else if((state.indexOf("RELAY,OFF") > 0) && (lastState != 0)) {  // turn OFF relay
    lastState = 0;
    digitalWrite(relayPin, lastState);
  }
  delay(1000);
}

String readRelay() {
  if (eth.connect(server, port)) {  // connect to server
    Serial.println("Connected...");
    eth.println("GET http://" + String(server) + ":" + String(port) + "/Wire/connector/get?id=" + devId + "&RELAY HTTP/1.1");  // request state from server
    eth.println("Authorization: Basic " + auth);
    eth.println("Connection: close");
    eth.println();
    Serial.println("Event Sent...");
    return readResponse();
  }
  else {
    Serial.println("Connection Error");
    return "";
  }
}

String readResponse()
{
  while (eth.connected() && !eth.available()) delay(1);          // wait for data

  String buff = "";                                              // read data in the variable buff
  while (eth.connected() || eth.available()) {
    char c = eth.read();
    buff += c;
  }
  eth.stop();
  return buff;
}

  

Mobile App

 

        The mobile application to control the security system is built with "App Inventor" (ai2.appinventor.mit.edu). If you just want to import the .aia file you can download it here. Otherwise you have to create next components.

 

Component Name Description
Label RelayMsg A text that shows the relay status (ON or OFF).
Web APIGetRelayState

The HTTP request that will be sent to retrieve the temperature value. Set Url to:

http://www.ic2pro.com/Wire/connector/get?id=<device_id>&RELAY

don't forget to put the device_id you set in the Arduino Code

Web APISetOn

A web component to set the relay On

http://www.ic2pro.com/Wire/connector/set?id=<device_id>&RELAY=ON

don't forget to replace device_id with the value you set in the Arduino Code

Web APISetOff

A web component to set the relay Off

http://www.ic2pro.com/Wire/connector/set?id=<device_id>&RELAY=OFF

don't forget to replace device_id with the value you set in the Arduino Code

Button OnBttn A button that will be used to turn the relay On.
Button OffBttn A button that will be used to turn the relay Off.

        In the code you have to set a proper authentication string. Replace the string "Basic dXNlcjpwYXNzd29yZA==" with your authentication string. Create a string from <email_address>:<API_Password> and encode it base64. The sample here "dXNlcjpwYXNzd29yZA==" is the encoding for "user:password". Make sure you append "Basic " at the begining of it.

        And this is the logic you have to create in App Inventor. Once that is done you just build the APK and install it on your Android phone.

 

    Final Product

       Here you have a picture of the "final" product, hardware and screenshots from App.