Go Down

Topic: Cell phone Control! (Read 5379 times) previous topic - next topic

bilbo

Alright now I know that there have been lots of threads on this, but I think this is a different approach. I wanted to be able to control my arduino from my cell phone, and without a cell shield or ethernet shield. Thus, i did it through twitter through my computer.

First, I setup mobile status updates in twitter, with a new account, and wrote this php script:
Code: [Select]
<?
function twitterCapture() {
       // Set your username and password here
       $user = 'USERNAME';
       $password = 'PASSWORD';

       $ch = curl_init("https://twitter.com/statuses/user_timeline.xml");
       curl_setopt($ch, CURLOPT_HEADER, 1);
       curl_setopt($ch,CURLOPT_TIMEOUT, 30);
       curl_setopt($ch,CURLOPT_USERPWD,$user . ":" . $password);
       curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
       $result=curl_exec ($ch);
       $data = strstr($result, '<?');

       $xml = new SimpleXMLElement($data);

       return $xml;
}
      $xml = twitterCapture();  
     echo $xml->status[0]->text;
?>


Then, I wrote this python script:
Code: [Select]

from urllib import urlopen
import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 9600)
while(1):
     lala = urlopen('PATH TO WEB SCRIPT').read()

     if lala == "Ledon":
           ser.write('1')
     elif lala == "Ledoff":
           ser.write('0')
     time.sleep(30)


Lastly, this arduino program.
Code: [Select]
int inByte;
void setup()
{
 // start serial port at 9600 bps:
 Serial.begin(9600);
}
void loop()
{
 if (Serial.available() > 0) {
   // get incoming byte:
   inByte = Serial.read();
 }
 if (inByte == '1') {
   digitalWrite(13, HIGH);
 }
 if (inByte == '0') {
   digitalWrite(13, LOW);  
}
}

Obviously, i will do more interesting things later, but for now, this is cool.
Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

SilverCG

thats pretty awesome.. i think i might try that when i get home today.. thanks for posting.

cr0sh

In your PHP script, you might want to put your login/password in a separate included file that is outside of your web accessible scripting area (but which to php still has access to include), otherwise someone could easily find out them out...
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

bilbo

cr0sh: Already did that, thanks for the suggestion!

Any ideas as to what i should control with my new setup?
Thanks!
Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

ArduinoM

A mobile crane!
or
the TV downstairs (twitter TV-remote)
or
Mood lights in the room, or just all the lights
Locking the door
Start the Car on freezing mornings to get it warm

have it tweet back status on things, did I lock the door? then lock it.
Tweet if the doorbell rings, then turn on the TV ;)

Ansh

#5
Apr 30, 2010, 03:02 am Last Edit: Apr 30, 2010, 03:16 am by agug222 Reason: 1
I must say, this is a very creative way to combine php with the arduino!  However, I do not understand the python code very well, can someone please explain that to me?

Also, does anyone here know Ruby/Ruby on Rails?  Is there a way that I could do something like this in Ruby/Ruby on Rails?

bilbo

Hello Ansh,
Thank you!
The python script's purpose is to fetch the status (displayed on the php page) and send it to the arduino over serial.

Heres a little breakdown of the code:
Code: [Select]
from urllib import urlopen
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600)

That section imports the libraries necessary for the program, and opens a serial connection to the arduino.
Code: [Select]
while(1):
     lala = urlopen('PATH TO WEB SCRIPT').read()

     if lala == "Ledon":
           ser.write('1')
     elif lala == "Ledoff":
           ser.write('0')
     time.sleep(30)

That section loops indefinitely (thats the while clause). First, it gets the contents of the status php script. If it says Ledon, it sends the number 1 over serial to the arduino, which turns the LED on. If it gets Ledoff, it sends a 0 to the arduino over serial, which then turns the LED off. It then waits 30 seconds as to not overload my poor little php server, and runs again.

Thanks!
Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

Ansh

Thank you for explaining the python code to me!

Now that you have explained this, another question has popped up.  If I wanted to use this code, do I just have to run the code after replacing the username and password fields, or is there more work to be done?  When you mentioned your php server I was not sure if you were running the php, python, and sketch in a special way so that they collaborate properly.

bilbo

Well after replacing the username and password in the php file, because Twitter will not let you do the same status more than once, post a random 4 characters after the each status update (example: "Ledoff1234"), and then also in the php replace the "echo $xml->status[0]->text;" with:
Code: [Select]
$stat = $xml->status[0]->text;
echo substr($stat, 0, -5);

This gets rid of the last four characters before displaying, so twitter sees them as different messages each time you update.
Then, upload this to a webserver, and place its path in the python script where it says PATH TO WEB SCRIPT.
Now, in the python script replace the /dev/ttyUSB0 with the path to your arduino. On windows this would be something like COM1 or COM5, and mac and linux would be similar to the path above.
Then, download the arduino script and run the python, and update your twitter status from your cell phone!

Sorry its so complicated, i bet this could all be done in one language, but I had to make use of the bits and pieces of what languages I knew.

Good luck!
Bilbo
Alice asked the Chesire Cat, who was sitting in a tree, "What road do I take?"
The cat asked, "Where do you want to go?"
"I don't know," Ali

Ansh

Are there and webservers that you recommend for this task?  Additionally, I saw cr0sh's comment:

Quote
In your PHP script, you might want to put your login/password in a separate included file that is outside of your web accessible scripting area (but which to php still has access to include), otherwise someone could easily find out them out...


Is this bit of the php already edited in your original post?  I just want to make sure that my twitter doesn't get hacked; wouldn't want someone I don't know controlling various aspects of my home  ;)

bld

#10
Apr 30, 2010, 11:00 am Last Edit: Apr 30, 2010, 11:04 am by bld Reason: 1
@cr0sh
Quote
otherwise someone could easily find out them out

How? Only way, as far as I know, is to get hold of the file, before it is processed by the webserver. As soon as it is through, the visitors won't have any of the source, except the html inside the php that is meant to be written to the visitor.

But some are naming their includes with .inc, that is a bad idea, because the web server doesn't process it before showing it. So if you move the password out in a file with the ext .inc it will be open for everyone to read, where if you did it with a .php (or in my case .asp), they won't see anything at all.

This http://bld-live.dk/sql.asp opens the connection to my MySQL database, username, password and all is in.
captain-slow.dk | non contagious!

Bored321

Like above, theres really no need to move the login details. Its highly unlikely even if PHP fails that the source would be shown.

I did a similar thing before i had an Ethernet shield, except I just used a netbook, using php sockets to connect to the arduino directly.


Ansh

I also have a netbook, however I would not want to have to keep it running 24-7.  How can I make the a comprduino connect twitter/internet without a dedicated computer?

www.ge-th.com

pretty good, will try to finish this project!


met_fredrik

How can you get the python script to read a php file without the use of pipes?

I have done a similar project with controlling an rc car from an iphone, but then I used named pipes. Could I just used this python script instead reading coordinates created with javascript in a php file?

Thanks.

Go Up