Implementation of AMQP on Raspberry Pi
Objective
To establish a message queuing system using AMQP with RabbitMQ on a Raspberry Pi. The Raspberry Pi will act as a publisher, sending sensor data, while a client device will act as a subscriber.
Methodology
- Install RabbitMQ as the message broker on Raspberry Pi.
- Use Python and Pika library to publish and consume messages.
- Simulate sensor data using a Python script.
Implementation Steps
Step 1: Install RabbitMQ on Raspberry Pi
1sudo apt update
2sudo apt install rabbitmq-server -y
3sudo systemctl enable rabbitmq-server
4sudo systemctl start rabbitmq-server
Step 2: Install Pika Library for Python
1pip install pika
Step 3: Create a Publisher Script on Raspberry Pi
This script sends random temperature values as messages.
1import pika
2 import random
3 import time
4
5 # Connection to RabbitMQ
6 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
7 channel = connection.channel()
8
9 # Declare a queue
10 channel.queue_declare(queue='sensor_data')
11
12 while True:
13 temp = random.uniform(20.0, 35.0) # Simulated temperature data
14 message = f"Temperature: {temp:.2f}°C"
15
16 # Publish message to queue
17 channel.basic_publish(exchange='',
18 routing_key='sensor_data',
19 body=message)
20
21 print(f"Sent: {message}")
22 time.sleep(5) # Send data every 5 seconds
Step 4: Create a Subscriber Script to Read Data
This script will run on another device (or the same Raspberry Pi) to receive and process messages.
1import pika
2
3 # Connection to RabbitMQ
4 connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
5 channel = connection.channel()
6
7 # Declare a queue
8 channel.queue_declare(queue='sensor_data')
9
10 # Callback function to process received messages
11 def callback(ch, method, properties, body):
12 print(f"Received: {body.decode()}")
13
14 # Consume messages from the queue
15 channel.basic_consume(queue='sensor_data',
16 on_message_callback=callback,
17 auto_ack=True)
18
19 print("Waiting for messages. Press CTRL+C to exit.")
20 channel.start_consuming()
Applications
- Smart Agriculture: Send real-time sensor data to the cloud.
- Industrial IoT: Ensure reliable communication between IoT nodes.
- Home Automation: Control smart home devices via message queues.
Future Research Concepts
- Integrating AMQP over WebSockets for real-time browser-based monitoring.
- Using AMQP with AI models for predictive analytics in IoT.
- Combining AMQP with Blockchain for secure IoT data transmission.