Alex Swan
Published

Love Notes

A small portable box that prints messages sent by a loved one.

IntermediateFull instructions provided2 hours4,017
Love Notes

Things used in this project

Hardware components

Electron
Particle Electron
×1
Adafruit Mini Thermal Receipt Printer Starter Pack
×1

Software apps and online services

Twilio Functions
Particle Build Web IDE
Particle Build Web IDE

Story

Read more

Schematics

Breadboard setup

How the components are connected.

Fritzing Sketch

Code

lovenotes.ino

Arduino
Arduino code used to control the Particle Electron
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_Thermal.h>
#include "cellular_hal.h"

Adafruit_Thermal printer;


// Connect to the Particle Cloud when we say so, so that we can set up
// cloud variables and functions and only connect once
SYSTEM_MODE(SEMI_AUTOMATIC)

// Soracom.io Credentials
STARTUP(cellular_credentials_set("soracom.io", "sora", "sora", NULL));


// This variable is for double checking the device is communicating
bool ready = false;

// This function will print whatever message is sent to it
int receiveMessage(String msg) {
    // For debug purposes, send an event saying we received a message
    Particle.publish("message-received", msg, PUBLIC);
    // Print the message with some ascii borders (32 characters wide)
    printer.println("XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO");
    printer.println(msg);
    printer.println("XOXOXOXOXOXOXOXOXOXOXOXOXOXOXOXO");
    // Done printing, feed it some paper to move above the tear line
    printer.feed(2);
    // Reset
    printer.setDefault();
    // When the function is done, return 1 so the sender knows it worked
    return 1;
}

void setup() {
    // Set up the Particle variable and function, then connect
    // to the Particle Cloud
    Particle.variable("ready", ready);
    Particle.function("message", receiveMessage);
    Particle.connect();
    
    // Serial1 is configured to the RX and TX pins on the Electron
    Serial1.begin(19200);
    // Start communicating with the printer via the Serial1 pins
    printer.begin(&Serial1);
    
    ready = true;
}

void loop() {
    // Unused in this implementation
}

Twilio Function

JavaScript
Make a function at https://www.twilio.com/console/runtime/functions/manage and use this code as a guide
const request = require('request')

exports.handler = function(context, event, callback) {
    // Send the message to the Particle Electron's 'message' function
    request
        .post('https://api.particle.io/v1/devices/' + context.PARTICLE_DEVICE_ID + '/message')
        .form({
            access_token: context.PARTICLE_ACCESS_TOKEN,
            args: event.Body
        })
        .on('response', function(response) {
            // Tell the sender if it worked
            let responseText = response.statusCode == 200 ? "Success!" : "Failure!"
            let twiml = new Twilio.twiml.MessagingResponse();
            twiml.message(responseText);
            callback(null, twiml);
        })
};

Credits

Alex Swan

Alex Swan

4 projects • 13 followers

Comments

Add projectSign up / Login