Skip to content

Commit 784d741

Browse files
committed
Alpha release
1 parent 114ac64 commit 784d741

File tree

1 file changed

+127
-125
lines changed

1 file changed

+127
-125
lines changed

HiGrowEsp32/HiGrowEsp32.ino

Lines changed: 127 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,188 @@
11
#include <WiFi.h>
2-
#include <WiFiClient.h>
3-
#include <WiFiServer.h>
42
#include <HTTPClient.h>
53
#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;
713

814
Preferences preferences;
915

1016
WiFiServer server(80);
1117

12-
SimpleDHT11 dht11;
13-
1418
HTTPClient http;
1519

1620
uint64_t chipid;
1721

1822
long timeout;
1923

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+
2041
char deviceid[21];
2142

2243
void setup() {
44+
dht.begin();
45+
2346
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);
2552
preferences.begin("higrow", false);
2653

2754
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;
4559

4660
timeout = 0;
4761

4862
chipid = ESP.getEfuseMac();
4963
sprintf(deviceid, "%" PRIu64, chipid);
5064
Serial.println(deviceid);
5165

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");
5968

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+
7472
WiFi.mode(WIFI_AP_STA);
7573
WiFi.beginSmartConfig();
7674

77-
//Wait for SmartConfig packet from mobile
78-
Serial.println("Waiting for SmartConfig.");
7975
while (!WiFi.smartConfigDone()) {
80-
int led = digitalRead(16);
81-
if (led == HIGH) {
82-
digitalWrite(16, LOW);
83-
} else {
84-
digitalWrite(16, HIGH);
85-
}
8676
delay(500);
8777
Serial.print(".");
8878
}
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+
9382
while (WiFi.status() != WL_CONNECTED) {
9483
delay(500);
9584
Serial.print(".");
9685
}
97-
Serial.println("");
98-
Serial.println("WiFi Connected.");
86+
87+
Serial.println("WiFi connected");
9988

10089
preferences.putString("ssid", WiFi.SSID());
10190
preferences.putString("pwd", WiFi.psk());
10291

103-
Serial.print("IP Address: ");
92+
delay(1000);
10493
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+
}
105104
}
106105

107-
server.begin();
108106
}
109107

110108
void loop() {
111-
digitalWrite(16, HIGH);
112-
bool isSetup = preferences.getBool("isSetup", true);
109+
113110
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){
116160

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+
121175
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);
125177

126178
strcpy(body, "{\"deviceId\":\"");
127-
strcat(body, deviceid);
179+
strcat(body, did.c_str());
128180
strcat(body, "\",\"water\":\"");
129181
strcat(body, water.c_str());
130182
strcat(body, "\",\"humidity\":\"");
131-
strcat(body, humi.c_str());
183+
strcat(body, humidityTemp);
132184
strcat(body, "\",\"temperature\":\"");
133-
strcat(body, temp.c_str());
185+
strcat(body, celsiusTemp);
134186
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-
}
186187
}
188+

0 commit comments

Comments
 (0)