breadboard
documentação

Projeto de exemplo · Casa inteligente

Sensor de temperatura e hub Zigbee

Neste exemplo, o sensor Zigbee envia leituras ao hub: um sensor de temperatura fechado, no estilo dos alimentados por bateria, entra no hub do termostato e informa pelo cluster padrão de medição de temperatura. O trimpot na sonda dele (0–3,3 V = −10…+50 °C) ajusta a temperatura simulada: arraste-o e veja a aba serial do hub imprimir cada informe recebido. Os dois sketches de firmware estão em Code.

O circuito Sensor de temperatura e hub Zigbee desenhado pelo simulador

Como funciona

Neste exemplo, o sensor Zigbee envia leituras ao hub. O C6 do sensor de temperatura fechado lê o pino da sonda com o ADC real (0–3,3 V corresponde a −10…+50 °C) e envia o valor pelo cluster padrão de medição de temperatura. O DevKit do hub roda o firmware de coordenador de termostato: ele forma a rede, espera o sensor se vincular, pede a configuração do sensor e se inscreve para informes rápidos. A partir daí, cada mudança de temperatura chega como um informe de atributo real que a callback do hub imprime. O potenciômetro na sonda ajusta a temperatura simulada.

O que está na bancada

  • Bateria (Battery)
  • ESP32-C6
  • Sensor de temperatura Zigbee (Zigbee Temp Sensor)
  • Trimpot (Trim Pot)

Como está ligado

  • Bateria (Battery) · pos→Sensor de temperatura Zigbee (Zigbee Temp Sensor) · vcc
  • Bateria (Battery) · neg→Sensor de temperatura Zigbee (Zigbee Temp Sensor) · gnd
  • Bateria (Battery) · neg→ESP32-C6 · gnd2
  • Trimpot (Trim Pot) · a→ESP32-C6 · gnd2
  • Trimpot (Trim Pot) · b→ESP32-C6 · 3v3
  • Trimpot (Trim Pot) · wiper→Sensor de temperatura Zigbee (Zigbee Temp Sensor) · probe

O código

Os sketches Arduino C++ exatos usados para compilar o firmware incluído. No simulador, abra “Code” para editá-los e clique em “Compile & upload” para recompilar o firmware da placa.

Thermostat hub (coordinator)

// Zigbee thermostat hub — the coordinator for the temperature sensor.
//
// Forms the network, waits for the sensor to join and bind, asks it for its
// configuration and a fast reporting schedule (no typing needed), and then
// narrates every temperature report it receives over the air.

#include <Arduino.h>
#ifndef ZIGBEE_MODE_ZCZR
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
#endif

#include "Zigbee.h"

#define THERMOSTAT_ENDPOINT 1

ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT);

static float lastTemp = -1000;

void receiveSensorTemp(float temperature) {
  lastTemp = temperature;
  Serial.printf("Sensor reports %.1f C\r\n", temperature);
}

void receiveSensorConfig(float min_temp, float max_temp, float tolerance) {
  Serial.printf("Sensor config: %.1f..%.1f C, tolerance %.1f C\r\n", min_temp, max_temp, tolerance);
}

void setup() {
  Serial.begin(115200);

  zbThermostat.onTempReceive(receiveSensorTemp);
  zbThermostat.onTempConfigReceive(receiveSensorConfig);
  zbThermostat.setManufacturerAndModel("Breadboard", "ThermostatHub");

  Zigbee.addEndpoint(&zbThermostat);
  Zigbee.setRebootOpenNetwork(180);

  if (!Zigbee.begin(ZIGBEE_COORDINATOR)) {
    Serial.println("Zigbee failed to start!");
    Serial.println("Rebooting...");
    ESP.restart();
  }

  Serial.println("Network formed and open. Waiting for the temperature sensor to bind");
  while (!zbThermostat.bound()) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Sensor bound.");

  // Ask the sensor for its settings and a fast reporting schedule — the
  // stock example does this on a button press; the hub just does it.
  std::list<zb_device_params_t *> boundSensors = zbThermostat.getBoundDevices();
  for (const auto &device : boundSensors) {
    if (device->short_addr == 0x0000 || device->short_addr == 0xFFFF) {
      zbThermostat.getTemperatureSettings(device->endpoint, device->ieee_addr);
    } else {
      Serial.printf("Sensor on endpoint %u, short address 0x%04x\r\n", device->endpoint, device->short_addr);
      zbThermostat.getTemperatureSettings(device->endpoint, device->short_addr);
    }
  }
  delay(500);
  zbThermostat.setTemperatureReporting(0, 10, 0.5);
  Serial.println("Reporting requested: every temperature change arrives here.");
}

void loop() {
  static uint32_t lastPrint = 0;
  if (millis() - lastPrint > 10000) {
    lastPrint = millis();
    if (lastTemp > -1000) {
      Serial.printf("Latest sensor temperature: %.1f C\r\n", lastTemp);
    }
  }
  delay(100);
}

Temp sensor (end device)

// Zigbee temperature sensor — a battery-style end device that measures on
// its analog probe (GPIO2 / ADC1_CH2) and reports over the air.
//
// The probe voltage maps 0..3.3 V onto -10..+50 °C, so a potentiometer
// wiper on the probe pin sweeps the reported temperature. Reports ride the
// standard temperature-measurement cluster: the coordinator (e.g. the
// thermostat hub) receives genuine attribute reports.

#include <Arduino.h>
#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif

#include "Zigbee.h"

#define ZIGBEE_TEMP_ENDPOINT 10
const uint8_t probe = 2;  // GPIO2 = ADC1_CH2

ZigbeeTempSensor zbTemp = ZigbeeTempSensor(ZIGBEE_TEMP_ENDPOINT);

static float readProbeC() {
  int raw = analogRead(probe);  // 0..4095 across 0..3.3 V
  return (raw / 4095.0f) * 60.0f - 10.0f;
}

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);

  zbTemp.setManufacturerAndModel("Breadboard", "TempSensor");
  zbTemp.setMinMaxValue(-10, 50);
  zbTemp.setTolerance(0.5);

  Serial.println("Adding ZigbeeTempSensor endpoint to Zigbee Core");
  Zigbee.addEndpoint(&zbTemp);

  if (!Zigbee.begin()) {
    Serial.println("Zigbee failed to start!");
    Serial.println("Rebooting...");
    ESP.restart();
  }
  Serial.println("Connecting to network");
  while (!Zigbee.connected()) {
    Serial.print(".");
    delay(100);
  }
  Serial.println();

  // Report at most every second, at least every 10 s, or on a 0.5 °C move.
  zbTemp.setReporting(1, 10, 0.5);
  Serial.println("Joined. Reporting probe temperature (GPIO2, 0-3.3V = -10..50C).");
}

void loop() {
  static uint32_t lastRead = 0;
  if (millis() - lastRead >= 2000) {
    lastRead = millis();
    float t = readProbeC();
    zbTemp.setTemperature(t);
    Serial.printf("probe %.1f C\r\n", t);
  }
  delay(100);
}

Licença do firmware (em inglês)

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

arduino-zigbee-thermostat-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.

It also links Espressif’s ZBOSS Zigbee stack, supplied unchanged under its own licence, which forbids reverse engineering, decompiling, modifying or disassembling the ZBOSS binary itself. Relink with the ZBOSS archives unchanged.

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

arduino-zigbee-tempsensor-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.

It also links Espressif’s ZBOSS Zigbee stack, supplied unchanged under its own licence, which forbids reverse engineering, decompiling, modifying or disassembling the ZBOSS binary itself. Relink with the ZBOSS archives unchanged.

Experimente

  • Observe a aba serial do hub: a rede se forma, o sensor se vincula, a configuração chega e então os informes vão chegando.
  • Arraste o potenciômetro. A serial do próprio sensor imprime cada leitura da sonda, e o hub imprime cada informe recebido.
  • Clique no sensor de temperatura: o Inspector mostra a temperatura da sonda ao vivo.
  • Abra Code: os dois sketches reais (hub + sensor), exatamente os usados para compilar o firmware.
Informar um problema na tradução

Projetos relacionados