##Moment of truth
Getting p5/DOM, Node, and Twilio to work together in the first go is going to be a long shot. But I’m trying anyway. I have declared variables to create DOM objects button and input text boxes (setPhoneNumber and inputSMS (message)).
Next, I define callback functions updateSMSto() and updateSMSbody() which change the “client.sendMessage.to” JSON field of the Twilio code.
It didn’t work, but I have a feeling I’m on the right path and need help. Halp!
var serialport = require ( 'serialport' ),
SerialPort = serialport .SerialPort ,
portname = process .argv [ 2 ];
client = require ( 'twilio' )( '<--[removed]-->' , '<--[removed]-->' );
// variables DOM & sketch
var h1 ; // header text for SMS
var setSMS ; // button
var inputSMS ; // input SMS text
var setPhoneNumber ; // input phone number
// setup port
var myPort = new SerialPort ( portname , {
baudRate : 9600 ,
options : false ,
parser : serialport . parsers . readline ( "\n" )
} );
myPort .on ( 'open' , function () {
console.log('port is open');
} );
myPort .on ( 'close' , function () {
console.log('port is closed');
} );
myPort .on ( 'error' , function () {
console.log('there is an error');
} );
// An IF statement saying if myPort .on ( 'data' , function ( data )) is returning values , client .sendMessage should execute
myPort .on ( 'data' , function ( data ) {
console.log(data);
myPort.write('x');
} );
// DOM for SMS setup
function setup () {
h1 = createElement('h3', "Input SMS Reminder Text");
h1.position(width/2, height/2);
setSMS = createButton("set SMS");
setSMS.mousePressed(updateSMSbody);
setPhoneNumber = createInput("phone number +17185701757");
setPhoneNumber.input(updateSMSto);
inputSMS = createInput("sms text goes here...");
inputSMS.input(updateSMSbody);
}
function updateSMSto () {
client.sendMessage.to = setPhoneNumber.value(); //should it be? client.sendMessage.to(setPhoneNumber.value());
}
function updateSMSbody () {
client.sendMessage.body = inputSMS.value(); //shoud it be? client.sendMessage.body(inputSMS.value());
}
function draw () {
}
// Send an SMS text message - where in p5 does this go ? works on its own "node sendsms.js"
client .sendMessage ( {
to : '+17185701757' , // Any number Twilio can deliver to
from : '+17182159247' , // A number you bought from Twilio and can use for outbound communication
body : 'Hello Osama - this is a test message.' // body of the SMS message
} , function ( err , responseData ) { //this function is executed when a response is received from Twilio
if (!err) { // "err" is an error received during the request, if any
// "responseData" is a JavaScript object containing data received from Twilio.
// A sample response from sending an SMS message is here (click "JSON" to see how the data appears in JavaScript):
// http :// www . twilio . com / docs / api / rest / sending-sms #e xample-1
console . log ( responseData . from ); // outputs "+14506667788"
console.log(responseData.body); // outputs "word to your mother."
}
} );