IoT: Raspberry Pi 2, C# and blinking LED
This week I decided to visit Lee’s Electronic in order to buy some sensors and capacitors for my future posts and, just in case, I asked if they have Raspberry PI 2 boards. The answer was: “Yes, we just received the package with plenty of them. Do you need just one?”
Of course, I was happy to get Raspberry Pi 2 because I still didn’t make any experiments with this board. I have a Raspberry Pi B+ but it’s “too lazy” for IoT projects. Of course, it has better CPU than Arduino and Netduino but developer experience is crazy there. So, I decided to avoid Raspberry up to more complex projects, which require cameras, GPU etc. But Raspberry Pi 2 should have better performance. I even heard that it should be in 6 times fasterJ So, I decided to buy a board.
So, Raspberry Pi 2 has the same form factor like B+ model.
And you can use the same operation system like before (several versions of Linux and RISK OS) but if you have a B+ board and you are going to use the same microSD, you need to rewrite operation system there. I have checked several articles and looks like that everybody recommends to use Debian/Raspbian, so I decided to use Raspbian as well.
Additionally I am using Edimax WiFi adapter to connect my Raspberry to WiFi. It’s a very common adapter and you should not make any additional actions in order to configure it. Just open Wi-Fi configuration window to select the right WiFi network and once your board is connected, you need to note assigned IP address. Finally, before disconnecting your board from the keyboard and the monitor, you need to activate xrdp:
sudo apt-get install xrdp
I discovered that many people use Putty and Xming in order to connect Raspberry in desktop mode. I installed these tools and I was playing with them some time but, finally, I decided to use Remote Desktop Connection, which is part of Windows 8, doesn’t require any actions to install it and allows to make any configuration changes very quickly.
Of course, I am a C# guy, so in the next step I installed mono-runtime and Mono Develop there.
$ sudo apt-get update
$ sudo apt-get install mono-runtime
$ sudo apt-get install monodevelop
Once Mono develop is installed I was ready to start my experiments. First of all, the latest board is really faster. I didn’t feel “6 times performance” but it requires no more than 10 seconds to launch Mono develop and 3-5 seconds to create a new project. At the same time Intellisense system works pretty good, compiler and debugger are fast as well. It still is no as fast as my Lenovo W530J but it’s possible to develop and debug IoT projects directly on board.
Additionally I think that there are some opportunities to increase speed of development once Windows 10 is available there. Probably it’s just my feeling but I think that Raspbian has the same problem as some versions of Android and simply doesn’t use all CPU cores in effective manner. So, you cannot see real performance improvement, which is guaranteed by additional cores.
The first problem, which I got during my experiments, was unusual behavior of Mono Develop. Once I created a new Console application and wrote my first “Hello World” there I got several compilation errors. I believe that it’s related to configuration of the template. I spent about 10 minutes to understand what has happened there but, finally, I decided to use a trick: I just created an Empty Project and added a class there. Inside the class I added Main method and launched my first “Hello World” without any problems.
The second problem is the right library to use GPIO from C#. It required much more time (I even wanted to start a new open source project) but finally I found a good library. Compared to other libraries this one doesn’t have problems with resources and pin locks, doesn’t require any additional actions to start using it, has good architecture and provides many examples.
In order to start using the library you need to download and recompile it. I made it in Mono develop and didn’t have any problems there. After that you need to add the assembly to your project. It’s easy to do using Edit references… dialog from the context menu there. Once you add the reference to the assembly you are ready to start development. Pay special attention that the library contains two similar sets of classes: GPIOMem and GPIOFile. The first one requires an additional library and because already precompiled version doesn’t work on my board I decided to use GPIOFile.
In order to initialize a pin you need to use the following code:
GPIOFile l=new GPIOFile(GPIOPins.V2_Pin_P1_07, GPIODirection.Out);
In order to understand, which pin you need to initialize, you can check the specification on raspberrypi.org. You can see that pin 7 is GPIO 4 and you can use it to output. You can use pins 2 or 4 in order to get 5V voltage.
Once you pin is ready you can use it to send signals there:
So, finally I made my blinking LED on Raspberry.