I'm trying to make the Arduino Ethernet connect to 2 servers (not simultaneously), but on the same code, and everytime I try to do that I get the error message "Failed to configure Ethernet using DHCP". But it works fine if I try to specify just one server.
If someone could help it would be very much appreciated.
Follows my code:
#include <SPI.h>
#include <Ethernet.h>
#include <StopWatch.h>
#include <stdio.h>
#include <string.h>
#define APIKEY "0mHaYIze8rsoLDw3VWZ2XSSTZ3-SAKw1QXpXNFh2TW9tVT0g" // replace your Cosm api key here
#define FEEDID 52753 // replace your feed ID
#define USERAGENT "Temperatura" // user agent is the project name
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,1,20);
EthernetClient client;
char server[] = "api.cosm.com";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
char receiver[5];
char buffer;
int counter = 1;
boolean wassend = false;
int i;
unsigned long tempo;
StopWatch sw_secs;
void setup(){
Serial.begin(9600);
delay(1000);
if (Ethernet.begin(mac) == 0){
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac,ip);
}
Serial.print("CONEC");
sw_secs.start();
}
void loop(){
if(wassend == false){
Serial.print("0000");
Serial.println(counter);
wassend = true;
sw_secs.start();
}
if(Serial.available() > 0){
sw_secs.stop();
sw_secs.reset();
buffer = Serial.read();
receiver[i] = buffer;
i++;
}
//Serial.println(receiver);
if(receiver[4] > 0){
//Serial.print("string: ");
counter++;
if(counter==5) counter=1;
wassend = false;
//Serial.println(receiver);//////////////////////
String dataString = "Hidro";
dataString = dataString+(counter-1);
dataString = dataString+',';
dataString += receiver;
sendData(dataString);
delay(1000);
sendData(dataString);
for(int x=0;x<5;x++) receiver[x] = 0;
i=0;
}
if(sw_secs.elapsed() > 5000){
Serial.println("ERROR");
Serial.print("0000");
Serial.println(counter);
//char server[] = "hidro2012.com";
//client.println("GET /?status=Failure HTTP/1.0");
//client.println();
sw_secs.reset();
counter++;
if(counter==5) counter=1;
wassend=0;
}
}
void sendData(String thisData) {
//char server[] = "api.cosm.com";
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("client connected");/////////////////////////////////
// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.cosm.com");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");
client.println(thisData.length()-1);
// last pieces of the HTTP PUT request:
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
// here's the actual content of the PUT request:
client.println(thisData);
//Serial.println("Enviado");//////////////////////////////////////
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
}
else {
// if you couldn't make a connection:
//Serial.println("Conexao nao feita.");////////////////////////////
client.stop();
}
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}