LiliyGo-HiGrow-Sensor/fs/init.js

199 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-02-18 05:47:44 +00:00
load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_http.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');
load('api_esp32.js');
load('api_dht.js');
load('api_adc.js');
load('api_rpc.js');
load('api_mqtt.js');
2018-02-18 05:47:44 +00:00
2018-02-20 07:44:16 +00:00
// Pins
2018-02-18 05:47:44 +00:00
let resetPin = 0;
2018-02-20 07:44:16 +00:00
let statusLightPin = 16;
2018-02-18 05:47:44 +00:00
let dhtPin = 22;
let moisturePin = 32;
// Turn on status led
2018-02-20 07:44:16 +00:00
GPIO.set_mode(statusLightPin, GPIO.MODE_OUTPUT);
GPIO.write(statusLightPin, 0);
2018-02-18 05:47:44 +00:00
2018-02-20 07:44:16 +00:00
// Reset Handler
2018-02-18 05:47:44 +00:00
GPIO.set_mode(resetPin, GPIO.MODE_INPUT);
GPIO.set_int_handler(resetPin, GPIO.INT_EDGE_NEG, function(resetPin) {
print('Pin', resetPin, 'got interrupt');
for (let i=0; i<=5; i++)
{
GPIO.toggle(statusLightPin);
Sys.usleep(200000);
}
2018-03-15 23:08:16 +00:00
GPIO.write(statusLightPin, 1);
// enable bluetooth
2018-02-18 05:47:44 +00:00
Cfg.set({bt:{enable:true}});
// disable wifi
Cfg.set({wifi:{sta:{enable:true}}});
Cfg.set({wifi:{ap:{enable:true}}});
// clear wifi-config
// Cfg.set({wifi:{sta:{ssid:'',pass:''}}});
2018-02-18 05:47:44 +00:00
Sys.reboot(1000);
}, null);
print("Starting...");
GPIO.enable_int(resetPin);
2018-02-18 05:47:44 +00:00
ADC.enable(moisturePin);
let dht = DHT.create(dhtPin, DHT.DHT11);
let deviceId = Cfg.get("device.id");
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");
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() {
2018-02-18 05:47:44 +00:00
let t = dht.getTemp();
let h = dht.getHumidity();
let m = ADC.read(moisturePin);
print("DeviceId:", deviceId, "Temperature:", t, "Humidity:", h, "Moisture:", m);
2018-02-18 05:47:44 +00:00
if (deviceId !== "" && netConnected)
2018-02-18 05:47:44 +00:00
{
GPIO.write(statusLightPin, 0);
let jsonData = {'DeviceId': deviceId, 'Temperature': t, 'Humidity': h, 'Moisture': m};
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);
}
2018-02-18 05:47:44 +00:00
GPIO.write(statusLightPin, 1);
2018-02-18 05:47:44 +00:00
//Timer.del(readSensors);
}
else
{
print("DeviceId:", deviceId, "Net-Con:", netConnected, "MQTT-Con:", mqttConnected);
2018-03-15 23:08:16 +00:00
GPIO.write(statusLightPin, 1);
}
2018-02-18 05:47:44 +00:00
}, null);
// RPC Handlers
RPC.addHandler('HG.Temp.Read', function(args){
return { value: dht.getTemp() };
});
RPC.addHandler('HG.Humidity.Read', function(args){
return { value: dht.getHumidity() };
});
RPC.addHandler('HG.Moisture.Read', function(args){
return { value: ADC.read(moisturePin) };
});
RPC.addHandler('HG.StatusLED.Read', function(args){
return { value: GPIO.read(statusLightPin) };
});
2018-02-18 05:47:44 +00:00
RPC.addHandler('HG.StatusLED.On', function(args){
2018-02-20 07:44:16 +00:00
GPIO.write(statusLightPin, 0);
2018-02-18 05:47:44 +00:00
print("LED On");
2018-02-20 07:44:16 +00:00
if (GPIO.read(statusLightPin) !== 0)
2018-02-18 05:47:44 +00:00
{
return false;
}
return true;
});
RPC.addHandler('HG.StatusLED.Off', function(args){
2018-02-20 07:44:16 +00:00
GPIO.write(statusLightPin, 1);
if (GPIO.read(statusLightPin) !== 1)
2018-02-18 05:47:44 +00:00
{
return false;
}
return true;
});
// Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
let status = true && netConnected;
2018-02-18 05:47:44 +00:00
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED';
netConnected = false;
2018-02-18 05:47:44 +00:00
} else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING';
netConnected = false;
2018-02-18 05:47:44 +00:00
} else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED';
netConnected = false;
2018-02-18 05:47:44 +00:00
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
netConnected = true;
2018-02-18 05:47:44 +00:00
}
print(evs);
2018-02-18 05:47:44 +00:00
}, 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);