Skip to content

Commit 3c35538

Browse files
committed
OpenSource connection to app
1 parent 784d741 commit 3c35538

File tree

1 file changed

+29
-95
lines changed

1 file changed

+29
-95
lines changed

HiGrowEsp32/HiGrowEsp32.ino

Lines changed: 29 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <WiFi.h>
22
#include <HTTPClient.h>
3-
#include <Preferences.h>
43
#include "DHT.h"
54
#include <ESP.h>
65

@@ -9,9 +8,8 @@
98
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
109
#define uS_TO_S_FACTOR 1000000
1110

12-
bool wifi_setup_mode = true;
13-
14-
Preferences preferences;
11+
unsigned long now;
12+
int DEEPSLEEP_SECONDS = 1800;
1513

1614
WiFiServer server(80);
1715

@@ -23,6 +21,8 @@ long timeout;
2321

2422
const int dhtpin = 22;
2523
const int soilpin = 32;
24+
const int POWER_PIN = 34;
25+
const int LIGHT_PIN = 33;
2626

2727
// Initialize DHT sensor.
2828
DHT dht(dhtpin, DHTTYPE);
@@ -35,8 +35,8 @@ static char humidityTemp[7];
3535
char linebuf[80];
3636
int charcount=0;
3737

38-
String ssid;
39-
String pwd;
38+
String ssid = "YOURSSID";
39+
String pwd = "YOURPWD";
4040

4141
char deviceid[21];
4242

@@ -49,119 +49,46 @@ void setup() {
4949
}
5050
esp_deep_sleep_enable_timer_wakeup(1800 * uS_TO_S_FACTOR);
5151
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
52-
preferences.begin("higrow", false);
5352

5453
pinMode(16, OUTPUT);
55-
pinMode(34, INPUT);
54+
pinMode(POWER_PIN, INPUT);
5655
digitalWrite(16, LOW);
5756

58-
wifi_setup_mode = digitalRead(34)==HIGH;
59-
6057
timeout = 0;
6158

6259
chipid = ESP.getEfuseMac();
6360
sprintf(deviceid, "%" PRIu64, chipid);
61+
Serial.print("DeviceId: ");
6462
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-
10663
}
10764

10865
void loop() {
10966

11067
char body[1024];
11168
digitalWrite(16, LOW); //switched on
11269

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();
13077

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-
}
15778
}
15879

15980
void sensorsData(char* body){
16081

16182
//This section read sensors
16283
timeout = millis();
16384

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);
16592

16693
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
16794
float humidity = dht.readHumidity();
@@ -174,15 +101,22 @@ void sensorsData(char* body){
174101

175102
String did = String(deviceid);
176103
String water = String((int)waterlevel);
104+
String light = String((int)lightlevel);
177105

178106
strcpy(body, "{\"deviceId\":\"");
179107
strcat(body, did.c_str());
180108
strcat(body, "\",\"water\":\"");
181109
strcat(body, water.c_str());
110+
strcat(body, "\",\"light\":\"");
111+
strcat(body, light.c_str());
182112
strcat(body, "\",\"humidity\":\"");
183113
strcat(body, humidityTemp);
184114
strcat(body, "\",\"temperature\":\"");
185115
strcat(body, celsiusTemp);
186116
strcat(body, "\"}");
117+
118+
if(lightlevel<100){
119+
DEEPSLEEP_SECONDS = 10800;
120+
}
187121
}
188122

0 commit comments

Comments
 (0)