breadboard
documentación

Proyecto de ejemplo · Hogar inteligente

Red de sensores Thread (central y nodo)

Thread visible: la central DevKit forma la red como líder y escucha UDP; el sensor cerrado se conecta como hijo e informa su temperatura cada 5 segundos, cada informe en una trama 802.15.4 protegida por MAC visible en Wi-Fi sniffer. Observa cada datagrama en la pestaña serial de la central; la CLI completa de OpenThread sigue disponible (`child table`, `ipaddr`). Ambos sketches están en Code.

El circuito Red de sensores Thread (central y nodo) tal como lo dibuja el simulador

Cómo funciona

Todo Thread en dos placas: la central DevKit confirma un conjunto de datos operativo completo, forma la red como líder y abre UDP en el puerto 12345. El sensor cerrado tiene los mismos datos, espera unos segundos para que se forme el líder y se conecta como hijo mediante MLE protegido por MAC. Después informa su temperatura cada 5 segundos: datagramas UDP al multicast local de malla ff03::1 que la central imprime al llegar. La CLI completa OpenThread sigue en ambas pestañas seriales.

Componentes del montaje

  • Batería (Battery)
  • ESP32-C6
  • Nodo sensor Thread (Thread Sensor Node)

Cómo está conectado

  • Batería (Battery) · pos→Nodo sensor Thread (Thread Sensor Node) · vcc
  • Batería (Battery) · neg→Nodo sensor Thread (Thread Sensor Node) · gnd

El código

Los sketches de Arduino C++ exactos con los que se compiló el firmware incluido. En el simulador, abre Code para editarlos y pulsa Compile & upload para volver a compilar el firmware de la placa.

Thread hub (leader)

// Thread hub leader — forms the Breadboard Thread network and listens on
// UDP :12345 for the sensor node's reports, printing each one. Same
// complete fixed dataset as the sensor sketch; this board starts
// immediately so it becomes the leader.

#include <Arduino.h>
#include "OThread.h"
#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"

OpenThread node;

// Must be identical to the ThreadSensorNode sketch's dataset.
static const char *DATASET[][2] = {
  {"dataset activetimestamp", "1"},
  {"dataset channel", "15"},
  {"dataset channelmask", "0x07fff800"},
  {"dataset panid", "0x1234"},
  {"dataset extpanid", "1122334455667788"},
  {"dataset networkname", "Breadboard"},
  {"dataset networkkey", "00112233445566778899aabbccddeeff"},
  {"dataset pskc", "00112233445566778899aabbccddeeff"},
  {"dataset meshlocalprefix", "fd11:2233:4455:6677::"},
  {"dataset securitypolicy", "672 onrc"},
  {"dataset commit", "active"},
  {"ifconfig", "up"},
  {"thread", "start"},
};

static bool udpBound = false;

void setup() {
  Serial.begin(115200);
  OThread.begin(false);  // fresh start — don't resume a dataset from NVS
  OThreadCLI.begin();
  Serial.println("Thread hub starting: forming the Breadboard network.");
  for (auto &cmd : DATASET) {
    if (!otExecCommand(cmd[0], cmd[1])) {
      Serial.printf("[thread] '%s %s' failed\r\n", cmd[0], cmd[1]);
    }
  }
  Serial.println("Thread started - CLI ready, try `state` or `child table`.");
  Serial.print("ot> ");
}

// Serial ↔ CLI pump (received UDP datagrams print through the CLI stream).
static void pumpCli() {
  while (Serial.available() > 0) {
    char ch = (char)Serial.read();
    Serial.write(ch);
    OThreadCLI.write((uint8_t)ch);
  }
  static String lineBuf;
  while (OThreadCLI.available() > 0) {
    char ch = (char)OThreadCLI.read();
    Serial.write(ch);
    if (ch == '\n') {
      if (lineBuf.startsWith("Done") || lineBuf.startsWith("Error")) {
        Serial.print("ot> ");
      }
      lineBuf = "";
    } else if (ch != '\r') {
      lineBuf += ch;
    }
  }
}

void loop() {
  static ot_device_role_t lastRole = OT_ROLE_DISABLED;
  ot_device_role_t role = node.otGetDeviceRole();
  if (role != lastRole) {
    lastRole = role;
    Serial.printf("\r\n[thread] role: %s\r\n", node.otGetStringDeviceRole());
    if (role == OT_ROLE_LEADER) {
      Serial.printf("[thread] network \"%s\" formed, rloc16 0x%04x\r\n", node.getNetworkName().c_str(), node.getRloc16());
    }
  }

  // Once the network is up, listen for the sensor's reports.
  if (!udpBound && role == OT_ROLE_LEADER) {
    if (otExecCommand("udp", "open") && otExecCommand("udp", "bind :: 12345")) {
      udpBound = true;
      Serial.println("[hub] listening on udp :12345 - sensor reports will print below.");
      Serial.print("ot> ");
    }
  }

  pumpCli();
  delay(50);
}

Sensor node

// Thread sensor node — a battery-style sensor that attaches to the
// Breadboard Thread network as a child and reports its temperature over
// UDP to the mesh-local all-nodes multicast (ff03::1), where the hub
// leader is listening. Same complete fixed dataset as the leader; `thread
// start` held back so the leader forms first.

#include <Arduino.h>
#include "OThread.h"
#include "OThreadCLI.h"
#include "OThreadCLI_Util.h"

OpenThread node;

// Must be identical to the ThreadHubLeader sketch's dataset.
static const char *DATASET[][2] = {
  {"dataset activetimestamp", "1"},
  {"dataset channel", "15"},
  {"dataset channelmask", "0x07fff800"},
  {"dataset panid", "0x1234"},
  {"dataset extpanid", "1122334455667788"},
  {"dataset networkname", "Breadboard"},
  {"dataset networkkey", "00112233445566778899aabbccddeeff"},
  {"dataset pskc", "00112233445566778899aabbccddeeff"},
  {"dataset meshlocalprefix", "fd11:2233:4455:6677::"},
  {"dataset securitypolicy", "672 onrc"},
  {"dataset commit", "active"},
  {"ifconfig", "up"},
  {"thread", "start"},
};

static bool udpOpen = false;

void setup() {
  Serial.begin(115200);
  OThread.begin(false);  // fresh start — don't resume a dataset from NVS
  OThreadCLI.begin();
  Serial.println("Thread sensor starting: attaching to the Breadboard network once the hub is up.");
  delay(8000);  // let the hub form first → deterministic roles
  for (auto &cmd : DATASET) {
    if (!otExecCommand(cmd[0], cmd[1])) {
      Serial.printf("[thread] '%s %s' failed\r\n", cmd[0], cmd[1]);
    }
  }
  Serial.println("Thread started - reports begin once attached.");
}

// Synthetic temperature: a slow triangle wave 18..28 C, deterministic.
static float sensorTempC() {
  uint32_t phase = (millis() / 1000) % 40;              // 40 s period
  float ramp = phase < 20 ? phase : 40 - phase;         // 0..20..0
  return 18.0f + ramp * 0.5f;                           // 18..28 C
}

static void pumpCli() {
  while (Serial.available() > 0) {
    char ch = (char)Serial.read();
    Serial.write(ch);
    OThreadCLI.write((uint8_t)ch);
  }
  while (OThreadCLI.available() > 0) {
    Serial.write((char)OThreadCLI.read());
  }
}

void loop() {
  static ot_device_role_t lastRole = OT_ROLE_DISABLED;
  ot_device_role_t role = node.otGetDeviceRole();
  if (role != lastRole) {
    lastRole = role;
    Serial.printf("\r\n[thread] role: %s\r\n", node.otGetStringDeviceRole());
    if (role == OT_ROLE_CHILD || role == OT_ROLE_ROUTER) {
      Serial.printf("[thread] attached, rloc16 0x%04x\r\n", node.getRloc16());
    }
  }

  bool attached = role == OT_ROLE_CHILD || role == OT_ROLE_ROUTER || role == OT_ROLE_LEADER;
  if (attached && !udpOpen) {
    udpOpen = otExecCommand("udp", "open");
    if (udpOpen) {
      Serial.println("[sensor] udp open - reporting every 5 s to ff03::1:12345");
    }
  }

  static uint32_t lastReport = 0;
  if (attached && udpOpen && millis() - lastReport >= 5000) {
    lastReport = millis();
    char args[64];
    float t = sensorTempC();
    // OT CLI: udp send <ip> <port> <ascii-payload>
    snprintf(args, sizeof(args), "send ff03::1 12345 temp=%.1fC", t);
    if (otExecCommand("udp", args)) {
      Serial.printf("[sensor] sent temp=%.1fC\r\n", t);
    }
  }

  pumpCli();
  delay(100);
}

Licencia del firmware (en inglés)

Notice: this firmware uses the LGPL-licensed Arduino core library

arduino-thread-hub-c6.bin is statically linked with Arduino-ESP32 3.3.10. Parts of this library are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version. You may modify this work for your own use and reverse engineer it to debug those modifications. Download the relink package and corresponding source to rebuild it with a changed library.

Notice: this firmware uses the LGPL-licensed Arduino core library

arduino-thread-sensor-c6.bin is statically linked with Arduino-ESP32 3.3.10. Parts of this library are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version. You may modify this work for your own use and reverse engineer it to debug those modifications. Download the relink package and corresponding source to rebuild it with a changed library.

Prueba esto

  • Observa Serial de la central: función leader, después 'listening on udp :12345' y datagramas temp=…C cada 5 s.
  • Escribe `child table` en Serial de la central para ver el sensor como hijo.
  • Abre Wi-Fi sniffer: cada informe es una trama de datos 802.15.4 protegida por MAC.
  • Reinicia el sensor desconectando y reconectando la batería; observa cómo se conecta y vuelve a informar.
Informar de un problema de traducción

Proyectos relacionados