http - 为什么 ESP8266 Get Request Payload 是奇怪的字符串
问题描述
我有一个与 esp8266 通信的 asp.net mvc 服务器,它运行良好,但是对于第一个小时,有效负载包含奇怪的字符串并且不工作,对于接下来的几个小时,有效负载是正确的。这是 asp.net mvc 操作代码:
[HttpGet]
public string DeviceGetData(string Info)
{
string[] Data = Info.Split('-');
string DeviceType = Data[0];
string DeviceSerial = Data[1];
string DeviceKey = Data[2];
using (SqlConnection newConnection = new SqlConnection(
"Persist Security Info = True;" +
"User ID = VenaServer;" +
"Password = ********;" +
"Initial Catalog = engineer_VenaServer;" +
"Data Source = 178.22.122.9;"
))
{
newConnection.Open();
SqlDataAdapter LoginAdapter = new SqlDataAdapter()
{
SelectCommand = new SqlCommand("SELECT * FROM [Engineer_VenaServer].[VenaServer].[Device] " +
"WHERE DeviceType = @DeviceType AND DeviceSerial = @DeviceSerial AND DeviceKey = @DeviceKey;", newConnection)
};
LoginAdapter.SelectCommand.Parameters.AddWithValue("DeviceType", DeviceType.ToLower());
LoginAdapter.SelectCommand.Parameters.AddWithValue("DeviceSerial", DeviceSerial.ToLower());
LoginAdapter.SelectCommand.Parameters.AddWithValue("DeviceKey", DeviceKey.ToLower());
DataTable DataTable = new DataTable();
LoginAdapter.Fill(DataTable);
if (DataTable.Rows.Count == 0)
{
newConnection.Close();
return "First Add Device To Server";
}
else
{
return (DataTable.Rows[0][4].ToString()) + "-" + (DataTable.Rows[0][5].ToString());
}
}
}
这是 esp8266 代码:
//-------------Libraries----------
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
//-------------Public Objects----------
ESP8266WiFiMulti wifiMulti;
WiFiClient client;
HTTPClient http;
//-------------Public Variables----------
String ApiKey = "A1-0000-00000000";
int Sun = 0;
int Moon = 0;
unsigned long previousMillis = 0;
//-------------initial Commands----------
void setup() {
Serial.begin(115200);
//WiFi.mode(WIFI_AP_STA);
//-------------Wifi Connection SSID And PASSWORD----------
wifiMulti.addAP("HUAWEI nova 5T", "987654321");
wifiMulti.addAP("Bing", "&@$*M*$@&");
wifiMulti.addAP("Jimboo", "jimboo4757");
Serial.println("Connecting Wifi...");
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
}
}
//-------------Loop----------
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 250) {
previousMillis = currentMillis;
if (wifiMulti.run() == WL_CONNECTED) {
HttpGet("http://venaserver.protronics.ir/Communication/DeviceGetData?Info=" + ApiKey);
}
else {
Serial.println("WiFi Disconnected");
}
}
}
void HttpGet(String Route) {
http.begin(client, Route);
//Serial.println(Route);
int httpResponseCode = http.GET();
String payload = http.getString();
if (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY) {
Sun = (payload.substring(0, payload.indexOf('-'))).toInt();
Moon = (payload.substring((payload.indexOf('-') + 1), payload.length())).toInt();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Payload : ");
Serial.println(http.getString());
Serial.println("");
/*Serial.print("Sun Value :");
Serial.println(Sun);
Serial.print("Moon Value :");
Serial.println(Moon);*/
}
else {
Serial.println(http.errorToString(httpResponseCode).c_str());
}
http.end();
}
它返回代码 200 并且可以工作,但有效负载应该返回介于“0-255”到“255-0”之间的东西,但返回奇怪的字符,例如:ESP8266 控制台
我应该怎么做?注意:它适用于更新 esp8266 库和 Arduino IDE。
您可以在此链接上测试数据:http: //venaserver.protronics.ir/Communication/DeviceGetData?Info= A1-0000-00000000
解决方案
推荐阅读
- c# - 单元测试没有完成,被测试的方法只是抛出了一个异常。我错过了什么?
- javascript - react-router:如何在不导致导航/重新加载的情况下更新 url?
- python - 将关键字值添加到下一行,直到找到下一个关键字 - Python
- c# - 你可以在实体框架中使用分组吗
- jbpm - 嵌入 jbpm Web Designer 时属性编辑器不显示
- java - 等到元素中出现的文本不是提供的字符串
- javascript - 如何根据最近点击的按钮调用不同的功能?
- delphi - 为 RAD 服务器动态设置数据库参数
- python - 如何在不创建实例的情况下选择性地访问类方法
- c++ - 文字 `0` 是 int 和 const string& 重载的有效候选者会导致模棱两可的调用