From a3e98e676e0c93f4e98faeadc1cd8bd9a3449331 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Fri, 8 Jun 2018 16:44:14 +0200 Subject: [PATCH] Added MQTT and made sample_time configurable. --- fs/init.js | 108 ++++++++++++++++++++++++++++++++++++++--------------- mos.yml | 9 +++++ 2 files changed, 87 insertions(+), 30 deletions(-) diff --git a/fs/init.js b/fs/init.js index ab5225e..5c0abec 100755 --- a/fs/init.js +++ b/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,42 +55,61 @@ 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); + 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}; - HTTP.query({ - headers: {'Content-Type' : 'application/json'}, - url: 'http://httpbin.org/post', // replace with your own endpoint - 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 - }, - error: function(err) - { - print(err); - //ESP32.deepSleep(30000000); // 30 seconds - }, - }); + if (sendHttp) { + HTTP.query({ + headers: {'Content-Type' : 'application/json'}, + url: sendHttpUrl, + 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 + }, + error: function(err) + { + 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); //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); diff --git a/mos.yml b/mos.yml index 2125a96..60ab06d 100755 --- a/mos.yml +++ b/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