The Raspberry Pi's IO Functionality in an easy-to-use API for Mono/.NET/C#
The Raspberry Pi's IO Functionality in an easy-to-use API for Mono/.NET/C#
** THIS REPO HAS BEEN ARCHIVED **
WE ARE LOOKING FOR A NEW HOME FOR THIS PROJECT. APPLY AT: https://adoptoposs.org/p/9f5b74b9-04f2-42b6-891f-c5294c9ef1c5
:star: Please star this project if you find it useful!
The Raspberry Pi's IO Functionality in an easy-to-use API for .NET (Mono/.NET Core). Our mission is to make .NET a first-class citizen in the Python-centric community of Raspberry Pi developers.
This library enables developers to use the various Raspberry Pi's hardware modules:
Pi.Camera Provides access to the official Raspberry Pi Camera module.Pi.Info Provides information on this Raspberry Pi's CPU and form factor.Pi.Gpio Provides access to the Raspberry Pi's GPIO as a collection of GPIO Pins.Pi.Spi Provides access to the 2-channel SPI bus.Pi.I2c Provides access to the functionality of the I2C bus.Pi.Timing Provides access to The PI's Timing and threading API.We offer an additional package with helpful classes to use peripherals, many of them are from pull requests from our contributors. The current set of peripherals supported are:
This version requires .NET core 3.0 to build and run.
In the beginning, RaspberryIO was built around WiringPi library and all our classes, properties, enums, etc. was based on those ones used in WiringPi too.
Now, we are working on a more general version of RaspberryIO (Abstractions) that, could use any core library (WiringPi, PiGpio or even new ones). So, it was necessary to change certain properties and enums for more general ones.
A breaking change in this new general version is the pinout numbering system. As we already explained above, RaspberryIO was using the WiringPi pinout numbering system, but now it uses the BCM pinout numbering system.
Note: The pin numbers are totally different in both systems, so we recommend you to double check carefully the physical pins where you connect any device.
Install basic Raspberry.IO package:
PM> Install-Package Unosquare.Raspberry.IOIf you want to implement your own provider for RaspberryIO, you must use the following package to implement all the Pi providers:
PM> Install-Package Unosquare.WiringPiNote: For now, we have fully implemented the WiringPi library and we are working in the PiGpio implementation.
Note: The latest development builds require .NET core 3.0 to build and run. You should upgrade to the latest Version of Visual Studio first.
Install Raspberry.IO Peripherals package (Optional):
PM> Install-Package Unosquare.RaspberryIO.PeripheralsIt is recommended that you install the latest available release of Mono because what is available in the Raspbian repo is quite old (3.X). These commands were tested using Raspbian Stretch. The version of Mono that is installed at the time of this writing is:
Mono JIT compiler version 5.20.1.19 (tarball Thu Apr 11 19:13 UTC 2019)The commands to get Mono installed are the following:
sudo apt install apt-transport-https dirmngr gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb https://download.mono-project.com/repo/debian stable-raspbianstretch main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
sudo apt update
sudo apt-get install mono-completeNow, verify your version of Mono by running mono --version. Version 4.6 and above should be good enough.
This project can also run in .NET Core. To install .Net Core 3.1 sdk please execute the following commands:
$ sudo apt-get -y update
$ sudo apt-get -y install libunwind8 gettext
$ wget https://download.visualstudio.microsoft.com/download/pr/67766a96-eb8c-4cd2-bca4-ea63d2cc115c/7bf13840aa2ed88793b7315d5e0d74e6/dotnet-sdk-3.1.100-linux-arm.tar.gz
$ sudo mkdir -p /usr/share/dotnet
$ sudo tar -xvf dotnet-sdk-3.1.100-linux-arm.tar.gz -C /usr/share/dotnet
$ sudo ln -s /usr/share/dotnet/dotnet /usr/bin/dotnetIf you want to install just the runtime, use the following commands:
$ sudo apt-get -y update
$ sudo apt-get -y install libunwind8 gettext
$ wget https://download.visualstudio.microsoft.com/download/pr/60d21925-7f8f-4004-9afe-aebd041d2d4a/0db2946738642d7f88f71f7800522e8c/dotnet-runtime-3.1.0-linux-arm.tar.gz
$ sudo mkdir -p /usr/share/dotnet
$ sudo tar -xvf dotnet-runtime-3.1.0-linux-arm.tar.gz -C /usr/share/dotnet
$ sudo ln -s /usr/share/dotnet/dotnet /usr/bin/dotnetNow, verify your version of .Net Core by running dotnet --info.
Visit https://aka.ms/dotnet-download (or https://dotnet.microsoft.com/download/dotnet-core/3.1) to check for newer versions of the .Net Core runtime (or SDK). The Raspberry Pi running the default Raspbian OS needs the versions named "Linux ARM32".
Before start using RaspberryIO, you must initialize Pi class (bootstrapping process) with the valid Abstractions implementation, in order to let Pi know what implementation is going to use:
Pi.Init();Note: This sample code uses the wiringpi-dotnet implementation, you can use this implementation adding the Unosquare.WiringPi nuget package to your project.
172.16.17.54
/home/pi/Playground
pi
raspberry$ dotnet tool install -g dotnet-sshdeploydotnet-sshdeploy push in the same folder where Unosquare.RaspberryIO.Playground.csproj resides and if everything executes correctly you should see an output like this:…net461 but you can change this by either modifying the RuntimeIdentifier property inside the csproj file or supplying it as a parameter like this dotnet-sshdeploy push -f netcoreapp2.2. More information about dotnet-sshdeploy see thisAlternatively, you can use Visual Studio's Publish feature and a SCP (SSH Copy) application such as WinSCP or FileZilla. This uses the example project from this library:
Using Mono:
First of all, you need to give permissions to run the project by running the next command on your target folder:
ubuntu@ubuntu:~/publish$ sudo chmod u+x *Or you just can add the SshDeployExecutePermission tag in your project's csproj file:
trueOr use the appropriate command line argument:
dotnet-sshdeploy push ... -x trueThen you can run your project:
ubuntu@ubuntu:~/publish$ ./Unosquare.RaspberryIO.PlaygroundUsing .Net Core
ubuntu@ubuntu:~/publish$ dotnet Unosquare.RaspberryIO.Playground.dllor
ubuntu@ubuntu:~/publish$ chmod +x Unosquare.RaspberryIO.Playground
ubuntu@ubuntu:~/publish$ ./Unosquare.RaspberryIO.Playground(the first line only needs to be executed once or after an update, since copying may not automatically set the execute bit)
Note: Depending on the underlying library, you may need to use root user privileges to run the app.
RaspberryIO contains useful utilities to obtain information about the board it is running on. You can simply call the Pi.Info.ToString() method to obtain a dump of all system properties as a single string, or you can use the individual properties such as Installed RAM, Processor Count, Raspberry Pi Version, Serial Number, etc. There's not a lot more to this.
Pin reference for the B plus (B+) - Header P1
| BCM | Name | Mode | V | L | R | V | Mode | Name | BCM |
|---|---|---|---|---|---|---|---|---|---|
| 3.3v | 01 | 02 | 5v | ||||||
| 2 | SDA.1 | ALT0 | 1 | 03 | 04 | 5V | |||
| 3 | SCL.1 | ALT0 | 1 | 05 | 06 | GND | |||
| 4 | GPIO. 7 | IN | 1 | 07 | 08 | 1 | ALT0 | TxD | 14 |
| GND | 09 | 10 | 1 | ALT0 | RxD | 15 | |||
| 17 | GPIO. 0 | IN | 0 | 11 | 12 | 0 | IN | GPIO. 1 | 18 |
| 27 | GPIO. 2 | IN | 0 | 13 | 14 | GND | |||
| 22 | GPIO. 3 | IN | 0 | 15 | 16 | 0 | IN | GPIO. 4 | 23 |
| 3.3v | 17 | 18 | 0 | IN | GPIO. 5 | 24 | |||
| 10 | MO |
No open issues yet, or sync has not completed.