Skip to content

How To Send Data using HTTP API to Machinechat JEDI and Machinechat 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's HTTP Data Collector allows receiving data sent through HTTP POST requests. This feature enables various devices and applications to send data in a structured JSON format to Machinechat's real-time data visualization and analytics applications for aggregation, visualization, and monitoring.

This guide explains how to send data to Machinechat JEDI/Vision using the HTTP API, with examples in Python and Arduino.

Understanding the HTTP API for Machinechat JEDI/Vision

Machinechat JEDI/Vision's HTTP API is a set of HTTP endpoints that allow external devices and applications to send data using standard HTTP requests. It is particularly useful for IoT devices and scripts that can generate HTTP requests.

Key Components of the API

  1. Endpoint: The HTTP POST request should be sent to http://[IP_ADDRESS]:8100/v1/data/mc, replacing [IP_ADDRESS] with your Machinechat JEDI/Vision server's IP.
  2. Headers: Set Content-Type to application/json.
  3. JSON Payload: Structure your data in a JSON format with context and data sections.

JSON Payload Structure: - context: Contains metadata like target_id (a unique identifier for the data source) and target_ip (the IP address of the data source). Optionally, a timestamp can be included. - data: Holds the actual sensor data or metrics in key-value pairs.

Example JSON Payload:

{
  "context": {
    "target_id": "sensor01",
    "target_ip": "192.168.1.50"
  },
  "data": {
    "temperature": 22,
    "humidity": 45
  }
}

Sending Data to Machinechat JEDI/Vision Using Python

Step 1: Set Up Your Python Environment

  1. Ensure you have Python installed on your system. You can download it from python.org.
  2. Install the requests library, which simplifies HTTP requests in Python. You can install it using pip:
    pip install requests
    

Step 2: Write the Python Script

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

Step 3: Python Code to Send Data

  1. Import the requests library.
    import requests
    
  2. Define the endpoint and your JSON payload.
    jedi_endpoint = "http://[IP_ADDRESS]:8100/v1/data/mc"  # Replace with your JEDI/Vision IP
    json_payload = {
        "context": {
            "target_id": "sensor01",
            "target_ip": "192.168.1.50"
        },
        "data": {
            "temperature": 22,
            "humidity": 45
        }
    }
    
  3. Send the data using a POST request.
    response = requests.post(jedi_endpoint, json=json_payload)
    print(f"Status Code: {response.status_code}, Response: {response.text}")
    
  4. Run the script to send data

Step 4: Verify the Data in Machinechat JEDI/Vision

  • After running the script, log into Machinechat JEDI/Vision.
  • Check if the data from your Python script appears in the Device Dashboard.

Sending Data to Machinechat JEDI/Vision Using Arduino

Step 1: Prepare Your Arduino Environment

  1. Ensure you have the Arduino IDE installed. If not, download it from the Arduino website.
  2. Make sure your Arduino board (like an ESP8266 or ESP32) is connected to your computer.

Step 2: Install Necessary Libraries

  1. Open the Arduino IDE.
  2. Install the ESP8266 or ESP32 board package via the Board Manager, if you haven't already.
  3. Install the ArduinoJson and WiFi libraries using the Library Manager.

Step 3: Write the Arduino Sketch

  1. Create a new sketch in the Arduino IDE.

Step 4: Arduino Code to Send Data

  1. Include the necessary libraries.
    #include <WiFi.h>
    #include <HTTPClient.h>
    #include <ArduinoJson.h>
    
  2. Set up your WiFi credentials and Machinechat JEDI/Vision endpoint.
    const char* ssid = "Your_SSID";
    const char* password = "Your_Password";
    const char* jedi_endpoint = "http://[IP_ADDRESS]:8100/v1/data/mc";  // Replace with JEDI/Vision IP
    
  3. Connect to WiFi in the setup() function.
    void setup() {
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
      }
      Serial.println("Connected to WiFi");
    }
    
  4. In the loop() function, send data to Machinechat JEDI/Vision.
    void loop() {
      if(WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        http.begin(jedi_endpoint);
        http.addHeader("Content-Type", "application/json");
    
        // Create JSON payload
        StaticJsonDocument<200> jsonDoc;
        jsonDoc["context"]["target_id"] = "arduino_sensor";
        jsonDoc["context"]["target_ip"] = WiFi.localIP().toString();
        jsonDoc["data"]["temperature"] = 22; // Replace with real sensor data
        jsonDoc["data"]["humidity"] = 45;    // Replace with real sensor data
    
        String payload;
        serializeJson(jsonDoc, payload);
    
        // Send the request
        int httpResponseCode = http.POST(payload);
        if(httpResponseCode > 0) {
          String response = http.getString();
          Serial.println(httpResponseCode);
          Serial.println(response);
        } else {
          Serial.print("Error on sending POST: ");
          Serial.println(httpResponseCode);
        }
    
        http.end();
      }
      delay(10000);  // Wait for 10 seconds
    }
    
  5. Upload the sketch to your Arduino board.

Step 5: Verify the Data in Machinechat JEDI/Vision

  • After running the script, log into Machinechat JEDI/Vision.
  • Check if the data from your Python script appears in the Device Dashboard in JEDI (Vision doesn't support Device Dashboards).

You've now learned how to send data to Machinechat JEDI/Vision using the HTTP API with both Python and Arduino. You can now integrate various sensors, devices, data from scripts and other applications into Machinechat JEDI/Vision for data visualization and monitoring.