I have pulse sensor connected to NodeMCU. This sensor publishes data to Mosquitto by MQTT protocol. I want to receive data from broker and publish it to tangle using MQTT and MAM. Mosquitto broker is working fine and receive data from sensor. But the code to publish data to tangle is not working correctly. Here is my code on virtual machine acting as a server.
app.js
const mqtt = require("mqtt");
const Mam = require("@iota/mam");
const Converter = require("@iota/converter");
const provider = require("./config/config").provider;
const brokerUrl = require("./config/config").brokerUrl;
const port = require("./config/config").port;
const topic = require("./config/config").topic;
const client = mqtt.connect(`${brokerUrl}:${port}`);
let state = Mam.init(provider);
console.log(state);
client.on("connect", function () {
client.subscribe(topic);
console.log(`Client has connected successfully to ${topic}`);
});
client.on("message", (topic, message) => {
if (topic.toString() == topic) {
console.log(`Values are ${message}`);
publish(JSON.parse(message));
}
});
const publish = async function (packet) {
const trytes = Converter.asciiToTrytes(JSON.stringify(packet));
const message = Mam.create(state, trytes);
state = message.state;
console.log(state);
await Mam.attach(message.payload, message.address);
console.log(message);
return message.root;
};
config.js
module.exports = {
brokerUrl: "192.168.0.105",
port: "1883",
topic: "/pulse",
provider: "https://nodes.devnet.iota.org:443"
};
the output: It can be seen it does not receive data from broker it should be like this: MQTT broker:
please help!