首页 > 解决方案 > 为什么 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

标签: httpgethttpclientesp8266payload

解决方案


推荐阅读