Skip to content

Examples

Practical examples for user-side integration with diy core.

1) Minimal project wiring

cpp
void setup() {
  diy_core_set_config(&cfg);
  diy_core_setup();
  user_setup();
}

void loop() {
  diy_core_loop();
  user_loop();
}

2) Custom command handling

cpp
extern "C" void diy_user_on_command(const char* cmd) {
  if (!cmd) return;

  if (strcmp(cmd, "relay_on") == 0) {
    digitalWrite(RELAY_PIN, HIGH);
    return;
  }

  if (strcmp(cmd, "relay_off") == 0) {
    digitalWrite(RELAY_PIN, LOW);
    
    // you can publish back to dashboard an event triggered by command
    diy_core_publishUserJson("event", "{\"event\":\"relay is off\"}");
    return;
  }
}

3) Built-in reserved commands

  • reboot
  • wifi_reset
  • ota_check

Do not reuse these names for custom handlers.

4) User event example

cpp
diy_core_publishUserJson("event", "{\"event\":\"button_click\"}");

5) User telemetry example

cpp
char payload[160];
snprintf(payload, sizeof(payload),
         "{\"temp\":%.1f,\"humidity\":%.1f}",
         tempC, hum);
diy_core_publishUserJson("telemetry", payload);

6) Good practice

  • Keep JSON valid and compact.
  • Keep loop non-blocking.
  • Send event/telemetry acknowledgments for custom commands.