2015-09-04 36 views
1

我正在使用ESP8266模块,以便在线传感传感数据。我将它设置为thingspeak,并使用简单的GET请求发送值。正确的POST请求从Arduino流式传输数据

现在我试图复制流程与情节数据流服务,但我无法弄清楚我的请求有什么问题。

传统图书馆(Wifi,以太网等)有一个println()方法打印到套接字。由于找不到任何可靠的东西,我不得不实现我自己的ESP库,并且注意到设备在将某些东西发送到套接字后经常处于“忙碌”状态,这阻止了我通过发送请求块块像这样的:

client.println("POST/HTTP/1.1") 
client.println("Host: arduino.plot.ly") 
client.println("{\"x\":15, \"y\": 3, \"streamtoken\": \"urqcbfmjot\"\"}") 

于是,我就一下子写请求。我通过潜入依赖Wifi工作的剧情arduino图书馆员找到了这个请求的参数(这就是为什么我不能和ESP一起使用)。直到现在我还没有推送任何数据。这里是代码reponsible的发送请求块:

void pushData(String temp, String humid, String pres, String lum) 
{ 
    bool status = esp8266.openTCPConnection(IP, PORT); 

    char call[] = "POST/HTTP/1.1\r\n"; 
    strcat(call, "Host: arduino.plot.ly\r\n"); 
    strcat(call, "User-Agent: Arduino\r\n"); 
    strcat(call, "Transfer-Encoding: chunked\r\n"); 
    strcat(call, "Connection: close\r\n"); 
    strcat(call, "\r\n"); 
    strcat(call, "\r\n{\"x\":15, \"y\": 3, \"streamtoken\": \"urqcbfmjot\"\"}\n\r\n"); 

    if (!status) return; 

    esp8266.send(call); 
} 

void Esp8266::send(String content) 
{ 
    String cmd; 
    String msg = "Sent : "; 
    bool status; 

    printDebug("Writing to TCP connection"); 
    printDebug("Content to write :"); 
    printDebug(content); 
    cmd = "AT+CIPSEND=" + String(content.length()); 
    espSerial.println(cmd); 
    printDebug("Sent : " + cmd); 

    status = checkResponse(">", 10); 
    if (status) 
    { 
     espSerial.print(content); 
     printDebug("Content sent"); 

     } else { 
     printDebug("Cursor error"); 
     closeTCPConnection(); 
    } 

} 

我想补充一点,我已经成功地测试了他们的documentation,卷曲提供的请求,但它在我的实现太失败了。要求是:

POST HTTP/1.1 
Host: stream.plot.ly 
plotly-streamtoken: urqcbfmjot 

{ "x": 10, "y": 2 } 

任何帮助将不胜感激。有关参考资料,请参阅我的项目的repository

随意使用我的图形进行测试。主机是stream.plot.ly(来自doc)或arduino.plot.ly(来自库)。我的流令牌是urqcbfmjot并且这里是the link to the plot

回答

1

您需要将您的数据发送到chunked format

POST/HTTP/1.1 
Host: stream.plot.ly 
Transfer-Encoding: chunked 
plotly-streamtoken: urqcbfmjot 

14 
{ "x": 10, "y": 2 } 

的单数据更新HTTP请求应该是这样的:

char call[] = "POST/HTTP/1.1\r\n"; 
strcat(call, "Host: stream.plot.ly\r\n"); 
strcat(call, "Transfer-Encoding: chunked\r\n"); 
strcat(call, "plotly-streamtoken: urqcbfmjot\r\n"); 
strcat(call, "\r\n"); 
strcat(call, "11\r\n"); // 11 is the length of the JSON string + "\n" in hexadecimal format 
strcat(call, "{\"x\":15, \"y\": 3}\n\r\n"); 

发送另一个数据点,则不需要再进行一次连接。如果保持现有的流连接打开,你可以发送下一个数据块

esp8266.send("12\r\n{\"x\":30, \"y\": 10}\n\r\n"); 

但acording该文件,

在客户端停止超过一分钟的时间周期较长的写入数据。如果一分钟没有从客户端收到任何数据,流连接将被关闭。 (可以通过在60秒窗口内写入心跳来维持连接,心跳只是一个换行符)。

如果您在一分钟内不发送任何内容,服务器将关闭连接。如果你不需要在一分钟内发送任何东西,你可以用一个新的行字符"1\r\n\n\r\n"发送一个块。

但是如果连接因其他原因而下降并尝试重新连接,您可能仍然需要检测断开连接。