实时数据库 - Arduino 和 REST API
Realtime Database - Arduino and REST API
我正在尝试使用 Firebase 实时数据库的其余 API 将数据从 Controllino MAXI(本质上是带有以太网芯片的 arduino mega 2560)传输到数据库。但是我在处理 HTTP 请求时遇到了问题。所有类型的请求都失败,但我对 PUT 请求感兴趣。
使用此 online tool,PUT 请求有效,原始数据如下:
PUT /.json HTTP/1.1
Host: *rtdb-name*.firebaseio.com
Content-Type: application/json
Content-Length: 26
{"message":"hello world!"}
那个请求returns这个响应:
{
"message": "hello world!"
}
还有这些headers:
Server: nginx
Date: Wed, 03 Feb 2021 17:02:55 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 26
Connection: keep-alive
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Strict-Transport-Security: max-age=31556926; includeSubDomains; preload
并将数据写入实时数据库的根目录:
但是当我使用以太网库在 arduino 上做同样的事情时:
char server[] = "rtdb-name.firebaseio.com"
if (client.connect(server,80)){
String data = "{\"message\":\"hello world!\"}";
Serial.println("connected");
client.println("PUT /.json HTTP/1.1");
client.println("Host: *rtdb-name*.firebaseio.com");
client.println("User-Agent: Arduino/1.0");
client.println("Cache-Control: no-cache, no-store, must-revalidate");
client.println("Pragma: no-cache");
client.println("Expires: 0");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
while(client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println("disconnected");
}else{
Serial.println("Failed to connect to server");
}
我收到 404 错误:
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1566
Date: Wed, 03 Feb 2021 17:06:07 GMT
Connection: close
我不太确定如何进行这项工作。我认为这是因为该网站使用 HTTPS 而 Mega 只能做 HTTP?如有任何帮助,我们将不胜感激
要使用 arduino、nodemcu 进行实时数据记录,请考虑使用 MQTT。
应该刚开始 documentation:
You can use any Firebase Realtime Database URL as a REST endpoint. All
you need to do is append .json to the end of the URL and send a
request from your favorite HTTPS client.
HTTPS is required. Firebase only responds to encrypted traffic so that
your data remains safe.
Arduino(Nano、UNO、Mega 等)根本没有能力执行与 Firebase 通信所必需的 SSL (HTTPS)。
我已经创建了一个 Netlify 函数来响应来自 Arduino 的 HTTP POST 请求,然后该函数将数据写入 Firebase。我的想法来自 this tutorial。
按照下面的建议,您可以使用 MQTT 代理。
我正在尝试使用 Firebase 实时数据库的其余 API 将数据从 Controllino MAXI(本质上是带有以太网芯片的 arduino mega 2560)传输到数据库。但是我在处理 HTTP 请求时遇到了问题。所有类型的请求都失败,但我对 PUT 请求感兴趣。
使用此 online tool,PUT 请求有效,原始数据如下:
PUT /.json HTTP/1.1
Host: *rtdb-name*.firebaseio.com
Content-Type: application/json
Content-Length: 26
{"message":"hello world!"}
那个请求returns这个响应:
{
"message": "hello world!"
}
还有这些headers:
Server: nginx
Date: Wed, 03 Feb 2021 17:02:55 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 26
Connection: keep-alive
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Strict-Transport-Security: max-age=31556926; includeSubDomains; preload
并将数据写入实时数据库的根目录:
但是当我使用以太网库在 arduino 上做同样的事情时:
char server[] = "rtdb-name.firebaseio.com"
if (client.connect(server,80)){
String data = "{\"message\":\"hello world!\"}";
Serial.println("connected");
client.println("PUT /.json HTTP/1.1");
client.println("Host: *rtdb-name*.firebaseio.com");
client.println("User-Agent: Arduino/1.0");
client.println("Cache-Control: no-cache, no-store, must-revalidate");
client.println("Pragma: no-cache");
client.println("Expires: 0");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.println(data);
while(client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println("disconnected");
}else{
Serial.println("Failed to connect to server");
}
我收到 404 错误:
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1566
Date: Wed, 03 Feb 2021 17:06:07 GMT
Connection: close
我不太确定如何进行这项工作。我认为这是因为该网站使用 HTTPS 而 Mega 只能做 HTTP?如有任何帮助,我们将不胜感激
要使用 arduino、nodemcu 进行实时数据记录,请考虑使用 MQTT。
应该刚开始 documentation:
You can use any Firebase Realtime Database URL as a REST endpoint. All you need to do is append .json to the end of the URL and send a request from your favorite HTTPS client.
HTTPS is required. Firebase only responds to encrypted traffic so that your data remains safe.
Arduino(Nano、UNO、Mega 等)根本没有能力执行与 Firebase 通信所必需的 SSL (HTTPS)。
我已经创建了一个 Netlify 函数来响应来自 Arduino 的 HTTP POST 请求,然后该函数将数据写入 Firebase。我的想法来自 this tutorial。
按照下面的建议,您可以使用 MQTT 代理。