In my last post, I discussed how to connect the Arduino to the internet and how to receive commands.

But how am I going to send commands to the Arduino?

There are dozens of methods I could use to make that happen – but my favorite platform to work on these days is the Twilio communication platform. Specifically for this project, I am going to leverage the SMS and MMS functionality so that users can send a text message which will then be sent to the Arduino.

This required a few things – first, I had to set up a phone number in Twilio. Once that was done, I had to declare a webhook where the information would be parsed. Since PHP is my second language, I decided to write a hook that would gather the data from Twilio and store it in a mySQL database.

So, this is how it works –  a user texts a string of 25 characters to my number. Twilio acts as my communication bridge and sends the text to my webhook which looks like this:

<?php

//Include database connection details
require_once('config.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}

$number = clean($_POST['From']);
$msg = clean($_POST['Body']);

$length = strlen($msg);

$msg = strtoupper($msg);

if ($length <= 25){
$qry="INSERT INTO message (_number,_message)
VALUES('".$number."','".$msg ."')";
$result=mysql_query($qry);
}

$photoAreas = array("
https://media.giphy.com/media/3o6Ztitqby2sGKTDLq/giphy.gif","https://media.giphy.com/media/l0HlSYBHR78sdHZDy/giphy.gif","https://media.giphy.com/media/3o6ZsTLcyDEAKznQHe/giphy.gif","https://media.giphy.com/media/26BRPZTCZVcCWUUak/giphy.gif","https://media.giphy.com/media/3o6Zt2hoEFxgiAgNOw/giphy.gif","https://media.giphy.com/media/l0MYwHlIt5dj6M5ry/giphy.gif","https://media.giphy.com/media/3o6Ztm0Lv2eAMmNiZq/giphy.gif","https://media.giphy.com/media/l41Y9SKrl3kLVamfC/giphy.gif","https://media.giphy.com/media/3oEjHNdJf6ilveYXIc/giphy.gif","https://media.giphy.com/media/l0HlF6jWLu5cl7iww/giphy.gif","https://media.giphy.com/media/26ufnw7I96fRGTYFq/giphy.gif","https://media.giphy.com/media/l0HlPkqAvV6fwJTyM/giphy.gif","https://media.giphy.com/media/3oz8xE7gjBmo2wFrS8/giphy.gif",);
$randomNumber = array_rand($photoAreas);
$randomImage = $photoAreas[$randomNumber];

header('Content-Type: text/xml');

if ($length <= 25){
?>
<Response>
<Message>
<Body>Thank you, your message has been sent to the Upside Down.</Body>
<Media><?php echo $randomImage ?></Media>
</Message>
</Response>
<?php } else { ?>
<Response>
<Message>
<Body>Sorry, submissions are restricted to 25 characters. Please try again.</Body>
</Message>
</Response>

<?php } ?>

So, this is what’s happening above – Twilio sends an HTTP POST to my url, I’m pulling the message into my script where I’m now doing a couple different things. First of all, I’m taking the message and posting it to a mySQL database so I can access the information from the Arduino (more on that in a minute). Secondly, I’m sending a confirmation back to the user that there message has been queued for playback. If there is an error, in this case, they have more than 25 characters, an alternate message is sent back telling them they can try a different message. This is actually a pretty cool foundation for an AI if you wanted to build one out, you would just need to put in a regular expression search and automate the replies based off the message, but we’ll talk about that in a future post. What we’ve essentially got here is our method of inputting data to the Arduino.

So, what’s the next step? Well, there is a reason I put the data into a database, and that’s so when the Arduino connects – it can loop through the messages that it has been sent.

For the Arduino to get the data, it needs to be in a format that will be readable. Because I’m sending the message to the LEDs, I want to convert the letters to a numerical format. Instead of doing this on the Arduino, I figured I would take the load off the processor and do it all online first.

Here’s the code for that:


<?php

//Include database connection details
require_once('config.php');

//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}

$qry="SELECT * FROM message WHERE _fetch != '1' ORDER BY _time DESC LIMIT 1";
$result=mysql_query($qry);

function toNumber($dest)
{
if ($dest)
return (ord(strtolower($dest)) - 97)*2;
else
return 0;
}

function convertString($msg){
$arr1 = str_split($msg);
$outputArray = array();
foreach ($arr1 as &$value) {
array_push($outputArray,toNumber($value));
}
$output = implode(",",$outputArray);
return $output;
}
for($x = 0 ; $x < mysql_num_rows($result) ; $x++){
$row = mysql_fetch_assoc($result);
$message = convertString(str_replace(' ', '', $row['_message']));
echo "&lt;".$message."&gt;";
}
?>

What’s happening in the script above? Simple – we’re going to the database and looking for the most recent message that hasn’t been read.

We need to make it easy for the Arduino to read – so there is a function where we take the string and convert it to an array, I then parse through the array and convert all of the letters to the number of the LED that I assigned to them.

If I sent the following text:

“Jeremy is awesome”

it would be converted to this in the database:

“JEREMY IS AWESOME”

And when this script grabs it, it strips the spaces and makes it an array:

$outputArray = array(“J”,”E”,”R”,”E”,”M”,”Y”,”I”,”S”,”A”,”W”,”E”,”S”,”O”,”M”,”E”);

But since I would have to do an additional conversion in Arduino, why not save space and convert all the letters to numerical values in the script instead? I run it through a for loop and come up with this output:

$output = array(“18″,”8″,”34″,”8″,”24″,”48″,”16″,”36″,”38″,”28″,”38″,”0″,”22″,”22″,”48″,”22″,”0″,”24″,”8”)

Arduino will then be able to take these numbers and loop through an animation sequence where it triggers the assigned LED to turn on/off based on the letter value.

Here’s the code in action:

 

 

Pretty awesome.

These are the basics to my project. I will do a detailed write-up once I’ve finished, but I wanted to document this while it’s still fresh in my mind!

Stay tuned for the final part next week!