I now have my code working… sort of. I’ve yet to add any actions but I’ve got the Arduino reading my tweets.
The problem was with the formatting of char host and the http request code. I’m now using the ip address of my server rather than the url in the char host variable above and my http request code is as follows:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /mentions_timeline.php HTTP/1.1");
client.println("Host: stuarthenrywilson.eu5.org");
client.println("Connection: close");
client.println();
Any deviation from that stops the sketch from working. I also had to move my files to the main directory on the server instead of a sub folder.
It should be relatively simple for me to start adding actions depending on the tweet content.
One small problem I am still experiencing is that the sketch won’t run 1st time on upload every time. I continually read “connecting…” and “connection failed” in the serial monitor until I physically unplug the ethernet cable from the Arduino, plug it back in and re-upload the sketch. It then works fine until I change the code.
The code I have working is:
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,14);
// IP address of server
IPAddress server(5,9,106,214);
// DEFINE CONSTANTS
const unsigned long postingInterval = 40000;
// DEFINE VARIABLES
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
char tweet[140];
EthernetClient client;
void setup() {
Serial.begin(9600);
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
// Send any incoming data to the serial port for debugging.
if (client.available()) {
printTweet();
}
// if there’s no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();
}
// if you’re not connected, and 60 seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
httpRequest();
printTweet();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
void httpRequest() {
Serial.println(“connecting…”);
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“connected”);
// Make a HTTP request:
client.println(“GET /mentions_timeline.php HTTP/1.1”);
client.println(“Host: stuarthenrywilson.eu5.org”);
client.println(“Connection: close”);
client.println();
}
else {
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
}
// note the time that the connection was made:
lastConnectionTime = millis();
}
void printTweet() {
TextFinder finder(client);
finder.getString("", “”, tweet, 140);
Serial.println(tweet);
}
I hope that if someone else finds this thread as I did then the above code might be of use to them.