client: demo sketch
This commit is contained in:
+52
-16
@@ -5,18 +5,18 @@
|
|||||||
#include <Hash.h>
|
#include <Hash.h>
|
||||||
|
|
||||||
// ---- Configure these ----
|
// ---- Configure these ----
|
||||||
const char *WIFI_SSID = "YOUR_WIFI_SSID";
|
const char *WIFI_SSID = "seminardemo";
|
||||||
const char *WIFI_PASS = "YOUR_WIFI_PASS";
|
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 uint16_t MQTT_PORT = 1883;
|
||||||
const char *MQTT_USER = "testuser"; // from test/.env
|
const char *MQTT_USER = "testuser"; // from test/.env
|
||||||
const char *MQTT_PASS = "testpass"; // from test/.env
|
const char *MQTT_PASS = "testpass"; // from test/.env
|
||||||
|
\
|
||||||
const char *MQTT_TOPIC = "lambdaiot"; // main topic
|
const char *MQTT_TOPIC = "lambdaiot"; // main topic
|
||||||
|
|
||||||
// GPIO
|
// GPIO
|
||||||
const uint8_t ACTOR_PIN = D1; // digital output
|
const uint8_t ACTOR_PIN = 14; // digital output
|
||||||
|
|
||||||
// Publishing interval (ms)
|
// Publishing interval (ms)
|
||||||
const unsigned long SENSOR_PUBLISH_INTERVAL = 5000;
|
const unsigned long SENSOR_PUBLISH_INTERVAL = 5000;
|
||||||
@@ -73,18 +73,46 @@ String formatUuid(const uint8_t bytes[16]) {
|
|||||||
return String(buf);
|
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) {
|
String uuidV5(const uint8_t namespaceBytes[16], const String &name) {
|
||||||
// SHA1(namespace + name)
|
// SHA1(namespace + name)
|
||||||
SHA1Builder sha;
|
size_t totalLen = 16 + name.length();
|
||||||
sha.begin();
|
uint8_t *buf = (uint8_t *)malloc(totalLen);
|
||||||
sha.add(namespaceBytes, 16);
|
if (!buf) {
|
||||||
sha.add((const uint8_t *)name.c_str(), name.length());
|
return String("");
|
||||||
sha.calculate();
|
}
|
||||||
uint8_t hash[20];
|
memcpy(buf, namespaceBytes, 16);
|
||||||
sha.getHash(hash);
|
memcpy(buf + 16, name.c_str(), name.length());
|
||||||
|
|
||||||
|
String hashHex = sha1(buf, totalLen);
|
||||||
|
free(buf);
|
||||||
|
|
||||||
uint8_t out[16];
|
uint8_t out[16];
|
||||||
memcpy(out, hash, 16);
|
if (!parseHexBytes(hashHex, out, sizeof(out))) {
|
||||||
|
return String("");
|
||||||
|
}
|
||||||
|
|
||||||
// Set version (5) and variant (RFC 4122)
|
// Set version (5) and variant (RFC 4122)
|
||||||
out[6] = (out[6] & 0x0F) | 0x50;
|
out[6] = (out[6] & 0x0F) | 0x50;
|
||||||
@@ -96,7 +124,7 @@ String uuidV5(const uint8_t namespaceBytes[16], const String &name) {
|
|||||||
// ---- MQTT messaging ----
|
// ---- MQTT messaging ----
|
||||||
|
|
||||||
void publishDiscovery() {
|
void publishDiscovery() {
|
||||||
StaticJsonDocument<512> doc;
|
StaticJsonDocument<1024> doc;
|
||||||
doc["mac_address"] = macAddress;
|
doc["mac_address"] = macAddress;
|
||||||
|
|
||||||
JsonObject dev = doc.createNestedObject("device");
|
JsonObject dev = doc.createNestedObject("device");
|
||||||
@@ -120,11 +148,16 @@ void publishDiscovery() {
|
|||||||
a0["type"] = "GPIO";
|
a0["type"] = "GPIO";
|
||||||
a0["data_type_id"] = 1;
|
a0["data_type_id"] = 1;
|
||||||
|
|
||||||
char buf[512];
|
char buf[1024];
|
||||||
size_t n = serializeJson(doc, buf, sizeof(buf));
|
size_t n = serializeJson(doc, buf, sizeof(buf));
|
||||||
|
|
||||||
|
Serial.println(n);
|
||||||
|
Serial.println(buf);
|
||||||
|
|
||||||
String topic = String(MQTT_TOPIC) + "/discovery";
|
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() {
|
void publishSensorReading() {
|
||||||
@@ -234,6 +267,7 @@ void ensureMqtt() {
|
|||||||
String clientId = "esp-demo-" + String(ESP.getChipId());
|
String clientId = "esp-demo-" + String(ESP.getChipId());
|
||||||
if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
|
if (mqtt.connect(clientId.c_str(), MQTT_USER, MQTT_PASS)) {
|
||||||
mqtt.subscribe(MQTT_TOPIC);
|
mqtt.subscribe(MQTT_TOPIC);
|
||||||
|
delay(1000);
|
||||||
publishDiscovery();
|
publishDiscovery();
|
||||||
} else {
|
} else {
|
||||||
delay(2000);
|
delay(2000);
|
||||||
@@ -244,6 +278,7 @@ void ensureMqtt() {
|
|||||||
void setup() {
|
void setup() {
|
||||||
pinMode(ACTOR_PIN, OUTPUT);
|
pinMode(ACTOR_PIN, OUTPUT);
|
||||||
digitalWrite(ACTOR_PIN, LOW);
|
digitalWrite(ACTOR_PIN, LOW);
|
||||||
|
Serial.begin(9600);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
@@ -265,6 +300,7 @@ void setup() {
|
|||||||
sensorId = uuidV5(deviceNs, "sensor-0");
|
sensorId = uuidV5(deviceNs, "sensor-0");
|
||||||
actorId = uuidV5(deviceNs, "actor-0");
|
actorId = uuidV5(deviceNs, "actor-0");
|
||||||
|
|
||||||
|
mqtt.setBufferSize(4096); // Otherwise discovery fails spectacularly
|
||||||
mqtt.setServer(MQTT_HOST, MQTT_PORT);
|
mqtt.setServer(MQTT_HOST, MQTT_PORT);
|
||||||
mqtt.setCallback(mqttCallback);
|
mqtt.setCallback(mqttCallback);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user