This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
HiGrowEsp32.ino
122 lines (91 loc) · 2.85 KB
/
HiGrowEsp32.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHT.h"
#include <ESP.h>
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
#define uS_TO_S_FACTOR 1000000LL
unsigned long now;
int DEEPSLEEP_SECONDS = 1800;
WiFiServer server(80);
HTTPClient http;
uint64_t chipid;
long timeout;
const int dhtpin = 22;
const int soilpin = 32;
const int POWER_PIN = 34;
const int LIGHT_PIN = 33;
// Initialize DHT sensor.
DHT dht(dhtpin, DHTTYPE);
// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
// Client variables
char linebuf[80];
int charcount=0;
String ssid = "YOURSSID";
String pwd = "YOURPWD";
char deviceid[21];
void setup() {
dht.begin();
Serial.begin(115200);
while(!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
esp_deep_sleep_enable_timer_wakeup(1800 * uS_TO_S_FACTOR);
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
pinMode(16, OUTPUT);
pinMode(POWER_PIN, INPUT);
digitalWrite(16, LOW);
timeout = 0;
chipid = ESP.getEfuseMac();
sprintf(deviceid, "%" PRIu64, chipid);
Serial.print("DeviceId: ");
Serial.println(deviceid);
}
void loop() {
char body[1024];
digitalWrite(16, LOW); //switched on
sensorsData(body);
http.begin("http://api.higrow.tech/api/records");
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(body);
Serial.println(httpResponseCode);
esp_sleep_enable_timer_wakeup(DEEPSLEEP_SECONDS * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void sensorsData(char* body){
//This section read sensors
timeout = millis();
int waterlevel = analogRead(soilpin);
int lightlevel = analogRead(LIGHT_PIN);
waterlevel = map(waterlevel, 0, 4095, 0, 1023);
waterlevel = constrain(waterlevel, 0, 1023);
lightlevel = map(lightlevel, 0, 4095, 0, 1023);
lightlevel = constrain(lightlevel, 0, 1023);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
float hic = dht.computeHeatIndex(temperature, humidity, false);
dtostrf(hic, 6, 2, celsiusTemp);
dtostrf(humidity, 6, 2, humidityTemp);
String did = String(deviceid);
String water = String((int)waterlevel);
String light = String((int)lightlevel);
strcpy(body, "{\"deviceId\":\"");
strcat(body, did.c_str());
strcat(body, "\",\"water\":\"");
strcat(body, water.c_str());
strcat(body, "\",\"light\":\"");
strcat(body, light.c_str());
strcat(body, "\",\"humidity\":\"");
strcat(body, humidityTemp);
strcat(body, "\",\"temperature\":\"");
strcat(body, celsiusTemp);
strcat(body, "\"}");
if(lightlevel<100){
DEEPSLEEP_SECONDS = 10800;
}
}