var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://mqtt.chris.gunawardena.id.au');
client.on('connect', function () {
client.subscribe('/lights');
})
client.on('message', function (topic, message) {
console.log(message.toString());
var msg_obj = JSON.parse(message.toString());
client.publish(msg_obj.reply_id, 'xoxo');
});
https://github.com/chris-gunawardena/http2mqtt.js/blob/master/esp32/src/main.cpp
void callback(char* topic, byte* payload, unsigned int length) {
if (strcmp(topic, "/lights") == 0) {
JsonObject& root = jsonBuffer.parseObject((char *) payload);
const char* reply_id = root["reply_id"]; // "/reply/id_8106"
const char* light_state = root["body"]["result"]["parameters"]["light-state"]; // "on"
if (strcmp(light_state, "on") == 0) {
client.publish(reply_id, "{\"speech\": \"Switching lights on\", \"displayText\": \"Switching lights on\", \"data\": {}, \"contextOut\": [], \"source\": \"mqtt\" }");
} else {
client.publish(reply_id, "{\"speech\": \"Switching lights off\", \"displayText\": \"Switching lights off\", \"data\": {}, \"contextOut\": [], \"source\": \"mqtt\" }");
}
}
}
https://github.com/chris-gunawardena/IoT-H2O/blob/master/esp32/src/main.cpp
while (!pubSubClient.connected()) {
String clientId = "esp32client" + String(random(0xffff), HEX);
reply_id = "/request/reply/" + clientId;
if (pubSubClient.connect(clientId.c_str())) {
Serial.println("mqtt connected");
JsonObject& root = jsonBuffer.parseObject(request_json_template);
root["reply_id"] = reply_id;
String request;
root.printTo(request);
pubSubClient.subscribe(reply_id.c_str());
pubSubClient.publish("/request", request.c_str());
}
- Install docker. https://certbot.eff.org/#ubuntuxenial-other
- User certbot to create SSL certs on the host machine. https://certbot.eff.org/#ubuntuxenial-other
- Clone this repo.
- Update the SSL_HOST env var here: https://github.com/chris-gunawardena/http2mqtt.js/blob/master/docker-compose.yml#L8
- Run
docker-compose up -d
from the root folder of this repo.