Code & Schematics
** Warning ** This code is absolutely not the easiest way to accomplish the task, but it works for my project specifically. If you do want to use this code for a personal or professional project than I advise you to make many changes.
PHP IMAP CONNECTION
<?php header('Refresh: 10');
/* Ok peeps! Here's my code. If you want to make this work download go-pear.php, put it on your server, access it through a browser, install, add NET and IMAP packages. Then go to your gmail and in setting enable IMAP connections. Change this code to your email adress and put your password and voila. IMAP is pretty sweet and will let you access through different strings different parts of your emails. */
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'youremail@gmail.com';
$password = 'yourpassword';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
/* put the newest emails on top commented out because of bug */
//rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
$message_count = imap_num_msg($inbox);
$result = imap_fetch_overview($inbox,$message_count, 0);
foreach ($result as $overview) {
$sms = "{$overview->from}";
//echo $sms;
}
$string = imap_fetchbody($inbox,$email_number,1);
$less_pillow = substr($string, 0, 750);
/* identifying the keywords */
/* hug */
if (preg_match("/\bhug\b/i", $less_pillow)) {
$pillow = 1;
}
/* love */
if (preg_match("/\blove\b/i", $less_pillow)) {
$pillow = 2;
}
/* laugh */
if (preg_match("/\blaugh\b/i", $less_pillow)) {
$pillow = 3;
}
/* smile */
if (preg_match("/\bsmile\b/i", $less_pillow)) {
$pillow = 4;
}
/* sleep */
if (preg_match("/\bsleep\b/i", $less_pillow)) {
$pillow = 5;
}
}
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<websites id = '" . $pillow . "'>\n";
$xml_output .= "<values id = '" . $pillow . "'>";
$xml_output .= "" . $sms . "";
$xml_output .= "</values>";
$xml_output .= "</websites>";
echo $xml_output;
}
/* close the connection */
imap_close($inbox);
?>
PHP CODE PART 2
<?php
/////////////////////////////////////////////////////////
// Code for sending SMS with PHP //
////////////////////////////////////////////////////////
// If a value is in the navigation bar, get it and store it
if (isset($_GET['sms'])) {
echo "The address is ", $_GET['sms'], ".\n";
// Defining address to send SMS to from value in navigation bar
$to = $_GET['sms'];
$subject = "Pillobot sent you a...";
// Defining message to be sent
$message = "HUG!";
$from = "";
$headers .= 'MIME-Version: 1.0' . "\n" . 'Content-type: text/plain; charset=UTF-8'
. "\n" . '' . $from . "";
// Send the SMS!
$mail_sent = mail( $to, '=?UTF-8?B?'.base64_encode($subject) .'?=',$message, $headers);
// If the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent!!" : "Mail failed...";
} else {
echo "No address to send to!";
}
?>
PROCESSING
////////////////////////////////////////////////////////////////
// Pillobot Processing! //
////////////////////////////////////////////////////////////////
import processing.serial.*; // Import XML Library
XMLElement xml;
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // Serial port you are using
float num; // Int for Serial reading
int counter = 0; // Int for counter (explained later)
int val;
void setup()
{
size(100, 100); // Size doesn't matter (hehe)
String portName = Serial.list()[0]; // Getting Port
myPort = new Serial(this, portName, 9600); // Opening communication
myPort.clear();
}
void draw() {
// Establishing connection to XML file generated by PHP file
xml = new XMLElement(this, "http://www.magaliecn.com/pillobot.php");
// XML funtion
XML_load();
}
///////////////////////
// Getting XML info! //
///////////////////////
void XML_load() {
// This doesn't really matter, as there is only 1 XML value generated
int numSites = xml.getChildCount();
// Again, only one value, but it doesn't change anything
for (int i = 0; i < numSites; i ) {
// Getting the info from tags
XMLElement kid = xml.getChild(i);
// Int with the keyword values
// 1: Hug
// 2: Love
// 3: Laugh
// 4: Smile
// 5: Sleep
int id = kid.getIntAttribute("id");
// Getting the From Adress of the last message
String sms = kid.getContent();
// Print values to see what's going on
println(id);
println(sms);
//////////////////////
// Sending a hug <3 //
//////////////////////
// While port is available
//while (myPort.available() > 0) {
// Reading from Port Analog Read Value
myString = myPort.readStringUntil(lf);
// If it's not a null value...
if (myString != null) {
// Print value
//print(myString);
// Converting to float. This is not really important either. But I
// had a lot of problems earlier for some reason doing this worked great
num=float(myString);
// Print
println(num);
//////////////////////////////////////////////////////
// This part is crucial! Since when you read analog //
// values from my soft switch you sometimes get //
// values of 0, I made an auto incrementing counter //
// that goes 1 whenever you receive the value 0. //
// So it can tell the difference between a read hug //
// and only a regular value :) //
//////////////////////////////////////////////////////
// If the value from AnalogRead is 0
if (num == 0) {
// Counter 1
counter = counter 1;
// If not, counter is = 0
// This part is also really important, because it prevents
// the counter to increment to 2 even if there is no huggage,
// because it always goes back to 0 after a few frames.
// So if it makes the counter only increment to 3 if you
// get 0 for 2 consecutive frames
} else {
counter = 0;
}
// If the value has been 0 for 2 consecutive frames
if (counter >= 3) {
// Send SMS!
println("sending SMS!");
String lines[] = loadStrings("http://www.magaliecn.com/hugs.php?sms=" sms);
// and put the counter back to 0
counter = 0;
}
delay(25);
}
myPort.clear();
// Send keyword values to Arduino
myPort.write(id);
delay(25);
} }
// And we're done for Processing!
ARDUINO
int val; // Data received from the serial port
int led12 = 12;
int led11 = 11;
int led10 = 10;
int led9 = 9;
int led8 = 8;
int led7 = 7;
int led6 = 6;
int led5 = 5;
int analogPin = 0;
float magic = 0;
void setup() {
pinMode(led12, OUTPUT);
pinMode(led11, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led9, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led5, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) { // If data is available to read,
magic=analogRead(analogPin);
Serial.println(magic); // println add Linefeed to my float
delay(1000);
}
val = Serial.read(); // read it and store it in val
if (val <= 1) { //hug (happy blush)
smileblush();
} else if (val <= 2) { // love
love();
} else if (val <= 3) { // laugh
laugh();
} else if (val <= 4) { // smile
smile();
} else if (val <= 5) { // sleep
}
}
void smileblush() {
analogWrite(led9, 250); // two top
analogWrite(led12, 250); // top
analogWrite(led8, 250); // top
analogWrite(led5, 250); // eyes
delay(500);
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
delay(500);
analogWrite(led11, 0); // bottom
analogWrite(led10, 0); // bottom
analogWrite(led7, 0); // bottom
delay(500);
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
delay(1000);
analogWrite(led6, 250); // blush
delay(500);
analogWrite(led6, 0); // blush
delay(500);
analogWrite(led6, 250); // blush
delay(500);
analogWrite(led6, 0); // blush
}
void laugh() {
analogWrite(led9, 250); // two top
analogWrite(led12, 250); // top
analogWrite(led8, 250); // top
analogWrite(led5, 250); // eyes
delay(500);
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
delay(500);
analogWrite(led11, 0); // bottom
analogWrite(led10, 0); // bottom
analogWrite(led7, 0); // bottom
delay(500);
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
delay(1000);
}
void smile() {
analogWrite(led5, 250); // eyes
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
}
void love(){
analogWrite(led5, 250); // eyes
analogWrite(led11, 250); // bottom
analogWrite(led10, 250); // bottom
analogWrite(led7, 250); // bottom
delay(1000);
analogWrite(led6, 250); // blush
delay(500);
analogWrite(led6, 0); // blush
delay(500);
analogWrite(led6, 250); // blush
delay(500);
analogWrite(led6, 0); // blush
}

