Skip to content

How To Send Data Using MQTT to Machinechat JEDI/Vision

Note

This how-to is applicable to Machinechat JEDI and Machinechat Vision. For simplicity, we will refer to both the applications as Machinechat JEDI/Vision.

Overview

Machinechat JEDI/Vision provide a built-in MQTT Broker that allows you to easily receive data from sensors and devices that support the MQTT protocol. This how-to will walk you through setting up a basic MQTT client in Python and Arduino to send data to Machinechat JEDI/Vision.

Understanding MQTT

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. It is designed for connections with remote locations where a small code footprint is required or network bandwidth is limited.

Key Components of MQTT

  1. Broker: The server that receives all messages from the clients and then routes these messages to the appropriate destination clients. Machinechat JEDI/Vision act as an MQTT brokers.

  2. Client: Any device that sends data to or receives data from the broker.

  3. Topic: A string that the broker uses to filter messages for each connected client.

Machinechat JEDI or Machinechat Vision as an MQTT broker

  • In Machinechat JEDI/Vision, enable the MQTT Broker Data Collector.
  • Note the broker's IP address and port number (default port is 1853)

Topic Structure in Machinechat JEDI/Vision

Machinechat JEDI and Vision are flexible with the MQTT topic structure. The topic string can be any valid string. JEDI uses the MQTT client identifier as the unique ID for the data source.

MQTT Data Format

Data sent to Machinechat JEDI/Vision over MQTT must be in JSON format, so that the data can parsed and processed efficiently.

Sending Data to Machinechat JEDI/Vision Using MQTT in Python

Step 1: Set Up Your Python Environment

  1. Ensure Python is installed on your system. If not, download it from python.org.
  2. Install the paho-mqtt Python client library, which allows easy interaction with MQTT. Use pip to install:
    pip install paho-mqtt
    

Step 2: Write the Python MQTT Client Script

  1. Open your Python IDE or a text editor.
  2. Create a new Python file.

Step 3: Python Code to Publish Data

  1. Import the paho.mqtt.client library.
    import paho.mqtt.client as mqtt
    import json
    
  2. Define the MQTT broker's address and port (use Machinechat JEDI/Vision's IP and MQTT port).
    broker_address = "[IP_ADDRESS]"  # Replace with your JEDI IP
    port = 1853  # Default MQTT port in JEDI/Vision
    
  3. Connect to the MQTT broker and publish data.
    client = mqtt.Client("PythonClient")
    client.connect(broker_address, port=port)
    
    topic = "sensor/data"
    payload = {
        "temperature": 22,
        "humidity": 45
    }
    client.publish(topic, json.dumps(payload))
    
    print(f"Data published to topic: {topic}")
    client.disconnect()
    
  4. Run the script to send data to Machinechat JEDI/Vision using MQTT.

Step 4: Verify the Data

  • Once the script is executed, log into your Machinechat application and click on Device Dashboard in the navigation panel.
  • Check if the data from the Python script appears under the MQTT client ID.

Sending Data to Machinechat JEDI or Machinechat Vision Using MQTT in Arduino

Step 1: Prepare Your Arduino Environment

  1. Ensure you have the Arduino IDE installed. If not, download it from the Arduino website.
  2. Connect your Arduino board with networking capabilities (like ESP8266 or ESP32) to your computer.

Step 2: Install Necessary Libraries

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Install the PubSubClient library for MQTT communication.

Step 3: Write the Arduino MQTT Client Sketch

  1. Create a new sketch in the Arduino IDE.

Step 4: Arduino Code to Publish Data

  1. Include the necessary libraries for WiFi and MQTT.
    #include <WiFi.h>
    #include <PubSubClient.h>
    
  2. Set up your WiFi credentials and define the MQTT broker's details.
    const char* ssid = "Your_SSID";
    const char* password = "Your_Password";
    const char* mqtt_server = "[IP_ADDRESS]";  // Replace with your JEDI/Vision IP
    const int mqtt_port = 1853;  // Default MQTT port in JEDI/Vision
    
  3. Initialize the WiFi and MQTT client.
    WiFiClient espClient;
    PubSubClient client(espClient);
    
  4. Connect to WiFi in the setup() and set up the MQTT client.
    void setup() {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
      }
      client.setServer(mqtt_server, mqtt_port);
    }
    
  5. Publish data to Machinechat JEDI/Vision in the loop().
    void loop() {
      if (!client.connected()) {
        // Reconnect if the client is disconnected
        // Implement your reconnect logic here
      }
      client.loop();
    
      String payload = "{\"temperature\":22,\"humidity\":45}";
      client.publish("sensor/data", payload.c_str());
    
      delay(10000); // Wait for 10 seconds before sending next data
    }
    
  6. Upload the sketch to your Arduino board.

Step 5: Verify the Data

  • Once the script is executed, log into your Machinechat JEDI/Vision and click on Device Dashboard in the navigation panel.
  • Check if the data from the Python script appears under the MQTT client ID.

You've now learned how to send data to Machinechat JEDI/Vision using MQTT, a protocol widely used in IoT applications, with both Python and Arduino. This knowledge will allow you to integrate a variety of devices into IoT solutions with Machinechat JEDI/Vision.