From 958b76abe83dbbae52a913ea0317a4688cd8fa2a Mon Sep 17 00:00:00 2001 From: Dawid Chyrzynski Date: Sun, 6 Dec 2020 12:53:41 +0100 Subject: [PATCH] add sensor example --- examples/sensor/sensor.ino | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/sensor/sensor.ino diff --git a/examples/sensor/sensor.ino b/examples/sensor/sensor.ino new file mode 100644 index 0000000..981f86d --- /dev/null +++ b/examples/sensor/sensor.ino @@ -0,0 +1,49 @@ +#include +#include + +byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; +unsigned long lastSentAt = millis(); +double lastValue = 0; + +EthernetClient client; +HADevice device(mac, sizeof(mac)); +HAMqtt mqtt(client, device); + +/** + * Supported data types: + * - uint8_t + * - uint16_t (unsigned int) + * - uint32_t (unsigned long) + * - int8_t + * - int16_t (int) + * - int32_t (long) + * - double + * - float + */ +HASensor temp("temp", 0, mqtt); + +void setup() { + // you don't need to verify return status + Ethernet.begin(mac); + + // set device's details (optional) + device.setName("Arduino"); + device.setSoftwareVersion("1.0.0"); + + // you can set custom units for the sensor (optional) + temp.setUnitOfMeasurement("°C"); + + // please change IP address to yours + mqtt.begin(IPAddress(192,168,0,17)); +} + +void loop() { + Ethernet.maintain(); + mqtt.loop(); + + if ((millis() - lastSentAt) >= 5000) { + lastSentAt = millis(); + lastValue = lastValue + 0.5; + temp.setValue(lastValue); + } +}