So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By leocrptorres
#75884 Hi i have a problem: My code is working fine , but when i disconnect the board (power off) seems i can't to connect again with the webserver. And to work againg i need to load the sketch.

Check connection
Verify proxy and firewall
Run Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT

Whats is causing that error?

Code: Select all#include "NeoPixelBus.h" // Library to control Pixel Stick
#include "ESP8266WiFi.h" // WiFi Library
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Servo.h> // Include the Servo library
Adafruit_BMP085 bmp;
#define PixelCount 32 // Number of leds on stick
#define PixelPin 2 // NodeMCU pin 2 (RX) connected to Digital In of Pixel Stick
//DEFINIÇÃO DE IP FIXO PARA O NODEMCU
IPAddress ip(192,168,11,5); //COLOQUE UMA FAIXA DE IP DISPONÍVEL DO SEU ROTEADOR. EX: 192.168.1.110 **** ISSO VARIA, NO MEU CASO É: 192.168.0.175
IPAddress gateway(192,168,0,1); //GATEWAY DE CONEXÃO (ALTERE PARA O GATEWAY DO SEU ROTEADOR)
IPAddress subnet(255,255,255,0); //MASCARA DE REDE
const char* ssid = "8888"; // Name of WiFi Network
const char* password = "88888"; // Password of WiFi Network
int firstrun = 0; // Check if system was just powered on
int buttonpressed = 5; // To hold which button was pressed on Web Page
Servo servo;
int ledPin = 13; // GPIO13
// Initialize Library
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
RgbColor black(0);
// Define Arrays for colors

long switchled01[] =
{
0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19,
0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19,
0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19,
0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19, 0x19257B, 0x7B7A19

};


WiFiServer server(80); // Define Web Server Port
void setup() {
Serial.begin(115200);
servo.attach(2); //D4
servo.write(0);
delay(10);
if (!bmp.begin())
{
Serial.println("Sensorde temperatura BMP180 não localizado. Verifique os pinos");
while (1) {}
}
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

strip.Begin(); // Init of Pixel Stick
strip.Show(); // Clears any lit up Leds

// Connect to WiFi network
Serial.print("Conectando em .. ");
Serial.println(ssid);

WiFi.config(ip, gateway, subnet); //PASSA OS PARÂMETROS PARA A FUNÇÃO QUE VAI SETAR O IP FIXO NO NODEMCU
 WiFi.persistent(false);                 // Turn off persistent to fix flash crashing issue.   
  WiFi.mode(WIFI_OFF);                    // https://github.com/esp8266/Arduino/issues/3100   
WiFi.begin(ssid, password); //PASSA OS PARÂMETROS PARA A FUNÇÃO QUE VAI FAZER A CONEXÃO COM A REDE SEM FIO

// Wait until connected to WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}

// Confirmation that WiFi is connected
Serial.println("");
Serial.println("WiFi connected");

// Start the Web Server
server.begin();
Serial.println("Web Server Started");

// Display IP address
Serial.print("Conecte ao servidor em: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println();
Serial.println();

}


void loop() {

// Check if someone is connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);

int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
servo.attach(2); //D4
servo.write(0);
delay(10);
servo.write(90); // Make servo go to 0 degrees
delay(500);
servo.detach();
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);

value = LOW;
servo.attach(2); //D4
servo.write(90);
delay(10);
servo.write(0); // Make servo go to 0 degrees
delay(500);
servo.detach();
}


// Light Up leds based on the button pressed
if (request.indexOf("/OFF=1") != -1) {
for (int led = 0; led < 32; led++) {
strip.SetPixelColor(0, black);
strip.SetPixelColor(0, black);
strip.SetPixelColor(1, black);
strip.SetPixelColor(2, black);
strip.SetPixelColor(3, black);
strip.SetPixelColor(4, black);
strip.SetPixelColor(5, black);
strip.SetPixelColor(6, black);
strip.SetPixelColor(7, black);
strip.SetPixelColor(8, black);
strip.SetPixelColor(9, black);
strip.SetPixelColor(10, black);
strip.SetPixelColor(11, black);
strip.SetPixelColor(12, black);
strip.SetPixelColor(13, black);
strip.SetPixelColor(14, black);
strip.SetPixelColor(15, black);
strip.SetPixelColor(16, black);
strip.SetPixelColor(17, black);
strip.SetPixelColor(18, black);
strip.SetPixelColor(19, black);
strip.SetPixelColor(20, black);
strip.SetPixelColor(21, black);
strip.SetPixelColor(22, black);
strip.SetPixelColor(23, black);
strip.SetPixelColor(24, black);
strip.SetPixelColor(25, black);
strip.SetPixelColor(26, black);
strip.SetPixelColor(27, black);
strip.SetPixelColor(28, black);
strip.SetPixelColor(29, black);
strip.SetPixelColor(30, black);
strip.SetPixelColor(31, black);
strip.Show();
}
strip.Show();
buttonpressed = LOW;
firstrun=1;
}
if (request.indexOf("/ON=1") != -1) {
for (int led = 0; led < 32; led++) {
strip.SetPixelColor(led, HtmlColor(switchled01[led]));
}
strip.Show();
buttonpressed = HIGH;
firstrun=1;
}
// Display the HTML web page

client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");

client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
// Web Page Heading
client.println("<body><h1>Sistema de Leds Quarto do Matteo</h1>");
// Create Web Page
client.println("HTTP/1.1 200 OK"); // HTML Header
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<meta charset=ISO-8859-15>");
client.println("<html>"); // Start of HTML

client.println("<style>");
client.println("body {background-color: #ACAEAD;}"); // Set Background Color
client.println("Button {background-color: #195B6A; border: none; color: white; padding: 16px 40px;"); // Set Background Color
client.println("</style>");

client.println("<h1>Leds do Matteo</h1>");
if(buttonpressed == LOW) {
client.print("OFF");
}
if (buttonpressed == HIGH){
client.print("ON");
}


client.println("<br><br>");
client.println("<a href=\"/ON=1\"\"><button class=\"/Butme\">Ligar</button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/OFF=1\"\"><button class=\"/Butme\">Desligar</button></a>");
client.println("<br><br>");
if(value == HIGH) {
client.print("On");

} else {
client.print("Off");

}
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>");
client.println("<h3>Temperatura agora = ");
client.println(bmp.readTemperature());
client.println("&deg;C</h3>");
client.println("</html>");
delay(10);

}