ESP8266与ESP12E web ip地址无响应

时间:2018-04-20 23:49:23

标签: arduino esp8266 arduino-esp8266

我有一个控制遥控车的程序。代码编译和上传很好。它还会在串行监视器中打印出ip地址。当我在我的浏览器中键入IP地址时,我收到一条错误消息,指出无法访问IP地址。以下是我的代码。

谢谢

Alex

 #include <ESP8266WiFi.h>
 #include <WiFiClient.h>
 #include <ESP8266WebServer.h>
 #include <ESP8266mDNS.h>

//使用引脚值定义变量

 const int DRIVE_MOTOR_POWER = D2;
 const int DRIVE_MOTOR_DIRECTION = D4;
 const int STEER_MOTOR_POWER = D1;
 const int STEER_MOTOR_DIRECTION = D3;

// drivePower设定汽车行驶的速度 //可以设置在0到1023之间(虽然如果值太低,汽车可能不会移动)

 int drivePower = 1023;

// driveDirection设置汽车行驶的方向 //如果按下前进按钮时汽车向后移动,请将其更改为低

 uint8_t driveDirection = HIGH;

// steeringPower设定汽车转弯的速度 //可以设置在0到1023之间(再次,如果值太低,汽车可能会赢得&#39; t)

 int steeringPower = 1023;

// steerDirection设定汽车转向的方向 //如果按下左按钮时汽车正在转向,请将其更改为低

 uint8_t steerDirection = HIGH;

 const char* ssid = "wifi_username";
 const char* password = "wifi_password";

 ESP8266WebServer server(80);

 void handleRoot() {

  server.send(200, "text/plain", String("Hello from esp8266!"));
 }

 void handleNotFound(){
   String message = "File Not Found\n\n";
   message += "URI: ";
   message += server.uri();
   message += "\nMethod: ";
   message += (server.method() == HTTP_GET)?"GET":"POST";
   message += "\nArguments: ";
   message += server.args();
   message += "\n";
   for (uint8_t i=0; i<server.args(); i++){
     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
   }
   server.send(404, "text/plain", message);
 }

 void setup(void){

   pinMode(DRIVE_MOTOR_POWER, OUTPUT);     
   pinMode(DRIVE_MOTOR_DIRECTION, OUTPUT);
   pinMode(STEER_MOTOR_POWER, OUTPUT);
   pinMode(STEER_MOTOR_DIRECTION, OUTPUT);

   Serial.begin(115200);
   WiFi.begin(ssid, password);
   Serial.println("");

//等待连接

   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
   }
   Serial.println("");
   Serial.print("Connected to ");
   Serial.println(ssid);
   Serial.print("IP address: ");
   Serial.println(WiFi.localIP());

   server.on("/", handleRoot);

   server.on("/forward", [](){
     Serial.println("forward");
     analogWrite(DRIVE_MOTOR_POWER, drivePower);
     digitalWrite(DRIVE_MOTOR_DIRECTION, driveDirection);
     server.send(200, "text/plain", "forward");
   });

   server.on("/driveStop", [](){
     Serial.println("driveStop");
     analogWrite(DRIVE_MOTOR_POWER, 0);
     server.send(200, "text/plain", "driveStop");
   });

   server.on("/back", [](){
     Serial.println("back");
     analogWrite(DRIVE_MOTOR_POWER, drivePower);
     digitalWrite(DRIVE_MOTOR_DIRECTION, !driveDirection);
     server.send(200, "text/plain", "back");
   });

   server.on("/right", [](){
     Serial.println("right");
     analogWrite(STEER_MOTOR_POWER, steeringPower);
     digitalWrite(STEER_MOTOR_DIRECTION, steerDirection);
     server.send(200, "text/plain", "right");
   });

   server.on("/left", [](){
     Serial.println("left");
     analogWrite(STEER_MOTOR_POWER, steeringPower);
     digitalWrite(STEER_MOTOR_DIRECTION, !steerDirection);
     server.send(200, "text/plain", "left");
   });

   server.on("/steerStop", [](){
     Serial.println("steerStop");
     analogWrite(STEER_MOTOR_POWER, 0);
     server.send(200, "text/plain", "steerStop");
   });

   server.onNotFound(handleNotFound);

   server.begin();
   Serial.println("HTTP Server Started");
 }

 void loop(void){
   server.handleClient();
 }

1 个答案:

答案 0 :(得分:-1)

您确定已连接到Wi-Fi吗?

这两行看起来不正确:

$this->dpi

应该是:

const char* ssid = "wifi_username";
const char* password = "wifi_password";