|
| 1 | +#include <DHT.h> |
| 2 | +#define RX 0 |
| 3 | +#define TX 1 |
| 4 | + |
| 5 | +#define LIMIT_TEMPERATURE 30 |
| 6 | +#define DHTPIN 11 |
| 7 | +#define DHTTYPE DHT11 |
| 8 | +#define smo_sensor 27 |
| 9 | +#define motor 22 |
| 10 | +#define DEBUG true |
| 11 | + |
| 12 | +DHT dht(DHTPIN, DHTTYPE); |
| 13 | +int connectionId; |
| 14 | + |
| 15 | +void setup() { |
| 16 | + Serial1.begin(115200); |
| 17 | + dht.begin(); |
| 18 | + pinMode(smo_sensor, INPUT); |
| 19 | + pinMode(motor, OUTPUT); |
| 20 | + |
| 21 | + sendData("AT+RST\r\n", 2000, DEBUG); // reset module |
| 22 | + sendData("AT+GMR\r\n", 1000, DEBUG); // configure as access point |
| 23 | + sendData("AT+CIPSERVER=0\r\n", 1000, DEBUG); // configure as access point |
| 24 | + sendData("AT+RST\r\n", 1000, DEBUG); // configure as access point |
| 25 | + sendData("AT+RESTORE\r\n", 1000, DEBUG); // configure as access point |
| 26 | + sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point |
| 27 | + sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point |
| 28 | + sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point |
| 29 | + sendData("AT+CWJAP=\"WIFI_ID\",\"WIFI_PASSWORD\"\r\n", 5000, DEBUG); // ADD YOUR OWN WIFI ID AND PASSWORD |
| 30 | + delay(3000); |
| 31 | + sendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip address |
| 32 | + delay(3000); |
| 33 | + sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections |
| 34 | + delay(1000); |
| 35 | + sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80 |
| 36 | + delay(1000); |
| 37 | +} |
| 38 | + |
| 39 | +void loop() { |
| 40 | + if (Serial1.find("+IPD,")) { |
| 41 | + delay(300); |
| 42 | + connectionId = Serial1.read() - 48; |
| 43 | + String serialIncoming = Serial1.readStringUntil('\r'); |
| 44 | + Serial.print("SERIAL_INCOMING:"); |
| 45 | + Serial.println(serialIncoming); |
| 46 | + |
| 47 | + if (serialIncoming.indexOf("/WATERING") > 0) { |
| 48 | + Serial.println("Irrigation Start"); |
| 49 | + digitalWrite(motor, HIGH); |
| 50 | + delay(1000); // 10 sec. |
| 51 | + digitalWrite(motor, LOW); |
| 52 | + Serial.println("Irrigation Finished"); |
| 53 | + Serial.println("! Incoming connection - sending WATERING webpage"); |
| 54 | + String html = ""; |
| 55 | + html += "<html>"; |
| 56 | + html += "<body><center><H1>Irrigation Complete.<br/></H1></center>"; |
| 57 | + html += "</body></html>"; |
| 58 | + espsend(html); |
| 59 | + } |
| 60 | + if (serialIncoming.indexOf("/SERA") > 0) { |
| 61 | + delay(300); |
| 62 | + |
| 63 | + float smo = analogRead(smo_sensor); |
| 64 | + float smopercent = (460-smo)*100.0/115.0 ; //min ve max değerleri değişken. |
| 65 | + Serial.print("SMO: %"); |
| 66 | + Serial.println(smo); |
| 67 | + |
| 68 | + float temperature = dht.readTemperature(); |
| 69 | + Serial.print("Temp: "); |
| 70 | + Serial.println(temperature); |
| 71 | + |
| 72 | + float humidity = dht.readHumidity(); |
| 73 | + Serial.print("Hum: "); |
| 74 | + Serial.println(humidity); |
| 75 | + |
| 76 | + Serial.println("! Incoming connection - sending SERA webpage"); |
| 77 | + String html = ""; |
| 78 | + html += "<html>"; |
| 79 | + html += "<body><center><H1>TEMPERATURE<br/></H1></center>"; |
| 80 | + html += "<center><H2>"; |
| 81 | + html += (String)temperature; |
| 82 | + html += " C<br/></H2></center>"; |
| 83 | + |
| 84 | + html += "<body><center><H1>HUMIDITY<br/></H1></center>"; |
| 85 | + html += "<center><H2>"; |
| 86 | + html += (String)humidity; |
| 87 | + html += "%<br/></H2></center>"; |
| 88 | + |
| 89 | + html += "<body><center><H1>SMO<br/></H1></center>"; |
| 90 | + html += "<center><H2>"; |
| 91 | + html += (String)smopercent; |
| 92 | + html += "%<br/></H2></center>"; |
| 93 | + |
| 94 | + html += "</body></html>"; |
| 95 | + espsend(html); |
| 96 | + } |
| 97 | + else |
| 98 | + Serial.println("! Incoming connection - sending MAIN webpage"); |
| 99 | + String html = ""; |
| 100 | + html += "<html>"; |
| 101 | + html += "<body><center><H1>CONNECTED.<br/></H1></center>"; |
| 102 | + html += "<center><a href='/SERA'><h4>INFO:Get Sensor Data</a></br><a href='/WATERING'>WATERING:Run Water Pump</a></h4></center>"; |
| 103 | + html += "</body></html>"; |
| 104 | + espsend(html); |
| 105 | + String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command |
| 106 | + closeCommand += connectionId; // append connection id |
| 107 | + closeCommand += "\r\n"; |
| 108 | + sendData(closeCommand, 3000, DEBUG); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | +//////////////////////////////sends data from ESP to webpage/////////////////////////// |
| 114 | + |
| 115 | +void espsend(String d) |
| 116 | +{ |
| 117 | + String cipSend = " AT+CIPSEND="; |
| 118 | + cipSend += connectionId; |
| 119 | + cipSend += ","; |
| 120 | + cipSend += d.length(); |
| 121 | + cipSend += "\r\n"; |
| 122 | + sendData(cipSend, 1000, DEBUG); |
| 123 | + sendData(d, 1000, DEBUG); |
| 124 | +} |
| 125 | + |
| 126 | +//////////////gets the data from esp and displays in serial monitor/////////////////////// |
| 127 | + |
| 128 | +String sendData(String command, const int timeout, boolean debug) |
| 129 | +{ |
| 130 | + String response = ""; |
| 131 | + Serial1.print(command); |
| 132 | + long int time = millis(); |
| 133 | + while ( (time + timeout) > millis()) |
| 134 | + { |
| 135 | + while (Serial1.available()) |
| 136 | + { |
| 137 | + char c = Serial1.read(); // read the next character. |
| 138 | + response += c; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (debug) |
| 143 | + { |
| 144 | + Serial.print(response); //displays the esp response messages in arduino Serial monitor |
| 145 | + } |
| 146 | + return response; |
| 147 | +} |
0 commit comments