Added deep sleep
This commit is contained in:
parent
8f0a94734e
commit
a140597aa8
|
@ -44,7 +44,8 @@ config_schema:
|
|||
- ["higrow.send_mqtt", "b", true, {title: "Send data using MQTT"}]
|
||||
- ["higrow.send_http", "b", false, {title: "Send data using HTTP"}]
|
||||
- ["higrow.send_http_url", "s", "http://example.org/what/ever", {title: "Send data using HTTP"}]
|
||||
- ["higrow.sample_time", "i", 60, {title: "Take a sample after that amount of seconds"}]
|
||||
- ["higrow.sample_time", "i", 300, {title: "Take a sample after that amount of seconds"}]
|
||||
- ["higrow.wait_after_last_action_before_sleep", "i", 10, {title: "Wait this number of seconds before we go into deep sleep"}]
|
||||
|
||||
To change the configuration it is recommended to use either UART or MQTT communications.
|
||||
|
54
fs/init.js
54
fs/init.js
|
@ -11,6 +11,8 @@ load('api_adc.js');
|
|||
load('api_rpc.js');
|
||||
load('api_mqtt.js');
|
||||
|
||||
let esp_sleep_enable_ext0_wakeup = ffi("void esp_sleep_enable_ext0_wakeup(int, int)"); // (GpioPin, 0=LOW, 1=HIGH)
|
||||
|
||||
// Pins
|
||||
let resetPin = 0;
|
||||
let statusLightPin = 16;
|
||||
|
@ -36,7 +38,7 @@ GPIO.set_int_handler(resetPin, GPIO.INT_EDGE_NEG, function(resetPin) {
|
|||
Cfg.set({bt:{enable:true}});
|
||||
// disable wifi
|
||||
Cfg.set({wifi:{sta:{enable:true}}});
|
||||
Cfg.set({wifi:{ap:{enable:true}}});
|
||||
Cfg.set({wifi:{ap:{enable:false}}});
|
||||
// clear wifi-config
|
||||
// Cfg.set({wifi:{sta:{ssid:'',pass:''}}});
|
||||
|
||||
|
@ -66,15 +68,36 @@ print("MQTT:", sendMqtt, "Topic:", mqttTopic, "PowerOn Topic:", mqttPowerOnTopic
|
|||
let netConnected = false;
|
||||
let mqttConnected = false;
|
||||
let sampleTime_ms = Cfg.get("higrow.sample_time") * 1000;
|
||||
let httpReadyForDeepSleep = false;
|
||||
let mqttReadyForDeepSleep = false;
|
||||
let lastActionBeforeSleep = Sys.uptime(); // tracked to be able to wait some time after last action to be sure it is completed
|
||||
let waitAfterLastActionBeforeSleep = Cfg.get("higrow.wait_after_last_action_before_sleep");
|
||||
|
||||
let readSensors = Timer.set(sampleTime_ms, Timer.REPEAT, function() {
|
||||
esp_sleep_enable_ext0_wakeup(resetPin, 0);
|
||||
|
||||
function enterDeepSleepMode() {
|
||||
print("Entering deep sleep mode for", sampleTime_ms/1000, "seconds");
|
||||
ESP32.deepSleep(sampleTime_ms * 1000);
|
||||
// never reaches this here, except for a couple of milliseconds
|
||||
Sys.usleep(300000); // 300ms
|
||||
print("DID NOT ENTER DEEP SLEEP!");
|
||||
}
|
||||
|
||||
let readSensors = Timer.set(/*sampleTime_ms*/ 1000, Timer.REPEAT, function() {
|
||||
if ((!sendHttp || (sendHttp && httpReadyForDeepSleep)) && (!sendMqtt || (sendMqtt && mqttReadyForDeepSleep))) {
|
||||
if (Sys.uptime() > lastActionBeforeSleep + waitAfterLastActionBeforeSleep) {
|
||||
enterDeepSleepMode();
|
||||
}
|
||||
print("wait after last action before deep sleep");
|
||||
return; // do nothing for some time, but RPC still is available
|
||||
}
|
||||
let t = dht.getTemp();
|
||||
let h = dht.getHumidity();
|
||||
let m = ADC.read(moisturePin);
|
||||
|
||||
print("DeviceId:", deviceId, "Temperature:", t, "Humidity:", h, "Moisture:", m);
|
||||
print("DeviceId:", deviceId, "Temperature:", t, "Humidity:", h, "Moisture:", m, "DeepSleep:", httpReadyForDeepSleep, "/", mqttReadyForDeepSleep);
|
||||
|
||||
if (deviceId !== "" && netConnected)
|
||||
if (deviceId !== "" && (!sendHttp || (sendHttp && netConnected)) && (!sendMqtt || (sendMqtt && mqttConnected)))
|
||||
{
|
||||
GPIO.write(statusLightPin, 0);
|
||||
let jsonData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
|
||||
|
@ -85,23 +108,26 @@ let readSensors = Timer.set(sampleTime_ms, Timer.REPEAT, function() {
|
|||
data: jsonData,
|
||||
success: function(body, full_http_msg)
|
||||
{
|
||||
//print(body);
|
||||
// sleep for 15 seconds, then (re)boot up and do it all over again
|
||||
//ESP32.deepSleep(15000000); // 15 seconds
|
||||
print(body);
|
||||
httpReadyForDeepSleep = true;
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
},
|
||||
error: function(err)
|
||||
{
|
||||
print(err);
|
||||
//ESP32.deepSleep(30000000); // 30 seconds
|
||||
print("ERROR!", err);
|
||||
httpReadyForDeepSleep = true;
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
},
|
||||
});
|
||||
print("Sent HTTP message: ", jsonData);
|
||||
}
|
||||
|
||||
if (sendMqtt && mqttConnected) {
|
||||
if (sendMqtt) {
|
||||
let jsonDataString = JSON.stringify(jsonData);
|
||||
// print("Try to send MQTT message: ", jsonDataString, "To:", mqttTopic);
|
||||
MQTT.pub(mqttTopic, jsonDataString, 1, true);
|
||||
mqttReadyForDeepSleep = true;
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
}
|
||||
|
||||
GPIO.write(statusLightPin, 1);
|
||||
|
@ -117,18 +143,23 @@ let readSensors = Timer.set(sampleTime_ms, Timer.REPEAT, function() {
|
|||
|
||||
// RPC Handlers
|
||||
RPC.addHandler('HG.Temp.Read', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
return { value: dht.getTemp() };
|
||||
});
|
||||
RPC.addHandler('HG.Humidity.Read', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
return { value: dht.getHumidity() };
|
||||
});
|
||||
RPC.addHandler('HG.Moisture.Read', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
return { value: ADC.read(moisturePin) };
|
||||
});
|
||||
RPC.addHandler('HG.StatusLED.Read', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
return { value: GPIO.read(statusLightPin) };
|
||||
});
|
||||
RPC.addHandler('HG.StatusLED.On', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
GPIO.write(statusLightPin, 0);
|
||||
print("LED On");
|
||||
if (GPIO.read(statusLightPin) !== 0)
|
||||
|
@ -139,6 +170,7 @@ RPC.addHandler('HG.StatusLED.On', function(args){
|
|||
return true;
|
||||
});
|
||||
RPC.addHandler('HG.StatusLED.Off', function(args){
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
GPIO.write(statusLightPin, 1);
|
||||
if (GPIO.read(statusLightPin) !== 1)
|
||||
{
|
||||
|
@ -148,7 +180,6 @@ RPC.addHandler('HG.StatusLED.Off', function(args){
|
|||
return true;
|
||||
});
|
||||
|
||||
|
||||
// Monitor network connectivity.
|
||||
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
|
||||
let status = true && netConnected;
|
||||
|
@ -184,6 +215,7 @@ MQTT.setEventHandler(function(conn, ev, evdata) {
|
|||
}
|
||||
mqttConnected = true;
|
||||
} else if (ev === MQTT.EV_PUBLISH) {
|
||||
lastActionBeforeSleep = Sys.uptime();
|
||||
subscribedMessageCount++;
|
||||
} else if (ev === MQTT.EV_PUBACK) {
|
||||
publishedMessageCount++;
|
||||
|
|
8
mos.yml
8
mos.yml
|
@ -1,6 +1,7 @@
|
|||
author: Jason Harrell <info@latitude17.io>
|
||||
author: Dirk Jahnke <dirk.jahnke@mailbox.org>
|
||||
# forked from: Jason Harrell <info@latitude17.io>
|
||||
#original author Luca Fabbri <luca@higrow.tech>
|
||||
description: LilyGo HiGrow ESP32 Plant Sensor v1
|
||||
description: LilyGo HiGrow ESP32 Plant Sensor with MQTT (HW v1)
|
||||
# arch: PLATFORM
|
||||
version: 1.0
|
||||
|
||||
|
@ -27,7 +28,8 @@ config_schema:
|
|||
- ["higrow.send_mqtt", "b", true, {title: "Send data using MQTT"}]
|
||||
- ["higrow.send_http", "b", false, {title: "Send data using HTTP"}]
|
||||
- ["higrow.send_http_url", "s", "http://example.org/what/ever", {title: "Send data using HTTP"}]
|
||||
- ["higrow.sample_time", "i", 60, {title: "Take a sample after that amount of seconds"}]
|
||||
- ["higrow.sample_time", "i", 300, {title: "Take a sample after that amount of seconds"}]
|
||||
- ["higrow.wait_after_last_action_before_sleep", "i", 10, {title: "Wait this number of seconds before we go into deep sleep"}]
|
||||
|
||||
filesystem:
|
||||
- fs
|
||||
|
|
Loading…
Reference in New Issue