This is the first chapter of our ongoing book on using the Raspberry Pi in C. It starts the ball rolling by explaining how to get NetBeans, a C/C++ IDE, up and running. We get as far as a first "Hello World" program..
There are lots of ways to program the Pi from Scratch, through Python to assembler. Each language has its advantages and disadvantages but there are times when subtle arguments aren't necessary. If you want to program the Pi in a way that extracts the maximum efficiency and without any limits then C is the obvious choice. It is easier to program in C than in assembler and the results are almost the same in terms of speed.
You can describe C as a machine independent assembly language and hence when you learn it you get deeper into the system than with other languages - you tend to really know what is going on. This makes it a good way to improve your understanding of computers and computing in general.
Now On Sale!
You can now buy a print or ebook edition of Raspberry Pi IoT in C from Amazon.
For Errata and Listings Visit: IO Press
This our ebook on using the Raspberry Pi to implement IoT devices using the C programming language. The full contents can be seen below. Notice this is a first draft and a work in progress.
Chapter List
-
Introducing Pi (paper book only)
-
Getting Started With NetBeans In this chapter we look at why C is a good language to work in when you are creating programs for the IoT and how to get started using NetBeans. Of course this is where Hello C World makes an appearance.
-
First Steps With The GPIO
The bcm2835C library is the easiest way to get in touch with the Pi's GPIO lines. In this chapter we take a look at the basic operations involved in using the GPIO lines with an emphasis on output. How fast can you change a GPIO line, how do you generate pulses of a given duration and how can you change multiple lines in sync with each other? -
GPIO The SYSFS Way
There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the bcm2835 library. Sometimes you need this because you are working in a language for which direct access to memory isn't available. It is also the only way to make interrupts available in a C program. -
Input and Interrupts
There is no doubt that input is more difficult than output. When you need to drive a line high or low you are in command of when it happens but input is in the hands of the outside world. If your program isn't ready to read the input or if it reads it at the wrong time then things just don't work. What is worse is that you have no idea what your program was doing relative to the event you are trying to capture - welcome to the world of input. -
Memory Mapped I/O
The bcm2835 library uses direct memory access to the GPIO and other peripherals. In this chapter we look at how this works. You don't need to know this but if you need to modify the library or access features that the library doesn't expose this is the way to go. -
Near Realtime Linux
You can write real time programs using standard Linux as long as you know how to control scheduling. In fact it turns out to be relatively easy and it enables the Raspberry Pi to do things you might not think it capable of. There are also some surprising differences between the one and quad core Pis that make you think again about real time Linux programming. -
PWM
One way around the problem of getting a fast response from a microcontroller is to move the problem away from the processor. In the case of the Pi's processor there are some builtin devices that can use GPIO lines to implement protocols without the CPU being involved. In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos. -
I2C Temperature Measurement
The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one. -
A Custom Protocol - The DHT11/22
In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in implementing a custom protocol directly in C. -
One Wire Bus Basics
The Raspberry Pi is fast enough to be used to directly interface to 1-Wire bus without the need for drivers. The advantages of programming our own 1-wire bus protocol is that it doesn't depend on the uncertainties of a Linux driver. -
iButtons
If you haven't discovered iButtons then you are going to find of lots of uses for them. At its simples an iButton is an electronic key providing a unique coce stored in its ROM which can be used to unlock or simply record the presence of a particular button. What is good news is that they are easy to interface to a Pi. -
The DS18B20
Using the software developed in previous chapters we show how to connect and use the very popular DS18B20 temperature sensor without the need for external drivers. -
The Multidrop 1-wire bus
Some times it it just easier from the point of view of hardware to connect a set of 1-wire devices to the same GPIO line but this makes the software more complex. Find out how to discover what devices are present on a multi-drop bus and how to select the one you want to work with. -
SPI Bus
The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. -
SPI MCP3008/4 AtoD (paper book only)
-
Serial (paper book only)
-
Getting On The Web - After All It Is The IoT (paper book only)
-
WiFi (paper book only)
Sometimes you don't need speed, even in an IoT application. For example, if you just want to flash a few LEDs or read a temperature sensor in a human time scale then you can write in almost any language. However, if you want to directly interface with other systems and control externally connected hardware at its full speed then C is your best choice. Similarly, if timing is critical, then C is the only way to go.
Put simply if you can't make your application work fast enough in C then you probably can't in any language.
You might be worried that you are some how trapping yourself in a low level language and will be unable to take advantage of sophisticated programming practices. Your get out clause is that C is a subset of C++ and you can break out of C anytime you feel like it and start writing code that has objects and all the trimmings of a modern language. Of course the code you write might not be quite as efficient as the C code you have had to give up but it is still fast.
So C is worth learning.
How do we get started?
Getting Started In C
You can program in C in many different ways. All of the software you need to run a C program is already installed on a standard Pi running Raspbian. You can use an editor on the Pi to create your program and then compile and run it using the command line. However there are easy to use IDEs that make programming in C fast and painless and they provide debugging facilities that make finding bugs much quicker.
You could use Genny, Eclipse or NetBeans - they are all open source but in practice NetBeans is the most powerful and this is the approach we are going to use. If you want to use one of the others or just a text editor and the command line then you can and everything described will work - but NetBeans is worth a try because the help it gives you with generating code and debugging will save you a lot of time in the long run. It is also open source and free to use.
There are two distinct ways you can work with NetBeans and the Pi:
- You could install NetBeans on a Raspberry Pi and work with it via the GUI desktop i.e. directly on the Pi. In this case you are using the Pi as if it was a full desktop computer and it is your development and test machine.
- You could connect the Pi to a desktop machine and make use of remote development features of NetBeans. In this case you are using the Pi as a Build Host and the desktop machine as the development environment.
In both cases the Pi does the compiling and the running of the program. In practice the remote development approach tends to work better because the desktop machine has the power to run NetBeans fast.
It also means that you can change the Pi that you are testing the code on very easily with all of the code stored on the desktop machine. This is very useful because you can try your program out on a range of Raspberry Pis and swap machines simply by changing the build host used for the project.
The desktop machine can be a Windows, OSX or even another Linux machine.
Before we can get down to work we need to find out how to set up NetBeans in both modes of use.
NetBeans On Raspberry Pi
As NetBeans is a Java program it will work on any machine that supports a Java Virtual Machine JVM and the Pi comes with one already installed as part of Raspbian.
It is assumed that you have your target Pi set up with Raspbian and you either have a monitor, keyboard and mouse connected or you have an SSH connection. If you haven't reached this stage there are lots of introductory tutorials including my own: Real Raspberry Pi - Getting Started And Custom NOOBS
As NetBeans is a fairly demanding program it would have to be a Raspberry Pi 2 or preferably 3.
Getting NetBeans working is fairly simple.
From the desktop first run a browser and navigate to netbeans.org. Find the download page and select: OS Independent Zip and the Language you want.
Click the download button of the version of NetBeans you want to work with. I recommend that you select the specific C/C++ version. The download is smaller and you don't get a lot of confusing additional features which have nothing to do with programming in C. You can always add additional language support at a later date.
After the download is complete you have to extract the files in the zip to a suitable directory. If you are the only person going to use NetBeans then your home directory is suitable. Extract all of the files into the directory.
Navigate to you home directory and then to netbeans/bin and run the file netbeans - this is a shell script that the first time it is run installs Netbeans and subsequently runs the IDE. Run it in a terminal, accept the licence agreement and wait while it installs. The other files in the directory are to insall NetBeans on a Windows or OSX machine.
At this point you will have a working NetBeans IDE ready to create your first C program.
Optionally you can add NetBeans to the Programming group in the menu. All you have to do is select Preferences, Main Menu Editor. Then select the Programming group and select New Item. Fill in the dialog that appears as shown:
The command is: /home/pi/netbeans/bin/netbeans adjusted to reference your home directory rather than user pi's if necessary.
After this you can run NetBeans from the menu.
NetBeans On A Remote Machine
Although installing NetBeans on Raspberry Pi is easy and it works very well there are good reasons for installing on a desktop machine and using the Pi as a remote build server.
The most obvious advantage is that the desktop machine is likely to be more powerful than even a Pi 3. In addition you can use the same code base and machine to program multiple Pis. All you have to do is change the IP address of the remote build server.
It is assumed that you have your target Pi set up with Raspbian and connected to a network, a wired connection works best because it is faster but a WiFi connection works.
You also need to make an SSH connection to the Pi - you can just make the connection using NetBeans but it is better to use an SSH client such as Putty under Windows.
It is assumed you also know the IP address of the Pi you want to work with.
If you can't make an SSH connection to the Pi then you aren't going to get NetBeans working remotely with it - so check that you can open an SSH terminal.
The first step is to install NetBeans on the desktop machine you want to work with. You can do this by selecting the download that targets the correct OS - Windows, Linux x86/x64 or OS X. Again you can install one of the distributions that supports other languages but there are advantages of simplicity to just using the C/C++ version.
Once downloaded simply run the installer and you should have a fully working version of NetBeans on the desktop machine.
Making the connection between the desktop installation and the Pi acting at the remote build server is described in the next section.
- Prev
- Next >>