首页 > 解决方案 > 如何从服务器获取 Arduino esp 8266 的答案

问题描述

我有一个 Arduino esp 8266 代码,它接收数据,例如实时温度,并通过此代码将数据发送到外部服务器:代码取自这篇文章的基础

           // Check WiFi connection status
          if (WiFi.status () == WL_CONNECTED) {
            HTTPClient http;
            http.begin (serverName);
// If you need an HTTP request with a content type: text / plain
http.addHeader ("Content-Type", "text / plain");
int httpResponseCode = http.POST ("**** The new data for the server ****");
                       if (httpResponseCode> 0) {
                      Serial.print ("HTTP Response code:");
                      Serial.println (httpResponseCode);
                    }
                    else {
                      Serial.print ("Error code:");
                      Serial.println (httpResponseCode);
                    }
                    // Free resources
                    http.end ();
                  }

我现在正在努力实现的目标 --- 一旦服务器接收到信息,它就会做出相应的响应,例如:Pin 2 - On / Off Arduino 解码响应并完成它的工作。
我的问题是我无法了解如何接收服务器对 Arduino 的响应(在我向服务器发送消息之后)

标签: iparduino-esp8266

解决方案


必须在代码中添加 2 行才能从服务器获取 Arduino 的答案: 我在这里找到了它:

                                  String payload = http.getString ();
                                  Serial.println (payload);

如下:

          // Check WiFi connection status
          if (WiFi.status () == WL_CONNECTED) {
            HTTPClient http;
            http.begin (serverName);
 // If you need an HTTP request with a content type: text / plain
    http.addHeader ("Content-Type", "text / plain");
    int httpResponseCode = http.POST ("**** The new data for the server ****");
                       if (httpResponseCode> 0) {
                      Serial.print ("HTTP Response code:");
                      Serial.println (httpResponseCode);
                                          String payload = http.getString ();
                                          Serial.println (payload);
                    }
                    else {
                      Serial.print ("Error code:");
                      Serial.println (httpResponseCode);
                    }
                    // Free resources
                    http.end ();
                  }

(我真的很惊讶超过两天没有人知道如何为我回答这个简单的问题!!!!)


推荐阅读