Is .NET Core cool enough to cool a Raspberry Pi? - Part 224 Apr 2020
Bring it all together
During the development, I could easily test my app by using the Inverse of Control pattern and utilizing Dependency Injection to inject a fake temperature provider and fan controller. At a certain point, I was ready to test it on the Raspi.
At first, I was convinced to deploy the app via Docker. But after some time, I was not sure whether a
sudo command executed from within a Docker container will be forwarded to the OS (remember the temperature measurement). So I decided to ship it as a self-contained executable. This can be done as follows:
The following command copies the build results to the Raspi:
On the Raspi, we have to allow the app to be executed:
And finally, start the app using
sudo. This is important because otherwise, reading the temperature doesn’t work.
There were some firewall/reverse proxy issues in my case, but that would be beyond this post. In the end, I could successfully access the app via http://raspberry:5000/cool and it was showing the current temperature.
This was definitely the hardest part for me in this project. But several other blog posts like the following gave me the necessary information which components I had to buy and how to connect them:
- Automatically Control Your Raspberry Pi Fan (and Temperature) with Python
- Temperature Controlled Fan for Raspberry Pi 4
Finally, I bought:
- Red LED
- Transistor BC 337
- Resistor 680 Ω for the Transistor
- Resistor 1 kΩ for the LED
- Jumper wires
Because I was afraid to somehow destroy the fan, I made a first test with my controller software and the LED:
After a successful test, I switched over and used the fan:
And it was working! So I had no more excuses to solder everything and do the final assembly:
Register the app as a service
Now that everything was working fine, I wanted to register my little app as a service. This will ensure that the controller automatically gets started after a reboot.
For this, I had to create a service unit configuration file on the Raspi:
It has the following content:
With the following commands, the service will be created:
Now the app will start on every reboot.
Because of recent developments in the .NET ecosystem, I was able to write a controller for a Linux device like a Raspberry Pi. I could leverage all the new features my favorite platform provides:
- Worker Services
- ASP.NET Core Blazor Server
In theory, this app could be easily ported to any other OS like Windows 10 IoT Core or device like Arduino - if the necessary parts like temperature retrieval are available.
For me, this was another great experience of how the modern development world can look like: serve every platform with the tools of your choice.
Thank you for reading!