使用 Arduino UNO R3 将 ESP8266-01 连接到 WiFi 网络

Connect ESP8266-01 to WiFi Network with Arduino UNO R3

你好,我是 Arduino 开发的新手。一般来说,我正在开发 iOS 应用程序。 我有 Arduino Uno R3 和 ESP8266-01 模块。我想通过 ESP8266-01 将我的 arduino 板连接到我的 wifi 网络。我的原理图是这样的:

Arduino -> ESP8266

power 3.3V -> vcc , CH_PD

ground -> ground

pin2 -> rx

pin3 -> tx

现在我已经从 http://arduino.esp8266.com/stable/package_esp8266com_index.json

安装了 ESP8266 库到 Arduino Board

我的arduino程序是这样的:

#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";
const char* password = "your-password";

int ledPin = 2; // GPIO2
WiFiServer server(80);

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=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}

// Set ledPin according to the request
//digitalWrite(ledPin, value);

// 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("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF<br>");
client.println("</html>");

delay(1);
Serial.println("Client disonnected");
Serial.println("");

}

当我编译我的代码时出现错误:

In file included from /Users/Mohak/Documents/Arduino/libraries/ESP8266WiFi/src/ESP8266WiFi.h:39:0, from /Users/Mohak/Desktop/sohan Demos/Arduino Sketches/wifiweb_Server/wifiweb_Server.ino:1: /Users/Mohak/Documents/Arduino/libraries/ESP8266WiFi/src/WiFiClient.h:24:18: fatal error: memory: No such file or directory #include ^ compilation terminated. exit status 1 Error compiling.

请帮我解决这个问题或者给我任何其他正确的路径来连接我的arduino板和我的wifi网络。任何帮助,将不胜感激。抱歉我的英语不好,在此先感谢。 :)

重要: 不要这样连接你的ESP!你的 arduino 的 RX 会用它的 5V 高信号破坏 ESP!使用分压器来降低 arduino 的 TX 电压!

这是一个例子:

要包含 ESP 库,请从 here 下载 ESP8266wifi.hESP8266wifi.cpp。现在将这两个文件复制到*.ino 文件的目录中。将您的包含更改为:

#include "ESP8266WiFi.h"

现在应该可以编译了。