Implement WLAN connection and deep sleep
This commit is contained in:
parent
6450fadd9a
commit
8658148646
|
@ -1 +1,2 @@
|
||||||
.pio
|
.pio
|
||||||
|
src/credentials.h
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
|
|
||||||
|
@ -12,14 +13,17 @@ int soil_threshold = 40;
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
dht.begin();
|
dht.begin();
|
||||||
|
wlan_connection();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
char buffer[200];
|
unsigned long start_time = millis();
|
||||||
int analog_val = analogRead(fc28_pin);
|
int analog_val = analogRead(fc28_pin);
|
||||||
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
int soil_percentage = map(analog_val, 0, 1023, 0, 100);
|
||||||
|
char buffer[200];
|
||||||
sprintf(buffer, "Temperature: %.2f°C Humidity: %.2f%% Soil humidity: %i%%",
|
sprintf(buffer, "Temperature: %.2f°C Humidity: %.2f%% Soil humidity: %i%%",
|
||||||
dht.readTemperature(), dht.readHumidity(), soil_percentage);
|
dht.readTemperature(), dht.readHumidity(), soil_percentage);
|
||||||
Serial.println(buffer);
|
Serial.println(buffer);
|
||||||
delay(30000);
|
delay(30000);
|
||||||
|
enter_deep_sleep(start_time);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -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 */
|
Loading…
Reference in New Issue