Pages: [1] 2   Go Down
Author Topic: Cell phone Control!  (Read 4252 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
<?
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:
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:
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.
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 5519
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cr0sh: Already did that, thanks for the suggestion!

Any ideas as to what i should control with my new setup?
Thanks!
Logged

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

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 344
Just dip it in mercury and power it up
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: April 29, 2010, 08:16:16 pm by agug222 » Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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!
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 360
I'm 15. I like making things. I like breaking things better.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
$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
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink
Logged

Vejen, Denmark
Offline Offline
Edison Member
*
Karma: 7
Posts: 1178
RAWR! ^,..,^
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
« Last Edit: April 30, 2010, 04:04:40 am by bld » Logged

captain-slow.dk | non contagious!

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

pretty good, will try to finish this project!

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: