Progress Bar

Skip to main content

Jaycar
...

WiFi Mains Switch

WiFi Mains Switch

Difficulty
Power

Summary

We can easily set our power points to turn on or off when we have left the house, during certain times of the day, or using a button widget on our phone. Thanks to the power of IFTTT we can also configure it to connected to anything else, such as if we receive an email on our gmail account or when we get a phone call. The sky's the limit!

Materials Required

1Wi-Fi Mini ESP8266 Main BoardXC3802
1Wireless Modules (Transmitter) - 433MHzZW3100
1Outlet Suitable for MS6148 or MS6147MS6149

There's 3 main parts below, 

, 

, and 

.

There's two accounts we need to set up, one for AdafruitIO and the other for IFTTT.

Head over to 

 to create an account with an easy username, then log in to 

 to arrive at a blank looking screen.

Click on Feeds up the top, and "view all"

You can come to a screen with "Group / Feed" and an empty default. Click on the 

actions

 button, and select "Create a new feed."

Give it a useful name; you'll need to remember this when you plug it to other services as well, so we used "espswitch". You can put a description if you want.

Now in the top right corner, you should see 

"AIO Key"

 written in yellow. Click this to bring up you AIO_KEY.

You could already imagine what this is; you'll need this key to talk to the AIO service on the ESP8266. You can either copy and paste this into the code now or just remember where it is later. (

Note we use different #defines for the code, so just copy the string and not the arduino code.

)

Now we can head over to 

 and sign up. If you've done projects with this before, you'll already have an account which you can log into, or otherwise create an account.

Once you're in, you can click on your profile image in the top right, (next to explore). And click 

Create

.

You should come to the following page; This should be second nature to you if you've done this type of thing before.

Click on the 'this' and set up the trigger that you want to set. For instance, we will use the Location service, and set it to when we "enter or exit an area"

We're going to set it for Jaycar head office, but you can set this to your home address or your friends address if you want, or any random location which you might enter/exit and want the switch to turn on/off accordingly.

After you have created this trigger, click the "That" and search for "Adafruit" - you will want to send data to Adafruit IO.

For this part, you need to set some specific data relating to whether you've entered or exited the area. For this, we use the "Entered or Exited" ingredient, which will mean the data fed to the AIO feed (and thus, to the ESP8266) will be a string with "entered" or "exited". you could also put the time and date in after this if you want.

Note:

 We put the "Entered/Exited" ingredient first, so we can use String functions such as String.startsWith() further down the line. If you put it last in the line, then you'd have to use String.endsWith()

Finally, confirm what the trigger is set up to do. You want to send data to espswitch (your feed name) when you enter or exited an area.

If you want, you can enable or disable the notifications on your phone when this applet runs. This will just buzz your phone when you enter and exit the area.

In order for this whole process to work, you must install the IFTTT app on your phone and log into it so that it can trigger the correct location event. This gets the location from your phone's GPS and sends it to the IFTTT servers to be triggered.

You can also set up a button widget on the IFTTT service. You want to set it up so "IF you push a button then send data to espswitch feed". The button obviously can't have entered or exited, but you can set the string to be something such as "toggle" to toggle switches.

Now we have the IFTTT service being triggered, sending data to AIO, we need to set up the ESP to receive data from AIO.

You will need to install the following library from the Arduino Library Manager.

Library

Version

Author

Adafruit_MQTT

1.1.1.

Adafruit

We have also included a "radio.h" library in the sketch folder, so you don't have to worry about managing the 433 MHz transmitter.

If you have never used the 

 before, you 

will

 have to set it up to work with the arduino IDE. We have included a manual on the product page for the 

 on the jaycar website for you to follow.

Once the libraries are installed and set up, you should be able to just open the code in the arduino IDE and press upload to upload it to the ESP8266. Make sure you have the correct board (LOLIN Wemos D1 R2 & Mini) and port selected.

Pin connections are as followed:

ESP8266 Module

Transmitter Module

Comment

5V

VCC

Power

G

GND

Ground

D4

DATA

Signal Data

ANT

17cm of loose wire

You must attach around 17cm of loose wire to the Antenna port on the module so that the wireless signal can be transmitted across the air.

If you have a breadboard laying around, you can connect up the TX module and test it with the following code to see how well it performs at turning on and off the 

 mains switch, once you have soldered on a small antenna for it to use.

1 #include "radio.h" //from this repository
2
3 void setup()
4 {
5 Serial.begin(9600);
6 pinMode(D4, OUTPUT); //connect to D4
7 }
8
9 void loop()
10 {
11 Serial.println("Turning ON");
12
13 // send radio command on D4 to turn switch 1, ON)
14 radioSwitch(D4, 1, true);
15 delay(3000); //3 seconds
16
17 Serial.println("Turning OFF");
18 radioSwitch(D4, 1, false); //sw1 OFF
19 delay(3000); //3 seconds
20 }

When you first plug in the 

 the LED indicator will blink rapidly as it's looking for an ID to use. You must try to activate it when it blinks, so it can receive a new id. Try running the above code before you plug in the 

.

You can bind multiple switches to the same ID (1,2,3,4) or use 0 to turn them all on and off. You should be able to have more than 4 switches too, which is explained further in the 

 section.

When it comes to our code, we must put in the correct key, feed, and username from 

 into our program. This is the first couple of lines in the sketch:

1 #include <ESP8266WiFi.h> //to connect to wifi
2 #include <Adafruit_MQTT.h> //to connect to MQTT
3 #include <Adafruit_MQTT_Client.h>
4 #include "radio.h" //to connect to mains switch
5
6 const short output_pin = D4; //pin that radio transmitter is on
7
8 //to connect to aio
9 #define ADAFRUIT_USERNAME "your-username"
10 #define ADAFRUIT_AIOKEY "aio_AXXXXXXXXXXXXY51oaAUB67uz4qc"
11 #define ADAFRUIT_FEED "espswitch"
12
13 //to connect to your wifi
14 #define WIFI_SSID "wireless name"
15 #define WIFI_PASS "wireless password"

In order to connect the 

 and the Transmitter module together, we must first remove the pins on the transmitter and replace them with wires (

 are solid core, which make it much easier to solder into the small holes).

First remove the pins on the transmitter by gripping them with pliers and heating up the solder so that you can remove them.

Once they are removed, you can use some solder wick to clean up the holes to make it easier to put the wires through.

Then strip a small section of 

 wire to place through the holes and solder on. Don't forget the 17cm of wire for the antenna port.

After all that, we can simply trim them short (We used around 2cm for each wire) and strip a small section to solder into the ESP. If you are using the 

3D Printed Case

 be sure to bring them up from below, rather than through the top as shown below.

Here's a picture of our 3D printed case version; If you are using a case, it will fit much much easier if you come up from the underside as shown below.

To put it in the case; once you have printed it out, you should see that the ESP mounts "upside down" in the case, with a small cavity for the wireless sensor, and the antenna should fit through the hole on the side. We used 

 on our case version so the hole is a little small, you could try increasing the size with a simple model editor (like 

 if you want to get real fancy). Use some small M3 screws (

 and the like) to close up the case.

Once done, you can try opening up the serial monitor on the esp, then go for a walk around the block (keeping your phone on you). When you come back, you should see a notice in the serial monitor telling you that it has turned off and turned on again. (or at least, got the entered and exited signals).

If you have also set the button widget in IFTTT, you can put a button widget on your phone's home screen to see "toggle" messages in the serial monitor, which is a little easier to test.

Once you can see the information in the serial monitor, go check the code and put whatever switches you want to turn on/off in that section. For instance (line 63 of the source code:)

1 if (value.equals("toggle")) //button press
2 {
3 /* ... */
4 }
5 else if (value.startsWith("enter")) //we entered the location
6 {
7 //turn on switch 1
8 radioSwitch(output_pin, 1, 1);
9 //turn off switch 4
10 radioSwitch(output_pin, 4, 0);
11 }

There's two main parts to this project, the MQTT handling and the radio handling. MQTT is handled by the adafruit library and introduces things such as Adafruit_MQTT_Client and Adafruit_MQTT_Subscribe objects. These handle our connection to the adafruit.io service and manages things such as keep-alives, unique ID's and 

last read

 commands.

The radio is handled through our custom radio.h library; this is a simple list of functions, the main being:

1 radioSwitch(outputPin, switchNumber, booleanState );

If you wanted to hack into it, you can see what this does in the radio.h header:

1 // ....
2
3 unsigned long packet = makePacket( address, cmd);
4 //address is a random number; 4 switches per address, however you should be able
5 // to manage multiple switches if you manage different addresses internally.
6 //command is a simple nibble of information, contained within
7 // radiocommands[];
8
9 //send it a few times just to make sure.
10 for (int i = 0; i < 4; i++) { sendPacket(pin, packet);}
11
12 // ...

Similar projects you may be interested in

Power
Bluetooth Power Point
Difficulty

Power
High Efficiency Power Supply
Difficulty

Power
Hand Gesture Power Socket
Difficulty

Power
LoRa Remote Relay
Difficulty