.NET Core and RaspberryPi (Raspbian) to read temperature from DHT11 sensor

In the previous article I introduced how install .NET Core on the Raspberry running Raspbian.
Today I will present how to read data from the DHT11 sensor. It’s a temperature and humidity sensor.

Using the GPIO

Before trying to cable the sensor, you will need to download and install WiringPi. This set of librairies will let you manage in an easy way the GPIO port.
In my case, I didn’t waited to clone the github repo so I just downloaded a copy:
sudo wget https://git.drogon.net/?p=wiringPi;a=snapshot;h=96344ff7125182989f98d3be8d111952a8f74e15;sf=tgz
I then unzip the downloaded archive:
sudo tar xfz wiringPi-96344ff.tar.gz

Finally just go to the unzip folder and run:
sudo./build
This will run a script that will install WiringPi on your machine.
In order to validate the installation you should run:
gpio -v

This is the first step, you need now to create the shared objects, like explain in this page

Navigate to the directory where WiringPi is install and then run:

sudo cc -shared wiringPi.o -o libwiringPi.so
sudo cc -shared wiringPiI2C.o -o libwiringPiI2C.so
sudo cc -shared wiringPiSPI.o -o libwiringPiSPI.so

The installation of WiringPi is now complete to be used from a .NET application

Plug the DHT11

Nothing really complicated here, I just plugged the data pin of the sensor to the GPIO22 of the raspberry.
raspib-gpio_lightbox

Making the .NET Core application

Now you can focus on the .NET code. You will need to make a wrapper to access the methods of the wiringPi object.
To do so, you can go here and have a look to the wrapper implemented. It’s a classic .NET project, however, this code would perfectly compile in .NET Core project.

The WiringPi wrapper

To create the wrapper, create a library project with Powershell. In my case I will just take the method related to initialisation and GPIO.

Run dotnet restore and then dotnet build p:AllowUnsafeBlocks=true

You can see here all the parameters you can use for the build command here

The consuming project

Let’s create a new console application and you will add a reference to the wrapper we did before.
In order to do that, you will have to edit the csproj file and add something like that:

<ItemGroup>
<ProjectReference Include="..\path_to_my_project\my_project.csproj" />
</ItemGroup>

Now you reference the wrapper you just created, you can now try to access the GPIO.

You can find here an exemple of code in c.

Before you try to translate this code to C#, let me tell you that it will not work… I know that’s sad.
But why it will not work? Unfortunately, the sensor needs to receive really fast input in order to send back datas. We are speaking here of time like 20 to 80µs.
And .NET will not be able to be as fast with a wrapper in the middle.
So your only chance is to write some C++ code!
Not much but here you have the content of the .h file:

#ifndef _DHTLIB
#define _DHTLIB

#ifdef __cplusplus
extern "C" {
#endif
extern bool InitDHT(int pinval);
extern float getTemp();
extern float getHumidity();
extern bool dht11_read_val();
#ifdef __cplusplus
}
#endif

#endif

I will let you reuse the code you saw in the c library on the previous link.

Then you will have to send the .h file and the .cpp file over the raspberry pi in order to compile them.

Building C++ code

So to build this code you will just do this:
sudo gcc -o mycode.o mycode.cpp -lwiringPi
-lwiringPi will telle the compiler to link to the wiringPi shared object.
Then you may want to put your utility code in a shared object also:
sudo cc -shared mycode.o -o mycode.so
Now you create a wrapper for your utility.
It’s exactly the same logic as before.

Now you can continue to work on your console application in order to call your utility.

Once it’s done you can do like in the previous article to deploy your code.

Votre commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l’aide de votre compte WordPress.com. Déconnexion /  Changer )

Photo Google

Vous commentez à l’aide de votre compte Google. Déconnexion /  Changer )

Image Twitter

Vous commentez à l’aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l’aide de votre compte Facebook. Déconnexion /  Changer )

Connexion à %s