Visual Studio and Raspberry Pi

As this blog is mostly about using the Arduino platform this might seem slightly "off topic" but I am working on a robot project that will include a Raspberry Pi along with (at least one) Arduino.

Background is that I have been working on a project that means writing a lot of C and (inevitably) C++ code often building the same code base on an Arduino board and on Microsft Windows using Visual Studio.

I had seen the recent post called “Visual Studio Can Do That?” which covers handy “add in” items and lesser known features to illustrate the broad scope of this brilliant and free IDE. The last item “Deployment” got me wondering about just how easy it might be to write and debug code directly on a device. Looked like the Raspberry Pi is very much doable as the OS running on the board can support SSH (and Virtual Network Computing VNC) to make this straightforward.

To give this a whirl, I soldered a set of headers to a Raspberry Pi Zero W board. 40 pins of hell later… In fact I got the solder pretty neat after the first half dozen. Perhaps there is a lesson for the ham-fisted like me – have some practice stuff set up before tackling the main objective. Anyway, back to the task.

I faced the normal problem of trying to connect a zero to both a keyboard and mouse. I had a USB adapter for the micro port on the Pi board but that only provided a single USB type A female socket. So, just to get started I accepted that I would have to swap between the keyboard and mouse as I went along (well for the first few minutes anyway). This is doable while only being slightly annoying.

Once I had the Pi Zero hooked up, booted and updated I used the Configuration option from the Preferences Menu and clicked the Interfaces tab to set on the SSH and VNC options. I then started a PuTTY session on my windows machine to start an SSH session on the Pi. I needed the IP address for the Pi, typed that in and the connection worked perfectly. I have an IP scanner on my desktop Windows machine that told me the address but you can also get that (if you don’t know) from a terminal window on the Pi using the command ifconfig and looking for the wlan0: section of the output.

I also used a VNC viewer (see below) to connect to the Pi which greatly simplified my controls as I could now use the mouse and keyboard on my desktop machine to control the Pi GUI “desktop” as well. No more swapping USB plugs at the Pi end.

Before starting with Visual Studio you might need to download and install the “Visual C++ for IOT Development” extension from Microsoft.

Once installed you can create a new C++ Cross Platform project targeting Linux. As a first stab, I went for the option to create a “Blink (Raspberry)” project variety as I wanted the library to connect to the GPIO pins included. Visual Studio then displays a very nice “getting started” page to take you through the steps and options required to target and connect to a remote Linux system.

I cobbled together a simple set-up to attach an LED to pin 17 on my Pi.

Parts list.
Solderless breadboard
Small LED
330 ohm resistor
2 jump leads (male to female)
The jumpers connected to BCM pin 17 (GPIO 17) and ground on the Pi.


Once set up, I clicked the run button in Visual Studio to deploy and run the program. As this was the first connection, I was presented with a connection dialogue. This needed the IP address for the PI (as the host name), the username and my password.




The build proceeded and the connection was established but I then got an error message to say that the gdbserver could not be run on the remote device. A quick check found that this was not installed on the Pi so I used the following command (opened a terminal window using the VNC viewer).

sudo apt-get install gdbserver

Once that installation had reached a happy conclusion I re-started the “run” from Visual Studio.

And was treated to a nicely flashing LED on the breadboard.  Looks like I now have a very satisfactory development environment for C++ to target a Raspberry Pi over WiFi. How cool is that?

VNC viewer

You can download a VNC viewer for non-commercial use from RealVNC 

Instructions for installing and setting up a VNC server on your Pi available from the Raspberry project web site although it is built in to the default Raspbian OS so all I had to do was allow the option.

<EDIT>
Forgot to show the rather trivial code

#include <wiringPi.h>

#define LED 17

int main(void)
{
 wiringPiSetupSys();

 pinMode(LED, OUTPUT);

 while (true)
 {
  digitalWrite(LED, HIGH);  // On
  delay(500); // ms
  digitalWrite(LED, LOW);   // Off
  delay(500);
 }
 return 0;
}
</EDIT>

Addendum

Running the Raspberry Pi Zero from a VNC client is a very positive experience and (as mentioned above) reduces the required connections and clutter. One snag from my viewpoint is that I can shut the Pi down from the VNC client but I can't restart it. I could, of course remove and restore the power but that is not always going to be convenient. My solution was to solder (sigh) two more header pins to the board in the positions marked as "RUN" - highlighted, bottom right, with a white background on this image.

I could then attach these to a "momentary make" micro panel switch (12.5 pence each) to trigger a re-boot. This may not be the most powerful Pi around but it is conveniently small, full featured and easy to mount in a project - and I can program it using C.

Comments

Popular posts from this blog

Unicode output from an Arduino.

Arduino Regular Expressions

Arduino: How to scroll long text on OLED screen