如何使用 ESP8266 和 Arduino 构建 HTTP 响应
How to build HTTP Response with ESP8266 and Arduino
我想用我的 Android 设备切换一些 LED。这些 LED 连接到我的 Arduino 的数字引脚,Arduino 也连接到 ESP8266。现在,我的 ESP8266 被定义为一个接入点,我的平板电脑可以发送 HTTP 请求(例如 http://192.168.4.1:80/?pin=11)。
我在这里找到代码 http://allaboutee.com/2015/01/20/esp8266-android-application-for-arduino-pin-control/
它工作正常,但我的问题是应该使用哪个 HTTP header 字段?在此代码中,他使用了一些(例如 Content-Length),但还有更多可能(日期、服务器、Content-Language、...)。
这些字段是可选的还是必须使用其中的哪些来构建正确的响应?
这是我不明白的一段代码:
void sendHTTPResponse(int connectionId, String content)
{
String httpResponse;
String httpHeader;
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " ";
sendCIPData(connectionId,httpResponse);
}
这在很大程度上取决于客户(即消费者)哪些字段是必需的,哪些是必填的。
唯一始终需要的是 "HTTP/1.1 200 OK"。当然,如果您没有发送 OK 消息,则需要替换该状态代码。
我想用我的 Android 设备切换一些 LED。这些 LED 连接到我的 Arduino 的数字引脚,Arduino 也连接到 ESP8266。现在,我的 ESP8266 被定义为一个接入点,我的平板电脑可以发送 HTTP 请求(例如 http://192.168.4.1:80/?pin=11)。 我在这里找到代码 http://allaboutee.com/2015/01/20/esp8266-android-application-for-arduino-pin-control/
它工作正常,但我的问题是应该使用哪个 HTTP header 字段?在此代码中,他使用了一些(例如 Content-Length),但还有更多可能(日期、服务器、Content-Language、...)。
这些字段是可选的还是必须使用其中的哪些来构建正确的响应?
这是我不明白的一段代码:
void sendHTTPResponse(int connectionId, String content)
{
String httpResponse;
String httpHeader;
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " ";
sendCIPData(connectionId,httpResponse);
}
这在很大程度上取决于客户(即消费者)哪些字段是必需的,哪些是必填的。
唯一始终需要的是 "HTTP/1.1 200 OK"。当然,如果您没有发送 OK 消息,则需要替换该状态代码。