This is my code and I’m stuck in this loop.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
char serverb[] = "blynk-cloud.com";
int port=80;
WiFiClient client;
WiFiServer server(80);
String command =""; // Command received from Android device
// Set Relay Pins
int relay1 = D0;
char auth[] = "*****************";
char ssid[] = "";
char pass[] = "";
void setup()
{
pinMode(relay1, OUTPUT);
Serial.begin(115200);
Serial.println("connecting wifi Manager");
connectWiFi();
Blynk.config(auth);
Serial.println("Check the blynk");
bool result = Blynk.connect(5);
Serial.println(result);
server.begin();
}
void loop()
{
if (WiFi.status() != WL_CONNECTED)
{
delay(50000);
Serial.println("Creating Hotspot");
ESP.reset();
}
Blynk.run();
client = server.available();
if (!client) return;
command = checkClient ();
Serial.println(command);
if (command == "r1on" || command == "on" || command == "open" )
{
digitalWrite(relay1,HIGH);
delay(400);
digitalWrite(relay1,LOW);
sendBackEcho(command);// send command echo back to android device
}
else if (command == "r1off" || command == "off" || command == "close")
{
digitalWrite(relay1,LOW);
sendBackEcho(command);// send command echo back to android device
}else if (command == "hotspot" || command == "setting" )
{
creatHotspot();
}
command = "";
}
/* connecting WiFi */
void connectWiFi()
{
WiFiManager wifiManager;
wifiManager.setAPStaticIPConfig(IPAddress(192,168,1,50), IPAddress(192,168,1,1), IPAddress(255,255,255,0));
//wifiManager.setSTAStaticIPConfig(IPAddress(192,168,0,119), IPAddress(192,168,0,1), IPAddress(255,255,255,0));
if (!wifiManager.autoConnect("Relay-Config", "pakistan")) {
Serial.println("failed to connect, we should reset as see if it connects");
delay(3000);
ESP.reset();
delay(5000);
}
}
/* check command received from Android Device */
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}
/* send command echo back to android device */
void sendBackEcho(String echo)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println(echo);
client.println("</html>");
client.stop();
delay(1);
}
void gateControl(int x)
{
if (x>0)
{
Serial.println("Gate Open");
digitalWrite(relay1,HIGH);
delay(400);
digitalWrite(relay1,LOW);
}
}
BLYNK_WRITE(V1)
{Serial.println("Check3 ");
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
Serial.print("V1 Slider value is: ");
Serial.println(pinValue);
gateControl(pinValue);
}
void creatHotspot()
{
WiFiManager wifiManager;
wifiManager.setAPStaticIPConfig(IPAddress(192,168,1,50), IPAddress(192,168,1,1), IPAddress(255,255,255,0));
wifiManager.startConfigPortal("OnDemandAP");
if (!wifiManager.startConfigPortal("OnDemandAP")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
// put your main code here, to run repeatedly: