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.


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);

client.on("connect", function () {
    console.log(`Client has connected successfully to ${topic}`);

client.on("message", (topic, message) => {
    if (topic.toString() == topic) {
        console.log(`Values are ${message}`);

const publish = async function (packet) {
    const trytes = Converter.asciiToTrytes(JSON.stringify(packet));

    const message = Mam.create(state, trytes);
    state = message.state;

    await Mam.attach(message.payload, message.address);
    return message.root;


module.exports = {
    brokerUrl: "",
    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 Answer 1


Disclaimer: I am not a developer

Have you tried following this tutorial?


Comparing the code might help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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