Internet of Things and Raspberry Pi

Hans-Petter Halvorsen

1 Introduction

Internet of Things and Raspberry Pi (YouTube) + PowerPoint (PDF)

With Internet of Things (IoT) and Cloud Services Datalogging has reach a new era. The Data are typically stored in the Cloud using traditional SQL databases or more modern systems like NoSQL databases or IoT cloud services. We will use Raspberry Pi or Raspberry Pi Pico. Raspberry Pi is popular to use in different IoT applications. We will primerly use Python, but also MATLAB as programming languages.

Internet of Things Applications typically run on small embedded systems that need to perform under the constraints of limited memory and processing power of small IoT devices. Use of proper Operating Systems for IoT Applications are cruical, meaning Windows 10/11 is not an option. Price is also an important factor in IoT systems, both hardware costs and software costs. Here we will focus on Raspberry Pi and the Linux operation system.

Topics: Internet of Things (IoT), Microcomputers/SBC (Raspberry Pi), Linux, IoT Sensors, SPI/I2C, Python, NoSQL (MongoDB), ThingSpeak (IoT Cloud Service)

You may go through some of the resources (tutorials, videos, examples, exercises, etc.) below or you may start directly on the final delivery. Then you can use these resources if you need help solving some of the different tasks within the delivery.

 

1.1 Hardware

You will need the following IoT hardware and components (recommended components in bold):

 

1.2 Software

You will need the following software:

 

 

2 Raspberry Pi

Raspberry Pi (RP) is a microcomputer and it has an ordinary Operating System (OS). You can connect USB devices, Keyboard, Mouse, Monitors, etc. It has a “hard-drive“ in form of a microSD card. RP has Bluetooth, Wi-Fi, and Ethernet connection. RP has basically all the features an ordinary computer has but in a much smaller package. Raspberry Pi has uptil 8 Gb RAM. RP runs Linux applications, the recommended OS is called Raspberry Pi OS.

Introduction to Raspberry Pi (YouTube) + PowerPoint (PDF)

Raspberry Pi Installation and Remote Access (YouTube) + PowerPoint (PDF)

Raspberry Pi - Overview of Remote Access Alternatives (PDF)

Raspberry Pi Beginner's Guide (PDF)

 

Remote Access

Remote Access using Remote Desktop Connection (RDC)

Do you get a blue screen (empty screen) after login using RDC/RDP on your Window PC? You may need to create another user on your Raspberry Pi. The Pi root user cannot use RDP for some reason?

 

Note! An alternative to creating a new user is to use VNC Viewer. You can download the VNC Viewer here. Another alternative is to use TeamViewer.

 

Raspberry Pi Pico

Raspberry Pi Pico is an alternative to Raspberry Pi. Raspberry Pi Pico is a "downscaled" version of the original Raspberry Pi and is more comparable with Arduino compared to the original Raspberry Pi. You also need to use a downscaled version of Python, called MicroPython.

 

 

3 DAQ and IoT Sensors

DAQ (Data Acquisition) and Sensors are needed and used in all IoT applications. DAQ is the process of getting data from the sensors into your software. Here will some popular IoT sensors be presented.

IoT sensors comes in many flavours. Below, some IoT sensors are presented they can be programmed with Python. Below you find more information about the sensors TMP36, Thermistor 10K, TC74, BME280, DHT11/22 and AM2320.

3.1 TMP36

A Temperature sensor like TM36 use a solid-state technique to determine the temperature. They use the fact as temperature increases, the voltage across a diode increases at a known rate.

To use TMP36 with Raspberry Pi, you will need an ADC, e.g., MCP3002 or similar.

3.2 Thermistor 10K

A thermistor is an electronic component that changes resistance to temperature - so-called Resistance Temperature Detectors (RTD). It is often used as a temperature sensor. There is a non-linear relationship between resistance and excitement. To find the temperature we can use the Steinhart-Hart equation.

3.3 TC74

The TC74 acquires and converts temperature information from its onboard solid-state sensor with a resolution of ±1°C. It stores the data in an internal register which is then read through the serial port. The system interface is a slave SMBus/I2C port, through which temperature data can be read at any time.

3.4 BME280

BME280 is a digital Humidity, Pressure and Temperature Sensor from Bosch. The sensor provides both SPI and I2C interfaces. Adafruit, Grove Seeed, SparkFun, etc. have breakout board bords for easy connection to Arduino, Raspberry Pi, etc. The price for these breakout boards are $1-20 depending on where you buy them.

3.5 DHT11/DHT22

The DHTXX sensors are temperature and humidity sensors and they comes in 2 flavours: DHT11 and DHT22. They contains a chip inside that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. Read more.

3.6 DS18B20

DS18B20 is a 1-Wire Digital Temperature Sensor, this means the Sensor only need 1 Pin for Communication (+ one pin for GND and one pin for 5V). Accuracy +/−0.5℃. It has a 9 to 12-bit resolution (Programmable). Temperature range −55°C to +125°C. Price: About $4. Datasheet.

3.7 AM2320

This AM2320 sensor looks a lot like the popular DHT11/DHT22 temperature and humidity sensors, but unlike classic DHT sensors, it has an I2C interface.

 

 

4 Digital Sensor Interfaces

Raspberry Pi has only digital I/O pins. In order to use analog sensors likes TMP36 and Thermistor 10K we typicaly need to use an external ADC (e.g., MCP3002).

Raspberry Pi has SPI, I2C and 1-Wire interfaces that can be used for communication with digital sensors like TC74, BME280, DHT11/22 and DS18B20 (and many others).

4.1 SPI/I2C

SPI and I2C are digital interface to communicate with different types of electronic components like Sensors, Analog to Digital Converts (ADC), etc. Most IoT devices, including Arduino and Raspberry Pi, support the SPI and I2C interfaces.

Sensor Examples:

Raspberry Pi using SPI and I2C with Python (PDF) - with TC74 and BME280 Examples

Raspberry Pi and AM2320 Temperature and Humidity Sensor with I2C Interface (PDF)

Raspberry Pi and AM2320 - Testing different existing Libraries (PDF)

Using AM2320 Temperature Sensor with CircuitPython (Website)

 

4.2 1-Wire

1-Wire is similar in concept to I2C, but with lower data rates and longer range. One-wire devices need a pull-up resistor connected to their signal line to be properly read by your board.

Raspberry Pi and CircuitPython (PDF) - with DTH11/DTH22 Examples

 

Sensor Examples:

Using DS18B20 Temperature Sensor with CircuitPython

Using DHT11/DHT22 Sensor with CircuitPython

 

 

5 Python

Python is a fairly old programming language but has become increasingly popular in the age of Internet of Things.

 

5.1 Raspberry Pi and Python

Raspberry Pi and Python is a powerful combination. Below you find some resources using Python in combination with Raspberry Pi.

Introduction to Raspberry Pi (YouTube) + PowerPoint (PDF)

 

Raspberry Pi + Python Tutorials:

Raspberry Pi with Python (YouTube) + PowerPoint (PDF)

Raspberry Pi GPIO with Python (PDF) - with LED, PWM, Pushbutton, ADC and ThingSpeak Examples

Raspberry Pi using SPI and I2C with Python (PDF) - with TMP36, MCP3002 ADC (SPI) , TC74 (I2C) and ThingSpeak Examples

Raspberry Pi and CircuitPython (PDF) - with BME280 and DHT11/DHT22 Examples

 

 

6 ThingSpeak IoT Cloud Service

ThingSpeak is an IoT cloud service used to store IoT data. ThingSpeak is an IoT analytics platform service that lets you collect and store sensor data in the cloud and develop Internet of Things applications. The ThingSpeak service also lets you perform online analysis and act on your data. Sensor data can be sent to ThingSpeak from any hardware that can communicate using a REST API. ThingSpeak has a Web Service (REST API) that lets you collect and store sensor data in the cloud and develop Internet of Things applications. In addition, it also has a MQTT API.

ThingSpeak Web Site: https://thingspeak.com

Python Library for ThingSpeak: https://pypi.org/project/thingspeak/

Raspberry Pi GPIO Python Examples (including ThingSpeak Examples) (PDF)

 

 

7 MQTT

Message Queueing Telemetry Transport (MQTT) is an IoT protocol MQTT is used in applications with thousands of sensors. MQTT is efficient in terms of bandwidth, battery, and resources. MQTT uses a publish/subscribe model. MQTT has become the defacto IoT communication protocol. This means if you want to work with IoT projects, you will sooner or later need to use this protocol.

MQTT (YouTube) + PowerPoint (PDF)

MQTT with Python (YouTube) + PowerPoint (PDF)

 

Additional MQTT Resources

MQTT consists of a MQTT Broker, MQTT Publishers and MQTT Subscribers.

Getting started with MQTT

MQTT Basics - Mathworks

 

MQTT Software:

HiveMQ Cloud - MQTT Broker in the Cloud

MQTT X - MQTT X is an MQTT 5.0 Open-source Desktop Client

 

MATLAB:

In order to use MQTT with MATLAB you can use the “Industrial Communication Toolbox”. The “Industrial Communication Toolbox” supports the following Protocols: Modbus, OPC, both OPC DA and OPC UA (previously OPC Toolbox) and MQTT. Note! “Industrial Communication Toolbox” is a new Toolbox that is included in “MATLAB R2022a” and newer versions.

MQTT API for MATLAB

 

Python:

Python Client Web Page

Eclipse Paho MQTT Python Client (GitHub)

MQTT and Python for Beginners - Tutorials

 

 

8 MongoDB

MongoDB is a so-called NoSQL Database. MongoDB is one of the most popular NoSQL database systems. NoSQL databases have become very popular and espesially in IoT applications.

Python and MongoDB (YouTube) + PowerPoint (PDF)

MongoDB Cloud and Python (YouTube) + PowerPoint (PDF)

 

You can download a free MongoDB database at https://www.mongodb.com.

You can also use MongoDB as a cloud service (MongoDB Atlas): https://www.mongodb.com/cloud/atlas.

 

Tutorial: Python MongoDB - w3schools.com

 

 

9 Raspberry Pi and MATLAB

With MATLAB support package for Raspberry Pi, the Raspberry Pi is connected to a computer running MATLAB. Processing is done on the computer with MATLAB.

Raspberry Pi with MATLAB (YouTube) + PowerPoint (PDF)

Raspberry Pi with MATLAB using SPI and I2C (YouTube) + PowerPoint - TC74 (I2C) and TMP36 with ADC MCP3002 (SPI) Examples are presented in the Video/Tutorial.

 

Additional Resources:

Raspberry Pi Programming with MATLAB and Simulink

MATLAB Support Package for Raspberry Pi

Getting Started with MATLAB Support Package for Raspberry Pi (YouTube)

 

 

10 Raspberry Pi and LabVIEW

In order to use LabVIEW in combination with Rasberry Pi, we can use LabVIEW LINX. Here you can learn LabVIEW Fundamentals.

Resources:

LabVIEW LINX and Raspberry Pi (YouTube) + PowerPoint (PDF)

LabVIEW LINX and Raspberry Pi - Part 2 (PDF) - PWM and Push Buttons

LabVIEW LINX and Raspberry Pi using SPI and I2C (PDF) - TC74 Temperature Sensor

Delivery

In this Assignment we will use Raspberry Pi. We will use the Raspberry Pi to read and log temperature data. We will mainly use Python, but also MATLAB.

Tip! Most of the subtasks can be developed using Python on your PC without the need for a Raspberry Pi. When it works properly on your PC, you can try it on your Raspberry Pi. The Thonny Python editor (that are included with Raspberry Pi) can also be installed on your PC.

The following Tasks should be done as part of the delivery:

Part 1: Raspberry Pi and Python

 

In addition to Part 1, choose one of the parts below.

Part 2a: MQTT and Python

This part can mostly be developed on your PC. When it works properly on your PC, you can try it on your Raspberry Pi.

 

Part 2b: MATLAB and Raspberry Pi

In this part we wil use Raspberry Pi in combination with MATLAB and the MATLAB Support Package for Raspberry Pi.

 

Part 2c: LabVIEW and Raspberry Pi

In this part we wil use Raspberry Pi in combination with LabVIEW and LabVIEW LINX.

 

You should start your work by creating a System sketch. In that way you will get an overview of the system you are going to create. Make System Sketch in PowerPoint (YouTube).

Note! All parts and tasks should finally be put together and presented as one final working system. Pretend you make this for a given customer/client.

Feel free to Explore! Make sure to Add Value and Creativity to your Applications! Try to add some extra value and be creative compared to the simplified examples given by me, in that way you learn so much more.

"Explore and Add Value" Examples (Voluntary, if you have some spare time):