Added MQTT and made sample_time configurable.
This commit is contained in:
parent
c4f76112c3
commit
a3e98e676e
76
fs/init.js
76
fs/init.js
|
@ -9,6 +9,7 @@ load('api_esp32.js');
|
|||
load('api_dht.js');
|
||||
load('api_adc.js');
|
||||
load('api_rpc.js');
|
||||
load('api_mqtt.js');
|
||||
|
||||
// Pins
|
||||
let resetPin = 0;
|
||||
|
@ -33,10 +34,11 @@ GPIO.set_int_handler(resetPin, GPIO.INT_EDGE_NEG, function(resetPin) {
|
|||
|
||||
// enable bluetooth
|
||||
Cfg.set({bt:{enable:true}});
|
||||
// disable and clear wifi-config
|
||||
Cfg.set({wifi:{sta:{enable:false}}});
|
||||
Cfg.set({wifi:{ap:{enable:false}}});
|
||||
Cfg.set({wifi:{sta:{ssid:'',pass:''}}});
|
||||
// disable wifi
|
||||
Cfg.set({wifi:{sta:{enable:true}}});
|
||||
Cfg.set({wifi:{ap:{enable:true}}});
|
||||
// clear wifi-config
|
||||
// Cfg.set({wifi:{sta:{ssid:'',pass:''}}});
|
||||
|
||||
Sys.reboot(1000);
|
||||
}, null);
|
||||
|
@ -53,22 +55,33 @@ if (deviceId === "")
|
|||
deviceId = Cfg.get("higrow.deviceId");
|
||||
Cfg.set("device.id", deviceId);
|
||||
}
|
||||
let mqttTopic = deviceId + '/sample';
|
||||
let mqttPowerOnTopic = deviceId + '/poweron';
|
||||
let sendMqtt = Cfg.get("higrow.send_mqtt");
|
||||
let sendHttp = Cfg.get("higrow.send_http");
|
||||
let sendHttpUrl = Cfg.get("higrow.send_http_url");
|
||||
|
||||
let connected = false;
|
||||
let readSensors = Timer.set(5000, Timer.REPEAT, function() {
|
||||
print("HTTP: ", sendHttp, "URL:", sendHttpUrl);
|
||||
print("MQTT:", sendMqtt, "Topic:", mqttTopic, "PowerOn Topic:", mqttPowerOnTopic);
|
||||
let netConnected = false;
|
||||
let mqttConnected = false;
|
||||
let sampleTime_ms = Cfg.get("higrow.sample_time") * 1000;
|
||||
|
||||
let readSensors = Timer.set(sampleTime_ms, Timer.REPEAT, function() {
|
||||
let t = dht.getTemp();
|
||||
let h = dht.getHumidity();
|
||||
let m = ADC.read(moisturePin);
|
||||
|
||||
print("DeviceId:", deviceId, "Temperature:", t, "Humidity:", h, "Moisture:", m);
|
||||
|
||||
if (deviceId !== "" && connected)
|
||||
if (deviceId !== "" && netConnected)
|
||||
{
|
||||
GPIO.write(statusLightPin, 0);
|
||||
let jsonData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
|
||||
if (sendHttp) {
|
||||
HTTP.query({
|
||||
headers: {'Content-Type' : 'application/json'},
|
||||
url: 'http://httpbin.org/post', // replace with your own endpoint
|
||||
url: sendHttpUrl,
|
||||
data: jsonData,
|
||||
success: function(body, full_http_msg)
|
||||
{
|
||||
|
@ -82,13 +95,21 @@ let readSensors = Timer.set(5000, Timer.REPEAT, function() {
|
|||
//ESP32.deepSleep(30000000); // 30 seconds
|
||||
},
|
||||
});
|
||||
print("Sent HTTP message: ", jsonData);
|
||||
}
|
||||
|
||||
if (sendMqtt && mqttConnected) {
|
||||
let jsonDataString = JSON.stringify(jsonData);
|
||||
print("Try to send MQTT message: ", jsonDataString, "To:", mqttTopic);
|
||||
MQTT.pub(mqttTopic, jsonDataString, 1, true);
|
||||
}
|
||||
|
||||
GPIO.write(statusLightPin, 1);
|
||||
//Timer.del(readSensors);
|
||||
}
|
||||
else
|
||||
{
|
||||
print("DeviceId:",deviceId,"Connected:",connected);
|
||||
print("DeviceId:", deviceId, "Net-Con:", netConnected, "MQTT-Con:", mqttConnected);
|
||||
GPIO.write(statusLightPin, 1);
|
||||
}
|
||||
|
||||
|
@ -130,21 +151,48 @@ RPC.addHandler('HG.StatusLED.Off', function(args){
|
|||
|
||||
// Monitor network connectivity.
|
||||
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
|
||||
let status = true && connected;
|
||||
let status = true && netConnected;
|
||||
let evs = '???';
|
||||
if (ev === Net.STATUS_DISCONNECTED) {
|
||||
evs = 'DISCONNECTED';
|
||||
connected = false;
|
||||
netConnected = false;
|
||||
} else if (ev === Net.STATUS_CONNECTING) {
|
||||
evs = 'CONNECTING';
|
||||
connected = false;
|
||||
netConnected = false;
|
||||
} else if (ev === Net.STATUS_CONNECTED) {
|
||||
evs = 'CONNECTED';
|
||||
connected = false;
|
||||
netConnected = false;
|
||||
} else if (ev === Net.STATUS_GOT_IP) {
|
||||
evs = 'GOT_IP';
|
||||
connected = true;
|
||||
netConnected = true;
|
||||
}
|
||||
|
||||
print(evs);
|
||||
}, null);
|
||||
|
||||
let publishedMessageCount = 0;
|
||||
let subscribedMessageCount = 0;
|
||||
|
||||
// Monitor MQTT connectivity.
|
||||
MQTT.setEventHandler(function(conn, ev, evdata) {
|
||||
let mqttStatus = '???';
|
||||
|
||||
if (ev === MQTT.EV_CONNACK) {
|
||||
mqttStatus = 'CONNECTED';
|
||||
if (!mqttConnected) {
|
||||
MQTT.pub(mqttPowerOnTopic, "HiGrow sensor booted", 1, true);
|
||||
}
|
||||
mqttConnected = true;
|
||||
} else if (ev === MQTT.EV_PUBLISH) {
|
||||
subscribedMessageCount++;
|
||||
} else if (ev === MQTT.EV_PUBACK) {
|
||||
publishedMessageCount++;
|
||||
} else if (ev === MQTT.EV_CLOSE) {
|
||||
mqttStatus = 'DISCONNECTED';
|
||||
netConnected = false;
|
||||
}
|
||||
|
||||
if (mqttStatus !== '???') {
|
||||
print("MQTT-Status:", mqttStatus, "Msg-Sub:", subscribedMessageCount, "Msg-Pub:", publishedMessageCount);
|
||||
}
|
||||
}, null);
|
||||
|
|
9
mos.yml
9
mos.yml
|
@ -8,6 +8,8 @@ libs_version: ${mos.version}
|
|||
modules_version: ${mos.version}
|
||||
mongoose_os_version: ${mos.version}
|
||||
|
||||
platform: esp32
|
||||
|
||||
config_schema:
|
||||
- ["higrow", "o", {title: "LilyGo HiGrow ESP32 Plant Sensor v1 App Settings"}]
|
||||
- ["higrow.deviceId", "s", "", {title: "DeviceId"}]
|
||||
|
@ -21,6 +23,11 @@ config_schema:
|
|||
- ["wifi.ap.enable", false]
|
||||
- ["device.id", "HiGrow_??????"]
|
||||
- ["bt.dev_name", "HiGrowBT_??????"]
|
||||
- ["mqtt.enable", true]
|
||||
- ["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", 10, {title: "Take a sample after that amount of seconds"}]
|
||||
|
||||
filesystem:
|
||||
- fs
|
||||
|
@ -31,10 +38,12 @@ libs:
|
|||
- origin: https://github.com/mongoose-os-libs/rpc-service-config
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-service-fs
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-uart
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-mqtt
|
||||
- origin: https://github.com/mongoose-os-libs/wifi
|
||||
- origin: https://github.com/mongoose-os-libs/dht
|
||||
- origin: https://github.com/mongoose-os-libs/mjs
|
||||
- origin: https://github.com/mongoose-os-libs/adc
|
||||
- origin: https://github.com/mongoose-os-libs/mqtt
|
||||
|
||||
tags:
|
||||
- js
|
||||
|
|
Loading…
Reference in New Issue