Firebase Rest on Raspberry Pi

I’ve tried to use Google’s Firebase with the Firebase Admin SDK in my C# project. The good news is that the Admin SDK is easy to use and all you need to do is call two methods. The authentication part is handled by an environment variable which points to some .json file which you download. The bad news is that the SDK depends on gRPC and the dotnet version of it doesn’t work well on the Raspberry Pi.

What I’m going to do is to replace the admin SDK with simple REST API calls, this way I can post data to Firebase from anywhere, at this moment I only want to post data from the Raspberry Pi.

I’ve been browsing the REST API Reference docs in in order to figure it out how to make requests and after some time of experimenting and reading the documentation I’ve translated by hand a simple telemetry request from my Raspberry Pi into a restful Firebase request.

This is how it looks like in the Firebase format:

PATCH https://firestore.googleapis.com/v1/projects/project_name/databases/(default)/documents/collection_name/doc_id

{
	"name": "batman",
	"fields": {
		"source": {
			"stringValue": "NucuCar.Sensors"
		},
		"timestamp": {
			"timestampValue": "2019-12-01T23:26:13.5537227+02:00"
		},
		"data": {
			"arrayValue": {
				"values": [
					{
						"mapValue": {
							"fields": {
								"sensor_state": {
									"integerValue": 2
								},
								"cpu_temperature": {
									"doubleValue": 48
								},
								"_id": {
									"stringValue": "CpuTemperature"
								}
							}
						}
					},
					{
						"mapValue": {
							"fields": {
								"sensor_state": {
									"integerValue": 2
								},
								"temperature": {
									"doubleValue": 32
								},
								"humidity": {
									"doubleValue": 100.0
								},
								"pressure": {
									"doubleValue": 6222
								},
								"voc": {
									"doubleValue": 0.0
								},
								"_id": {
									"stringValue": "Bme690-Sensor"
								}
							}
						}
					}
				]
			}
		}
	}
}

All what’s left to do now is to write a translator which translates raw C# dictionaries into the format from above. I want to reuse this without much hassle if my telemetry data format changes.

After a bit of trial and error I’ve managed to implement a translator, thus completely dropping the dependency for Firebase’s Admin SDK. I haven’t implemented authentication handling yet, but it’s on my board.

Thank you for reading!

My experience with .NET IoT (so far)

cover

I haven’t done any embedded development in a while and I was thinking to build myself a remote controlled toy car with video streaming.

The project is going to take me a while. To build it I’m going to use C#. I’m already too familiar with Python and a little challenge doesn’t hurt.

To interface with the hardware, I’m going to use the .Net Core IoT Libraries from Microsoft.


My idea was to create a set of .Net Core services that communicate internally via gRPC and an ASP.Net Core web application to control the car. (Now that I’m writing this I’m thinking that could possibly drop the web app and use gRPC directly from the client).

The client would be a desktop application that will display the video stream and data from the sensors. It should work with an Xbox controller or else it won’t be fun. 😦

To write it I will probably use Mono or Unity.


After burning the latest version of Raspbian on the board using Etcher and spending a few hours in Rider developing a base for the app. I’ve Installed .Net Core 3 SDK and ran the application. The results were amazing! I’ve checked the thermostat in the office and it was set on 25

readings

And the application only consumes ~10MB RAM! That’s awesome!


I’m really impressed and so far things are looking bright for dotnet/iot. I can’t wait to test the gRpc support and other hardware modules.

The full code can be found here: https://github.com/dnutiu/NucuCar

If you want to run the app on your Raspberry you’ll need to install the .Net Core Sdk (Instructions) and run the following commands:

git clone https://github.com/dnutiu/NucuCar.git
cd NucuCar
dotnet build
cd NucuCar.Sensors
dotnet run


Thanks for reading!

I hope you will give dotnet/iot a try for your next project!