flying03 发表于 2016-1-30 21:03:44

esp8266在loop中连续发包,但只有第一包服务端有返回

如题,使用一块ESP-01,例子代码如下,http请求到服务端,服务端是没问题的,但loop中只返回一次结果,后面就connection fail了,测好久了都没办法,请大家指点

/*
*This sketch sends data via HTTP GET requests to data.sparkfun.com service.
*
*You need to get streamId and privateKey at data.sparkfun.com and paste them
*below. Or just customize this script to talk to other HTTP servers.
*
*/

#include <ESP8266WiFi.h>

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

const char* host = "data.sparkfun.com";
const char* streamId   = "....................";
const char* privateKey = "....................";

void setup() {
Serial.begin(115200);
delay(10);

// We start by connecting to a 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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
delay(5000);
++value;

Serial.print("connecting to ");
Serial.println(host);

// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
}

// We now create a URI for the request
String url = "/input/";
url += streamId;
url += "?private_key=";
url += privateKey;
url += "&value=";
url += value;

Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}

flying03 发表于 2016-1-31 10:09:58

之前没用<ESP8266WiFi.h>库直接写AT指令时,记得有一个关闭TCP连接的AT指令,这个库里没有相关的关闭方法,难道数据包发送完只能delay几秒等他自动断开TCP连接才能开始下一个循环?

flying03 发表于 2016-1-31 10:59:09

为了排除服务端的问题,自建了WEB服务器,可惜也只能接到4包数据。。

Requesting URL: /test.ashx?private_key=key123456&value=4
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 31 Jan 2016 02:55:34 GMT
Connection: close
Content-Length: 7

value:4
closing connection


再向下就连接失败了。。
connecting to 139.196.12.2
connection failed
页: [1]
查看完整版本: esp8266在loop中连续发包,但只有第一包服务端有返回