From 77ad4e849b58950e674648c0f81e96a6515a8573 Mon Sep 17 00:00:00 2001 From: Kristjan Komlosi Date: Fri, 6 Feb 2026 00:04:32 +0100 Subject: [PATCH] client: demo sketch --- esp-demo/esp-demo.ino | 68 +++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/esp-demo/esp-demo.ino b/esp-demo/esp-demo.ino index 578ad97..7f4c316 100644 --- a/esp-demo/esp-demo.ino +++ b/esp-demo/esp-demo.ino @@ -5,18 +5,18 @@ #include // ---- Configure these ---- -const char *WIFI_SSID = "YOUR_WIFI_SSID"; -const char *WIFI_PASS = "YOUR_WIFI_PASS"; +const char *WIFI_SSID = "seminardemo"; +const char *WIFI_PASS = "seminardemo"; -const char *MQTT_HOST = "192.168.1.10"; // broker address (use host running mosquitto) +const char *MQTT_HOST = "192.168.1.16"; // broker address (use host running mosquitto) const uint16_t MQTT_PORT = 1883; const char *MQTT_USER = "testuser"; // from test/.env const char *MQTT_PASS = "testpass"; // from test/.env - +\ const char *MQTT_TOPIC = "lambdaiot"; // main topic // GPIO -const uint8_t ACTOR_PIN = D1; // digital output +const uint8_t ACTOR_PIN = 14; // digital output // Publishing interval (ms) const unsigned long SENSOR_PUBLISH_INTERVAL = 5000; @@ -73,18 +73,46 @@ String formatUuid(const uint8_t bytes[16]) { return String(buf); } +bool parseHexBytes(const String &hex, uint8_t *out, size_t outLen) { + if (hex.length() < (int)(outLen * 2)) { + return false; + } + for (size_t i = 0; i < outLen; i++) { + char c1 = hex[i * 2]; + char c2 = hex[i * 2 + 1]; + auto hexVal = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; + }; + int v1 = hexVal(c1); + int v2 = hexVal(c2); + if (v1 < 0 || v2 < 0) { + return false; + } + out[i] = (uint8_t)((v1 << 4) | v2); + } + return true; +} + String uuidV5(const uint8_t namespaceBytes[16], const String &name) { // SHA1(namespace + name) - SHA1Builder sha; - sha.begin(); - sha.add(namespaceBytes, 16); - sha.add((const uint8_t *)name.c_str(), name.length()); - sha.calculate(); - uint8_t hash[20]; - sha.getHash(hash); + size_t totalLen = 16 + name.length(); + uint8_t *buf = (uint8_t *)malloc(totalLen); + if (!buf) { + return String(""); + } + memcpy(buf, namespaceBytes, 16); + memcpy(buf + 16, name.c_str(), name.length()); + + String hashHex = sha1(buf, totalLen); + free(buf); uint8_t out[16]; - memcpy(out, hash, 16); + if (!parseHexBytes(hashHex, out, sizeof(out))) { + return String(""); + } // Set version (5) and variant (RFC 4122) out[6] = (out[6] & 0x0F) | 0x50; @@ -96,7 +124,7 @@ String uuidV5(const uint8_t namespaceBytes[16], const String &name) { // ---- MQTT messaging ---- void publishDiscovery() { - StaticJsonDocument<512> doc; + StaticJsonDocument<1024> doc; doc["mac_address"] = macAddress; JsonObject dev = doc.createNestedObject("device"); @@ -120,11 +148,16 @@ void publishDiscovery() { a0["type"] = "GPIO"; a0["data_type_id"] = 1; - char buf[512]; + char buf[1024]; size_t n = serializeJson(doc, buf, sizeof(buf)); + + Serial.println(n); + Serial.println(buf); String topic = String(MQTT_TOPIC) + "/discovery"; - mqtt.publish(topic.c_str(), buf, n); + Serial.println(topic); + int result = mqtt.publish(topic.c_str(), buf, n); + Serial.println(result); } void publishSensorReading() { @@ -234,6 +267,7 @@ void ensureMqtt() { String clientId = "esp-demo-" + String(ESP.getChipId()); if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) { mqtt.subscribe(MQTT_TOPIC); + delay(1000); publishDiscovery(); } else { delay(2000); @@ -244,6 +278,7 @@ void ensureMqtt() { void setup() { pinMode(ACTOR_PIN, OUTPUT); digitalWrite(ACTOR_PIN, LOW); + Serial.begin(9600); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); @@ -265,6 +300,7 @@ void setup() { sensorId = uuidV5(deviceNs, "sensor-0"); actorId = uuidV5(deviceNs, "actor-0"); + mqtt.setBufferSize(4096); // Otherwise discovery fails spectacularly mqtt.setServer(MQTT_HOST, MQTT_PORT); mqtt.setCallback(mqttCallback);