I’m trying to use the rest API for the Firebase Realtime Database to transmit data from a Controllino MAXI (essentially an arduino mega 2560 with an ethernet chip) to the database. But I’m having trouble with the HTTP request. All types of requests fail but I’m interested in the PUT request.
Using this online tool, the PUT request works, here’s the raw data:
PUT /.json HTTP/1.1
Host: *rtdb-name*.firebaseio.com
Content-Type: application/json
Content-Length: 26
{"message":"hello world!"}
That request returns this response:
{
"message": "hello world!"
}
And these 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
And writes the data to the root of the realtime db:
But when I do the same thing on the arduino using the Ethernet library:
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");
}
I get a 404 error:
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
I’m not entirely sure how to make this work. I think it’s because the website uses HTTPS and the Mega can only do HTTP? Any assistance would be appreciated
2
Answers
Should have just started with the documentation:
The Arduino (Nano, UNO, Mega and the like) simply don't have the power to do SSL (HTTPS) which is necessary to communicate with Firebase.
I've gone ahead and created a Netlify function that responds to a HTTP POST request from the Arduino and then that function writes the data to Firebase. I got the idea from this tutorial.
As suggested below you can use an MQTT broker.
For realtime datalogging with arduino, nodemcu, consider about using MQTT.