Files
odroid-power-mate/docs/API.md
YoungSoo Shin 2383894664 init commit
Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
2025-08-27 10:59:57 +09:00

6.5 KiB

ODROID Remote API Documentation

This document outlines the HTTP REST and WebSocket APIs for communication between the web interface and the ESP32 device.


WebSocket API

The WebSocket API provides a full-duplex communication channel for real-time data, such as sensor metrics and the interactive serial console.

Endpoint: /ws

Server-to-Client Messages

The server pushes messages to the client, which can be either JSON objects or raw binary data. JSON messages always contain a type field to identify the payload.

JSON Messages

Type Description Payload Example
sensor_data Pushed periodically (e.g., every second) with the latest power metrics. {"type":"sensor_data", "voltage":12.01, "current":1.52, "power":18.25, "uptime_sec":3600, "timestamp": 1672531200}
wifi_status Pushed periodically or on change to update the current Wi-Fi connection status. {"type":"wifi_status", "connected":true, "ssid":"MyHome_WiFi", "rssi":-65}

Field Descriptions:

  • sensor_data:
    • voltage (float): The measured voltage in Volts (V).
    • current (float): The measured current in Amperes (A).
    • power (float): The calculated power in Watts (W).
    • uptime_sec (integer): The system uptime in seconds.
    • timestamp (integer): A Unix timestamp (seconds) of when the measurement was taken.
  • wifi_status:
    • connected (boolean): true if connected to a Wi-Fi network, false otherwise.
    • ssid (string): The SSID of the connected network. Null if not connected.
    • rssi (integer): The Received Signal Strength Indicator in dBm. Null if not connected.

Raw Binary Data

  • Description: Raw binary data from the ODROID's serial (UART) port is forwarded directly to the client. This is used to display the terminal output.
  • Payload: (binary data)

Client-to-Server Messages

The client primarily sends raw binary data, which is interpreted as terminal input.

  • Description: Raw binary data representing user input from the web terminal. This data is forwarded directly to the ODROID's serial (UART) port.
  • Payload: (binary data)

HTTP REST API

The REST API is used for configuration and to trigger specific actions. All request and response bodies are in application/json format.

Endpoint: /api/control

Manages power relays and system actions.

GET /api/control

Retrieves the current status of the power relays.

  • Success Response (200 OK)
    {
      "load_12v_on": true,
      "load_5v_on": false
    }
    
    • load_12v_on (boolean): The state of the main 12V power relay.
    • load_5v_on (boolean): The state of the 5V USB power relay.

POST /api/control

Sets the state of power relays or triggers a power action. You can send one or more commands in a single request.

  • Request Body Examples:

    • To turn the main power on:
      { "load_12v_on": true }
      
    • To trigger a system reset:
      { "reset_trigger": true }
      
    • To toggle the power button:
      { "power_trigger": true }
      
  • Request Fields:

    • load_12v_on (boolean, optional): Set the state of the 12V relay.
    • load_5v_on (boolean, optional): Set the state of the 5V relay.
    • reset_trigger (boolean, optional): If true, momentarily triggers the reset button.
    • power_trigger (boolean, optional): If true, momentarily triggers the power button.
  • Success Response: 204 No Content

  • Error Response: 400 Bad Request if the request body is invalid.


Endpoint: /api/wifi

Manages all Wi-Fi and network-related configurations.

GET /api/wifi

Retrieves the complete current network configuration.

  • Success Response (200 OK)
    {
      "connected": true,
      "ssid": "MyHome_WiFi",
      "mode": "apsta",
      "net_type": "static",
      "ip": {
          "ip": "192.168.1.100",
          "gateway": "192.168.1.1",
          "subnet": "255.255.255.0",
          "dns1": "8.8.8.8",
          "dns2": "8.8.4.4"
      }
    }
    
  • Response Fields:
    • connected (boolean): Current Wi-Fi connection state.
    • ssid (string): The SSID of the connected network.
    • mode (string): The current Wi-Fi mode ("sta" or "apsta").
    • net_type (string): The network type ("dhcp" or "static").
    • ip (object): Contains IP details. Present even if using DHCP (may show last-leased IP).

POST /api/wifi

This is a multi-purpose endpoint. The server determines the action based on the fields provided in the request body.

  • Action: Connect to a Wi-Fi Network

    • Request Body:
      {
        "ssid": "MyHome_WiFi",
        "password": "my_secret_password"
      }
      
    • Success Response (200 OK):
      { "status": "connection_initiated" }
      
  • Action: Configure Network Type (DHCP/Static)

    • Request Body (for DHCP):
      { "net_type": "dhcp" }
      
    • Request Body (for Static IP):
      {
          "net_type": "static",
          "ip": "192.168.1.100",
          "gateway": "192.168.1.1",
          "subnet": "255.255.255.0",
          "dns1": "8.8.8.8",
          "dns2": "8.8.4.4"
      }
      
    • Success Response: 204 No Content
  • Action: Configure Wi-Fi Mode (STA/APSTA)

    • Request Body (for STA mode):
      { "mode": "sta" }
      
    • Request Body (for AP+STA mode):
      {
          "mode": "apsta",
          "ap_ssid": "ODROID-Remote-AP",
          "ap_password": "hardkernel"
      }
      
      Note: ap_password is optional. If omitted, the AP will be open.
    • Success Response: 204 No Content

Endpoint: /api/wifi/scan

Scans for available Wi-Fi networks.

GET /api/wifi/scan

  • Success Response (200 OK): Returns a JSON array of found networks.
    [
      {
        "ssid": "MyHome_WiFi",
        "rssi": -55,
        "authmode": "WPA2_PSK"
      },
      {
        "ssid": "GuestNetwork",
        "rssi": -78,
        "authmode": "OPEN"
      }
    ]
    
  • Response Fields:
    • ssid (string): The network's Service Set Identifier.
    • rssi (integer): Signal strength in dBm.
    • authmode (string): The authentication mode (e.g., "OPEN", "WPA_PSK", "WPA2_PSK").