ESP8266代码无法正常工作

问题描述:

这是打开/关闭LED的ESP8266代码。我正在使用Arduino IDE内置的示例代码。该LED工作正常,但我想发送HTTP请求到我本地托管的网站(发送电子邮件),但它不起作用。ESP8266代码无法正常工作

  1. 跟我的本地WiFi
  2. 分配一个静态IP
  3. 当我打192.168.1.101/gpio/1(Led ON)
  4. 当我打192.168.1.101/gpio/0(Led关)它的工作,但无法打我的网站。
  5. 当我打192.168.1.101/gpio/1那么就应该打我的本地托管的URL(192.168.1.100/home/ardchk)

请帮助我理清这个问题。

#include <ESP8266WiFi.h> 

const char* ssid = "SMART"; 
const char* password = "123456789"; 
const char* host = "192.168.1.100"; // Your domain 
IPAddress ip(192, 168, 1, 101); // where xx is the desired IP Address 
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network 
IPAddress subnet(255, 255, 255, 0); 
WiFiServer server(80); 

void setup() { 
    Serial.begin(115200); 
    delay(10); 
    // prepare GPIO3 
    pinMode(3, OUTPUT); 
    digitalWrite(3, 0); 
    // Connect to WiFi network 
    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 
    WiFi.config(ip,gateway,subnet); 
    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.println(WiFi.localIP()); 
} 

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 req = client.readStringUntil('\r'); 
    Serial.println(req); 
    // Match the request 
    int val; 
    if (req.indexOf("/gpio/0") != -1) { 
    val = 0; 
    if (client.connect(host, 80)) { 
     //starts client connection, checks for connection 
     Serial.println("connected to website/server"); 
     client.println("GET /home/ardchk HTTP/1.1"); //Send data 
     client.println("Host: 192.168.1.100"); 
     Serial.println("Email Sended"); 
     client.println("Connection: close"); 
     //close 1.1 persistent connection 
     client.println(); //end of get request 
    } 
    } else if (req.indexOf("/gpio/1") != -1) { 
    val = 1; 
    } else { 
    Serial.println("invalid request"); 
    client.stop(); 
    return; 
    } 
    // Set GPIO2 according to the request 
    digitalWrite(3, val); 
    client.flush(); 
    // Prepare the response 
    String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now "; 
    s += (val)?"high":"low"; 
    s += "</html>\n"; 
    // Send the response to the client 
    client.print(s); 
    delay(1); 
    Serial.println("Client disconnected"); 
    // The client will actually be disconnected 
    // when the function returns and 'client' object is destroyed 
} 
+2

请解释当您尝试点击网址时会发生什么。 “不工作”不清楚 – Billa

+1

您需要学会拼写(随机大写字母不会被视为正确的拼写),并正确缩进您的代码。幸运的是,Arduino IDE为您做到了这一点:按下ctrl-T ... – dda

+0

代码块:if(client.connect(host,80)) {此代码未运行,因为未与主机连接}甚至网站托管于同一网络 –

根据您的问题/评论,我假设client.connect(host, 80)返回false。

我相信你无法连接到你的host,因为你正在尝试连接两次相同的client

你的代码如下所示:

// returns an already connected client, if available 
WiFiClient client = server.available() 

//... 

if (client.connect(host, 80)) {/*...*/} 

你看,你正在使用的已经连接客户端尝试连接到主机。相反,尝试创建该作业单独WiFiClient

WiFiClient requesting_client = server.available(); 
//... 
if (req.indexOf("/gpio/0") != -1) { 
    val = 0; 
    // create a fresh, new client 
    WiFiClient emailing_client; 
    if (emailing_client.connect(host, 80)) { 
    // ... 
    // don't forget that you need to close this client as well! 
    emailing_client.stop(); 
    } 

希望这有助于!