#include // Inclusion de la librairie OneWire byte SWITCHPIN=3; //Set Pin 2 as Switch byte ledpin=8; byte temp_PIN=5; //4 is used by SD //10,11,12,13 by ethernet #include #include #include #define DS18B20 0x28 // Adresse 1-Wire du DS18B20 #define BROCHE_ONEWIRE 5 // Broche utilisée pour le bus 1-Wire OneWire ds(BROCHE_ONEWIRE); // Création de l'objet OneWire ds // MAC address from Ethernet shield sticker under board byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 180); // IP address, may need to change depending on network EthernetServer server(80); // create a server at port 80 File webFile; unsigned long last_temp=0; //delay int last=0; volatile int water=0; volatile unsigned long last_water=0; //delay volatile boolean change=false; volatile int pulse=0; volatile unsigned long start=0; // ***************************** setup ******************************************************** void setup(){ last=-1; Serial.begin(9600); Serial.println("******* setup *************"); // pinMode(SWITCHPIN, INPUT); pinMode(ledpin, OUTPUT); digitalWrite(ledpin, LOW); pinMode(temp_PIN, INPUT); // Ethernet.begin(mac, ip); // initialize Ethernet device //Ethernet.begin(mac); // initialize Ethernet device server.begin(); // start to listen for clients // // initialize SD card Serial.println("Initializing SD card..."); if (!SD.begin(4)) { Serial.println("ERROR - SD card initialization failed!"); return; // init failed } Serial.println("SUCCESS - SD card initialized."); // check for index.htm file if (!SD.exists("log.txt")) { Serial.println("ERROR - Can't find log.txt file!"); return; // can't find index file } Serial.println("SUCCESS - Found log.txt file."); // delay(500); //cnt=EEPROMReadInt(0); water=readFile("log.txt"); if (water<0) water=0; //water=580; //here or call sleepnow in the loop //attachInterrupt(1, CallBack, FALLING); //Int 0 is Digital Pin 2,Int 1 is pin 3 attachInterrupt(1, IntChange, CHANGE); //Int 0 is Digital Pin 2,Int 1 is pin 3 //When the Input goes from High to Low it will trigger an interrupt //high=1 low=0 //rising 1>0 //falling 0>1 //low 1 //high 1 & 0 (change?) //change Serial.println("******* setup *************"); } // *************************** blinkLed ********************** void blinkLed() { digitalWrite(ledpin,HIGH); delay(2000); digitalWrite(ledpin,LOW); } // *************************** writeFile ******************************** void writeFile(char* file,int num) { File myFile; myFile = SD.open(file, FILE_WRITE); if (myFile) { myFile.seek(0); //Serial.println("Writing to log.txt..."); myFile.println(num); myFile.close(); //Serial.println("done writing."); } else { // if the file didn't open, print an error: //Serial.println("error opening test.txt"); } } // *************************** readFile ********************** int readFile(char* file ){ File myFile; int num=0; String tmp; char ch; myFile = SD.open(file); if (myFile) { while (myFile.available()) { ch= (myFile.read()); Serial.print(ch); if (isDigit(ch)) {tmp=tmp+ch;} } num=tmp.toInt(); myFile.close(); Serial.println(" ...done reading."); return num; } else { // if the file didn't open, print an error: Serial.println("error opening log.txt"); } } // *************************** web ********************** void web() { String s,req; EthernetClient client = server.available(); // try to get client if (client) { // got client? boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { // client data available to read char c = client.read(); // read 1 byte (character) from client // last line of client request is blank and ends with \n // respond to client only after last line received //Serial.println(c); req+=c; if (c == '\n' && currentLineIsBlank) { // send a standard http response header Serial.print("seconds=");Serial.println(millis() / 1000); Serial.println(req); client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); // send web page if (req.indexOf("/write") > 0) { Serial.println("/write"); writeFile("log.txt",water); } if (req.indexOf("/reset") > 0) { Serial.println("/reset"); water=0;last=-1; writeFile("log.txt",water); } if (req.indexOf("/temp") > 0) { float temp=0; get_temp_dallas(&temp); Serial.print("/temp=");Serial.println(temp); client.print(temp); } if (req.indexOf("/water") > 0) { Serial.print("/water=");Serial.println(water); client.print(water); } if (req.indexOf("/seconds") > 0) { Serial.println("/seconds"); client.print(millis()/1000); } if (req.indexOf("/xml") > 0) { float temp=0; get_temp_dallas(&temp); Serial.println("/xml"); client.print(""); client.print("");client.print(temp);client.print(""); client.print("");client.print(water);client.print(""); client.print("");client.print(millis()/1000);client.print(""); client.print(""); } /* webFile = SD.open("log.txt"); // open web page file if (webFile) { while(webFile.available()) { client.write(webFile.read()); // send web page to client } webFile.close(); } */ break; } // every line of text received from the client ends with \r\n if (c == '\n') { // last character on line of received text // starting new line with next character read currentLineIsBlank = true; } else if (c != '\r') { // a text character was received from client currentLineIsBlank = false; } } // end if (client.available()) } // end while (client.connected()) delay(1); // give the web browser time to receive the data client.stop(); // close the connection } // end if (client) } //**********get_temp ********************* boolean get_temp_dallas(float *temp){ byte data[9], addr[8]; // data : Données lues depuis le scratchpad // addr : adresse du module 1-Wire détecté if (!ds.search(addr)) { // Recherche un module 1-Wire ds.reset_search(); // Réinitialise la recherche de module return false; // Retourne une erreur } if (OneWire::crc8(addr, 7) != addr[7]) // Vérifie que l'adresse a été correctement reçue return false; // Si le message est corrompu on retourne une erreur if (addr[0] != DS18B20) // Vérifie qu'il s'agit bien d'un DS18B20 return false; // Si ce n'est pas le cas on retourne une erreur ds.reset(); // On reset le bus 1-Wire ds.select(addr); // On sélectionne le DS18B20 ds.write(0x44, 1); // On lance une prise de mesure de température delay(800); // Et on attend la fin de la mesure ds.reset(); // On reset le bus 1-Wire ds.select(addr); // On sélectionne le DS18B20 ds.write(0xBE); // On envoie une demande de lecture du scratchpad for (byte i = 0; i < 9; i++) // On lit le scratchpad data[i] = ds.read(); // Et on stock les octets reçus // Calcul de la température en degré Celsius *temp = ((data[1] << 8) | data[0]) * 0.0625; // Pas d'erreur return true; } // *************************** loop ******************************** void loop(){ //******************* /* if (millis()-last_temp>15000) { int temp=0; temp=get_temp(); Serial.print(temp);Serial.write(176); Serial.write('C'); Serial.println(); last_temp=millis(); } */ //************************ web(); //************************ if (water>last) { Serial.print("Water=");Serial.println(water); last=water; //do here or in the loop if you call sleepnow in the loop blinkLed(); //EEPROMWriteInt(0,cnt); writeFile("log.txt",water); }; //*********************** if (change==true) { change=false; Serial.print("pulse=");Serial.println(pulse); if (millis()-last_water>100) {water+=1;}; //debounce - we could use the pulse length as well? last_water=millis(); } //*********************** delay(100); //sleepNow(); } //*********************** myDelay in sec ************************************** void myDelay(int x) { for(int i=0; i<=x; i++) { delayMicroseconds(1000); } } // *************************** wakeUpNow ******************************** void IntChange() { if (digitalRead(SWITCHPIN)==LOW) { start=millis(); change=false; } else { if (start>0) { pulse=millis()-start; start=0; change=true; } else { start=0; change=false; }//if (start>0) } //if (digitalRead(SWITCHPIN)==LOW) { } void CallBack(){ //When the switch is pressed (Interrupt is triggered) the arduino enters this function if (millis()-last_water>500) { last_water=millis(); water=water+1; } }