3

I am trying to make a project to send Health data from my phone to Tangle via MAM. For this purpose, I have made a sample file that publishes sample health data to Tangle. When I am running the code, it is throwing an exception of 'failed to attach message'.

Can someone guide what could possibly be the reason?

const Mam = require('./lib/mam.client.js');
const IOTA = require('iota.lib.js');
const moment = require('moment');
const iota = new IOTA({ provider: 'https://nodes.devnet.iota.org'});
const { asciiToTrytes, trytesToAscii } = require('@iota/converter')
const mode = 'restricted'
const secretKey = 'VERYSECRETKEY'
const provider = 'https://nodes.devnet.iota.org'

const mamExplorerLink = `https://mam-explorer.firebaseapp.com/?provider=${encodeURIComponent(provider)}&mode=${mode}&key=${secretKey.padEnd(81, '9')}&root=`

let numberofSteps;
let kilometersWalked;

// Initialise MAM State
let mamState = Mam.init(provider)

// Set channel mode
mamState = Mam.changeMode(mamState, mode, secretKey)

// Publish data to the tangle
const publish = async packet => {
        // Create MAM Payload - STRING OF TRYTES
        const trytes = asciiToTrytes(JSON.stringify(packet))
        const message = Mam.create(mamState, trytes)
         // Save new mamState
        mamState = message.state
         // Attach the payload
        await Mam.attach(message.payload, message.address)    
        console.log('Published', packet, '\n');
        console.log (message.root);
        return message.root
    }

const publishAll = async () => {
    const root = await publish({
        numberofSteps: Math.floor((Math.random()*89)+10),
        kilometersWalked: (numberofSteps/1312.335),
        message: 'Message from Alice',
        timestamp: (new Date()).toLocaleString()
      })

      await publish({
        numberofSteps: Math.floor((Math.random()*89)+10),
        kilometersWalked: Math.floor(990/9),
        message: 'Message from Alice',
        timestamp: (new Date()).toLocaleString()
      })

      await publish({
        numberofSteps: Math.floor((Math.random()*89)+10),
        kilometersWalked: Math.floor(990/9),
        message: 'Message from Alice',
        timestamp: (new Date()).toLocaleString()
      })

      return root
}

publishAll()

Also, is there any possible way to automatically send Health data from iphone or android? I don't want the user to send the data, instead the process should be automated. Any help would be highly appreciated.

3

I just tested your code and it worked for me. This is an example of the response:

NVYRBTEAJAYAJMWIAHRMZNT9PQMIUWFTLQOB9RVAIWDLDOSZHPJCWEGNF9JTZLCTULRFCWFEAFAPUIMFM
Published
Object {numberofSteps: 14, kilometersWalked: 110, message: "Message from Alice", timestamp: "12/4/2019, 8:28:51 AM"}

To do this automatically, you could set up events in the code that collects health data. This way, your MAM code can be triggered whenever new health data events are emitted.

|improve this answer|||||
  • I'm getting this exception: failed to attach message: TypeError: Cannot read property 'sendTransfer' of undefined at _callee5$ (C:\Users\Sidra\Desktop\project\lib\mam.client.js:23390:46) at tryCatch (C:\Users\Sidra\Desktop\project\lib\mam.client.js:28056:40) at Generator.invoke [as _invoke] – degr8sid Dec 6 '19 at 15:28
  • After this exception, the data is published: <br/> Published { numberofSteps: 70, kilometersWalked: 110, message: 'Message from Alice', timestamp: '12/4/2019, 10:20:23 AM' } – degr8sid Dec 6 '19 at 15:29

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.