1
1
#include < WiFi.h>
2
2
#include < HTTPClient.h>
3
- #include < Preferences.h>
4
3
#include " DHT.h"
5
4
#include < ESP.h>
6
5
9
8
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
10
9
#define uS_TO_S_FACTOR 1000000
11
10
12
- bool wifi_setup_mode = true ;
13
-
14
- Preferences preferences;
11
+ unsigned long now;
12
+ int DEEPSLEEP_SECONDS = 1800 ;
15
13
16
14
WiFiServer server (80 );
17
15
@@ -23,6 +21,8 @@ long timeout;
23
21
24
22
const int dhtpin = 22 ;
25
23
const int soilpin = 32 ;
24
+ const int POWER_PIN = 34 ;
25
+ const int LIGHT_PIN = 33 ;
26
26
27
27
// Initialize DHT sensor.
28
28
DHT dht (dhtpin, DHTTYPE);
@@ -35,8 +35,8 @@ static char humidityTemp[7];
35
35
char linebuf[80 ];
36
36
int charcount=0 ;
37
37
38
- String ssid;
39
- String pwd;
38
+ String ssid = " YOURSSID " ;
39
+ String pwd = " YOURPWD " ;
40
40
41
41
char deviceid[21 ];
42
42
@@ -49,119 +49,46 @@ void setup() {
49
49
}
50
50
esp_deep_sleep_enable_timer_wakeup (1800 * uS_TO_S_FACTOR);
51
51
esp_deep_sleep_pd_config (ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
52
- preferences.begin (" higrow" , false );
53
52
54
53
pinMode (16 , OUTPUT);
55
- pinMode (34 , INPUT);
54
+ pinMode (POWER_PIN , INPUT);
56
55
digitalWrite (16 , LOW);
57
56
58
- wifi_setup_mode = digitalRead (34 )==HIGH;
59
-
60
57
timeout = 0 ;
61
58
62
59
chipid = ESP.getEfuseMac ();
63
60
sprintf (deviceid, " %" PRIu64, chipid);
61
+ Serial.print (" DeviceId: " );
64
62
Serial.println (deviceid);
65
-
66
- ssid = preferences.getString (" ssid" );
67
- pwd = preferences.getString (" pwd" );
68
-
69
- if (wifi_setup_mode) {
70
- Serial.println (" Smart Config" );
71
-
72
- WiFi.mode (WIFI_AP_STA);
73
- WiFi.beginSmartConfig ();
74
-
75
- while (!WiFi.smartConfigDone ()) {
76
- delay (500 );
77
- Serial.print (" ." );
78
- }
79
-
80
- Serial.println (" Got Credentials" );
81
-
82
- while (WiFi.status () != WL_CONNECTED) {
83
- delay (500 );
84
- Serial.print (" ." );
85
- }
86
-
87
- Serial.println (" WiFi connected" );
88
-
89
- preferences.putString (" ssid" , WiFi.SSID ());
90
- preferences.putString (" pwd" , WiFi.psk ());
91
-
92
- delay (1000 );
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
- }
104
- }
105
-
106
63
}
107
64
108
65
void loop () {
109
66
110
67
char body[1024 ];
111
68
digitalWrite (16 , LOW); // switched on
112
69
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 ) {
70
+ sensorsData (body);
71
+ http.begin (" http://api.higrow.tech/api/records" );
72
+ http.addHeader (" Content-Type" , " application/json" );
73
+ int httpResponseCode = http.POST (body);
74
+ Serial.println (httpResponseCode);
75
+ esp_deep_sleep_enable_timer_wakeup (DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);
76
+ esp_deep_sleep_start ();
130
77
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
78
}
158
79
159
80
void sensorsData (char * body){
160
81
161
82
// This section read sensors
162
83
timeout = millis ();
163
84
164
- int waterlevel = analogRead (soilpin)/4 ;
85
+ int waterlevel = analogRead (soilpin);
86
+ int lightlevel = analogRead (LIGHT_PIN);
87
+
88
+ waterlevel = map (waterlevel, 0 , 4095 , 0 , 1023 );
89
+ waterlevel = constrain (waterlevel, 0 , 1023 );
90
+ lightlevel = map (lightlevel, 0 , 4095 , 0 , 1023 );
91
+ lightlevel = constrain (lightlevel, 0 , 1023 );
165
92
166
93
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
167
94
float humidity = dht.readHumidity ();
@@ -174,15 +101,22 @@ void sensorsData(char* body){
174
101
175
102
String did = String (deviceid);
176
103
String water = String ((int )waterlevel);
104
+ String light = String ((int )lightlevel);
177
105
178
106
strcpy (body, " {\" deviceId\" :\" " );
179
107
strcat (body, did.c_str ());
180
108
strcat (body, " \" ,\" water\" :\" " );
181
109
strcat (body, water.c_str ());
110
+ strcat (body, " \" ,\" light\" :\" " );
111
+ strcat (body, light.c_str ());
182
112
strcat (body, " \" ,\" humidity\" :\" " );
183
113
strcat (body, humidityTemp);
184
114
strcat (body, " \" ,\" temperature\" :\" " );
185
115
strcat (body, celsiusTemp);
186
116
strcat (body, " \" }" );
117
+
118
+ if (lightlevel<100 ){
119
+ DEEPSLEEP_SECONDS = 10800 ;
120
+ }
187
121
}
188
122
0 commit comments