Short version: I have a program that connects my ESP8266 to WIFI so I can control a relay connected to it over the internet or a button. I also have a sensor for my door. This software works perfectly, if I add a light dependent resistor to it (which is connected correctly since I do get readings from it) and start reading data I lose my WIFI connection. Has anyone got any idea why this is happening?
Long version:
So I have this program running on my Esp8266
#include <ESP8266WiFi.h>
IPAddress ip(192, 168, 0, 150);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
const char* ssid = "mySSID";
const char* password = "myPSW";
const char* host = "192.168.0.228";
const int buttonPin = 0;
const int relayPin = 4;
const int doorPin = 5;
const int ldrPin = A0;
//int ldrState;
//int serialCount = 0;
//int ldrMin = 0;
//int ldrMax = 1024;
int buttonState = 0;
int doorState = 0;
byte Main_Light_State_Begin;
bool Main_Light_State;
bool doorNotified = false;
bool button_action_taken = false;
bool Main_Light_DBR = true;
String HTTP_Get_Response;
WiFiServer server(80);
//Functions
String HTTP_Get(char* Adress, String URL, bool Last_Line_Bool) {
WiFiClient client;
const int httpPort = 80;
if (!client.connect(Adress, httpPort)) {
Serial.println("connection failed");
return "Failed";
}
Serial.print("Requesting URL: ");
Serial.println(URL);
Serial.println("at adress");
Serial.println(Adress);
client.print(String("GET ") + URL + " HTTP/1.1\r\n" +
"Host: " + Adress + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
String Response;
String Last_Line;
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
Response = Response + line;
}
return Response;
Serial.println();
Serial.println("closing connection");
}
void setup() {
Serial.begin(115200);
delay(1000);
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");
server.begin(); -
Serial.println("Server started");
Serial.println(WiFi.localIP());
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(doorPin, INPUT);
// pinMode(ldrPin, INPUT);
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Wifi NOT Connected");
}
//Sensors
buttonState = digitalRead(buttonPin);
doorState = digitalRead(doorPin);
//ldrState = map(analogRead(ldrPin), ldrMin, ldrMax, 0, 100);
//Serial.println(analogRead(ldrPin));
// if (serialCount == 1000) {
// Serial.println(ldrState);
// serialCount = 0;
// } else {
// serialCount ++;
// }
//Doorsensor
if (doorState == HIGH) {
if (doorNotified == false) {
Serial.println("DOOR");
HTTP_Get_Response = HTTP_Get("192.168.0.228", "/index.php/?door_opened=true", false);
doorNotified = true;
}
} else if (doorState == LOW) {
if (doorNotified == true) {
doorNotified = false;
delay(500);
}
else {
doorNotified = false;
}
}
//Button
if (buttonState == LOW) {
if (button_action_taken == false) {
if (Main_Light_State == true) {
Serial.println("SWITCH");
Main_Light_State = false;
digitalWrite(relayPin, HIGH);
delay(500);
Main_Light_DBR = false;
}
else if (Main_Light_State == false) {
Serial.println("SWITCH");
Main_Light_State = true;
digitalWrite(relayPin, LOW);
delay(500);
Main_Light_DBR = false;
}
button_action_taken = true;
}
} else if (buttonState == HIGH) {
if (button_action_taken == true) {
button_action_taken = false;
delay(500);
}
else {
button_action_taken = false;
}
}
//Main_Light_State
if (Main_Light_State == true) {
digitalWrite(relayPin, LOW);
if (Main_Light_DBR == false) {
HTTP_Get_Response = HTTP_Get("192.168.0.228", "/index.php/?main_light_on_dbr=true", false);
Main_Light_DBR = true;
}
} else if (Main_Light_State == false) {
digitalWrite(relayPin, HIGH);
if (Main_Light_DBR == false) {
HTTP_Get_Response = HTTP_Get("192.168.0.228", "/index.php/?main_light_off_dbr=true", false);
Main_Light_DBR = true;
}
}
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
if (req.indexOf("/main_light/on") != -1) {
Main_Light_State = true;
Main_Light_DBR = false;
} else if (req.indexOf("/main_light/off") != -1) {
Main_Light_State = false;
Main_Light_DBR = false;
} else if (req.indexOf("/main_light/switch") != -1) {
Serial.println("SWITCH");
if (Main_Light_State == true) {
Main_Light_State = false;
}
else if (Main_Light_State == false) {
Main_Light_State = true;
}
Main_Light_DBR = false;
} else if (req.indexOf("/main_light/state") != -1) {
} else {
Serial.println("invalid request");
client.stop();
return;
}
client.flush();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += (Main_Light_State) ? "1" : "0";
client.print(s);
delay(1);
Serial.println("Client disonnected");
}
With the lines I commented like above everything works fine. I have a light dependant resistor connected (correctly, I get correct measurements from it) to A0. From the moment on I uncomment the lines involving this LDR and thus start reading data from it I lose my wifi connection. Has anyone got any idea why this is? Thank you!