Hello!
As I wrote in an earlier post (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235341200), I’m trying to set the color of a BlinkM remotely via my server.
Have achieved partial success!!!
My Arduino can now connect tom my server, check the current color and set the BlinkM to that color. The problem is that I haven’t got the whole thing to repeat in the loop() function. Here is my code, partially masked to fit this post:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 2 }; // The IP
byte server[] = { 100, 100, 100, 100 }; // The server
char Str[10]; // String to transcribe and send to BlinKM
byte BlinkM_addr = 0x09;
Client client(server, 80);
void setup()
{
Wire.begin();
Wire.beginTransmission(BlinkM_addr);
Wire.send('o');
Wire.endTransmission(); // Set the BlinkM to not start its own life..
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("GET /color.php HTTP/1.1");
client.println("Host: www.example.se");
client.println();
client.println();
} else {
Serial.println("connection failed");
}
} // End of setup()
void loop()
{
if (client.available()) {
// function to read and parse and change the colour,
// the whole page is parsed
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
Obviously this only make the loop to run the parsing once and the end with ‘client.stop();’, but still it works .
I’ve got inspired by this piece of code that seems to do the same as I want (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231812230):
void setup()
{
Serial.begin(9600);
Serial.println("Starting WebWx");
Serial.println("connecting...");
Ethernet.begin(mac, ip);
delay(1000);
if (client.connect()) {
Serial.println("connected");
client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");
client.println();
delay(2000);
} else {
Serial.println("connection failed");
}
}
void loop() {
// Read serial data in from web:
while (client.available()) {
serialEvent();
}
if (!client.connected()) {
//Serial.println();
//Serial.println("Disconnected");
client.stop();
// Time until next update
//Serial.println("Waiting");
for (int t = 1; t <= 15; t++) {
delay(60000); // 1 minute
}
if (client.connect()) {
//Serial.println("Reconnected");
client.println("GET /xml/current_obs/KRDU.xml HTTP/1.0");
client.println();
delay(2000);
} else {
Serial.println("Reconnect failed");
}
}
}
Problem is that when I insert the if(client.connected()) stuff again in the main loop (as in the above example), everything craches. I can’t get it to connect, and it doesn’t reconnect either, and even the parsing gets fucked up…
Is this the way to go to achieve a continious connection (and check up on the current colour) or am I missing something?
Thankful for any help on the subject!
-Per