This actually works rather well. However, the time to connect to the wifi varies from 1-100 seconds which can be a pain when I am already late.
When I open system settings on my iPhone, I can already see that the WiFi is connected to my iPhone but it sometimes takes a minute to fully connect and be visible in the top bar of my iPhone which is needed to open the garage.
- The design is build to never restart the ESP.
- The WiFi provides only the connection for the garage, no internet or anything else.
- We are only using this with a limited number of phones (2). So using a static IP for these phones would be no problem.
- The code has been written and uploaded using the Arduino IDE
Is anyone having an idea for a quick fix?
I provide my code below. It is rather messy but might help for clarification.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
String html1 = "<!DOCTYPE html>\r\n<html>\r\n\
<head>\r\n<meta content=\"text/html; charset=ISO-8859-1\" http-equiv=\"content-type\">\r\n\
<title>Webr</title>\r\n\
<form action=\"";
// String((val)?"/1.html":"/0.html")
String html2 = "\">\r\n<input value=\"GO!\" style=\"";
String html3 = " width:15em;height:7em; font-size: 50px;\" type=\"submit\">\
</form>\r\n</head>\r\n<body>\r\n</body>\r\n</html>";
ESP8266WebServer server(80); // Server Port
int val = 1; //Initial value (off)
String Temp = "";
bool TurnOn = false;
int timerValue = 1000;
int timer = timerValue;
int startTimer = 3000;
bool started = true;
const int outputpin = 4;
void Turn_ON() // When calling "http://<ip address>/1.html"
{
val = 0; // Relais Aus
digitalWrite(outputpin, 1); // GPIO0
Temp = html1 + String((val) ? "/1.html" : "/0.html");
Temp += html2 + String((val) ? "BACKGROUND-COLOR: DarkGray;" : "BACKGROUND-COLOR: Chartreuse;") + html3;
server.send(200, "text/html", Temp);
TurnOn = true;
}
void Turn_OFF() // when calling "http://<ip address>//0.html"
{
val = 1; // Relais Ein
digitalWrite(outputpin, 0); // GPIO0
Temp = html1 + String((val) ? "/1.html" : "/0.html");
Temp += html2 + String((val) ? "BACKGROUND-COLOR: DarkGray;" : "BACKGROUND-COLOR: Chartreuse;") + html3;
server.send(200, "text/html", Temp);
}
void Turn_Index() // when calling "http://<ip address>/"
{
Temp = html1 + String((val) ? "/1.html" : "/0.html");
Temp += html2 + String((val) ? "BACKGROUND-COLOR: DarkGray;" : "BACKGROUND-COLOR: Chartreuse;") + html3;
server.send(200, "text/html", Temp);
}
void setup()
{
//digitalWrite(outputpin, 0); // Relay off
pinMode(outputpin, OUTPUT); // GPIO0 out
// pinMode(2, INPUT_PULLUP); // GPIO2 Pullup
// digitalWrite(outputpin,0); // Relay off
Serial.begin(115200); // Serial connection
Serial.println(""); // print empty line
WiFi.mode(WIFI_AP); // access point modus
WiFi.softAP("XXXXX", "XXXXX"); // Name and PW of WiFi
delay(500); //Wait 0.5s
Serial.print("IP Adresse "); //Current server IP
Serial.println(WiFi.softAPIP());
// Bechandlung der Turnsen anschlissen
server.on("/", Turn_Index);
server.on("/1.html", Turn_ON);
server.on("/0.html", Turn_OFF);
server.begin(); // Server start
Serial.println("HTTP Server gestartet");
}
void loop()
{
/*
*
*if(started == true){
startTimer --;
Serial.println(startTimer);
}
if(startTimer < 0){
digitalWrite(0, 0);
Serial.println("Relais aus");
started = false;
}
*/
if(TurnOn == true)
{
timer --;
Serial.println(timer);
}
if(timer < 0){
Serial.println("switch off");
Turn_OFF();
TurnOn = false;
timer = timerValue;
}
server.handleClient();
/*
if (!digitalRead(2)) //GPIO2 press
{
val = !val; // change
digitalWrite(0, val);
while (!digitalRead(2))server.handleClient(); // button release.
}
*/
}