Skip to content

Instantly share code, notes, and snippets.

@wilberforce
Created April 29, 2019 03:11
Show Gist options
  • Save wilberforce/dfbc330386fa46a763ac52172b2f9b8b to your computer and use it in GitHub Desktop.
Save wilberforce/dfbc330386fa46a763ac52172b2f9b8b to your computer and use it in GitHub Desktop.
webthing-led
/*
* Copyright (c) 2018 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK.
*
* This work is licensed under the
* Creative Commons Attribution 4.0 International License.
* To view a copy of this license, visit
* <http://creativecommons.org/licenses/by/4.0>.
* or send a letter to Creative Commons, PO Box 1866,
* Mountain View, CA 94042, USA.
*
*/
import Timer from "timer";
import Digital from "pins/digital";
import Monitor from "monitor";
import config from "mc/config";
import MDNS from "mdns";
import {WebThings, WebThing} from "webthings";
const HOSTNAMES = ["modled", "modledtoggle"];
class OnOffLight extends WebThing {
constructor(host) {
super(host);
this.state = true;
}
get on() {
return this.state;
}
set on(state) {
trace(`State: ${state}\n`);
if (state == this.state) return;
this.state = state;
led1.write(state);
this.changed();
//application.distribute("onLightUpdate", state);
}
static get description() {
return {
"@context": "https://iot.mozilla.org/schemas",
"@type": ["Light", "OnOffSwitch"],
name: "light",
description: "On/off light",
properties: {
on: {
type: "boolean",
description: "light state",
txt: "on"
}
}
}
}
}
let things;
function onHostname( hostname) {
trace(`Got hostname ${hostname}.\n`);
things = new WebThings(mdns);
things.add(OnOffLight);
let monitor1 = new Monitor({pin: config.button1_pin, mode: Digital.InputPullUp, edge: Monitor.Falling});
monitor1.onChanged = function() {
trace(`Button 1: state: ${this.read()}\n`);
toggle();
}
function toggle() {
let old_state=things.things[0].instance["on"];
trace(`Old state: ${old_state}\n`);
if ( things ) {
things.things[0].instance["on"] = !old_state;
}
}
toggle();
let count = 0;
Timer.repeat(() => {
trace(`repeat ${++count} \n`);
toggle();
}, 15000);
}
let index=0;
let mdns = new MDNS({hostName: HOSTNAMES[index]}, (message, value) => {
switch (message) {
case 1:
if ("" !== value) onHostname(value);
break;
case 2:
index++;
if (index < HOSTNAMES.length) {
trace(`Failed to get hostname ${value}...\n`);
return HOSTNAMES[index];
} else {
trace(`Failed to get a hostname. Stopping.\n`);
return -1;
}
break;
}
});
// let state=0;
let led1 = new Digital({pin: config.led1_pin, mode: Digital.Output});
//led1.write(state);
let keys = Object.keys(config);
keys.forEach(key => trace(`key: ${key}, value: ${config[key]}\n`));
trace("\n");
{
"include": [
"$(MODDABLE)/examples/manifest_base.json",
"$(MODDABLE)/examples/manifest_net.json",
"$(MODDABLE)/modules/network/mdns/manifest.json",
"$(MODDABLE)/modules/network/webthings/manifest.json",
"$(MODULES)/pins/digital/manifest.json",
],
"modules": {
"*": [
"$(MODULES)/pins/digital/monitor/*",
"./main",
],
},
"platforms": {
"esp32": {
"config": {
"led1_pin": 21,
"button1_pin": 0,
},
"modules": {
"*": "$(MODULES)/pins/digital/monitor/esp32/*",
},
},
"esp": {
"config": {
"led1_pin": 2,
"button1_pin": 0,
},
"modules": {
"*": "$(MODULES)/pins/digital/monitor/esp/*",
},
},
"...": {
"error": "need button and led pins"
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment