Search code examples
pythonnode.jsexpressraspberry-pigpio

I'm not able to execute python script from an express app


I'm trying to execute a python script when I click on a button from an express app. The script is just turning on an LED in my Raspberry Pi. I have tested the scripts and they work, however when I try to execute them from the server it doesn't work at all. I am using "spawn" to create a child process and then write through stdin to execfile the script.

This is my router file:

var express = require('express')
var router = express.Router()

var python = require('child_process').spawn('python', [ '-i' ])
//python.setEncoding('utf-8')
python.stdout.pipe(process.stdout)

/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index')
})

router.get('/green', green)
router.get('/yellow', yellow)
router.get('/red', red)

module.exports = router

function green(req, res) {
    console.log('Turning on green led...')
    python.stdin.write("execfile('./public/python/green_led.py')")
    res.redirect('/')
}

function yellow(req, res) {
    console.log('Turning on yellow led...')
    python.stdin.write("execfile('./public/python/yellow_led.py')")
    res.redirect('/')
}

function red(req, res) {
    console.log('Turning on red led...')
    python.stdin.write("execfile('./public/python/red_led.py')")
    res.redirect('/')
}

You can check out the Github repo Here

Thanks!


Solution

  • I managed to fix it by using exec instead of spawn. This is my router file now:

    var express = require('express')
    var router = express.Router()
    
    var exec = require('child_process').exec
    
    /* GET home page. */
    router.get('/', function(req, res, next) {
        res.render('index')
    })
    
    router.get('/green', green)
    router.get('/yellow', yellow)
    router.get('/red', red)
    router.get('/auto', auto)
    
    module.exports = router
    
    function green(req, res) {
        console.log('Turning on green led...')
        var child = exec('python ./public/python/green_led.py')
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.stderr.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.on('close', function(code) {
            console.log('closing code: ' + code)
        })
        res.redirect('/')
    }
    
    function yellow(req, res) {
        console.log('Turning on yellow led...')
        var child = exec('python ./public/python/yellow_led.py')
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.stderr.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.on('close', function(code) {
            console.log('closing code: ' + code)
        })
        res.redirect('/')
    }
    
    function red(req, res) {
        console.log('Turning on red led...')
        var child = exec('python ./public/python/red_led.py')
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.stderr.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.on('close', function(code) {
            console.log('closing code: ' + code)
        })
        res.redirect('/')
    }
    
    function auto(req, res) {
        console.log('Turning on auto led...')
        var child = exec('python ./public/python/loop_led.py')
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.stderr.on('data', function(data) {
            console.log('stdout: ' + data)
        })
        child.on('close', function(code) {
            console.log('closing code: ' + code)
        })
        res.redirect('/')
    }