Reputation: 701
Here is my code from of a project where I can to control a servo motor. I have tried to use deep sleep because I'll use a battery. But looking at my serial monitor, I have this message, and i can't access my web server.
ERR_CONNECTION_TIMED_OUT
Connecting to Louise
WiFi connected Web Server Started You can connect to the Server here: http://192.168.0.102 Going into deep sleep for 20 seconds {l
WiFi connected Web Server Started You can connect to the Server here: http://192.168.0.102 Going into deep sleep for 20 seconds {l
WiFi connected Web Server Started You can connect to the Server here: http://192.168.0.102 Going into deep sleep for 20 seconds {l
What is wrong with my code?
#include "ESP8266WiFi.h" // WiFi Library
#include <Wire.h>
#include <Servo.h> // Include the Servo library
const char* ssid = "Louise"; // Name of WiFi Network
const char* password = "passme"; // 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
int servoPin = 3; // Declare the Servo pin
Servo Servo1; // Create a servo object
WiFiServer server(80); // Define Web Server Port
void setup() {
Serial.begin(115200);
delay(10);
Servo1.attach(servoPin);
// We need to attach the servo to the used pin number
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// 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("You can connect to the Server here: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println();
Serial.println("Going into deep sleep for 20 seconds");
ESP.deepSleep(20e6); // 20e6 is 20 microseconds
}
void loop() {
// Check if someone is connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read which button was pressed on the Web Page
String request = client.readStringUntil('\r');
// Light up LEDs based on the button pressed
if (request.indexOf("/OFF=1") != -1) {
Servo1.attach(servoPin);
// attaches the servo on pin 9 to the servo object
delay(100);
Servo1.write(-90);
// sets the servo position according to the scaled value
delay(1000);
// waits for it to get to the position
Servo1.detach();
delay(1000);
buttonpressed = LOW;
firstrun = 1;
}
if (request.indexOf("/ON=1") != -1) {
Servo1.attach(servoPin);
// attaches the servo on pin 9 to the servo object
delay(100);
Servo1.write(90);
// sets the servo position according to the scaled value
delay(1000);
// waits for it to get to the position
Servo1.detach();
delay(1000);
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>Dog Feeder</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>Dog Food Dispenser</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\">Liberar Ração</button></a><br />");
client.println("<br><br>");
client.println("<a href=\"/OFF=1\"\"><button class=\"/Butme\">Fechar</button></a>");
client.println("<br><br>");
client.println("</html>");
delay(10);
}
Upvotes: 1
Views: 2384
Reputation: 915
Your code in loop()
will never run. Deep sleep on the ESP8266 (at least for the Arduino project) actually resets the processor on wake, losing everything but the RTC memory. So, when you call ESP.deepSleep()
, a flag is set in the RTC memory, the processor will reset after 20 seconds, and next time setup()
is called REASON_DEEP_SLEEP_AWAKE
will be set as the reset reason.
Since you're trying to run a web server, there's not an ideal sleep mode for you that will keep your web server alive. Even modem sleep will require you to tear down, and bring back up your web server each time. This means your browser connection would fail, unless you caught the chip at the time it was awake.
Upvotes: 4