I setup a ESP8266 NODEMCU in HA the other day and as long as it was powered it would show current status and allow me to control. I uploaded a new sketch to it with a few new lines(NOT) touching the wifi logic and now in HA it doesnt show available. BUT i can access it directly via the IP and control the LED light on the board from the webpage to it…
Here is my current sketch, the ONLY new lines i added for testing are the photocell lines, ive made them bold in the lines below.
Is there any way to refresh, so that is finds it? I found some mentions online about adding a dashboard ping, but cant figure out where that would be setup / inserted.
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "pass";
int ledPin = 16; // GPIO16
WiFiServer server(80);
**int photocellPin = 0; // the cell and 10K pulldown are connected to a0**
**int photocellReading; // the analog reading from the sensor divider**
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
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.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
**photocellReading = analogRead(photocellPin); **
** Serial.print("Analog reading = ");**
** Serial.println(photocellReading); // the raw analog reading**
// 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.print("Led pin is now: ");
if(value == HIGH) {
client.print("Off");
} else {
client.print("On");
}
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</html>");
delay(1);
}