diff --git a/.gitignore b/.gitignore index 03f4a3c..6c9187e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .pio +src/credentials.h diff --git a/src/main.cpp b/src/main.cpp index 540ffbe..f960cab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "wlan.h" #include #include @@ -12,14 +13,17 @@ int soil_threshold = 40; void setup() { Serial.begin(9600); dht.begin(); + wlan_connection(); } void loop() { - char buffer[200]; + unsigned long start_time = millis(); int analog_val = analogRead(fc28_pin); int soil_percentage = map(analog_val, 0, 1023, 0, 100); + char buffer[200]; sprintf(buffer, "Temperature: %.2f°C Humidity: %.2f%% Soil humidity: %i%%", dht.readTemperature(), dht.readHumidity(), soil_percentage); Serial.println(buffer); delay(30000); + enter_deep_sleep(start_time); } diff --git a/src/wlan.cpp b/src/wlan.cpp new file mode 100644 index 0000000..ed9572b --- /dev/null +++ b/src/wlan.cpp @@ -0,0 +1,31 @@ +#include "wlan.h" +#include "credentials.h" +#include + +void wlan_connection() { + if (WiFi.SSID() != SSID) { + WiFi.begin(SSID, PSK); + WiFi.persistent(true); + WiFi.setAutoConnect(true); + WiFi.setAutoReconnect(true); + } + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("WiFi connected"); + Serial.println(WiFi.localIP()); +} + +void mqtt_connection(char *server, int port, char *fingerprint) { + WiFiClientSecure client; + client.connect(server, port); +} + +void enter_deep_sleep(const int start_time) { + int elapsed = millis() - start_time; + if (elapsed >= WIFI_TIMEOUT) { + WiFi.disconnect(); + } + ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT); +} diff --git a/src/wlan.h b/src/wlan.h new file mode 100644 index 0000000..05e2c8f --- /dev/null +++ b/src/wlan.h @@ -0,0 +1,11 @@ +#ifndef WLAN_H +#define WLAN_H + +const int SLEEP_TIME = 480000000; +const int WIFI_TIMEOUT = 10000; + +void wlan_connection(); +void prometheus_connection(char *server, int port, char *fingerprint); +void enter_deep_sleep(const int start_time); + +#endif /* WLAN_H */