0

I want my BeagleBone machine to automatically start a script called server.js. It includes BoneScript (BeagleBone's unique function) and many functions including reading voltage and ampere from other machines.

I used this tutorial Executing a script on startup using BeagleBone Black

It kept showing status=203/EXEC error.

I tried the same tutorial for simple programs without Bonescript and Functions. Indeed, simple sending and receiving scripts are working without any issue.

How can I remove the error? I think it happens from importing BeagleScript and Functions. I'm not familiar with Linux environment.

Can anyone help to solve the issue?

Thanks,

https://stackoverflow.com/questions/28854705/executing-a-script-on-startup-using-beaglebone-black

-crontab
-service file 
-8 days of trying, fail and searching Google
  1. Service File (password hidden *****)
[Unit]
After=network-online.target
[Service]
Type=simple
ExecStart=/var/lib/cloud9/Projects echo ****** | sudo -S bash scriptname.sh
[Install]
WantedBy=multi-user.target
  1. Shell File
#!/bin/bash
cd /var/lib/cloud9/Projects
node server.js
  1. Javascript
// Constants and global parameters
const NUM_ADC_READS = 100;
const ADC_OFFSET = 0.027 // Offset for difference between ADC_GND and GND
const ZERO_OFFSET = 0.5; // Current sensor output centred at midpoint 
const ADC_GAIN = 1.8; // Function returns 0-1, 0=0V, 1=1.8V
const VOLT_DIVIDER = 0.3491; // Voltage divider to reduce from 5V current sensor output to <1.8V ADC input
const MAX_VOLTAGE = 1.7456; // Max voltage divider output for max current sensor output
const CURRENT_CONVERSION = 25; // Current sensor sensitivity 25 A/V
const COMBINED_GAIN = 128.9; //  CURRENT_CONVERSION * ADC_GAIN / VOLT_DIVIDER
const COMBINED_OFFSET = 0.473; // ZERO_OFFSET - ADC_OFFSET

//Loading modules
var http = require('http');
var fs = require('fs');
var path = require('path');
var b = require('bonescript');


// Create variables for relays
var relay_load_1 = "P8_12"; // Relay control for output load 1 
var relay_load_2 = "P8_11"; // Relay control for output load 2 
var relay_bat_bank_1 = "P8_7"; // Relay control for input battery bank 1 
var relay_bat_bank_2 = "P8_8"; // Relay control for input battery bank 2 
var relay_bat_bank_3 = "P8_10"; // Relay control for input battery bank 3 

// Create variables for current readings
var current_load = ["P9_39", "P9_40"];
var current_bat = ["P9_37", "P9_38", "P9_33"];

// Initialize the relay control variables as OUTPUTS
b.pinMode(relay_load_1, b.OUTPUT);
b.pinMode(relay_load_2, b.OUTPUT);
b.pinMode(relay_bat_bank_1, b.OUTPUT);
b.pinMode(relay_bat_bank_2, b.OUTPUT);
b.pinMode(relay_bat_bank_3, b.OUTPUT);

// Initialize the server on port 8888
var server = http.createServer(function (req, res) {
    // requesting files
    var file = '.'+((req.url=='/')?'/index.html':req.url);
    var fileExtension = path.extname(file);
    var contentType = 'text/html';
    // Uncoment if you want to add css to your web page
    /*
    if(fileExtension == '.css'){
        contentType = 'text/css';
    }*/
    fs.exists(file, function(exists){
        if(exists){
            fs.readFile(file, function(error, content){
                if(!error){
                    // Page found, write content
                    res.writeHead(200,{'content-type':contentType});
                    res.end(content);
                }
            })
        }
        else{
            // Page not found 
            res.writeHead(404);
            res.end('Page not found');
        }
    })
}).listen(1111);


// Loading socket io module
var io = require('socket.io').listen(server);


// When communication is established
io.on('connection', function (socket) {
    socket.on('changeState_load_1', handleChangeState_load_1);
});

io.on('connection', function (socket) {
    socket.on('changeState_load_2', handleChangeState_load_2);
});

io.on('connection', function (socket) {
    socket.on('changeState_bat_1', handleChangeState_bat_1);
});

io.on('connection', function (socket) {
    socket.on('changeState_bat_2', handleChangeState_bat_2);
});

io.on('connection', function (socket) {
    socket.on('changeState_bat_3', handleChangeState_bat_3);
});

// Change relay state when a button is pressed
function handleChangeState_load_1(data) {
    var newData = JSON.parse(data);


    var status;

    if (newData.state == 1) {

        status = "OFF"

    }

    else {

        status = "ON"
    }


    console.log("Load 1 Relay =" + status);
    //console.log("Load 1 Relay =" + newData.state);

    // turn the load relay ON or OFF
    b.digitalWrite(relay_load_1, newData.state);
        return status;
} 
//function handleChangeState_load_1(data) {
//    var newData = JSON.parse(data);
//    console.log("Load 1 Relay =" + newData.state);
    // turn the load relay ON or OFF
//    b.digitalWrite(relay_load_2, newData.state);
//  return newData.state;
//}


// Change relay state when a button is pressed
function handleChangeState_load_2(data) {
    var newData = JSON.parse(data);
    console.log("Load 2 Relay =" + newData.state);
    // turn the load relay ON or OFF
    b.digitalWrite(relay_load_2, newData.state);
}

// Change relay state when a button is pressed
function handleChangeState_bat_1(data) {
    var newData = JSON.parse(data);
    console.log("Bat 1 Relay =" + newData.state);
    // turn the battery bank relay ON or OFF
    b.digitalWrite(relay_bat_bank_1, newData.state);
}

// Change relay state when a button is pressed
function handleChangeState_bat_2(data) {
    var newData = JSON.parse(data);
    console.log("Bat 2 Relay =" + newData.state);
    // turn the battery bank relay ON or OFF
    b.digitalWrite(relay_bat_bank_2, newData.state);
}

// Change relay state when a button is pressed
function handleChangeState_bat_3(data) {
    var newData = JSON.parse(data);
    console.log("Bat 3 Relay =" + newData.state);
    // turn the battery bank relay ON or OFF
    b.digitalWrite(relay_bat_bank_3, newData.state);
}

// Read load currents from analog inputs
function readLoadCurrent(data) {
    var current = 0;
    var max_current = 0;
    var min_current =1.0;
    for (let i = 0; i < NUM_ADC_READS; i++) {
        current = b.analogRead(current_load[data]);
        if (max_current < current) max_current = current;
        if (min_current > current) min_current = current;
    }
//    console.log("Load Current =" + ((max_current - min_current)/2.0));
    return ((((max_current - min_current)/2.0)) * COMBINED_GAIN);
}

// Read battery bank currents from analog inputs
function readBatCurrent(data) {
    var current = 0;
    var max_current = 0;
    var min_current = 1.0;
    for (let i = 0; i < NUM_ADC_READS; i++) {
        current = b.analogRead(current_bat[data]);
        if (max_current < current) max_current = current;
        if (min_current > current) min_current = current;
    }
//    console.log("Bat Current =" + ((max_current - min_current)/2.0));
    return ((((max_current - min_current)/2.0)) * COMBINED_GAIN);
}

var listener = io.listen(server);

var currentBat_1 = readBatCurrent(1);

function battery1power(data){

 while(1)  { return currentBat_1*data;
}
}


listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('battery1power', {'battery1power': battery1power(0)});
    }, 2000);
}); 



listener.sockets.on('connection', function(socket){
    //send data to client 
    setInterval(function(){
        socket.emit('currentLoad_1', {'currentLoad_1': readLoadCurrent(0)});
    }, 2000);
});
listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('currentLoad_2', {'currentLoad_2': readLoadCurrent(1)});
    }, 2000);
});
listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('currentBat_1', {'currentBat_1': readBatCurrent(0)});
    }, 2000);
});
listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('currentBat_2', {'currentBat_2': readBatCurrent(1)});
    }, 2000);
});
/*listener.sockets.on('connection', function(socket){
    //send data to client
    setInterval(function(){
        socket.emit('currentBat_3', {'currentBat_3': readBatCurrent(2)});
    }, 2000);
});*/


// Displaying a console message for user feedback
server.listen(console.log("Server Running ..."));


var currentBat_2 = readBatCurrent(1);

function battery2power(voltage){

   while(1){ return currentBat_2*voltage;}}



var currentBat_3 = readBatCurrent(2);

function battery3power(voltage){

    return currentBat_3*voltage;

}
var currentLoad_1 = readLoadCurrent(0);

function load1power(voltage){

    return currentLoad_1*voltage;

}
var currentLoad_2 = readLoadCurrent(1);

function load2power(voltage){

    return currentLoad_2*voltage;

}

var dgram = require('dgram');
var client = dgram.createSocket('udp4');

// Create periodical which ends a message to the client every 5 seconds
// 1000 ms is 1 second, so 5 second is 5000 ms

var interval = setInterval(function() {
    //console.log('powerBat_1 ' + battery1power(116).toFixed(4));
    client.send(
                //'Serial Number: A33******************' + load1shandleChangeState_load_1(0) +

                '33333powerBat_1 ' + battery1power(116).toFixed(4)  + 
                ';powerBat_2 ' + battery2power(110).toFixed(4) +
                ';powerBat_3 ' + battery3power(110).toFixed(4) +
                ';powerLoad_1 ' + load1power(116).toFixed(4) +
                ';powerLoad_2 ' + load2power(110).toFixed(4) +
                ';currentLoad_1 ' + readLoadCurrent(0).toFixed(4)  +  
                ';currentLoad_2 ' + readLoadCurrent(1).toFixed(4) + 
                ';currentBat_1 ' + readBatCurrent(0).toFixed(4) + 
                ';currentBat_2 ' + readBatCurrent(1).toFixed(4) + 
                ';currentBat_3 ' + readBatCurrent(2).toFixed(4), 0, 10000, PORT, HOST, 0);
},5);

Error Message ● scriptname.service Loaded: loaded (/etc/systemd/system/scriptname.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Thu 2019-09-05 23:47:06 UTC; 8s ago Main PID: 27859 (code=exited, status=203/EXEC)

Albert
  • 1
  • 1

1 Answers1

0

You may want to try to set up your .service file like this:

[Unit]
Description=Your Description for your File!

[Service]
ExecStart=/home/debian/var/lib/cloud9/file.js

[Install]
WantedBy=multi-user.target

Use the .service file outside of the Cloud9 IDE. So, this will help:

  1. Create a directory
  2. cd into that directory
  3. Put your files in the directory you just created and cd'd into
  4. Last but not least, use the /etc/systemd/system/ dir. to establish your .servive file.

Now...

If you are in your /home/debian/ dir. and have already created your .service file, change the .service file to look like this idea:

[Unit]
Description=Whatever you want goes here to describe your file use...

[Service]
ExecStart=/home/debian/<NewlyMadeDirectory>/<TheFileYouWantToRunWithFileSuffix>

[Install]
WantedBy=multi-user.target

Try that idea and get back to me.

Seth

P.S. I will wait to hear from you on how this worked out. Oh and you need, in the node.js file, to make sure it is executable and then use chmod a+x yourFile.js to set permissions.

De Funct
  • 452
  • 2
  • 10