1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
// the interval in mS unsigned long interval = 10000; // the time we need to wait to finish watering the plant. unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop() unsigned long previousMillis = 0; // millis() returns an unsigned long. void setup() { power_to_solenoid = false; pinMode(solenoidPin, OUTPUT); // Sets the pin as an output } void loop() { currentMillis = millis(); // capture the latest value of millis() 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 // buffer first part of HTTP request in HTTP_req array (string) // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1) if (req_index < (REQ_BUF_SZ - 1)) { HTTP_req[req_index] = c; // save HTTP request character req_index++; } //read char by char HTTP request if (readString.length() < 100) { //store characters to string readString += c; } // last line of client request is blank and ends with \n // respond to client only after last line received if (c == '\n' && currentLineIsBlank) { // 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) //controls the Arduino if you press the buttons if (readString.indexOf("/?waterPlant1") > 0) { // check if "interval" time has passed (1000 milliseconds) digitalWrite(solenoidPin, HIGH); // sets the solenoid power based on power_to_solenoid } if ((unsigned long)(currentMillis - previousMillis) >= interval) { //clearing string for next read readString = ""; // save the "current" time previousMillis = millis(); } } |
First I capture the URL "/?waterPlant1" and check if it is greater than 0. That would mean it found the word we are looking for in the header recieved from the client browser.
Next I turn on the solenoid. However we want to keep it on for 10 seconds to so sufficient amout of water gets poured into pot(depending how big the pot is.) so instead of resetting the readString to empty string. I wait for 10 seconds, as soon as 10 seonds passes. it is reset to empty string and default of solenoid turns it off.
I hope it was useful for you. Let me know in case you need any help in the comments below.