Using Node.js, express and cylon.js, I'm getting a post request and blinking an LED using a raspberry Pi. I receive the first post request fine, the code runs and the LED blinks. However on the second post request I receive an error:
events.js:146
throw err;
^
Error: Uncaught, unspecified "error" event. (Error occurred while writing value 1 to pin 23)
at DigitalPin.emit (events.js:144:17)
at DigitalPin.<anonymous> (/home/pi/tech-corner/devTracker/node_modules/cylon/lib/io/digital-pin.js:70:12)
at fs.js:1134:21
at FSReqWrap.oncomplete (fs.js:82:15)
Note that in the code below I'm using pin: 16
. So I'm confused as to why it's trying to write to pin 23.
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var Cylon = require('cylon')
var pi = Cylon.robot({
connections: {
raspi: { adaptor: 'raspi' }
},
devices: {
led: { driver: 'led', pin: 16 }
},
work: function (my) {
after((1).second(), function () {
console.log('Post request recieved! Toggle the LED')
my.led.toggle()
})
after((2).second(), function () {
my.led.toggle()
})
after((5).seconds(), function () {
console.log('I\'m shutting down now.')
pi.halt()
})
}
})
app.set('port', process.env.PORT || 1337 - 1)
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.post('/', function (req, res) {
console.log(req.body.user_name)
pi.start()
res.end('Hello')
})
app.listen(app.get('port'), function () {
console.log('Node app is running at http://localhost:' + app.get('port'))
})
Any help greatly appreciated. I assume my raspberryPi set-up is fine since it works on the first hook.
EDIT: Incase anyone comes across this and has a similar problem. I decided to go with pigpio which was a lot easier.
npm start
for the first time -Setting up pin direction failed
. However once I try the second time it seems to work fine.npm start
reads thepackage.json
and runs thestart
command which would benode index.js