|
| 1 | +#include <Wire.h> |
| 2 | + |
| 3 | +#define RX 0 |
| 4 | +#define TX 1 |
| 5 | + |
| 6 | +#define LIMIT_TEMPERATURE 30 |
| 7 | +#define smo_sensor 27 |
| 8 | +#define DEBUG true |
| 9 | + |
| 10 | +int connectionId; |
| 11 | + |
| 12 | +void dc(int dcNumber, int speed, int direction){ |
| 13 | + Wire.beginTransmission(0x22); |
| 14 | + Wire.write(0x26); |
| 15 | + Wire.write(dcNumber); |
| 16 | + Wire.write(speed); |
| 17 | + Wire.write(direction); |
| 18 | + int cs = dcNumber ^ speed ^ direction; |
| 19 | + Wire.write(cs); |
| 20 | + Wire.endTransmission(); |
| 21 | +} |
| 22 | + |
| 23 | +void shtc_init(){ |
| 24 | + Wire.beginTransmission(0x70); |
| 25 | + Wire.write(0x35); |
| 26 | + Wire.write(0x17); |
| 27 | + Wire.endTransmission(); |
| 28 | + delay(500); |
| 29 | + Wire.beginTransmission(0x70); |
| 30 | + Wire.write(0xEF); |
| 31 | + Wire.write(0xC8); |
| 32 | + Wire.endTransmission(); |
| 33 | + delay(500); |
| 34 | + Wire.requestFrom(0x70, 3); |
| 35 | +} |
| 36 | + |
| 37 | +float temperature(){ |
| 38 | + int rcv1 = 0; |
| 39 | + int rcv2 = 0; |
| 40 | + Wire.beginTransmission(0x70); |
| 41 | + Wire.write(0x78); |
| 42 | + Wire.write(0x66); |
| 43 | + Wire.endTransmission(); |
| 44 | + delay(100); |
| 45 | + Wire.requestFrom(0x70, 2); |
| 46 | + while(Wire.available()) { |
| 47 | + rcv1 = Wire.read(); |
| 48 | + rcv2 = Wire.read(); |
| 49 | + } |
| 50 | + delay(100); |
| 51 | + float temp = (((4375 * ((rcv1 << 8) | rcv2)) >> 14) - 4500) / 100; |
| 52 | + return temp; |
| 53 | +} |
| 54 | + |
| 55 | +float humidity(){ |
| 56 | + int rcv1 = 0; |
| 57 | + int rcv2 = 0; |
| 58 | + Wire.beginTransmission(0x70); |
| 59 | + Wire.write(0x78); |
| 60 | + Wire.write(0x66); |
| 61 | + Wire.endTransmission(); |
| 62 | + delay(100); |
| 63 | + Wire.requestFrom(0x70, 2); |
| 64 | + while(Wire.available()) { |
| 65 | + rcv1 = Wire.read(); |
| 66 | + rcv2 = Wire.read(); |
| 67 | + } |
| 68 | + delay(100); |
| 69 | + return (((4375 * ((rcv1 << 8) | rcv2)) >> 14) - 4500) / 100; |
| 70 | +} |
| 71 | + |
| 72 | +void setup() { |
| 73 | + Serial1.begin(115200); |
| 74 | + pinMode(smo_sensor, INPUT); |
| 75 | + shtc_init(); |
| 76 | + |
| 77 | + sendData("AT+RST\r\n", 2000, DEBUG); // reset module |
| 78 | + sendData("AT+GMR\r\n", 1000, DEBUG); // configure as access point |
| 79 | + sendData("AT+CIPSERVER=0\r\n", 1000, DEBUG); // configure as access point |
| 80 | + sendData("AT+RST\r\n", 1000, DEBUG); // configure as access point |
| 81 | + sendData("AT+RESTORE\r\n", 1000, DEBUG); // configure as access point |
| 82 | + sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point |
| 83 | + sendData("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point |
| 84 | + sendData("AT+CWMODE?\r\n", 1000, DEBUG); // configure as access point |
| 85 | + sendData("AT+CWJAP=\"WIFI_ID\",\"WIFI_PASSWORD\"\r\n", 5000, DEBUG); // ADD YOUR OWN WIFI ID AND PASSWORD |
| 86 | + delay(3000); |
| 87 | + sendData("AT+CIFSR\r\n", 1000, DEBUG); // get ip address |
| 88 | + delay(3000); |
| 89 | + sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections |
| 90 | + delay(1000); |
| 91 | + sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80 |
| 92 | + delay(1000); |
| 93 | +} |
| 94 | + |
| 95 | +void loop() { |
| 96 | + if (Serial1.find("+IPD,")) { |
| 97 | + delay(300); |
| 98 | + connectionId = Serial1.read() - 48; |
| 99 | + String serialIncoming = Serial1.readStringUntil('\r'); |
| 100 | + Serial.print("SERIAL_INCOMING:"); |
| 101 | + Serial.println(serialIncoming); |
| 102 | + |
| 103 | + if (serialIncoming.indexOf("/WATERING") > 0) { |
| 104 | + Serial.println("Irrigation Start"); |
| 105 | + dc(1,255,1); |
| 106 | + delay(1000); // 10 sec. |
| 107 | + dc(1,0,1); |
| 108 | + Serial.println("Irrigation Finished"); |
| 109 | + Serial.println("! Incoming connection - sending WATERING webpage"); |
| 110 | + String html = ""; |
| 111 | + html += "<html>"; |
| 112 | + html += "<body><center><H1>Irrigation Complete.<br/></H1></center>"; |
| 113 | + html += "</body></html>"; |
| 114 | + espsend(html); |
| 115 | + } |
| 116 | + if (serialIncoming.indexOf("/SERA") > 0) { |
| 117 | + delay(300); |
| 118 | + |
| 119 | + float smo = analogRead(smo_sensor); |
| 120 | + float smopercent = (460-smo)*100.0/115.0 ; //min ve max değerleri değişken. |
| 121 | + Serial.print("SMO: %"); |
| 122 | + Serial.println(smo); |
| 123 | + |
| 124 | + float temp = temperature(); |
| 125 | + Serial.print("Temp: "); |
| 126 | + Serial.println(temp); |
| 127 | + |
| 128 | + float hum = humidity(); |
| 129 | + Serial.print("Hum: "); |
| 130 | + Serial.println(hum); |
| 131 | + |
| 132 | + Serial.println("! Incoming connection - sending SERA webpage"); |
| 133 | + String html = ""; |
| 134 | + html += "<html>"; |
| 135 | + html += "<body><center><H1>TEMPERATURE<br/></H1></center>"; |
| 136 | + html += "<center><H2>"; |
| 137 | + html += (String)temp; |
| 138 | + html += " C<br/></H2></center>"; |
| 139 | + |
| 140 | + html += "<body><center><H1>HUMIDITY<br/></H1></center>"; |
| 141 | + html += "<center><H2>"; |
| 142 | + html += (String)hum; |
| 143 | + html += "%<br/></H2></center>"; |
| 144 | + |
| 145 | + html += "<body><center><H1>SMO<br/></H1></center>"; |
| 146 | + html += "<center><H2>"; |
| 147 | + html += (String)smopercent; |
| 148 | + html += "%<br/></H2></center>"; |
| 149 | + |
| 150 | + html += "</body></html>"; |
| 151 | + espsend(html); |
| 152 | + } |
| 153 | + else |
| 154 | + Serial.println("! Incoming connection - sending MAIN webpage"); |
| 155 | + String html = ""; |
| 156 | + html += "<html>"; |
| 157 | + html += "<body><center><H1>CONNECTED.<br/></H1></center>"; |
| 158 | + html += "<center><a href='/SERA'><h4>INFO:Get Sensor Data</a></br><a href='/WATERING'>WATERING:Run Water Pump</a></h4></center>"; |
| 159 | + html += "</body></html>"; |
| 160 | + espsend(html); |
| 161 | + String closeCommand = "AT+CIPCLOSE="; ////////////////close the socket connection////esp command |
| 162 | + closeCommand += connectionId; // append connection id |
| 163 | + closeCommand += "\r\n"; |
| 164 | + sendData(closeCommand, 3000, DEBUG); |
| 165 | + } |
| 166 | + |
| 167 | +} |
| 168 | +//////////////////////////////sends data from ESP to webpage/////////////////////////// |
| 169 | +void espsend(String d) |
| 170 | +{ |
| 171 | + String cipSend = " AT+CIPSEND="; |
| 172 | + cipSend += connectionId; |
| 173 | + cipSend += ","; |
| 174 | + cipSend += d.length(); |
| 175 | + cipSend += "\r\n"; |
| 176 | + sendData(cipSend, 1000, DEBUG); |
| 177 | + sendData(d, 1000, DEBUG); |
| 178 | +} |
| 179 | + |
| 180 | +//////////////gets the data from esp and displays in serial monitor/////////////////////// |
| 181 | +String sendData(String command, const int timeout, boolean debug) |
| 182 | +{ |
| 183 | + String response = ""; |
| 184 | + Serial1.print(command); |
| 185 | + long int time = millis(); |
| 186 | + while ( (time + timeout) > millis()) |
| 187 | + { |
| 188 | + while (Serial1.available()) |
| 189 | + { |
| 190 | + char c = Serial1.read(); // read the next character. |
| 191 | + response += c; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + if (debug) |
| 196 | + { |
| 197 | + Serial.print(response); //displays the esp response messages in arduino Serial monitor |
| 198 | + } |
| 199 | + return response; |
| 200 | +} |
0 commit comments