Added MQTT and made sample_time configurable.

This commit is contained in:
Dirk Jahnke 2018-06-08 16:44:14 +02:00
parent c4f76112c3
commit a3e98e676e
2 changed files with 87 additions and 30 deletions

View File

@ -9,6 +9,7 @@ load('api_esp32.js');
load('api_dht.js'); load('api_dht.js');
load('api_adc.js'); load('api_adc.js');
load('api_rpc.js'); load('api_rpc.js');
load('api_mqtt.js');
// Pins // Pins
let resetPin = 0; let resetPin = 0;
@ -33,10 +34,11 @@ GPIO.set_int_handler(resetPin, GPIO.INT_EDGE_NEG, function(resetPin) {
// enable bluetooth // enable bluetooth
Cfg.set({bt:{enable:true}}); Cfg.set({bt:{enable:true}});
// disable and clear wifi-config // disable wifi
Cfg.set({wifi:{sta:{enable:false}}}); Cfg.set({wifi:{sta:{enable:true}}});
Cfg.set({wifi:{ap:{enable:false}}}); Cfg.set({wifi:{ap:{enable:true}}});
Cfg.set({wifi:{sta:{ssid:'',pass:''}}}); // clear wifi-config
// Cfg.set({wifi:{sta:{ssid:'',pass:''}}});
Sys.reboot(1000); Sys.reboot(1000);
}, null); }, null);
@ -53,42 +55,61 @@ if (deviceId === "")
deviceId = Cfg.get("higrow.deviceId"); deviceId = Cfg.get("higrow.deviceId");
Cfg.set("device.id", 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; print("HTTP: ", sendHttp, "URL:", sendHttpUrl);
let readSensors = Timer.set(5000, Timer.REPEAT, function() { 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 t = dht.getTemp();
let h = dht.getHumidity(); let h = dht.getHumidity();
let m = ADC.read(moisturePin); let m = ADC.read(moisturePin);
print("DeviceId:",deviceId,"Temperature:",t,"Humidity:",h,"Moisture:",m); print("DeviceId:", deviceId, "Temperature:", t, "Humidity:", h, "Moisture:", m);
if (deviceId !== "" && connected) if (deviceId !== "" && netConnected)
{ {
GPIO.write(statusLightPin, 0); GPIO.write(statusLightPin, 0);
let jsonData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m}; let jsonData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
HTTP.query({ if (sendHttp) {
headers: {'Content-Type' : 'application/json'}, HTTP.query({
url: 'http://httpbin.org/post', // replace with your own endpoint headers: {'Content-Type' : 'application/json'},
data: jsonData, url: sendHttpUrl,
success: function(body, full_http_msg) data: jsonData,
{ success: function(body, full_http_msg)
//print(body); {
// sleep for 15 seconds, then (re)boot up and do it all over again //print(body);
//ESP32.deepSleep(15000000); // 15 seconds // sleep for 15 seconds, then (re)boot up and do it all over again
}, //ESP32.deepSleep(15000000); // 15 seconds
error: function(err) },
{ error: function(err)
print(err); {
//ESP32.deepSleep(30000000); // 30 seconds print(err);
}, //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); GPIO.write(statusLightPin, 1);
//Timer.del(readSensors); //Timer.del(readSensors);
} }
else else
{ {
print("DeviceId:",deviceId,"Connected:",connected); print("DeviceId:", deviceId, "Net-Con:", netConnected, "MQTT-Con:", mqttConnected);
GPIO.write(statusLightPin, 1); GPIO.write(statusLightPin, 1);
} }
@ -130,21 +151,48 @@ RPC.addHandler('HG.StatusLED.Off', function(args){
// Monitor network connectivity. // Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) { Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
let status = true && connected; let status = true && netConnected;
let evs = '???'; let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) { if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED'; evs = 'DISCONNECTED';
connected = false; netConnected = false;
} else if (ev === Net.STATUS_CONNECTING) { } else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING'; evs = 'CONNECTING';
connected = false; netConnected = false;
} else if (ev === Net.STATUS_CONNECTED) { } else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED'; evs = 'CONNECTED';
connected = false; netConnected = false;
} else if (ev === Net.STATUS_GOT_IP) { } else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP'; evs = 'GOT_IP';
connected = true; netConnected = true;
} }
print(evs); print(evs);
}, null); }, 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);

View File

@ -8,6 +8,8 @@ libs_version: ${mos.version}
modules_version: ${mos.version} modules_version: ${mos.version}
mongoose_os_version: ${mos.version} mongoose_os_version: ${mos.version}
platform: esp32
config_schema: config_schema:
- ["higrow", "o", {title: "LilyGo HiGrow ESP32 Plant Sensor v1 App Settings"}] - ["higrow", "o", {title: "LilyGo HiGrow ESP32 Plant Sensor v1 App Settings"}]
- ["higrow.deviceId", "s", "", {title: "DeviceId"}] - ["higrow.deviceId", "s", "", {title: "DeviceId"}]
@ -21,6 +23,11 @@ config_schema:
- ["wifi.ap.enable", false] - ["wifi.ap.enable", false]
- ["device.id", "HiGrow_??????"] - ["device.id", "HiGrow_??????"]
- ["bt.dev_name", "HiGrowBT_??????"] - ["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: filesystem:
- fs - 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-config
- origin: https://github.com/mongoose-os-libs/rpc-service-fs - 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-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/wifi
- origin: https://github.com/mongoose-os-libs/dht - origin: https://github.com/mongoose-os-libs/dht
- origin: https://github.com/mongoose-os-libs/mjs - origin: https://github.com/mongoose-os-libs/mjs
- origin: https://github.com/mongoose-os-libs/adc - origin: https://github.com/mongoose-os-libs/adc
- origin: https://github.com/mongoose-os-libs/mqtt
tags: tags:
- js - js