ESP8266 WiFi relay switch (Arduino IDE)

8 years 8 months ago - 8 years 7 months ago #2049 by sueche
Thanks for the idea, it's worth considering.

Please Log in or Create an account to join the conversation.

8 years 8 months ago #2051 by asm7100
Hint
It's posebel to get them as free samples from www.microchip.com/
PLUS a lot more :-)

:-)

//
Allan

Please Log in or Create an account to join the conversation.

8 years 8 months ago #2057 by sueche
Can you share a simple schematic for connecting it to the ESP8266 ?

Please Log in or Create an account to join the conversation.

8 years 8 months ago #2058 by asm7100
Google mcp23017 :-)

//
Allan

Please Log in or Create an account to join the conversation.

8 years 3 months ago - 8 years 3 months ago #2774 by f8vpe

sueche wrote: Hello,
The other thing that bothers me is that on initial power up there is a fast ON/OFF/ON cycle with the relay. Any idea how to avoid ths ?


You really don't need the transistor, there is an optocoupler on the relais board, your ESP can handle the current.
On the boards with two or four relais you may supply 5volt to the secondairy circuit and drive the opto's with 3V3. The on/off is reversed.
I adapted the code of Igor with a possibility to reverse or not.
For the resistor on the 5 volt line I used a SMD LF33 (DPAK version) with 100nF on the 5 volt and 3V3 and 10µF on the 3v3. My code is working fine for 1 to 4 relais.
I also made some changes in the webpage to add some colors and to fit on a phone screen.
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "whatever your password is";
IPAddress ip(192, 168, 178, 251);	// this 3 lines for a fix IP-address
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
// #define DEBUG
#define INVERSE  // low = On -- high = Off for relay prints
#define Port1 2
#define Port2 4
#define Port3 5
#define Port4 13
// ********************** end user settings ***************************
WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);
#ifdef INVERSE
    #define On 0
    #define Off 1
  #else
    #define On 1
    #define Off 0
#endif
  pinMode(Port1, OUTPUT);
  digitalWrite(Port1, Off);
  pinMode(Port2, OUTPUT);
  digitalWrite(Port2, Off);
  pinMode(Port3, OUTPUT);
  digitalWrite(Port3, Off);
  pinMode(Port4, OUTPUT);
  digitalWrite(Port4, Off);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Start the server
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.println(WiFi.localIP());
}
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  // Wait until the client sends some data
#ifdef DEBUG
  Serial.println("new command");
#endif
  while (!client.available()) {
    delay(1);
  }
  // Read the first line of the request
  String request = client.readStringUntil('\r');
#ifdef DEBUG
  Serial.println(request);
#endif
  client.flush();
  // Match the request
  if (request.indexOf("/light1on") > 0)  {
    digitalWrite(Port1, On);
  }
  if (request.indexOf("/light1off") > 0)  {
    digitalWrite(Port1, Off);
  }
  if (request.indexOf("/light2on") > 0)  {
    digitalWrite(Port2, On);
  }
  if (request.indexOf("/light2off") > 0)  {
    digitalWrite(Port2, Off);
  }
  if (request.indexOf("/light3on") > 0)  {
    digitalWrite(Port3, On);
  }
  if (request.indexOf("/light3off") > 0)  {
    digitalWrite(Port3, Off);
  }
  if (request.indexOf("/light4on") > 0)  {
    digitalWrite(Port4, On);
  }
  if (request.indexOf("/light4off") > 0)  {
    digitalWrite(Port4, Off);
  }
  // Set ledPin according to the request
  //digitalWrite(ledPin, value);
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.println("<head>");
  client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  client.println("</head>");
  client.println("<body bgcolor = darkgrey>");
  client.println("<hr/><hr>");
  client.println("<h4><center> WiFi Electrical Device Control </center></h4>");
  client.println("<hr/><hr>");
  client.println("<br><br>");
  client.println("<center>");
  client.println("Device 1 ");
  client.println("<a href=\"/light1on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light1off\"\"><button>Turn Off </button></a><br />");
  client.println("<br>");
  client.println("Device 2 ");
  client.println("<a href=\"/light2on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light2off\"\"><button>Turn Off </button></a><br />");
  client.println("<br>");
  client.println("Device 3 ");
  client.println("<a href=\"/light3on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light3off\"\"><button>Turn Off </button></a><br />");
  client.println("<br>");
  client.println("Device 4 ");
  client.println("<a href=\"/light4on\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/light4off\"\"><button>Turn Off </button></a><br />");
  client.println("<br>");
  client.println("<table border=\"5\">");
  client.println("<tr>");
#ifdef INVERSE
  if (not(digitalRead(Port1)))
#else
  if (digitalRead(Port1))
#endif
  {
    client.print("<td bgcolor=red> Device 1 is ON</td>");
  }
  else {
    client.print("<td bgcolor=green> Device 1 is OFF</td>");
  }
#ifdef INVERSE
  if (not(digitalRead(Port2)))
#else
  if (digitalRead(Port2))
#endif
  {
    client.print("<td bgcolor=red> Device 2 is ON</td>");
  }
  else {
    client.print("<td bgcolor=green> Device 2 is OFF</td>");
  }

  client.println("</tr>");
  client.println("<tr>");
#ifdef INVERSE
  if (not(digitalRead(Port3)))
#else
  if (digitalRead(Port3))
#endif
  {
    client.print("<td bgcolor=red> Device 3 is ON</td>");
  }
  else {
    client.print("<td bgcolor=green> Device 3 is OFF</td>");
  }
#ifdef INVERSE
  if (not(digitalRead(Port4)))
#else
  if (digitalRead(Port4))
#endif
  {
    client.print("<td bgcolor=red> Device 4 is ON</td>");
  }
  else {
    client.print("<td bgcolor=green> Device 4 is OFF</td>");
  }
  client.println("</tr>");
  client.println("</table>");
  client.println("</center>");
  client.println("</html>");
  delay(1);
#ifdef DEBUG
  Serial.println("Client disonnected");
  Serial.println("");
#endif
}
The following user(s) said Thank You: donvukovic

Please Log in or Create an account to join the conversation.

8 years 3 months ago - 8 years 3 months ago #2779 by sueche
Hello,

thanks for your effort and reply but unfortunately I had to use the trasistor, because without it I can't drive the relay. I've tested this with ESP-201 and ESP-02 and it's the same in both cases - the transistor is needed.
As for the logic I have reversed it in the code.
I don't know if it depends on the relay boards I am using therefor I will post the type of these:
normal relay - 10A - www.aliexpress.com/item/New-1-Channel-H-...-5V/32313464005.html
power relay - 30A - www.aliexpress.com/item/2-5V-30A-1-Chann...ing/32342340182.html

Please Log in or Create an account to join the conversation.

Time to create page: 0.541 seconds

Forum latest

  • No posts to display.