Control an Arduino from a Cellphone (No extra hardware purchase needed)

Hi All,
A while ago, I posted a how-to about controlling arduino (and anything connected to it) from a cell phone through twitter and a computer: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1268669525 . This worked great, and required no extra hardware. Unfortunately, twitter changed their auth system so this no longer works. Also, it used a php script which really wasn't necessary. Thus, I have eliminated it, and retooled the other programs, it now works great with a python script running on the computer, and an arduino program.
First, you need to setup twitter via SMS: http://support.twitter.com/forums/23786/entries/14589
Then, run the following python script on your computer that the arduino is connected to. Note: replace /dev/ttyUSB0 with the name of your serial port, and change the twitteruser variable to your twitter username.

import urllib
import serial
import time
from xml.dom import minidom 
ser = serial.Serial('/dev/ttyUSB0', 9600)
twitteruser = "arduinocomnet"
urlparts = ['http://search.twitter.com/search.atom?q=from:', twitteruser, '&rpp=1']  
foobar = ''.join(urlparts)  
while(1):
	xmldoc = minidom.parse(urllib.urlopen(foobar))
	title = xmldoc.getElementsByTagName('content')[0].firstChild.data 
	message = title[0:-5]that 

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

One important thing is that twitter only lets you post the same status once every few days, so I wrote the python script to ignore the last 5 characters of the twitter message. This allows you to add a space and then a random 4 digit number (i usually just use the time) after your twitter status, to prevent twitter from giving you the same status error. If this doesn't make sense, just look at my sample twitter feed for how it should work: http://twitter.com/#!/arduinocomnet

Then, this program on the arduino listens to the serial port and (in this case) turns an LED on or off.

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);  
}
}

Now just send a text message to twitter like "Ledon 1234", and in 30 seconds, your led will turn on.
I have used this setup to turn on and off a few large appliances (through relays) from anywhere with cell service. It works great!
Please feel free to adapt this code to your liking and ask me any questions you have here,

Good Luck!

probably a dumb question but can i run the script on a windows or mac computer?

heh, had a look at this because it would be very handy to control an arduino from a phone, without any extra hardware... but well... You have connected an entire computer to it, I would call that a lot of extra hardware, even a lot more than needed. :stuck_out_tongue:

This script has been tested on linux, mac, and windows. If you do not have pyserial you can download it here. pyserial · PyPI

As for the comment about the hardware, everyone who uses arduino has a computer, not everyone has an ethernet shield.

bld:
heh, had a look at this because it would be very handy to control an arduino from a phone, without any extra hardware... but well... You have connected an entire computer to it, I would call that a lot of extra hardware, even a lot more than needed. :stuck_out_tongue:

Hey Bilbo, Maybe your next project should run on FM 8)

Thanks a lot for showing us how to do this kind of stuff!!!

This prompted me to actually create a Twitter account. I feel so dirty...

FYI this is the actual page you need to setup Twitter on your phone: Help Center

Cheers,

bld:
heh, had a look at this because it would be very handy to control an arduino from a phone, without any extra hardware... but well... You have connected an entire computer to it, I would call that a lot of extra hardware, even a lot more than needed. :stuck_out_tongue:

i agree with bld here.
I entered this thread cause it looked really interesting the "no extra hardware needed" but having a computer running 24h a day, is too much. buying a 30 dollard ethernet does not seem so expensive,right?

Dont get me wrong, i like your project, but the title is not just about right.

thanks for sharing.

Did you honestly believe someone had figured out how to control an Arduino with nothing else but a cell phone...? Really...? :roll_eyes:

Everyone who has an Arduino has a computer - so no extra hardware is required.

Great work bilbo - I got a variant of this working already - it's very cool. Keep us posted on any updates.

Cheers,

I am not looking for a debate here, but if he had used a router, a switch to connect the arduino to internet, then i would have undestand more the "no extra hardware" but a full computer? come on !!!

As i said, i respect the proyect as it looks interesting, just a misleading title. reminds me of some newspaper titles.

Thanks for your feedback. My take on it was that using a computer which I already own and keep on is a lot less extra hardware than a new $20 ethernet shield and a $15 hub. Also, I use this with my $30 dockstar home server running debian linux, so it works particularly well for my setup.

Everyone who has an Arduino has a computer - so no extra hardware is required.

I support what you are saying here. While the Arduino boards are great, the simple fact is that they would be useless without the PC needed to run the IDE/compiler/uploader. So while one can get started with a $25 dollar Arduino board, it comes with the assumption that you have at least access to a $400+ PC.

Lefty

I agree 100%, no extra hardware is needed, though to appease the naysayers, just add change it to "No extra hardware purchases needed"

but doubt that'll fit in the title

Hope thats better. Sorry for the confusion, guess my next project ought to be Twitter from arduino by bit-banging wifi with a piece of wire as my antenna. Oh wait, thats extra hardware (and not possible...)

Happy friday!

I've been playing with this and have made a few improvements to the Python side (bearing in mind this is my first foray into Python!). It's basically extra error handling and some basic logic so that commands are only sent once to the Arduino side - not everytime the Python code reads the same tweet over and over. No rocket surgery going on that's for sure!

import serial
import urllib
import time
from xml.dom import minidom
ser = serial.Serial('/dev/tty.usbserial-A700eUdm', 9600)
twitteruser = "iArduino"
urlparts = ['http://search.twitter.com/search.atom?q=from:', twitteruser, '&rpp=1']
url = ''.join(urlparts)
print(url)
# init alarm OFF
alarmON = 0
while(1):
	# get the data from twitter
	xmldoc = minidom.parse(urllib.urlopen( url ))
	# pull the first tweet out - if there is one
	data = xmldoc.getElementsByTagName('content')
	if len( data ) > 0:
		#retrieve tweet and force uppercase
		tweet = data[0].firstChild.data.upper()
		#split into space delimited tokens
		tokens = tweet.split(' ')
		numTokens = len( tokens )
		if numTokens > 1:
			# split the tweet using space char as delimiter
			message = tokens[ 0 ]
			if message == "HELLO":
				print("hello back!")
			elif message == "ALARM":
				command = tokens[ 1 ]
				if command == "ON" and alarmON == 0:
					print("ALARM ON")
					alarmON = 1;
					ser.write('1')
				elif command == "OFF" and alarmON == 1:
					print("ALARM OFF")
					alarmON = 0
					ser.write('0')
				else:
					print("alarm status quo")
			else:
				print("Unrecognised command!")
		else:
			print("Nothing to do")
	else:
		print("No tweets!")

	time.sleep(5)

I'm using the iPhone Twitter app (free), so whilst the SMS control is cool and works just as well - the Twitter app is cheaper (SMS's are 22c a pop down here!).

Cheers,

Does this still work?