ip - 如何从服务器获取 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 的响应(在我向服务器发送消息之后)
解决方案
必须在代码中添加 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 ();
}
(我真的很惊讶超过两天没有人知道如何为我回答这个简单的问题!!!!)
推荐阅读
- javascript - 使用 service worker 从 firebase/firestore 获取数据
- php - 带有 SMTP 的 AJAX jQuery JSON PHPMailer
- multithreading - 当我使用 form.ShowDialog 显示表单时,C# 线程将不起作用
- vbscript - 通过 VBScript 的快捷目标路径
- node.js - 入口点未定义 = index.html 使用 HtmlWebpackPlugin
- android - 片段可以在android的新窗口中启动吗?
- ruby-on-rails - Prawn - Rails 5.2 将两位文本放在同一行但在两侧
- javascript - 将聊天气泡内容向右对齐
- mysql - MySQL 5.7 JSON_EXTRACT 不适用于对象中的带引号的字符串:[错误]“字符串中缺少右引号”
- json - 如何将 JSON 字符串设置为 Postman 变量并在正文中使用它?