ESP8266代码无法正常工作

时间:2017-09-01 15:28:21

标签: asp.net-mvc arduino localhost iot esp8266

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

  1. 与我当地的Wifi连接
  2. 分配静态IP
  3. 当我点击192.168.1.101/gpio/1(LED ON)
  4. 当我点击192.168.1.101/gpio/0(Led OFF)它正在工作但无法访问我的网站。
  5. 当我点击192.168.1.101/gpio/1时,它应该点击我本地托管的网址(192.168.1.100/home/ardchk)
  6. 请帮助我解决这个问题。

    #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
    }
    

1 个答案:

答案 0 :(得分:0)

根据您的问题/评论,我假设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();
  }

希望这有帮助!