Close
0%
0%

IoT power strip using ESP8266

IoT power strip controlled by ESP8266 and SSR.

Similar projects worth following
In this project, I'm using the ESP8266 module to control a power strip over the WiFi connection.
I'm using TRIAC with the MOC opto driver to control up to 12A.

The idea is to control the power strip using my smart phone over the internet.

The software on the module will be the nodeMCU with LUA scripts.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "hal.h"


//Inicializacao so servidor http na porta 80
WiFiServer server(80);
//Status da GPIO
uint8_t status_gpio = 0;

void setup() {

  initGPIO();


  Serial.begin(115200);
  Serial.println("Booting");

  //Inicia o WiFi para modo AP (Access Point)
  WiFi.mode(WIFI_AP);

  /* Configura o AP
  SSID
  senha
  canal (1-14)
  esconder rede (0-nao 1-sim)
  */
  //Configura o AP
  WiFi.softAP("IoTStrip","control42",2,0);

  //Logs na porta serial
  Serial.print("AP IP: ");
  Serial.println(WiFi.softAPIP());

  server.begin();

}

void webserver_handle(void){

  //Aguarda uma nova conexao
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  Serial.println("Nova conexao requisitada...");

  while(!client.available()){
    delay(1);
  }

  Serial.println("Nova conexao OK...");

  //Le a string enviada pelo cliente
  String req = client.readStringUntil('\r');
  //Mostra a string enviada
  Serial.println(req);
  //Limpa dados/buffer
  client.flush();

  //Trata a string do cliente em busca de comandos
  if (req.indexOf("rele_on") != -1){
    digitalWrite(LOAD, HIGH);
    digitalWrite(LED_A, HIGH);
    digitalWrite(LED_B, LOW);
    status_gpio = HIGH;
  } else if (req.indexOf("rele_off") != -1) {
    digitalWrite(LOAD, LOW);
    digitalWrite(LED_B, HIGH);
    digitalWrite(LED_A, LOW);
    status_gpio = LOW;
  }
  else {
    Serial.println("Requisicao invalida");
  }

  //Prepara a resposta para o cliente
  String buf = "";
  buf += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n";
  buf += "<html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>\r\n";
  buf += "<title>Regua IoT</title>";
  buf += "<style>.c{text-align: center;} div,input{padding:5px;font-size:1em;} input{width:80%;} body{text-align: center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;} .q{float: right;width: 64px;text-align: right;}</style>";
  buf += "</head>";
  buf += "<h3>Regua IoT</h3>";

  //De acordo com o status da GPIO envia o comando
  if(status_gpio)
    buf += "<div><h4>Regua: </h4><a href=\"?function=rele_off\"><button>Desligar</button></a></div>";
  else
    buf += "<div><h4>Regua: </h4><a href=\"?function=rele_on\"><button>Ligar</button></a></div>";

  buf += "<h4>Laboratorio Hacker de Campinas</h4>";
  buf += "</html>\n";

  //Envia a resposta para o cliente
  client.print(buf);
  client.flush();
  client.stop();
  Serial.println("Client disconectado!");
}

void loop() {
  webserver_handle();
}

  • 1 × ESP-03 Module ESP8266 Module
  • 1 × MOC3041 Opto and Fiber Optic Semiconductors and ICs / Optocouplers and OptoisolatorsDriver
  • 1 × BTA12 Discrete Semiconductors / Thyristors (DIACs, SIDACs, TRIACs, SCRs)
  • 1 × 14K275 Varistor

  • nodeMCU Lua code

    Pedro Minatel07/28/2015 at 19:04 0 comments

    srv=net.createServer(net.TCP)
    pin=6
    led_1=8
    led_2=5
    led_3=4
    status=true
    gpio.mode(pin,gpio.OUTPUT)
    gpio.mode(led_1,gpio.OUTPUT)
    gpio.mode(led_2,gpio.OUTPUT)
    gpio.mode(led_3,gpio.OUTPUT)
    gpio.write(pin,1)
    gpio.write(led_1,0)
    gpio.write(led_2,0)
    gpio.write(led_3,1)
    srv:listen(80,function(conn)
        conn:on("receive", function(client,request)
            local buf = "";
            local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
            if(method == nil)then
                _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
            end
            local _GET = {}
            if (vars ~= nil)then
                for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                    _GET[k] = v
                end
            end
            buf = buf.."<h1> ESP8266 IoT Power Strip Web Server</h1>";
            buf = buf.."<p>Lampada <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
            buf = buf.."<br><br>";
            buf = buf.."<h2>Developed by Pedro Minatel</h2>";
            local _on,_off = "",""
            if(_GET.pin == "ON1")then
                  gpio.write(pin,1)
                  gpio.write(led_1,0)
                  gpio.write(led_2,1)
                  gpio.write(led_3,0)
                  status = true
            elseif(_GET.pin == "OFF1")then
                  gpio.write(pin,0)
                  gpio.write(led_1,1)
                  gpio.write(led_2,0)
                  gpio.write(led_3,0)
                  status = false
            elseif(_GET.pin == "TG1")then
                  if(status == true)then
                        gpio.write(pin,0)
                        status = false
                  else
                        gpio.write(pin,1)
                        status = true
                  end
            end
            client:send(buf);
            client:close();
            collectgarbage();
        end)
    end)

  • TRIAC Module

    Pedro Minatel05/04/2015 at 21:52 1 comment

    This is the TRIAC module with RC snubber for inductive load.

    Should I keep the filter?

  • Testing the module

    Pedro Minatel04/28/2015 at 23:01 0 comments

    Right now I'm testing the nodeMCU TCP client script! The idea is to develop a basic Android app to control the power strip.

View all 3 project logs

Enjoy this project?

Share

Discussions

Lauro Ilson wrote 10/28/2017 at 17:29 point

Olá Pedro,
Poderia me ajudar? Estou tentando incrementar o seu projeto, gostaria de adicionar ao projeto um push-bottom para que além de ligar a régua pela internet pudesse liga-la pelo botão físico acoplado a régua. 

  Are you sure? yes | no

Bruce Land wrote 04/29/2015 at 11:48 point

Good to hear!

  Are you sure? yes | no

Bruce Land wrote 04/28/2015 at 12:14 point

It is a really bad idea to use a white board for 110 volt power. Use a blue box like in 

http://people.ece.cornell.edu/land/courses/ece4760/FinalProjects/s2012/jdf226_ag537/index.html

  Are you sure? yes | no

davedarko wrote 04/28/2015 at 21:33 point

Probably even worse for 230 volts :(

  Are you sure? yes | no

Pedro Minatel wrote 04/28/2015 at 22:58 point

Hello Bruce, I know that is too risky! I was just testing the MOC plus the TRIAC. BTW the current was about 180mA and just for a few seconds.

The second prototype is a little bit more secure.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates