Hans-Petter Halvorsen
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.
You will need the following IoT hardware and components (recommended components in bold):
You will need the following software:
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)
For making wiring diagrams Circuit Canvas is recommended. Another alternative is Fritzing. These are software tools for design of electronics hardware and creating circuit diagrams. You can use these tools for designing or documentation of your electrical circuits.
Remote Access using Remote Desktop Connection (RDC)
sudo apt-get install xrdp
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?
sudo adduser <username>
sudo adduser <username> gpio
- similar for SPI, I2C, etc.
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 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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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 ADC/TMP36, 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)
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
Python is a fairly old programming language but has become increasingly popular in the age of Internet of Things.
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
Installing Python packages on Raspberry Pi
Python is installed by default as a "system-wide" installation as part of the Raspberry Pi OS and by default in the latest version of Raspberry Pi OS you cannot install Python packages in the global Python ("system-wide" installation) environment on Raspberry Pi OS. This is due to security reasons and the fact that you can destroy this environment by installing new packages which can cause other programs on Raspberry Pi OS to no longer work.
You have then several options when installing Python packages on Raspberry Pi:
Installing Python Packages on Raspberry Pi (YouTube) + PowerPoint (PDF)
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)
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)
MQTT consists of a MQTT Broker, MQTT Publishers and MQTT Subscribers.
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.
Python:
Eclipse Paho MQTT Python Client (GitHub)
MQTT and Python for Beginners - Tutorials
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
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)
In order to use LabVIEW in combination with Rasberry Pi, we can use the LabVIEW Hobbyist Toolkit. Here you can learn LabVIEW Fundamentals.
Note! The LabVIEW Hobbyist Toolkit has replaced the LabVIEW LINX Toolkit. Some of the resources below may still use the LINX term. You install the LabVIEW Hobbyist Toolkit using the NI Package Manager (included with LabVIEW).
Resources:
LabVIEW Hobbyist Toolkit and Raspberry Pi (YouTube) + PowerPoint (PDF)
LabVIEW Hobbyist Toolkit and Raspberry Pi - Part 2 (PDF) - PWM and Push Buttons
LabVIEW Hobbyist Toolkit and Raspberry Pi using SPI and I2C (PDF) - TC74 Temperature Sensor
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.
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).
For making wiring diagrams Circuit Canvas is recommended. Another alternative is Fritzing. These are software tools for design of electronics hardware and creating circuit diagrams. You can use these tools for designing or documentation of your electrical circuits.
Note! All parts and tasks (requirements) should finally be put together and presented as one final working system. Pretend you make this for a given customer/client.
An example of a use case may be that you want to have temperature (and, e.g., other parameters like air quality, humidity, barometric air pressure) monitoring in multiple rooms inside one or more buildings.
The following Tasks (requirements given by the customer) should be done as part of the delivery:
In addition to Part 1, choose one of the parts below (where Part 2a is recommended).
Normally it is not allowed or recommended to store data directly into the database from logging devices like Raspberry Pi due to security reasons, scaling, performance, etc., so in many cases you need to use a protocol like MQTT (and a MQTT Broker) as a middleware between the logging device and the database.
This part can mostly be developed on your PC. When it works properly on your PC, you can deploy the necessary parts on your Raspberry Pi.
In this part we wil use Raspberry Pi in combination with MATLAB and the Discuss.
Note! Hi, MathWorks has just released a new version of MATLAB, R2024b, which also has support for the new Raspberry Pi 5.
In this part we wil use Raspberry Pi in combination with LabVIEW and LabVIEW Hobbyist Toolkit.
Online students or Industry students do also have the possibility to present a work related project they are involved in as a replacement for the tasks given above. This work should then involve use of some kind of IoT hardware (Raspberry Pi or Industrial IoT hardware used by the company), data logging, data storage, cloud storage or services and use of an IoT protocol like MQTT. Also, make sure to discuss cyber security related issues in your system. The company related assignment must be approved by the supervisor before the work starts.
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):