|
1 | 1 | #include <WiFi.h>
|
2 |
| -#include <WiFiClient.h> |
3 |
| -#include <WiFiServer.h> |
4 | 2 | #include <HTTPClient.h>
|
5 | 3 | #include <Preferences.h>
|
6 |
| -#include <SimpleDHT.h> |
| 4 | +#include "DHT.h" |
| 5 | +#include <ESP.h> |
| 6 | + |
| 7 | +//#define DHTTYPE DHT11 // DHT 11 |
| 8 | +//#define DHTTYPE DHT21 // DHT 21 (AM2301) |
| 9 | +#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321 |
| 10 | +#define uS_TO_S_FACTOR 1000000 |
| 11 | + |
| 12 | +bool wifi_setup_mode = true; |
7 | 13 |
|
8 | 14 | Preferences preferences;
|
9 | 15 |
|
10 | 16 | WiFiServer server(80);
|
11 | 17 |
|
12 |
| -SimpleDHT11 dht11; |
13 |
| - |
14 | 18 | HTTPClient http;
|
15 | 19 |
|
16 | 20 | uint64_t chipid;
|
17 | 21 |
|
18 | 22 | long timeout;
|
19 | 23 |
|
| 24 | +const int dhtpin = 22; |
| 25 | +const int soilpin = 32; |
| 26 | + |
| 27 | +// Initialize DHT sensor. |
| 28 | +DHT dht(dhtpin, DHTTYPE); |
| 29 | + |
| 30 | +// Temporary variables |
| 31 | +static char celsiusTemp[7]; |
| 32 | +static char humidityTemp[7]; |
| 33 | + |
| 34 | +// Client variables |
| 35 | +char linebuf[80]; |
| 36 | +int charcount=0; |
| 37 | + |
| 38 | +String ssid; |
| 39 | +String pwd; |
| 40 | + |
20 | 41 | char deviceid[21];
|
21 | 42 |
|
22 | 43 | void setup() {
|
| 44 | + dht.begin(); |
| 45 | + |
23 | 46 | Serial.begin(115200);
|
24 |
| - |
| 47 | + while(!Serial) { |
| 48 | + ; // wait for serial port to connect. Needed for native USB port only |
| 49 | + } |
| 50 | + esp_deep_sleep_enable_timer_wakeup(1800 * uS_TO_S_FACTOR); |
| 51 | + esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); |
25 | 52 | preferences.begin("higrow", false);
|
26 | 53 |
|
27 | 54 | pinMode(16, OUTPUT);
|
28 |
| - pinMode(0, INPUT); |
29 |
| - digitalWrite(16, HIGH); |
30 |
| - |
31 |
| - for (int j = 0; j < 30; j++) { |
32 |
| - if (digitalRead(0) == LOW) { |
33 |
| - preferences.clear(); |
34 |
| - digitalWrite(16, LOW); |
35 |
| - delay(100); |
36 |
| - digitalWrite(16, HIGH); |
37 |
| - delay(100); |
38 |
| - digitalWrite(16, LOW); |
39 |
| - delay(100); |
40 |
| - digitalWrite(16, HIGH); |
41 |
| - j=30; |
42 |
| - } |
43 |
| - delay(100); |
44 |
| - } |
| 55 | + pinMode(34, INPUT); |
| 56 | + digitalWrite(16, LOW); |
| 57 | + |
| 58 | + wifi_setup_mode = digitalRead(34)==HIGH; |
45 | 59 |
|
46 | 60 | timeout = 0;
|
47 | 61 |
|
48 | 62 | chipid = ESP.getEfuseMac();
|
49 | 63 | sprintf(deviceid, "%" PRIu64, chipid);
|
50 | 64 | Serial.println(deviceid);
|
51 | 65 |
|
52 |
| - String ssid = preferences.getString("ssid"); |
53 |
| - String pwd = preferences.getString("pwd"); |
54 |
| - |
55 |
| - if (ssid.length() > 0 && pwd.length() > 0) { |
56 |
| - // We start by connecting to a WiFi network |
57 |
| - Serial.print("Connecting to "); |
58 |
| - Serial.println(ssid); |
| 66 | + ssid = preferences.getString("ssid"); |
| 67 | + pwd = preferences.getString("pwd"); |
59 | 68 |
|
60 |
| - WiFi.begin(ssid.c_str(), pwd.c_str()); |
61 |
| - |
62 |
| - while (WiFi.status() != WL_CONNECTED) { |
63 |
| - delay(500); |
64 |
| - Serial.print("."); |
65 |
| - } |
66 |
| - |
67 |
| - digitalWrite(16, HIGH); |
68 |
| - Serial.println(""); |
69 |
| - Serial.println("WiFi connected"); |
70 |
| - Serial.println("IP address: "); |
71 |
| - Serial.println(WiFi.localIP()); |
72 |
| - } else { |
73 |
| - //Init WiFi as Station, start SmartConfig |
| 69 | + if (wifi_setup_mode) { |
| 70 | + Serial.println("Smart Config"); |
| 71 | + |
74 | 72 | WiFi.mode(WIFI_AP_STA);
|
75 | 73 | WiFi.beginSmartConfig();
|
76 | 74 |
|
77 |
| - //Wait for SmartConfig packet from mobile |
78 |
| - Serial.println("Waiting for SmartConfig."); |
79 | 75 | while (!WiFi.smartConfigDone()) {
|
80 |
| - int led = digitalRead(16); |
81 |
| - if (led == HIGH) { |
82 |
| - digitalWrite(16, LOW); |
83 |
| - } else { |
84 |
| - digitalWrite(16, HIGH); |
85 |
| - } |
86 | 76 | delay(500);
|
87 | 77 | Serial.print(".");
|
88 | 78 | }
|
89 |
| - Serial.println("SmartConfig received."); |
90 |
| - |
91 |
| - //Wait for WiFi to connect to AP |
92 |
| - Serial.println("Waiting for WiFi"); |
| 79 | + |
| 80 | + Serial.println("Got Credentials"); |
| 81 | + |
93 | 82 | while (WiFi.status() != WL_CONNECTED) {
|
94 | 83 | delay(500);
|
95 | 84 | Serial.print(".");
|
96 | 85 | }
|
97 |
| - Serial.println(""); |
98 |
| - Serial.println("WiFi Connected."); |
| 86 | + |
| 87 | + Serial.println("WiFi connected"); |
99 | 88 |
|
100 | 89 | preferences.putString("ssid", WiFi.SSID());
|
101 | 90 | preferences.putString("pwd", WiFi.psk());
|
102 | 91 |
|
103 |
| - Serial.print("IP Address: "); |
| 92 | + delay(1000); |
104 | 93 | Serial.println(WiFi.localIP());
|
| 94 | + preferences.putBool("isSetup", true); |
| 95 | + server.begin(); |
| 96 | + }else{ |
| 97 | + Serial.println("WiFi"); |
| 98 | + |
| 99 | + WiFi.begin(ssid.c_str(), pwd.c_str()); |
| 100 | + |
| 101 | + while (WiFi.status() != WL_CONNECTED) { |
| 102 | + delay(500); |
| 103 | + } |
105 | 104 | }
|
106 | 105 |
|
107 |
| - server.begin(); |
108 | 106 | }
|
109 | 107 |
|
110 | 108 | void loop() {
|
111 |
| - digitalWrite(16, HIGH); |
112 |
| - bool isSetup = preferences.getBool("isSetup", true); |
| 109 | + |
113 | 110 | char body[1024];
|
114 |
| - timeout = millis(); |
115 |
| - WiFiClient client = server.available(); // listen for incoming clients |
| 111 | + digitalWrite(16, LOW); //switched on |
| 112 | + |
| 113 | + if(wifi_setup_mode){ |
| 114 | + WiFiClient client = server.available(); |
| 115 | + if (client) { |
| 116 | + sensorsData(body); |
| 117 | + Serial.println("New client"); |
| 118 | + memset(linebuf,0,sizeof(linebuf)); |
| 119 | + charcount=0; |
| 120 | + String currentLine = ""; |
| 121 | + while (client.connected()) { |
| 122 | + if (client.available()) { |
| 123 | + char c = client.read(); |
| 124 | + Serial.write(c); |
| 125 | + linebuf[charcount]=c; |
| 126 | + if (charcount<sizeof(linebuf)-1) charcount++; |
| 127 | + if (c == '\n') { |
| 128 | + |
| 129 | + if (currentLine.length() == 0) { |
| 130 | + |
| 131 | + client.println("HTTP/1.1 200 OK"); |
| 132 | + client.println("Content-type:application/json"); |
| 133 | + client.println(); |
| 134 | + client.print(body); |
| 135 | + client.println(); |
| 136 | + break; |
| 137 | + } else { |
| 138 | + currentLine = ""; |
| 139 | + } |
| 140 | + } else if (c != '\r') { |
| 141 | + currentLine += c; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + delay(1); |
| 146 | + client.stop(); |
| 147 | + Serial.println("client disconnected"); |
| 148 | + } |
| 149 | + }else{ |
| 150 | + sensorsData(body); |
| 151 | + http.begin("http://higrowapp.azurewebsites.net/api/records"); |
| 152 | + http.addHeader("Content-Type", "application/json"); |
| 153 | + int httpResponseCode = http.POST(body); |
| 154 | + Serial.println(httpResponseCode); |
| 155 | + esp_deep_sleep_start(); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void sensorsData(char* body){ |
116 | 160 |
|
117 |
| - byte temperature = 0; |
118 |
| - byte humidity = 0; |
119 |
| - byte data[40] = {0}; |
120 |
| - dht11.read(2, &temperature, &humidity, data); |
| 161 | + //This section read sensors |
| 162 | + timeout = millis(); |
| 163 | + |
| 164 | + int waterlevel = analogRead(soilpin)/4; |
| 165 | + |
| 166 | + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) |
| 167 | + float humidity = dht.readHumidity(); |
| 168 | + // Read temperature as Celsius (the default) |
| 169 | + float temperature = dht.readTemperature(); |
| 170 | + |
| 171 | + float hic = dht.computeHeatIndex(temperature, humidity, false); |
| 172 | + dtostrf(hic, 6, 2, celsiusTemp); |
| 173 | + dtostrf(humidity, 6, 2, humidityTemp); |
| 174 | + |
121 | 175 | String did = String(deviceid);
|
122 |
| - String water = String(analogRead(4)); |
123 |
| - String temp = String((double)temperature); |
124 |
| - String humi = String((int)humidity); |
| 176 | + String water = String((int)waterlevel); |
125 | 177 |
|
126 | 178 | strcpy(body, "{\"deviceId\":\"");
|
127 |
| - strcat(body, deviceid); |
| 179 | + strcat(body, did.c_str()); |
128 | 180 | strcat(body, "\",\"water\":\"");
|
129 | 181 | strcat(body, water.c_str());
|
130 | 182 | strcat(body, "\",\"humidity\":\"");
|
131 |
| - strcat(body, humi.c_str()); |
| 183 | + strcat(body, humidityTemp); |
132 | 184 | strcat(body, "\",\"temperature\":\"");
|
133 |
| - strcat(body, temp.c_str()); |
| 185 | + strcat(body, celsiusTemp); |
134 | 186 | strcat(body, "\"}");
|
135 |
| - if (!isSetup) { |
136 |
| - http.begin("http://higrowapp.azurewebsites.net/api/records"); |
137 |
| - http.addHeader("Content-Type", "application/json"); |
138 |
| - int httpResponseCode = http.POST(body); |
139 |
| - |
140 |
| - Serial.println(httpResponseCode); |
141 |
| - Serial.println(body); |
142 |
| - } |
143 |
| - if (client) { // print a message out the serial port |
144 |
| - String currentLine = ""; // make a String to hold incoming data from the client |
145 |
| - while (client.connected()) { // loop while the client's connected |
146 |
| - if (client.available()) { // if there's bytes to read from the client, |
147 |
| - char c = client.read(); // read a byte, then |
148 |
| - Serial.write(c); // print it out the serial monitor |
149 |
| - if (c == '\n') { // if the byte is a newline character |
150 |
| - // if the current line is blank, you got two newline characters in a row. |
151 |
| - // that's the end of the client HTTP request, so send a response: |
152 |
| - if (currentLine.length() == 0) { |
153 |
| - // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) |
154 |
| - // and a content-type so the client knows what's coming, then a blank line: |
155 |
| - client.println("HTTP/1.1 200 OK"); |
156 |
| - client.println("Content-type:application/json"); |
157 |
| - client.println(); |
158 |
| - |
159 |
| - // the content of the HTTP response follows the header: |
160 |
| - client.print(body); |
161 |
| - |
162 |
| - // The HTTP response ends with another blank line: |
163 |
| - client.println(); |
164 |
| - // break out of the while loop: |
165 |
| - break; |
166 |
| - } else { // if you got a newline, then clear currentLine: |
167 |
| - currentLine = ""; |
168 |
| - } |
169 |
| - } else if (c != '\r') { // if you got anything else but a carriage return character, |
170 |
| - currentLine += c; // add it to the end of the currentLine |
171 |
| - } |
172 |
| - } |
173 |
| - } |
174 |
| - // close the connection: |
175 |
| - client.stop(); |
176 |
| - Serial.println("client disonnected"); |
177 |
| - } |
178 |
| - if (timeout >= (60000 * 5)) { |
179 |
| - preferences.putBool("isSetup", false); |
180 |
| - digitalWrite(16, HIGH); |
181 |
| - } |
182 |
| - if (!isSetup) { |
183 |
| - Serial.println("Entering sleep mode"); |
184 |
| - system_deep_sleep(60000000 * 10); |
185 |
| - } |
186 | 187 | }
|
| 188 | + |
0 commit comments