Old Macdonald - Decentralised Controlled Environment Agriculture System Help

Messages

Overview

The inbound message handler is passed to the Websocket Server component when it is initialized. This function is automatically called whenever a message is received from a client. It parses the JSON message, verifies the type and action fields, and then forwards the request to the appropriate command handler.

Outbound messages are generated by the Orchestrator in response to events or command results and are sent to all connected clients.

Format

Messages use JSON-encoded UTF-8 format and follow this structure:

Field

Type

Description

type

string

Type of message:


"command" is sent from client to request an action


"info" is sent from device as a response or update

action

string

Describes the specific command or event

payload

object

Contains the relevant data for the given action

Example of a command from Client:

{ "type": "command", "action": "wifi.sta_connect", "payload": { "ssid": "MyWiFi", "password": "pass1234" } }

Example of info from Orchestrator:

{ "type": "info", "action": "wifi.status", "payload": { "status": "connected" } }

JSON Data

The cJSON library is used to parse and generate JSON data.

It is part of the ESP-IDF components. json should be added to the REQUIRES section of the CMakeLists.txt file.

Usage:

# Include cJSON in source code: #include "cJSON.h" # Parsing JSON: cJSON *root = cJSON_Parse(my_json_string); cJSON *format = cJSON_GetObjectItem(root, "format"); int framerate = cJSON_GetObjectItem(format, "frame rate")->valueint; cJSON_Delete(root); // Clean up after use # Creating JSON: cJSON *root = cJSON_CreateObject(); cJSON *fmt = cJSON_CreateObject(); cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble")); cJSON_AddItemToObject(root, "format", fmt); cJSON_AddStringToObject(fmt, "type", "rect"); cJSON_AddNumberToObject(fmt, "width", 1920); cJSON_AddNumberToObject(fmt, "height", 1080); cJSON_AddFalseToObject(fmt, "interlace"); cJSON_AddNumberToObject(fmt, "frame rate", 24); char *json_str = cJSON_Print(root); printf("%s\n", json_str); cJSON_Delete(root); // Clean up # Manual Traversal: void parse_object(cJSON *item) { cJSON *subitem = item->child; while (subitem) { printf("Key: %s\n", subitem->string); subitem = subitem->next; } }
Last modified: 07 May 2025