Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
pymata_aio_html_example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>pymata_aio</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<h2>Press A Button To Activate Pin 6 PWM Controlled LED</h2>
<input id="ledOn" type="button" value="On" onclick="ledOn();"/>
&nbsp;
<input id="ledOff" type="button" value="Off" onclick="ledOff();"/>
<script>
// connect to the server
var ipAddress = "localhost";
var ipPort = "9000";
var inputMode = "0";
var outputMode = "1";
var analogMode = "2";
var pwmMode = "3";
var socket = new WebSocket('ws://' + ipAddress + ':' + ipPort);
var pin_mode = "";
var pin = "";
var outputValue = "";
// Socket handlers
socket.onopen = function (event) {
$("#connected").text('PyMata IoT Has Successfully Connected');
};
socket.onclose = function (event) {
alert("The socket has closed! Did you start the server?");
};
socket.onmessage = function (message) {
// put your code that handles incoming messages from pymata_aio here.
console.log('got message' + message.data);
};
var msg;
// turn the led on
function ledOn() {
// create a set pin mode message and send it to the server
msg = JSON.stringify({"method": "set_pin_mode", "params": ["6", pwmMode]});
socket.send(msg);
// set the pin to 128
msg = JSON.stringify({"method": "analog_write", "params": ["6", "128"]});
socket.send(msg);
}
function ledOff() {
// create a set pin mode message and send it to the server
msg = JSON.stringify({"method": "set_pin_mode", "params": ["6", pwmMode]});
socket.send(msg);
// set the pin to zero
msg = JSON.stringify({"method": "analog_write", "params": ["6", "0"]});
socket.send(msg);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment