Skip to content

Instantly share code, notes, and snippets.

@mezelve
Created November 19, 2012 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mezelve/4111064 to your computer and use it in GitHub Desktop.
Save mezelve/4111064 to your computer and use it in GitHub Desktop.
Control a ThingM BlinkM MaxM with an Electric Imp
// BlinkM MaxM controlled by a web form
// http://thingm.com/products/blinkm-maxm.html
server.log("BlinkM Started");
local addr = 0x09;
local rgb = null;
local i2cPort = null;
// Hardware Configuration
//pin 8 -> SCL -> blinkm c
//pin 9 -> SDA -> blinkm d
hardware.configure(I2C_89);
hardware.i2c89.configure(CLOCK_SPEED_100_KHZ);
i2cPort = hardware.i2c89;
// Convert hex string to an integer
//borrowed from http://devwiki.electricimp.com/doku.php?id=webcolor&s[]=hextointeger
function hexToInteger(hex)
{
//server.log(hex);
local result = 0;
local shift = hex.len() * 4;
// For each digit..
for(local d=0; d<hex.len(); d++)
{
local digit;
// Convert from ASCII Hex to integer
if(hex[d] >= 0x61)
digit = hex[d] - 0x57;
else if(hex[d] >= 0x41)
digit = hex[d] - 0x37;
else
digit = hex[d] - 0x30;
// Accumulate digit
shift -= 4;
result += digit << shift;
}
return result;
}
class setRGB extends InputPort {
function set(col) {
server.show(col);
local r = hexToInteger(col.slice(1,3));
local g = hexToInteger(col.slice(3,5));
local b = hexToInteger(col.slice(5,7));
server.log(r+", "+g+", "+b);
//blinkm: stop script
i2cPort.write(0x00, format("%c%c", addr>>8, addr&0xff) + "o");
// "f" -> Set Fade Speed, 1 -> slow, 255 -> instantly
i2cPort.write(0x00, format("%c%c", addr>>8, addr&0xff) + format("f%c", 10));
// data c%c%c%c -> "c" + r + g + b
// "c" -> Fade to RGB Color
// "n" -> Go to RGB Color Now
i2cPort.write(0x00, format("%c%c", addr>>8, addr&0xff) + format("c%c%c%c", r, g, b));
}
}
imp.configure("BlinkM", [setRGB()], []);
<?php
$postdata = http_build_query(
array(
'value' => $_GET['value']
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents(___HTTP_IN_URL__YOU_CAN_FIND_IT_IN_YOUR_HTTP_IN_VIMP__, false, $context);
if($result){
echo true;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Pick a color for my light.</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function () {
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "color.php",
//GET method is used
type: "GET",
//pass the data
data: $("#pickColor").serialize(),
//Do not cache the page
cache: false,
//success
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//if process.php returned 0/false (send mail failed)
} else alert('Sorry, unexpected error. Please try again later.');
}
});
//cancel the submit button default behaviours
return false;
});
});
</script>
</head>
<body>
<p>Pick a color
</p>
<form id="pickColor" action="color.php" method="post" accept-charset="UTF-8" >
<input type="color" name="value" value="#000000"/>
<input type="submit" id="submit" value="set color"/>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment