IoT-3_Mentoring Pertemuan 12

IoT-3_Mentoring Pertemuan 12

Introduction to MQTT Implementation

Overview of the Session

  • The session begins with a greeting and an introduction to the topic of implementing MQTT, as previously discussed.
  • Attendance is taken, starting from the top of the list, ensuring all participants are present for the session.

Key Concepts in MQTT

  • MQTT is introduced as a protocol used for data exchange, similar to HTTP but specifically designed for lightweight messaging.
  • The functionality of MQTT depends on publishing and subscribing; messages sent (published) are received by clients that have subscribed to specific topics.

Setting Up MQTT

Steps for Client Program Creation

  • Participants are guided through creating a client program using temporary email registration and selecting appropriate settings.
  • Different packages available for use are explained: serverless and dedicated options, with emphasis on choosing free services located in Indonesia.

Connection Details

  • Instructions on connecting to the MQTT server using port 8883 are provided, highlighting its importance in establishing communication.

Comparing Protocols: MQTT vs. HTTP

Differences Between Protocols

  • A comparison between MQTT and HTTP is made; while both serve data transmission purposes, they differ significantly in their operational patterns.
  • It’s noted that MQTT is more suited for small data transmissions typical in IoT applications compared to larger payload transfers often handled by HTTP.

Messaging Patterns Explained

MQTT vs HTTP: Understanding the Differences

Key Differences Between MQTT and HTTP

  • In MQTT, requests can be made directly to receive specific data, while in HTTP, the process is more extensive as it typically involves larger payloads due to browser interactions.
  • The size of messages in MQTT is generally smaller compared to HTTP, making it more efficient for certain applications like video streaming.
  • Using EMQX with MQTT allows for easier project implementation for data transmission tasks.

Setting Up an EMQX Server

  • Initial setup requires configuring authentication settings on the EMQX server before proceeding with connections.
  • Users should ensure they have installed the necessary libraries (like mgttx) for effective communication with the server.

Understanding Topics in MQTT

  • Topics in MQTT are likened to corridors where messages travel; users can create their own topics within a broker.
  • Each topic serves as a pathway for messages sent by clients, which can be visualized as vehicles traveling through these corridors.

Message Flow in MQTT

  • Messages enter a topic via publishing and exit through subscribing; this dual mechanism facilitates message distribution among clients.
  • If a client does not subscribe to a topic, it will not receive any messages sent to that topic. This highlights the importance of subscription in receiving relevant information.

Subscription Dynamics

  • When a client publishes a message to a topic, only those who have subscribed will receive it; unsubscribed clients miss out on these communications.

MQTT Subscription and Notification Mechanisms

Understanding MQTT Subscriptions

  • The discussion begins with an invitation for questions regarding the assumptions made about MQTT subscriptions.
  • A participant inquires whether subscribing to a client sends data directly, drawing an analogy to YouTube notifications where users can choose to turn off alerts despite being subscribed.
  • It is clarified that unlike YouTube, MQTT does not allow users to manage notification preferences; once subscribed, notifications are sent regardless of relevance.

Notification Filtering in MQTT

  • Notifications are sent even if the subscriber does not require certain information; for example, a client may receive updates about vehicles it doesn't need.
  • The filtering mechanism is configured on the line rather than at the MQTT broker level, allowing clients to receive only relevant notifications based on their needs.

Topic Configuration and Data Grouping

  • Each topic can be tailored for specific data types; for instance, one topic could be dedicated solely to vehicle types like "Avanza."
  • Data grouping is essential; different topics should be created for various sensor data (e.g., temperature and distance sensors), ensuring organized communication.

Client-Side Management of Topics

  • Clients can control which data they receive by managing their subscriptions effectively. If they do not want certain data, they simply adjust their topic settings.

Troubleshooting Data Display Issues

  • Participants are encouraged to ask further questions about publishing and subscribing processes within MQTT.
  • A user shares an experience where only empty brackets appeared during testing, indicating no data was available from the database.
  • It is explained that empty brackets symbolize an array with no content; thus, adding necessary data will resolve this issue.

Implementing IoT Projects with MQTT

Project Setup Overview

  • The conversation shifts towards practical implementation of IoT projects using MQTT protocols without needing direct connections between devices.

Creating Topics for Sensors and Actuators

  • An example project involves creating two main components: a temperature sensor and an LED light.
  • Two distinct topics must be established—one for sending temperature readings and another for controlling the LED state (on/off).

Finalizing Project Structure

  • The structure of the project includes defining topics clearly: one each for temperature monitoring and actuator control (light).

ESP32 and MQTT Integration

Required Libraries for ESP32 and MQTT

  • The integration of ESP32 with MQTT requires several libraries, starting with the Wii library.
  • A Wi client is necessary because the free server must operate under TLS or SSL; standard HTTP will not suffice.
  • The DHT sensor library (dhtsp) is essential for retrieving data from the DHT11 sensor, along with publish/subscribe client libraries.
  • Arduino IDE is used to send data to the MQTT server in JSON format rather than as a string.

Project Setup and Configuration

  • When creating a project, there are three options: serverless, dedicated, or bring your own. The recommendation is to choose the server option due to its extended free usage period.
  • Users need to input their SSID and password for Wi-Fi connection on the ESP32 device, ensuring compliance with existing configurations.

Data Handling and Variables

  • Key variables include temperature and humidity readings from sensors, which will be published to specific topics for controlling devices like lights.
  • Connection parameters such as port numbers (e.g., 8883 for secure connections), last transmission time, and intervals are defined based on example programs shared by peers.

Certificate Management

  • Certificates required for establishing secure connections can be downloaded from provided links; these certificates must be integrated into the configuration of the ESP32.
  • After downloading certificates, they should be opened and copied into the appropriate sections of code within the project.

Initialization and Connection Process

  • Initialization involves setting up classes for DHT sensors and defining clients for publishing/subscribing using MQTT protocols over TLS/SSL.
  • Establishing a connection requires verifying that all settings are correct before proceeding with connecting to both Wi-Fi and MQTT servers.

Callback Functions and Subscription

  • Callback functions are crucial after establishing a connection; they handle incoming messages from subscribed topics effectively.

MQTT Implementation and Callback Mechanisms

Understanding MQTT Callbacks

  • The initial setup involves defining the MQTT callback, which includes parameters such as the topic. This is crucial for subscribing to multiple topics on an MQTT server.

Topic Selection and Subscription

  • Topics are essential for distinguishing between different components, like a lamp and a switch. Each component subscribes to its unique topic to avoid confusion.

Message Design and Variable Usage

  • An HTML file is created to facilitate user interaction with topics. Variables are used to design messages that will be sent based on user input.

Data Processing with Arduino

  • Data processing occurs in the Arduino environment, where values are monitored. For instance, if a certain value is detected, it triggers specific actions (e.g., turning on a red light).

Handling Disconnections and Reconnection Logic

  • If there’s a disconnection from the MQTT broker, the system automatically attempts to reconnect based on pre-configured settings.

Controlling Devices via MQTT

Implementing Control Commands

  • The implementation of control commands for devices like lamps is straightforward using ESP32. Users can modify existing code according to their needs.

User Interaction and Input Handling

  • Users can publish commands from their ESP32 devices while also subscribing to receive updates about device states (e.g., turning lights on or off).

Triggering Actions Based on Input Values

  • The data sent through MQTT serves as input that can trigger actions; for example, sending '1' turns the lamp on while '0' turns it off.

Setting Up an MQTT Server

Initial Setup Steps

  • The first step involves creating an MQTT server. Participants are encouraged to test its functionality using an MQTT client after setup.

Troubleshooting Connection Issues

Connecting to MQTT Server and Troubleshooting

Overview of Connecting to the MQTT Server

  • The server setup is complete, and participants are encouraged to connect their devices to the server.
  • Emphasis on understanding potential errors when coding with ESP32, particularly issues related to connecting to the MQTT server.

Troubleshooting Connection Issues

  • Key factors for troubleshooting include verifying if the MQTT server is properly installed and checking the configuration settings on ESP32.
  • Participants can use shared code examples or search for existing code snippets that successfully connect to an MQTT server.

Real-Time Data Transmission

  • Explanation of how IoT systems can monitor real-time data, such as room temperature, using integrated systems connected via MQTT.
  • Comparison between HTTP requests (which require refreshes for updates) and MQTT's subscription model that allows continuous data flow without manual refreshing.

Interactive Session and Hands-On Practice

  • Encouragement for participants to ask questions if they encounter confusion during the session.
  • Instructions for participants to attempt connecting their devices while providing a timeline for completion.

Group Collaboration and Error Resolution

  • Participants are invited to share screens if they encounter errors, fostering collaborative problem-solving.
  • A reminder that addressing issues now will prevent confusion later when assignments are due; encourages proactive engagement in troubleshooting.

Wrap-Up and Next Steps

  • Participants should try out their connections within a set timeframe, with an open invitation for questions throughout the process.

Database Connection and Configuration

Overview of Database Interaction

  • The speaker discusses displaying files from a specific folder related to academic years, emphasizing the importance of correct file naming for database operations.
  • A mention of operating databases is made, indicating that if certain packages are already installed, users do not need to specify the database in their controller settings.

User Guidance on Database Setup

  • Users are encouraged to ask questions if they encounter difficulties with the application, highlighting a supportive learning environment.
  • Instructions are provided for adjusting server settings according to individual configurations, ensuring that each user can tailor their setup effectively.

Authentication and Security Measures

  • Emphasis is placed on modifying user credentials (username and password) in accordance with access control menus for secure authentication.
  • Users are instructed on downloading certificates necessary for establishing secure connections, stressing the importance of not deleting critical parts of configuration files.

Practical Application and Testing

  • Once configurations are set up correctly, users can utilize existing code without needing to restart or reconfigure everything from scratch.
  • Future adjustments may be required as user needs evolve; flexibility in topic selection is encouraged.

Troubleshooting and Support

  • The speaker reassures users about potential issues with connecting to brokers and encourages them to share any problems encountered during setup.
  • Users should check downloaded files carefully; instructions include using Notepad or similar applications for viewing content without altering essential data.

Final Remarks on Learning Process

  • The session concludes with an invitation for further questions while acknowledging that some participants may still be experiencing technical difficulties due to connection issues.

Session Summary and Closing Remarks

Key Takeaways from the Mentoring Session

  • The speaker expresses hope that participants will be able to engage effectively with the material, despite some challenges in understanding.
  • The session is concluded with an invitation for questions; however, no inquiries are raised by the attendees. The speaker emphasizes the importance of completing tasks related to MQTTX and EMQX protocols.
  • Participants are encouraged to reach out for assistance regarding any errors or difficulties they encounter while working on their assignments.
  • The speaker reassures attendees that there will be no negative repercussions for not asking questions during the session, fostering a supportive environment.