2

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: enter image description here It can be seen it does not receive data from broker it should be like this: enter image description here MQTT broker: enter image description here

please help!

1

Disclaimer: I am not a developer

Have you tried following this tutorial?

https://hashnode.com/post/mqtt-and-mam-sending-temperature-and-humidity-data-to-the-iota-tangle-ck4wbbzap01artjs1t5dfog2t

Comparing the code might help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.