/////////////////////////////
// mode client //
// charge la page bdd.php //
// avec la température en argument //
// FONCTIONNEL //
/////////////////////////////
#include <Ethernet.h> //library for ethernet functions
#include <Client.h> //library for client functions
#include <SPI.h>
#include <EthernetUdp.h> // UDP library from:
bjoern@cs.stanford.edu 12/30/2008
// Ethernet settings
uint8_t hwaddr[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xBE}; // mac-adress of arduino
uint8_t ipaddr[4] = {192, 168, 0, 10}; // IP-adress of arduino
uint8_t gwaddr[4] = {192, 168, 0, 254}; // IP-adress of gateway ( for later DNS implementation)
uint8_t subnet[4] = {255, 255, 255, 0}; // subnetmask ( for later DNS implementation)
uint8_t serverip[4] = {88,167,233,135}; // IP-adress of server arduino sends data to
uint8_t serverport = 80; // the port the arduino talks to
//byte ip[] = {88,167,233,135}; // ip in lan
byte ip[] = { 192, 168, 0, 10 }; // ip in lan
//IPAddress server(88,167,233,135);
unsigned int localPort = 8888; // local port to listen on
// the next two variables are set when a packet is received
byte remoteIp[4]; // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
EthernetUDP Udp;
EthernetClient client; // make a new instance from type "Client" named "client", giving it
// serverip and serverport
bool connected = false; // yes-no variable (boolean) to store if the arduino is connected to the server
int i = 0; // variable to count the sendings to the server
//--------------------------
// heure
//int refresh=0;
int top=0;
int last_top=0;
int seconde=50;
int seconde2=0;
int minute=59;
int heure=23;
long last_time=0;
int dizaine;
//---------------------------
void setup(void) // setup-function runs at the startup of the arduino
{ pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600); // start the serial port
Serial.println("demarrage...");
Ethernet. begin(hwaddr,ip,gwaddr,subnet); // start up ethernet
digitalWrite(9, LOW);
// start the Ethernet and UDP:
// Ethernet.begin(mac,ip);
Udp.begin(localPort);
}
void loop(void) // loop function runs over and over again
{
listen_udp();
if (seconde==59)
{
digitalWrite(9, HIGH);
if(!connected) { // if "not" connected print: not connected

Serial.println("Not connected");
if (client.connect(serverip, 80)){ // if connected, set variable connected to "true" and
connected = true;
//delay(100);
client.print("GET /arduino/bdd.php?temp=22");
Serial.print("GET /arduino/bdd.php?temp=22");
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host:
www.google.fr");
Serial.println("Host:
www.google.fr");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Accept: text/html");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
Serial.println();
}
else{
Serial.println("Cannot connect to Server"); // else block if the server connection fails (debugging)
}
}
else {
delay(200); //
while (client.connected() && client.available()) { // when connected and availabe:
char c = client.read(); // read the answer of the server and
Serial.print(c); // print it to serial port
} //
Serial.println(); //
client.stop(); // stop the connection and set
connected = false; // "connected" to false
}
digitalWrite(9, LOW);
//delay(1000);
}
horloge();
if(top==2){top=0; }//on remet le compteur des tops seconde à0
}
int horloge()
{
if(millis()-last_time>=500)
{ last_time=millis();
top++;
if(top==2){seconde+=1;}//incrementation des secondes toutes les 2x 500ms
if(seconde>59)//incrementation des minutes
{seconde=0; minute=minute++;}
if(minute>59)//incrementation des heures
{minute=0;heure=heure++;}
if(heure>23)
{heure=0;}
//---------- affichage heures min sec et séparateurs
//if(seconde%2==0){lcd.setCursor(8, 1); lcd.print(" ");}//si secondes sont paires, affiche separateur droit
//else{lcd.setCursor(8, 1); lcd.print(":");} //sinon affiche le separateur gauche
dizaine=((heure*60+minute)/10);
//affiche_heure();
}
return dizaine;
return top;
}
void listen_udp()
{
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote
, DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
memset(packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE);
// read the packet into packetBufffer and get the senders IP addr and port number
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
analogWrite(9, atoi(packetBuffer)); //utiliser la valeur reçu pour allumer la led
}
}