1. Home
  2. Docs
  3. Documentation
  4. Integrations
  5. Raspberry Pi 4 / 5

Raspberry Pi 4 / 5

In this article, we install and run a temperature sensor in a Raspberry Pi to send data to Nimbus808 utilizing the paho-mqtt library programmed in the Python language.

Learn how to set up the Raspberry Pi using Ethernet and how to send/receive data over Nimbus808

Requirements:

  • Raspberry Pi 4 or 5
  • Nimbus808 account
  • IDE of choice
  • Python Language Experience

Raspberry Pi Configuration

This guide assumes your Raspberry Pi has been configured and is already connected to the internet. To learn more about how to perform this, we suggest following this quick start guide from Raspberry Pi Foundation.

Paho-MQTT: MQTT for humans

Paho-mqtt is a popular Python library that simplifies making MQTT requests from any python script which can be ran in your computer’s terminal or any embedded Linux device such as the Raspberry Pi.
To install the library run the command below in the Raspberry Pi Terminal:

pip install paho-mqtt

Sending data to Nimbus8083

Lets create a python script using using your text-editor (IDE) of choice. For this tutorial we’ll use “nano”. This script sends a random value to Nimbus which can be easily adapted to send values from digital inputs from your Raspberry Pi, or even analog readings when using special shields.

Creating Python Script

nano nimbus_connection.py

Code Portion

Copy and paste the below code in your terminal; assigning your nimbus808 broker information where indicated in the code.

import paho.mqtt.client as mqtt
import time
import random

# MQTT client and connect
client = mqtt.Client()
client.connect(“test.mosquitto.org”, 1883, 60) # Put your Nimbus808 Broker information here

# Start loop in background
client.loop_start()

#infinite loop to run until stopping the task
while True:
	sensor_value = randomint(0, 10)
	client.publish(“test/topic”, f”Sensor Value: {sensor_value}”)
	time.sleep(2)

Once the code has been written in your IDE of choice save the program to a location of your choice. Assign the file name the “.py” extension to be executable via python in a terminal.

Running the script

python nimbus_connection.py

Receiving data from Nimbus808

Code Portion

Copy and paste the below code in your terminal; assigning your nimbus808 broker information where indicated in the code.

import paho.mqtt.client as mqtt
import time

# MQTT Callbacks
def on_connect(client, userdata, flags, rc):
	Client.subscribe(“test/topic”)
def on_message(client, userdata, msg)
	Print(f”message received on {msg.topic}: {msg.payload.decode()})

# MQTT setup
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(“test.mosquitto.org”, 1883, 60) # Put your Nimbus808 Broker information here

# Start MQTT Loop
client.loop_start()

# Keep code running
while True:what 
	time.sleep(2)

Once the code has been written in your IDE of choice save the program to a location of your choice. Assign the file name the “.py” extension to be executable via python in a terminal.

  1. Running the Project
python nimbus_connection.py

Feedback, suggestions, and related articles

Feel free to post questions or suggestions on our community portal, or contact us via email at support@nimbus808.com

Other Users also found helpful…

  • another article
  • another another article
  • another another another article

Related Articles