Skip to content

Instantly share code, notes, and snippets.

@uzimith
Last active August 29, 2015 14:21
Show Gist options
  • Save uzimith/afd027fc54e012567867 to your computer and use it in GitHub Desktop.
Save uzimith/afd027fc54e012567867 to your computer and use it in GitHub Desktop.
Open Web BoardでArduinoと通信する
var socket = navigator.mozTCPSocket.open('127.0.0.1', 9943);
socket.onopen = function () {
var param = {
// adb shell dmesgでArduinoの名前を確認する
devicename: 'ttyACM0',
bitrate: 9600
};
console.log('Opened');
socket.send(JSON.stringify(param));
}
socket.onerror = function (evt) {
console.log('Error:' + evt.type);
}
function send(cmd) {
console.log('Sending ' + cmd);
socket.send(cmd + "\r\n");
}
window.addEventListener("load", function() {
$("#off").click(function() {
send("1");
})
$("#on").click(function() {
send("2");
})
});
/*
*/
const int OUTPUTS = 2;
byte pin[OUTPUTS] = {13, 12};
int current;
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
for (int i=0; i < OUTPUTS+1; ++i) pinMode(pin[i], OUTPUT);
}
void loop()
{
if (Serial.available() > 0) {
current = Serial.read();
}
if(current == 50) {
Serial.print(1);
// turn on
digitalWrite(pin[0], HIGH);
delay(1000);
}
if(current == 49) {
Serial.print(0);
// turn off all outputs
digitalWrite(pin[0], LOW);
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment