Hi! i have a arduino mega with a ethernet shield. I would like use sd and ethened. I can work with both in the code but the ethernet only connect one time and then I did not connect with him again
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include "DHT.h" //libreria sensor de humedad
#define DHTPIN 2 // pìn sensor de humedad
#define DHTTYPE DHT11 // tipo de sensor
#define SPI_SD 8 // pin tarjeta sd
#define SPI_ETHERNET 53 // para sd
DHT dht(DHTPIN, DHTTYPE); // sensor humedad
File myFile;
// modulo ethernet
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // viene sin mac
char server[] = "www.domain++++.com";
IPAddress ip(192,168,1,35);
EthernetClient client;
int mrelojes[9][7]; // matriz de relojes la ultima posicion la posicion de memoria En este orden fluo1 fluo2 ledapoyo luna aire uv co2 ko fe
// bluetooth variables
char letra; // cada caracter que va leyendo bluetooth
char cadena[255]; // array de caracteres
int i=0; // tamaño del array
boolean cadenacompleta=false; //si la cadena esta completa
// sondas
float temperatura[3]; // temperaturas
float ph=0;
int lluvia=0;
int boya=0;
// hora
byte second, minute, hour,dayOfMonth, month, year;
long tiempoblue=0;
long ultimotiempoblue=0;
//
int primerencendido=1;
// variables sensor de humedad
int humedad=0;
int tempservidor=0;
long tiempohumedad=0;
long ultimotiempohu=0;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
dht.begin();
// modulo sd y ethernet
pinMode(SPI_SD, OUTPUT);
pinMode(SPI_ETHERNET, OUTPUT);
Serial.print("Inicializando sd...");
EnableSD();
if (!SD.begin(4)) {
Serial.println("Fallo iniciando sd!");
return;
}
Serial.println("Tarjeta sd montada correctamente");
// cliente web
//Iniciamos cliente web
EnableEthernet();
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
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 /search?q=arduino HTTP/1.1");
client.println("Host: www.domain**.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
then in the code i use for example
EnableEthernet();
if (client.connect(server, 80))
{
Serial.println("Conectado servidor");
client.print("GET /iot.php?s1="); // Enviamos los datos por GET
client.print(temperatura[0]);
client.print("&s2=");
client.print(temperatura[1]);
client.print("&s3=");
client.print(temperatura[2]);
client.print("&ph=");
client.print(ph);
client.print("&boya=");
client.print(boya);
client.print("&lluvia=");
client.print(lluvia);
client.println(" HTTP/1.0");
client.println("Host: www.arduacuario.com");
client.println("User-Agent: Arduino 1.0");
client.println();
Serial.println("Conectado");
}
EnableSD();
myFile = SD.open("log.txt", FILE_WRITE);
if (myFile)
{
Serial.print("Escribiendo log en sd...");
myFile.println(" Enviados datos al servidor ");
myFile.close();
Serial.println("cerrado archivo");
}
else
{
Serial.println("error abriendo log.txt");
}
void EnableSD(){
//Para comunicar con sd desabilitamos w5100 spi (pin 10 HIGH)
// Para comunicar con sd habilitamos sd spi (pin 4 low)
digitalWrite(SPI_SD, LOW);
digitalWrite(SPI_ETHERNET, HIGH);
}
void EnableEthernet(){
//Para comunicar con sd habilitamos w5100 spi (pin 10 low)
// Para comunicar con sd deshabilitamos sd spi (pin 4 HIGH)
digitalWrite(SPI_SD, HIGH);
digitalWrite(SPI_ETHERNET, LOW);
}
when i connect in second time to ethernet
this if dont pass
if (client.connect(server, 80))
{