Implement WLAN connection and deep sleep

This commit is contained in:
coolneng 2023-03-30 01:28:31 +02:00
parent 6450fadd9a
commit 8658148646
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
4 changed files with 48 additions and 1 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.pio
src/credentials.h

View File

@ -1,3 +1,4 @@
#include "wlan.h"
#include <Arduino.h>
#include <DHT.h>
@ -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);
}

31
src/wlan.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "wlan.h"
#include "credentials.h"
#include <ESP8266WiFi.h>
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);
}

11
src/wlan.h Normal file
View File

@ -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 */