Photo by rawpixel.com from Pexels

Raspberry Pi: your Swiss army knife for IoT prototyping

Written by
mltadmin

Happy Pi Day!

When you hear the words “Raspberry Pi”, you might be thinking of a nice, tasty dessert. Turns out there is a special day for that: National Pie Day, on January 23rd. Today, however, we will be talking about different kind of pie. While I don’t advocate its consumption, it turns out to be pretty useful in IoT applications.

For starters, what is a Raspberry Pi? Put simply, it is a credit card sized, single board computer that typically run Linux based operating systems (some Windows variants are also available). It is significantly less powerful than your typical modern laptop or desktop, and even most smartphones. However, most of the operating systems available for it are meant to be very lightweight, freeing up resources. The board requires a basic 5 volt external power supply and the power consumption ratings range from 2 to 3 watts when idle, to 5 to 7 watts when under full load. In comparison, that is in the same range as a small LED light bulb. The Model B is marketed at 35 dollars US for the board itself. The only mandatory accessories are the power supply, and a micro SD card.

https://www.raspberrypi.org/ is a great place to get started.

Raspberry Pi and the Internet of Things

In a world where new “smart devices” seem to appear daily, and the increasing collection and monitoring of countless data sources, how can the Raspberry Pi help?

Collecting data

The two rows of pin headers on the board are known as the GPIO (General Purpose Input Output). With these, we can directly connect simple electronic environmental sensors (temperature, humidity, pressure, light, noise, distance, etc.) to the Pi. Some can be connected by using simple electronic jumper cables, while others may require a small conversion circuit in between. We can read a sensor’s data by using the Linux terminal directly, or by writing a small application to collect the information.

This is a great way to get started. However, this process typically only uses a small fraction of the Pi’s resources. For strict data collection, there are analog microcontrollers that might be better suited for the job. In fact, very inexpensive Wi-Fi enabled microcontrollers such as the Espressif ESP8266 and ESP32 have increased in popularity in the past years and are now commonly used in IoT projects. Thankfully, the Pi can easily communicate with microcontrollers through wireless or direct serial communication. This enables us to make more use of the Pi’s potential while collecting local data accurately and efficiently.

Raspberry Pi

Additional data sources

The Raspberry Pi Model B is equipped several physical ports and components that can acquire additional data. These include a Bluetooth chip for two way communication with Bluetooth enabled devices, 4 USB 2.0 ports to connect USB devices and a camera connector for plug and play support with the official Raspberry Pi camera accessory.

With the Pi’s networking capabilities, we can also fetch data from external APIs (examples: local weather metrics, stock market, statistics, etc.)

Processing Data

Raw sensor data can be directly used by consumers or be persisted. Though in some cases, the raw data will need to be processed and transformed to a more usable state. For example, temperature data might require to be converted to a specific scale or to be rounded up or down. As with data collection, small applications can be written to transform the data locally on the Pi before displaying it or persisting it.

Persisting data

Once the data is processed, it is up to the user to decide what to do with it. Several options are available. File based storage or local databases are a basic option. The default operating system, Raspbian, is powerful enough to host a small database server, and is another simple way to persist information. The data could also be uploaded to a remote database or even a cloud based solution such as Amazon Web Services or Google Cloud. If historical data is not relevant, real time results can simply be displayed and not persisted at all. The most adequate choice depends on the application.

Raspberry Pi as an IoT hub

A more exhaustive use of the Raspberry Pi would be to have it act as an IoT hub. In this scenario, the Pi’s role is to gather data from multiple external and local sources, and then expose it to make it available to clients. Data sources from remotes devices add complexity but different protocols can help mitigate this.

Cloud Computing

HTTP/HTTPS (HyperText Transfer Protocol)

Communication with remote devices can often be done with a standard HTTP/HTTPS requests. This is a widely used protocol that uses the request-response model. The Raspberry Pi can easily handle HTTP traffic. However, some remote devices might be very limited in resources or running on battery, which can make HTTP an expensive protocol for small and frequent messages such as simple sensor readings. It is also client to server based, which adds complexity to accomplish client to client communication.

MQTT (Message Queuing Telemetry Transport)

MQTT is another protocol commonly used in IoT applications. This is a point-to-multipoint protocol that uses the publisher/subscriber communication model and is designed to be very lightweight. The general idea is that several clients can communicate between each other by using a common server, also known as a broker. Each client can publish messages linked to user defined topics. They can also subscribe to topics from other clients. All messages are sent to the broker, which can be seen as the middleman that listens to published messages and automatically distributes it to all subscribed clients. This simplifies client to client communication, and allows more complex automatic message routing patterns.

Smaller devices benefit from MQTT due to small overhead of sending messages repeatedly while maintaining the same connection. The protocol offers different quality of service options for messages ranging from a simple fire-and-forget mechanism to two-level handshaking, depending on the need.

The Raspberry Pi is a great candidate to act a dedicated broker. The default Raspbian operating system comes with an MQTT broker preinstalled, Mosquitto. Once the broker is set up, clients can easily join the network by connecting to the broker, subscribing to the desired topics, and publish messages if needed.

HTTP/HTTPS and MQTT are examples of commonly used protocols, but note that the Pi is a flexible platform that can handle various communication protocols and can also act as a bridge between different IoT technologies (Bluetooth, Zigbee, LoRa, Cellular, etc.) At the end of the day, the goal is to get everything to talk safely and efficiently.

Putting ideas into practice

Selecting the proper tools depends on the problem you are trying to solve.

The Raspberry Pi provides several options to experiment with local and external data sources. The low price point makes it attractive at the consumer level, where users have the opportunity to be creative and quickly prototype and test their IoT ideas. When performance and scalability requirements begin to grow, there are other platforms out there that can be useful such as AWS IoT, Google Cloud or Azure IoT. The Pi, however, is a great and inexpensive way to get started and begin prototyping your IoT projects.

 

François Côté
Software Developer. B.Eng

Raspberry Pi, Software Development

Comments

*

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.