Skip to content

Instantly share code, notes, and snippets.

@danasf
Last active September 10, 2021 13:05
Show Gist options
  • Select an option

  • Save danasf/11131491 to your computer and use it in GitHub Desktop.

Select an option

Save danasf/11131491 to your computer and use it in GitHub Desktop.
Firmata & Arduino Examples
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Arduino connectivity test</title>
<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<h1>johnny-five test</h1>
<div class="row">
<div class="col-md-4">
<h4>From Analog 0</h4>
<textarea class="form-control" style="background:#222; color:#00ff00;" rows="8" id="inData"></textarea>
</div>
<div class="col-md-4">
<h4>Servo to position:</h4>
<div class="btn-group" data-toggle="buttons">
<label class="servobtn btn btn-primary">
<input type="radio" name="servo" id="servo1" value="0"> 0
</label>
<label class="servobtn btn btn-primary">
<input type="radio" name="servo" id="servo2" value="45"> 45
</label>
<label class="servobtn btn btn-primary">
<input type="radio" name="servo" id="servo3" value="90"> 90
</label>
<label class="servobtn btn btn-primary">
<input type="radio" name="servo" id="servo4" value="135"> 135
</label>
<label class="servobtn btn btn-primary">
<input type="radio" name="servo" id="servo5" value="180"> 180
</label>
</div>
</div>
<div class="col-md-4">
<h4>LED Pulse Delay (ms)</h4>
<p><input type="text" class="form-control" id="ledDelay" name="ledDelay" value="1000" /></p>
<p><button id="ledSet" class="btn btn-primary">Set Delay</button></p>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
});
socket.on('sensor', function (data) {
console.log("Incoming sensor data:",data.raw);
$("#inData").append(data.raw+"\r");
$("#inData").animate({scrollTop:$("#inData")[0].scrollHeight - $("#inData").height()},200);
});
$('.servobtn').button();
$('.servobtn').on('change',function(){
console.log("Setting Servo Pos:",$('input[name=servo]:checked').val())
socket.emit('servo',{pos:$('input[name=servo]:checked').val()});
});
$('#ledSet').on('click',function(){
var tmp = parseInt($('#ledDelay').val(),10);
console.log("Setting LED Delay:",tmp)
socket.emit('led',{delay:tmp});
});
</script>
</body>
</html>
{
"name": "node-arduino-test",
"version": "0.0.1",
"description": "Testing node, web sockets, johnny-five, arduino",
"main": "webtest.js",
"dependencies": {
"socket.io": "~0.9.16",
"johnny-five": "~0.7.28"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Dana",
"license": "MIT"
}
#!/usr/local/bin/python
# https://github.com/tino/pyFirmata
import pyfirmata
# write 0-180 to servo
def move_servo(a):
pin6.write(a)
# blink led on a delay
def blink_led(d):
# blink a led ON
board.digital[13].write(1)
board.pass_time(d)
# and OFF
board.digital[13].write(0)
board.pass_time(d)
# don't forget to change the serial port
board = pyfirmata.Arduino('/dev/tty.usbmodem1411')
# set initial led delay to 1 second
led_delay = 1.0
# start an iterator thread so
# serial buffer doesn't overflow
iter8 = pyfirmata.util.Iterator(board)
iter8.start()
# set up pin D6 as Servo Output
pin6 = board.get_pin('d:6:s')
print "Connecting to arduino board..."
print "Firmata Version:", board.get_firmata_version()
# enable reporting for pin 0
board.analog[0].enable_reporting()
print "Let's blink a LED!"
while True:
try:
# blink your LED
blink_led(led_delay)
# then read a value from analog pin 0
val = board.analog[0].read();
print "analog pin reading is ", val
# then move the servo according to the potentiometer value
servo_val = round(val*180,0)
#set the LED delay
led_delay = round(0.01+val*1,3)
move_servo(servo_val);
except KeyboardInterrupt:
board.exit()
break
exit(0)
/* Example code for http://www.instructables.com/id/Javascript-robotics-and-browser-based-Arduino-cont/
* Do 'npm install' to install dependencies
* Initialize web server, socket.io, filesystem, johnny-five, etc
*/
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, five = require("johnny-five"),
board,servo,led,sensor;
board = new five.Board();
// on board ready
board.on("ready", function() {
// init a led on pin 13, strobe every 1000ms
led = new five.Led(13).strobe(1000);
// setup a stanard servo, center at start
servo = new five.Servo({
pin:6,
range: [0,180],
type: "standard",
center:true
});
// poll this sensor every second
sensor = new five.Sensor({
pin: "A0",
freq: 1000
});
});
// make web server listen on port 8888
app.listen(8888);
console.log("http server listening on port 8888")
// handle web server
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// on a socket connection
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
// if board is ready
if(board.isReady){
// read in sensor data, pass to browser
sensor.on("data",function(){
socket.emit('sensor', { raw: this.raw });
});
}
// if servo message received
socket.on('servo', function (data) {
console.log(data);
if(board.isReady){ servo.to(data.pos); }
});
// if led message received
socket.on('led', function (data) {
console.log(data);
if(board.isReady){ led.strobe(data.delay); }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment