Skip to main content

WiFi-Enabled Power Plug Version 2.0

Version 2.0


A little over a year ago I built an Arduino-controlled electrical box connected to the web via an ESP8266 WiFi board. With an Arduino Uno rattling around loose in there, it was anything but pretty to look at and undoubtedly a fire hazard. The whole setup also felt unnecessarily large, a feeling that may or may not have been prompted by the sudden flood of commercially produced IoT-esque power switches flooding the market at around the same time. So, many months ago, I set out to give my solution a facelift.

The physical dimensions of the Uno and the 4-channel relay switch employed in the project did not lend themselves easily to being crammed into a smaller space. Eventually I saw no alternative but to swap out the Uno for a Nano and hope that the board would supply enough power to run everything reliably. I then grabbed a saw and went to town on the electrical box.


Everything is hidden just behind the power plugs.

It turned out to be a perfect fit, with a bit of room to spare. With each component either glued into position or secured with mounting screws, the end result is an equally capable solution in roughly half the size of the original. I even found space on which to proudly write the project version number.

The only thing not properly handled- apart from a real box cover to prevent someone from sticking their fingers in when the power lines are live- is getting input power to the Arduino Nano itself. It still requires USB power routed through a hole in the side of the box. Not an elegant solution, but it does work.


Hopefully, once my heater project is finished, I'll find a way to get this added into the same system for more complete DIY home automation.

Comments

  1. I saw your project on Hackster and tried it.
    Worked well - took about 2 minutes ...

    I have been playing around with the Alexa - esp control codes and found one that you might find interesting.
    It uses a library fauxmoESP
    I also works very well and has multiple devices functions builtin
    Much smaller code - maybe you could add the wifi to this code??
    This code has setup for 2 devices but you can add many more
    Joe


    #include
    #include
    #include "fauxmoESP.h"

    #define WIFI_SSID "xxxxxx"
    #define WIFI_PASS "yourpassword"

    #define SERIAL_BAUDRATE 115200

    fauxmoESP fauxmo;

    #define RELAY_PIN 13 // d7=gpio13
    //#define NEOPIX_PIN 2 //d4 =gpio2
    #define NEOPIX_PIN 5 //d1 =gpio5

    // Wifi

    void wifiSetup() {
    // Set WIFI module to STA mode
    WiFi.mode(WIFI_STA);

    // Connect
    Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
    WiFi.begin(WIFI_SSID, WIFI_PASS);

    // Wait
    while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
    }
    Serial.println();

    // Connected!
    Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
    }

    void callback(uint8_t device_id, const char * device_name, bool state) {
    Serial.printf("[MAIN] %s state: %s\n", device_name, state ? "ON" : "OFF");

    if ( (strcmp(device_name, "pixels") == 0) ) {

    if (state) {
    digitalWrite(NEOPIX_PIN, HIGH);
    } else {
    digitalWrite(NEOPIX_PIN, LOW);
    }
    }

    if ( (strcmp(device_name, "relay") == 0) ) {
    // adjust the relay immediately!
    if (state) {
    digitalWrite(RELAY_PIN, HIGH);
    } else {
    digitalWrite(RELAY_PIN, LOW);
    }
    }
    }

    void setup() {

    pinMode(RELAY_PIN, OUTPUT);
    digitalWrite(RELAY_PIN, LOW);//

    pinMode(NEOPIX_PIN, OUTPUT);
    digitalWrite(NEOPIX_PIN, LOW);

    // Init serial port and clean garbage
    Serial.begin(SERIAL_BAUDRATE);
    Serial.println();
    Serial.println();
    Serial.println("FauxMo demo sketch");
    Serial.println("After connection, ask Alexa/Echo to 'turn pixels on' or 'off' or 'turn relay on' or 'off'");

    // Wifi
    wifiSetup();

    // Fauxmo
    fauxmo.addDevice("relay");
    fauxmo.addDevice("pixels");
    fauxmo.onMessage(callback);
    }
    void loop() {
    fauxmo.handle();

    }

    ReplyDelete
    Replies
    1. Sorry - missed these - need to add at top of code

      Joe


      #include
      #include
      #include "fauxmoESP.h"

      Delete
    2. #include Arduino.h
      #include ESP8266WiFi.h

      Delete
    3. Hey Joe,

      Thanks for your comments and code snippets! I actually just rebuilt this hardware plug, replacing the Arduino Nano + ESP-01 boards with a single NodeMCU and managed to cram a little AC-to-USB board in there as well.

      I see your code seems to support toggling the power of multiple GPIOs using a single ESP device... is that true? I'll have to give it a shot, that's the last piece of the puzzle for me.

      Delete
  2. Yes - you can toggle as man switches as there are unused gpio's on the your single esp8266

    It works very well
    Been trying to add a wifi manager but now luck
    I have code which uses serial port as the wifi manager but wireless control would be much better.
    I have searched for many Alexa support device codes but the above is the smallest code since the guy who made it put it as a Arduino library.

    Joe

    ReplyDelete
  3. I have built several boxes with 4 - ports.
    Using this code and Alexa it discovers them very quickly and they stay connected ...

    (1 esp8266 nodemcu and 4 ch relay)
    Each individually controlled.


    joe

    ReplyDelete
  4. https://bitbucket.org/xoseperez/fauxmoesp

    This where I found the original code to use it as library


    Joe

    ReplyDelete
  5. Joe, try this code. It goes through the list of known SSIDs and Passwords until it finds a match. If it doesn't, the NodeMCU resets itself and tries again.
    Code:

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    boolean wifiFound = false;

    Serial.begin(115200);
    delay(10);
    int i, j=0;
    int n = WiFi.scanNetworks();
    if (n == 0)
    Serial.println("No Networks Found :(");
    else
    {
    Serial.print(n);
    Serial.println(" Networks Found :)");
    for (int i = 0; i < n; ++i)
    {
    // Print SSID and RSSI for each network found
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.print(WiFi.SSID(i));
    Serial.print(" (Strength: ");
    Serial.print(WiFi.RSSI(i));
    Serial.print(" %)\n");
    delay(10);
    }
    }
    for (i = 0; i < n; i++) {
    Serial.print("Trying to connect to ");
    Serial.println(WiFi.SSID(i)); // Print current SSID
    for (j = 0; j < KNOWN_SSID_COUNT; j++) { // walk through the list of known SSID and check for a match
    if (!(strcmp(KNOWN_SSID[j], WiFi.SSID(i).c_str()))) {
    wifiFound = true;
    break;
    }
    }
    if(wifiFound)
    break;
    } // end for each known wifi SSID
    if (!wifiFound) {
    Serial.println("No known networks found. Resetting and trying again...");
    delay(2500);
    while(true);
    }

    Serial.println("\nConnecting..");
    WiFi.begin(KNOWN_SSID[j], KNOWN_PASSWORD[j]);
    while (WiFi.status() != WL_CONNECTED) {
    delay(50);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.print(".");
    delay(50);
    digitalWrite(LED_BUILTIN, HIGH);
    }

    Serial.println("CONNECTED\n");
    digitalWrite(LED_BUILTIN, LOW);
    server.begin();
    Serial.println(WiFi.localIP());


    Initialize with:

    const char* KNOWN_SSID[] = {"HOME", "OnePlus", "Moto"};
    const char* KNOWN_PASSWORD[] = {"qwerty123", "asdfgh456", "zxcvbn789"};

    const int KNOWN_SSID_COUNT = sizeof(KNOWN_SSID) / sizeof(KNOWN_SSID[0]); // number of known networks

    ReplyDelete
    Replies
    1. It's a brute-force approach which I borrowed from some other code a long time ago for a Uni project. Will update with the original author once I find him.

      Delete

Post a Comment