代码之家  ›  专栏  ›  技术社区  ›  sohan vanani

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

  •  0
  • sohan vanani  · 技术社区  · 10 年前

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

    Arduino->西班牙8266

    功率3.3V->vcc,CH_PD

    接地->地

    引脚2->rx公司

    引脚3->时间x

    现在我已经将ESP8266 Library安装到Arduino Board http://arduino.esp8266.com/stable/package_esp8266com_index.json

    我的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("");
    
    }
    

    当我编译代码时,我遇到了错误:

    包含在文件中 /Users/Mohak/Documents/Arduino/librarys/ESP8266WiFi/src/ESP8266WiFi.h:39:0, 来自/Users/Mohak/Desktop/sohan Demos/Arduino Sketches/wifiweb_Server/wifiweb_Server.ino:1: /Users/Mohak/Documents/Arduino/librarys/ESP8266WiFi/src/WiFiClient.h:24:18: 致命错误:内存:没有这样的文件或目录#include ^编译已终止。exit status 1编译错误。

    请帮助我解决这个问题,或给我任何其他正确的路径,以连接我的arduino板和我的wifi网络。任何帮助都将不胜感激。对不起,我的英语不好,请提前表示感谢

    1 回复  |  直到 10 年前
        1
  •  2
  •   Fruchtzwerg    10 年前

    重要信息: 不要这样连接您的ESP!您的arduino的RX将以其5V高信号摧毁ESP!使用分压器降低您的arduino的TX电压!

    下面是一个例子: enter image description here

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

    #include "ESP8266WiFi.h"
    

    现在它应该编译了。