Skip to content

Instantly share code, notes, and snippets.

@craigsdennis
Last active October 19, 2016 19:17
Show Gist options
  • Save craigsdennis/8c8c4bd0ef25bdb21675b3281afae747 to your computer and use it in GitHub Desktop.
Save craigsdennis/8c8c4bd0ef25bdb21675b3281afae747 to your computer and use it in GitHub Desktop.
var shhh = "SECRETPASSWORD";
function hexToRgb(hex) {
// No RegEx in Espruino yet...
var R, G, B;
if (hex.length == 3) {
R = hex.substring(0, 1);
R = R + R;
G = hex.substring(1, 2);
G = G + G;
B = hex.substring(2, 3);
B = B + B;
} else {
R = hex.substring(0, 2);
G = hex.substring(2, 4);
B = hex.substring(4, 6);
}
return {
R: parseInt(R, 16),
G: parseInt(G, 16),
B: parseInt(B, 16)
};
}
// Usage
//E.on('init', function() {
var server = "192.168.128.62";
var mqtt = require("MQTT").create(server);
var wifi = require("Wifi");
function Witty() {
var self = this;
this.colorIntervalId = null;
this.colorDuration = 1000;
this.PIN_LDR = 0;
this.PIN_BUTTON = 4;
this.PIN_LED_R = 15;
this.PIN_LED_G = 12;
this.PIN_LED_B = 13;
this.buttonActions = [];
this._btnWatch = setWatch(
function(evt) {
for (var i = 0; i < self.buttonActions.length; i++) {
self.buttonActions[i].apply(self, [evt]);
}
},
this.PIN_BUTTON,
{repeat: true, edge: 'rising'}
);
// TODO:csd - Not working
this._ldrWatch = setWatch(
function(evt) {
console.log("LDR", evt);
},
this.PIN_LDR,
{repeat:true}
);
}
Witty.prototype.addButtonAction = function(fn) {
this.buttonActions.push(fn);
};
Witty.prototype.getPhotoResistance = function() {
return analogRead(0);
};
Witty.prototype.setLED = function(hex) {
console.log("Setting LED", hex);
var result = hexToRgb(hex);
console.log("Which is", result);
digitalWrite(this.PIN_LED_R, result.R);
digitalWrite(this.PIN_LED_G, result.G);
digitalWrite(this.PIN_LED_B, result.B);
};
Witty.prototype.setLEDTheme = function(colors) {
var self = this;
self.stopLEDTheme();
var index = 0;
this.setLED(colors[index]);
this.colorIntervalId = setInterval(function() {
index++;
if (index >= colors.length) {
index = 0;
}
self.setLED(colors[index]);
}, this.colorDuration);
};
Witty.prototype.stopLEDTheme = function(colors) {
if (this.colorIntervalId) {
clearInterval(this.colorIntervalId);
this.colorIntervalId = null;
}
};
var witty = new Witty();
witty.addButtonAction(function(evt) {
console.log("Button pressed", evt.state, evt.time, evt.lastTime);
console.log("Photo resistance is", witty.getPhotoResistance());
witty.stopLEDTheme();
});
witty.setLED("0f0");
wifi.connect("YourAccessPointName", {password: shhh}, function(err) {
if (err) throw err;
console.log("Wifi connected");
mqtt.connect();
});
mqtt.on('connected', function() {
console.log("mqtt connected");
mqtt.subscribe("witty");
});
mqtt.on('publish', function(pub) {
console.log("Topic:" + pub.topic);
console.log("Message:" + pub.message);
var message = JSON.parse(pub.message);
if (!message.colors || message.colors.length === 0) {
witty.stopLEDTheme();
return;
}
message.colors.push("000");
witty.setLEDTheme(message.colors);
});
//});
@stephenkeep
Copy link

Thanks @craigsdennis it's working perfectly!! Do you know how to access the GPIO pins?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment