MAM, currently one of IOTA’s best features, aims to become a standard for IoT devices, even though it is still under development. News about a full-featured rewrite of MAM: MAM+ was recently announced by the IOTA Foundation over a recent blog about the current state of its R&D projects.

In this short article you will learn about how to use MAM with your projects. But first, and before we begin coding, I highly recommend you to read:

IOTA: MAM Eloquently Explained. ABmushi, the author, will help understand perfectly the theory behind MAM. It is truly worth your time.

Once you are done with this reading, then you can return back to this tutorial and continue with this practice.

At the end of this tutorial you will be able to understand and use MAM in all its modes.

First, clone the following repository I built for this example.

You have 2 ways to do this, via Github or using Gitlab, Both are the same.

Now, execute “ node index.js [sec] [mode]“ with the following arguments.

sec: time for loop in seconds

mode: a number between 1 and 3 (Public: 1, Private: 2 and Restricted: 3)

i.e.

node index.js 60 1

This mean that you will send data to tangle each 60s in a public mode.

Executed you will have an output like this.

You can also use a fetcher as well. For each mode there is a specific file you can use. For this particular scenario we will use fetchPublic.js. The latter runs as “node fetchPublic.js [root]”

root: root since you want to fetch

i.e.

node fetchPublic.js MBNPVLPUPKDYKATZMSMVAWQKVUFMLAC9QSLTWWACMRKNLUDRORYFTNPCFESIMBIJTJUNDVRTELGAOAFZM

Fetching data from tangle.

Feel free to edit this code if your goal is to run an automatic process set at a specific time and for specific mode. In the restricted mode you will probably want to consider editing a bit more, since in a restricted mode you need a sidekey.

Go to lib->attachDataRestricted.js open it and then edit this line:

12. ‘CHRISLAIVELOVESIOTA’

Remember, sidekey is a Trytes encoded, consequently, if you want to use another String for sidekey you need to use the API to iota.lib.js and use iota.utils.toTrytes method. Once done, do the same with the fetcher.

Go to fetchData -> fetchRestricted.js open it and then edit this line:

18. let resp = await Mam.fetch(root, ‘restricted’,’CHRISLAIVELOVESIOTA’,logData)

Use the sidekey that you defined before and then save it.

For the next tutorial we will explore how to apply what you just learned in a simple use case: sending data from sensors to tangle. Also, if you are a beginner programmer, there will be a future tutorial explaining how to use NodeJS with Arduino, RaspberryPi, Spark, IntelEdison, and other boards with a simple use case.

Keep in mind that our final objective is graphically stated below:

For the most part, here in Peru, mobile connectivity is always a problem. LPWAN, like LoRaWAN for example, is one of the recommended solution to mitigate these type of issues. The idea is to have the data fetched from LoRaWAN sensors transferred to a LoRaWAN Gateway and then to a server (Cloud). Here’s where MAM comes into place. Running a script using MAM, the server can then transfer encrypted data to another server, or simply have it stored in the tangle. The latter can also process the data and transfer it downstream back to the internet or a Web App, and then back again to Gateway and other connected devices.

If you like this post and want the help me, here is my IOTA Address, donations is always appreciated :)

ETZKLIYPVHQCHKKX9KVZUGQ9CGMGKDVTQXHZKFBUCREHRIGJVATBAMVWZTOOXHVCZ9HOGVDQKLPKKJFHXXYYCNGVEY