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
- 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. - Headers: Set
Content-Typetoapplication/json. - JSON Payload: Structure your data in a JSON format with
contextanddatasections.
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
- Ensure you have Python installed on your system. You can download it from python.org.
- Install the
requestslibrary, which simplifies HTTP requests in Python. You can install it using pip:
Step 2: Write the Python Script
- Open your Python IDE or a text editor.
- Create a new Python file.
Step 3: Python Code to Send Data
- Import the
requestslibrary. - Define the endpoint and your JSON payload.
- Send the data using a POST request.
- 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
- Ensure you have the Arduino IDE installed. If not, download it from the Arduino website.
- Make sure your Arduino board (like an ESP8266 or ESP32) is connected to your computer.
Step 2: Install Necessary Libraries
- Open the Arduino IDE.
- Install the ESP8266 or ESP32 board package via the Board Manager, if you haven't already.
- Install the
ArduinoJsonandWiFilibraries using the Library Manager.
Step 3: Write the Arduino Sketch
- Create a new sketch in the Arduino IDE.
Step 4: Arduino Code to Send Data
- Include the necessary libraries.
- Set up your WiFi credentials and Machinechat JEDI/Vision endpoint.
- Connect to WiFi in the
setup()function. - 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 } - 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.