Steven W. Buehler

God, Games, and Geekery.

Use IFTTT With Your Memobird Again

IFTTT is a free online site that helps connect the Internet of Things together using “triggers” and “actions” such that a trigger from one app (say, your Fitbit logs some hours of sleep) produces an action in another app (a row is created in a Google Sheet with the Fitbit’s data).

The Memobird is a small wireless thermal printer, made in China and sold in Asia and North America, that works with a mobile or web app to print small notes (or long ones) on 2¼″ thermal receipt paper.

For a time, IFTTT offerred a Memobird action to send text and image notes to the device. And all was right with the world, until IFTTT quietly removed Memobird from its ecosystem, leaving users with no way to send notes outside of the mobile app.

The Memobird makers have published a documented API that allows developers to create their own apps to send text, images, or HTML to the printer, but it requires a little programming know-how. With an access key for the API, I was able to recreate the original IFTTT functionality by creating a Google Apps Script-based Web App that performs the same function, with IFTTT calling it via the Webhook action. In the process, I was able to improve the original action by getting the text to properly break lines to fit within the Memobird’s 32-column page width (something IFTTT’s version did not do, and something you wouldn’t have had to worry about, if you were sending content in Chinese).

The Memobird Open API

Memobird has opened up its API to developers and published documentation at http://api.memobird.cn, but it lacks important information (such as what format your requests need to be in). After some trial and error and back-and-forth with the API designers, I put together my own version of the API documentation that I feel better explains how to use it.

The first thing you’ll need, though, is an access key from Intertech (the Chinese company that makes the device). Both versions of the API docs have instructions on how to get one. Once you have a key, and use the setuserbind endpoint described in the docs to get a userID, you’re ready to start sending stuff to your Memobird via the API.

The Google Apps Script

Google Apps Script is a free Google product that lets you write scripts in a JavaScript-like syntax to access Google and other services via Google’s or others’ APIs, with many of Google’s services having built-in objects to access things more easily. You can publish your scripts as Web Apps that can be called with a POST webhook containing a payload (such as one from IFTTT) to make things happen in your Google Account—or, in this case, to send text to your Memobird.

When creating your Memobird script, you’ll need to first create Script Properties with three key pieces of information: Your access key, your Memobird’s Device ID, and the userID you got back from calling the setuserbind endpoint. To create these, go to the File menu in Google Apps Script and choose Project Properties. From there, one of the tabs is Script properties to add these three items.

Example of Script Properties

Now that you have those set, following is the actual code:

function doPost(e) {
  eval(UrlFetchApp.fetch('https://momentjs.com/downloads/moment.min.js').getContentText()); // use latest version of moment.js library for parsing timestamps (needed for 'timestamp' parameter) 
  myPostData = JSON.parse(e.postData.contents);
  var text = myPostData.text.replace(/(?![^\n]{1,32}$)([^\n]{1,32})\s/g, '$1\n');
  var textBase64 = Utilities.base64Encode(text);
  var result = UrlFetchApp.fetch('http://api.memobird.cn/home/printpaper',
                                 { muteHttpExceptions : false,
                                   method : 'POST',
                                  contentType: 'application/x-www-form-urlencoded',
                                  payload: {
                                    'ak' : PropertiesService.getScriptProperties().getProperty('ak'),
                                    'timestamp' : moment().format('YYYY-MM-DD HH:mm:ss'),
                                    'printcontent' : ('T:' + textBase64),
                                    'memobirdID' : PropertiesService.getScriptProperties().getProperty('memobirdID'),
                                    'userID' : PropertiesService.getScriptProperties().getProperty('userID')
                                  }
                                 });
}

You’ll notice an eval call at the very beginning to an external library, in this case the moment.js library, which provides date/time parsing and manipulation, in order to create the timestamp parameter needed for Memobird’s API call. This is an undocumented part of Google Apps Script which comes in handy when you want to call an outside JavaScript library. Not every library works this way, so you should first do a web search to see if a library you want to use hasn’t already been adapted into a Google Apps Script version.

The third line of the code has a .replace REGEX function that rewraps the text you’ve sent to fit the 32-column print width of the Memobird. The orignal IFTTT action did not perform any reformatting, so having words broken up across lines was common. A short web search found the elegant solution I incorporated as the .replace argument.

Once the code is in place, the next step is to make this script into a usable Web App. To do this, we go up to the Publish… menu and choose Deploy as web app….

Deploy as Web App dialog

To make the web app usable without having to log into Google (which is needed if you want to call this from IFTTT), you should set the app to be executed as you, and then set the “Who has access to the app:” parameter to “Anyone, even anonymous” (the likelihood of a random person figuring out the URL to your web app and abusing it is ridiculously small (unless you actually tell them what it is), so I do this setting). Once you’ve clicked on “Update” it gives you a URL you can use to call the script from the web.

Now to try it out.

On the IFTTT Side

Now to set up IFTTT to call this script—how you want to do so is really up to you, but the key thing to note is the body of the request. Just as an example, I’m going to have it print out my weight whenever I update it in the Fitbit app.

The first part is self-explanatory: Create a new trigger using the Fitbit application and choose “New Weight Logged”.

For the action, you need to choose “Webhooks” as the target app, then “Make a web request.” For the action settings, this is what I use:

  • URL: The web App URL provided to me in Google Apps Script when I set up the web app.

  • Method: POST

  • Content Type: application/json

  • Body: This is the important part. Notice I specified I was sending it a JSON payload by choosing application/json as the Content Type. This is what my body will look like:

{
	"text": "Logged weight at  "
}
  • Proceed to complete setting up the action as usual (including whether or not you want a notification when it runs).

So now, whenever I log my weight in my Fitbit app, a short time later I get this simple output on my Memobird.

This is just one example.

Caveats

  • Memobird’s Open API is not secure (it uses http, not https). While any content that you’re sending to the API is almost always base64 encoded, anyone can intercept it and decode it.

  • The API server is in China. For most everybody (including me), this isn’t a big deal. If you are concerned about sending confidential or personal data to your Memobird, though, both this and the fact that the API is not secure may prompt you to use a different IOT device with a local API on a secure connection, especially if you are paranoid about being snooped on considering the current trade tensions between the United States and China.

  • The original IFTTT action allowed you to break up lines with a <br> tag; this version of the action does not (it does, however, respect double-line-endings—\n\n—to break up paragraphs). I have a different script I use for sending HTML-formatted content which I’ll post on my public Gist and write about soon.

Visit my Public Gist Collection

You’re welcome to visit my public collection of Github Gists that I’ve made for other ways to send information to your Memobird printer, along with other Google Apps and PowerShell scripts I’ve put together for various purposes.

Github Twitter Facebook LinkedIn RSS