这是我可以控制伺服电机的项目代码。我曾尝试使用深度睡眠,因为我会使用电池。但是看着我的串口显示器,我收到了这条消息,我无法访问我的网络服务器。
ERR_CONNECTION_TIMED_OUT
连接路易斯
WiFi已连接 Web服务器已启动 您可以在此处连接到服务器:http://192.168.0.102 进入深度睡眠20秒 {1
WiFi已连接 Web服务器已启动 您可以在此处连接到服务器:http://192.168.0.102 进入深度睡眠20秒 {1
WiFi已连接 Web服务器已启动 您可以在此处连接到服务器:http://192.168.0.102 进入深度睡眠20秒 {1
我的代码出了什么问题?
#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);
}
答案 0 :(得分:3)
loop()
中的代码永远不会运行。 ESP8266上的深度睡眠(至少对于Arduino项目而言)实际上会在唤醒时重置处理器,除了RTC内存之外都会丢失所有内容。因此,当您调用ESP.deepSleep()
时,在RTC内存中设置一个标志,处理器将在20秒后重置,下一次setup()
被调用REASON_DEEP_SLEEP_AWAKE
将被设置为重置原因
由于您正在尝试运行Web服务器,因此没有理想的睡眠模式可以使您的Web服务器保持活动状态。即使是调制解调器睡眠也需要您拆卸,并且每次都要重新启动Web服务器。这意味着您的浏览器连接会失败,除非您在清醒时抓住了芯片。