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.