Post to Twitter?

Is there a tutorial on how to post information online using serial communication?

I keep seeing blog posts where people claim they are using the serial communication to post to Twitter, activate php scripts, etc.

I have searched the forums and Google for tutorials to walk me through, but to no avail. Can anyone point me to a tutorial or other walkthrough for making the connection between my Arduino and the Internet?

Actually, is there a way to just activate a php script from Arduino?

For example, when I press a button, it activates: ‘http://www.myphpserver.com/thescript.php

I think if I could do that, I can figure out the rest on my own.

Thanks for any help…

try to read this thread:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1208451736

Maybe I am missing something. That post doesn’t seem related to my question.

I want to know if there is some way to put a piece of code in my Arduino program so that if a certain condition is met (for example, pressing a button), it will execute a remote php script. I am running php on a remote server (GoDaddy) and not on my local machine.

Thanks.

You will need to deceide how you are going to communicate with godaddy first. You could get the ethernet shield and communicate over the net. This might give you some ideas.

Maybe I wasn’t clear enough with my post. I own several domains through Godaddy. I just want to be able to use the Arduino to activate a script on my domain that I own and operate.

For example, I own www.savethehatch.com. I would like the Arduino to be able to activate the script savethehatch.com is expired.

Is there a way to do this via my usb connection without buying an ethernet adapter?

Thank you.

Sorry, i misunderstood your first post.

If you want to get your Arduino to talk to the internet without using an ethernet controller you need’ll to use a proxy running on your PC.

Your Arduino is plugged into your PC and the sketch running on the Arduino talks over the serial line to a program running on the PC.

The program running on the PC then talks over the broadband line to a program running on the internet, i.e. your PHP script.

The bit in the middle, talking on both serial line and broadband line is called a proxy. You can write it in any language: Visual Basic, Perl, Java, yada yada. The following uses Processing since this will be somewhat familiar to Arduino coders.

The following is a very simple demonstration of this.

Arduino Sketch - this opens a connection on the serial line between the Arduino and your PC and writes the word “Tick” every five seconds.

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println(“Tick”);
delay(5000);
}

Processing Sketch: this runs on your PC and opens the other end of the serial line to the Arduino. Everytime the Arduino writes something (anything) this sketch opens a connection to your PHP script. (We don’t really care what the PHP script does or says so we don’t bother reading the stream.)

import java.net.;
import processing.serial.
;

Serial theSerial;

void setup() {
theSerial = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
if (theSerial.available() > 0) {
theSerial.readString();
try {
new URL(“tick.php”).openStream();
}
catch (Exception theException) {
theException.printStackTrace();
}
}
}

PHP Script: this sits on your web-server and, everytime it is called, increments the contents of the text file:

<?php file_put_contents("tick.txt", file_get_contents("tick.txt") + 1); ?>

Another PHP Script: this sits on your web-server and, everytime it is called, displays the contents of the text file - you could use this for debugging:

<?php echo file_get_contents("tick.txt"); ?>

That did not work for me. All of the php works. I can access the php file through my browser and see the number increment by 1.

However, the processing code never seems to trigger the php script.

Something I did wrong?

Thanks for all the help so far.

P.S. How much easier is it if I just buy an ethernet shield? I have been eyeing one up on ebay.

In the bit where it says:

new URL(“tick.php”).openStream();

you have to make sure you use the proper URI for the PHP script, i.e. you prefix the “tick.php” with your own domain name:

new URL(“http:/www.arduino.cc/tick.php”).openStream();

Also, are you sure you’re Processing sketch is listening on the correct serial port?

No I’m not sure at all. How do I check that?

Open Processing, create a new sketch and enter this verbatim:

import processing.serial.*;
println(Serial.list());

then run it (Ctrl+R). You should see output something like the following:

Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] “COM4”

In my case, I have one “serial” connection to an Arduino. My PC thinks this is called is COM4 (irrelevant) but, most importantly, it’s entry zero [0] in the list of serial ports.

If you have more than one serial port you might see something like the following:

[0] “COM4”
[1] “COM5”
[2] “COM6”

If you do then you need to choose whichever enty (0, 1 or 2) is the one that corresponds to the serial port connected to your Arduino.

Then, in the line of code that says:

theSerial = new Serial(this, Serial.list()[0], 9600);

change the [0] to [n] where [n] is the port you’ve chosen.

n.b. you must ensure that Processing is the only application talking to your Arduino on this port. This means that you must close down your Arduino sketchbook before starting any of this Processing stuff.

Am I correct in assuming that you cannot do all this from within the Arduino program? You need Processing to act as a go between?

Side question, what is the learning curve like with the Ethernet shield? I am leaning in that direction now…

Thanks Chumpster! That worked once I got the correct com port listening.

It’s always the simple things that make everything else break down, isn’t it?

No problem Agent47. Consider it a late Christmas present.

Remember, your Arduino has no direct connection to the internet so you need something sitting between the two. This is your proxy, acting as go-between/interpreter/translator/gate-keeper/whathaveyou.

You can write it in any language but Processing is quite nifty and comes with all the power & flexibility of Java.

One neat twist is that in Processing 1.0 you can export your sketch as an application (File >> Export Application). This will produce an executable file which you can copy into your Windows Startup folder so it runs automatically whenever you boot/reboot your PC.

Getting an Arduino Ethernet shield will make the connection to the internet much easier but the limitations of the Atmel chip and the Arduino language will then come into play. It’s much harder to do complex processing on the Arduino then on the PC. Only you can weigh up the relevant pros & cons.

Bon chance.

In an earlier post you said:

Everytime the Arduino writes something (anything) this sketch opens a connection to your PHP script. (We don’t really care what the PHP script does or says so we don’t bother reading the stream.)

What would I need to do if I did want to read the contents? Let’s say there was a text file that would either contain the character 1 or 2. Can processing get the contents of a text file and do something based on that?

Thanks.

You’d need to change the PHP side of things to output the contents of the file. Then you’d need to change the Processing side of things to read in the output from the PHP.

Following on from the earlier example, here’s a revised PHP script:

<?php file_put_contents("tick.txt", file_get_contents("tick.txt") + 1); file_get_contents("tick.txt") ; ?>

This increments the contents of the file then it outputs the contents of the file.

And here’s a revised Processing script:

import java.net.;
import processing.serial.
;

Serial theSerial;

void setup() {
theSerial = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
if (theSerial.available() > 0) {
print("S: "); println(theSerial.readString());
try {
URL theURL = new URL(“tick.php”);
InputStream theInputStream = theURL.openStream();
InputStreamReader theInputStreamReader = new InputStreamReader(theInputStream);
BufferedReader theBufferedReader = new BufferedReader(theInputStreamReader);
String theString = theBufferedReader.readLine();
print("I: "); println(theString);
}
catch (Exception theException) {
theException.printStackTrace();
}
}
}

Two things have changed here.

  1. I’ve added in a little debugging - the sketch will output any data it receives (the two println statements) plus it’ll tell you whether they arrive over the serial line (print("S: ")) or over the internet (print("I: ")).

  2. I’ve changed the original script such that it not only opens the tick.php script but it also reads the output. The combination of Java objects that you need to use may seem a little OTT but that’s Java IO for you.

If you wanted to change this so that your Processing sketch does something with the PHP output rather than just printing it to the screen you’ve got it stored in the local variable called theString and can play with it at will.

Be aware that you now have mechanisms to write to and read from the internet. Plus you have mechanisms to read to and write from the serial port. In other words, you now have the beginnings of two-way communication between your Arduino and the internet.

Some people have used this to connect their Arduinos to internet time servers, periodically connecting and reading the internet time from an NTP server then displaying it on a local LCD. Some people have LCD news tickers. Some people read their web-site and ring a local bell whenever their hit counters are incremented.

The ideas are limitless.

Bon chance.

Thanks so much!

I’ve been able to use that advice to make a processing program that polls a text file on my server and sends a message to the Arduino every time someone visits the site.

Here’s my problem: after a while (10-20 minutes), the Arduino does not respond any more.

Do I have to clear the Serial cache or something like that?

Thanks!