Skip to content

Instantly share code, notes, and snippets.

@CuyiGuaton
Created January 20, 2018 15:59
Show Gist options
  • Save CuyiGuaton/4d0efe55a82e01020e541b09a66c2e00 to your computer and use it in GitHub Desktop.
Save CuyiGuaton/4d0efe55a82e01020e541b09a66c2e00 to your computer and use it in GitHub Desktop.
Mongoose os conection to AWS IOT
load('api_mqtt.js');
load('api_gpio.js');
load("api_adc.js");
load('api_timer.js');
load('api_config.js');
load('api_net.js');
let pin = 0, topic = 'my/topic';
let value = ADC.enable(0);
print('bool:', value);
Timer.set(1000 /* 1 sec */, true /* repeat */, function() {
//GPIO.set_button_handler(pin, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
let message = JSON.stringify({
analog: ADC.read(0),
date: Timer.now(),
DeviceID: Cfg.get('device.id')
});
//print(message);
let ok = MQTT.pub('my/topic', message);
print(ok ? 'Tick' : 'Tock', ok ? 'yes' : 'no');
if(ok){
GPIO.set_mode(5, GPIO.MODE_OUTPUT);
GPIO.write(5, 1);
GPIO.write(5, 0);
}
}, null);
MQTT.sub('my/topic2', function(conn, topic, msg)
{
let m = JSON.parse(msg);
let led = m.led;
let state = m.state;
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
GPIO.write(led, state);
print('Pin:', led, 'State:', state);
}, null);
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
GPIO.set_mode(2, GPIO.MODE_OUTPUT);
let evs = '???';
if (ev === Net.STATUS_DISCONNECTED) {
GPIO.write(2, 1);
evs = 'DISCONNECTED';
} else if (ev === Net.STATUS_CONNECTING) {
GPIO.write(2, 0);
GPIO.write(2, 1);
evs = 'CONNECTING';
} else if (ev === Net.STATUS_CONNECTED) {
GPIO.write(2, 0);
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