Machine Learning for IoT - Report Homework 3
Giuseppe Acquaviva, Mario Capobianco, Marco Mungai Coppolino
Politecnico di Torino, Italy
I. E XERCISE 1 - DATA C OLLECTION , C OMMUNICATION , B. REST Client for Data Visualization
AND S TORAGE A REST client was developed in rest_client.ipynb
A. Implementation of Data Collection using MQTT to interact with the API:
• Server status was verified using GET /status.
The primary goal of this task was to collect temperature and
• Sensor node was added using POST /sensors.
humidity data using a DHT-11 sensor connected to a Raspberry
• Data was retrieved and visualized with Deepnote chart
Pi and publish the data to an MQTT broker. The following
steps were performed: blocks.
• The script publisher.py was developed to measure C. HTTP Method Justification
and publish data every 2 seconds. The most suitable HTTP method for implementing the
• JSON messages were structured with fields: MAC ad- /data/{mac_address} endpoint is GET because:
dress, timestamp, temperature, and humidity. • Read-only operation: GET is designed for retrieving
• The Eclipse MQTT broker was used as a communication
data without altering server resources, aligning with the
endpoint. endpoint’s purpose.
• Idempotency: Multiple GET requests with the same pa-
B. Data Storage using Redis
rameters will yield the same response, ensuring consistent
To store the data: and predictable behavior.
• An MQTT subscriber was implemented in • Unsuitability of other methods: POST is typically used
subscriber.ipynb. to create resources, while PUT and DELETE involve mod-
• Data was stored in Redis TimeSeries for efficient retrieval ifying or deleting resources, making them inappropriate
and management. for data retrieval.
C. MQTT vs REST for IoT Applications III. C ONCLUSIONS
The tasks highlighted the importance of MQTT for real-time
MQTT is a better choice than REST for this application
IoT communication and REST for structured data retrieval.
because:
The implemented solutions met the project requirements and
• Efficient for frequent updates: MQTT’s demonstrated efficient data management and visualization.
publish/subscribe model eliminates the need for
continuous client polling, reducing communication
overhead.
• Lightweight: MQTT requires minimal resources, making
it ideal for constrained IoT devices like the Raspberry Pi
and DHT-11 sensor.
• Real-time performance: MQTT ensures faster delivery
of data compared to REST, which involves higher latency
due to its request-response model.
In contrast, REST is better suited for less frequent interactions
and when resources are not constrained.
II. E XERCISE 2 - DATA M ANAGEMENT & V ISUALIZATION
A. REST API Design
A REST API was implemented in rest_server.ipynb
to enable retrieval of historical temperature and humidity data.
The endpoint /data/{mac_address} was designed with:
• HTTP GET method for data retrieval.
• Query parameters to specify a date range.
• Response codes for error handling and data availability.