Skip to content

Instantly share code, notes, and snippets.

@alvarowolfx
Created October 15, 2017 22:30
Show Gist options
  • Save alvarowolfx/3faa2079bfa6c30beefcdb2db0b670ce to your computer and use it in GitHub Desktop.
Save alvarowolfx/3faa2079bfa6c30beefcdb2db0b670ce to your computer and use it in GitHub Desktop.
Weather station code
load('api_config.js');
load('api_mqtt.js');
load('api_timer.js');
load('api_dht.js');
load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');
load('api_dht.js');
let deviceName = Cfg.get('device.id');
let topic = '/devices/' + deviceName + '/events';
print('Topic: ', topic);
let isConnected = false;
let dhtPin = Cfg.get('app.dht');
let dht = DHT.create(dhtPin, DHT.DHT11);
let getInfo = function() {
return JSON.stringify({
total_ram: Sys.total_ram() / 1024,
free_ram: Sys.free_ram() / 1024,
temp: dht.getTemp(),
hum: dht.getHumidity()
});
};
Timer.set(
60 * 1000,
true,
function() {
if (isConnected) {
publishData();
}
},
null
);
Timer.set(
5000,
true,
function() {
print('Info:', getInfo());
},
null
);
MQTT.setEventHandler(function(conn, ev) {
if (ev === MQTT.EV_CONNACK) {
print('CONNECTED');
isConnected = true;
publishData();
}
}, null);
function publishData() {
let ok = MQTT.pub(topic, getInfo());
if (ok) {
print('Published');
} else {
print('Error publishing');
}
}
// Monitor network connectivity.
Net.setStatusEventHandler(function(ev, arg) {
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
evs = 'DISCONNECTED';
} else if (ev === Net.STATUS_CONNECTING) {
evs = 'CONNECTING';
} else if (ev === Net.STATUS_CONNECTED) {
evs = 'CONNECTED';
} else if (ev === Net.STATUS_GOT_IP) {
evs = 'GOT_IP';
}
print('== Net event:', ev, evs);
}, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment