- Posts: 9
- Thank you received: 1
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
 
			Please Log in or Create an account to join the conversation.
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 ?
#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
}Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.