#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <OneWire.h>
 
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
 
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
int son = A0;
int air = A1;
int cour = A2;
int tens = A3;
int vit = A4;
int oil = A5;
int temp = 4;
int hall = 2;
// Démarrage de YunServer. Cettr classe réceptionne
// toutes les requêtes transférées par le serveur web côté Linux.
YunServer server;
void setup (){
 // Démarrage du Bridge
 Bridge.begin();
 // Lance l'écoute du YunServer sur le réseau
 server.listenOnLocalhost();
 //server.begin();
 Serial.begin(9600);
}
void loop () {
// Réceptionne les éventuels clients
 YunClient client = server.accept();
 
 //Test si un client est connecté
 if (client) {
 //Oui il y a un client
 //Récupère la requête envoyée par le client 
 String command = client.readStringUntil('\r');
 
 command = "{temp:";
 command += gwendal2en1();
 command = ",hall:";
 command += gwendal2en1();
 command += "}";
 
 command = "{vit:";
 command += bozec2en1();
 command = ",oil:";
 command += bozec2en1();
 command += "}";
 
 command = "{son:";
 command += jerome2en1();
 command = ",air:";
 command += jerome2en1();
 command += "}";
 
 command = "{cour:";
 command += vince2en1();
 command = ",tens:";
 command += vince2en1();
 command += "}";
 
 
 
 //client
 Serial.print(command);
//Renvoie une valeur
 //client.print("ok");
//Fermeture de la connexion et libèration des ressources
 client.stop();
}
}//fin loop
int gwendal2en1(){ 
 temp = getTemp();
 hall = digitalRead(4);
 compteur++; //incrémentation
 compteur = 0; //retour a zéro
 return temp;
 return hall;
}
int jerome2en1(){
 son = analogRead(A0); 
 air = analogRead(A1);
 return son;
 return air;
 
}
int vince2en1(){
int cour = analogRead(A2);
int tens = analogRead(A3);
return cour;
return tens;
}
int bozec2en1(){
 vit = analogRead(A4);
 oil = analogRead(A5); //récupérer la valeur sur A0
 return vit;
 return oil;
}
 
 float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius
 
 byte data[12];
 byte addr[8];
 
 if ( !ds.search(addr)) {
 //no more sensors on chain, reset search
 ds.reset_search();
 return -1000;
 }
 
 if ( OneWire::crc8( addr, 7) != addr[7]) {
 Serial.println("CRC is not valid!");
 return -1000;
 }
 
 if ( addr[0] != 0x10 && addr[0] != 0x28) {
 Serial.print("Device is not recognized");
 return -1000;
 }
 
 ds.reset();
 ds.select(addr);
 ds.write(0x44,1); // start conversion, with parasite power on at the end
 
 byte present = ds.reset();
 ds.select(addr); 
 ds.write(0xBE); // Read Scratchpad
 
 
 for (int i = 0; i < 9; i++) { // we need 9 bytes
 data[i] = ds.read();
 }
 
 ds.reset_search();
 
 byte MSB = data[1];
 byte LSB = data[0];
 
 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 
 return TemperatureSum;
 
}
 
	
    
       
	
    		 
 