YUN runShellCommand can't work while not open SerialMonitor/password

Greetings,

Sorry english.

I take this example to run a lua’s script on Yun/Dragino/mega 2560.

When I power on the Yun, the “p.runShellCommand” not work.

If I run arduino IDE and run serial monitor, and type password… touché!
The “p.runShellCommand” works!

Missing start anything?

Someone can help?

I try run two ways.

The idea is…

  1. crontab run every ‘x’ minutes and send command to arduino
  2. arduino receive, read sensors, and run shell command with values, to write log file

If not open IDE SerialMonitor and type password, the #2 not work.

  if( command == "sensor" ){
    Process proc;
    String s =  String( sensor.luminosidade ) + " " +
                String( sensor.pressao      ) + " " +
                String( sensor.temperatura  ) + " " +
                String( sensor.humidade     ) + " " +
                String( sensor.altitude     ) + " " +
                String( sensor.gas          ) + " " +
                String( sensor.eventos      ) ;
    String cmd = "/usr/bin/lua /usr/wer/temperatura.lua " + s;
    proc.runShellCommandAsynchronously(cmd);
    // proc.runShellCommand(cmd);
    while( proc.available() ){
      //char c = proc.read();
      //startString += c;
      delay( 10 );
    }
    proc.close();
  }

  if( command == "sensor2" ){
    Process proc;
    String s =  String( sensor.luminosidade ) + " " +
                String( sensor.pressao      ) + " " +
                String( sensor.temperatura  ) + " " +
                String( sensor.humidade     ) + " " +
                String( sensor.altitude     ) + " " +
                String( sensor.gas          ) + " " +
                String( sensor.eventos      ) ;

    String cmd = "/usr/bin/lua /usr/wer/temperatura.lua " + s;
    proc.runShellCommand(cmd);
    while( proc.running() ){
      delay( 10 );
    };
    proc.close();
  }

Full arduino code:

#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <Process.h>
#include <Wire.h>
#include <avr/wdt.h>

BridgeServer server;
BridgeClient client;
Process proc;

#include "BlueDot_BME680.h"

BlueDot_BME680 bme680 = BlueDot_BME680();

#include "BH1750FVI.h"

BH1750FVI myLux(0x23);

int incomingByte;

typedef struct{
  float luminosidade;
  float pressao;
  float temperatura;
  float humidade;
  float altitude;
  float gas;
  unsigned long eventos = 0;
}Sensor;

uint32_t lastUpdate = 0;
int interval = 5000;
String entrada = "";
byte byte_entrada = 0;
Sensor sensor;
String startString = "";


void setup(){
  pinMode(13, OUTPUT);

  Bridge.begin();

  Wire.begin();

  bme680.parameter.I2CAddress = 0x77;
  bme680.parameter.sensorMode = 0b01;
  bme680.parameter.IIRfilter = 0b100;
  bme680.parameter.humidOversampling = 0b101;
  bme680.parameter.tempOversampling = 0b101;
  bme680.parameter.pressOversampling = 0b101;
  bme680.parameter.pressureSeaLevel = 1013.25;
  bme680.parameter.tempOutsideCelsius = 15;
  bme680.parameter.target_temp = 320;

  delay(1000);

  myLux.powerOn();
  myLux.setContHighRes();

  if( bme680.init() != 0x61 ){
    while (10);
  }

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

  server.listenOnLocalhost();
  //server.noListenOnLocalhost();
  server.begin();

  //scanner();
  proc.runShellCommand("date");
  while( proc.available() ){
    char c = proc.read();
    startString += c;
  }
}

byte stringToByte(String a){
  byte b = (byte) a.toInt();
  return b;
}

void process(BridgeClient client){
  //String command = client.readStringUntil('/');
  String command = client.readString();

  if( command == "on" ){
    digitalWrite(13, HIGH);
  }
  if( command == "off" ){
    digitalWrite(13, LOW);
  }

  if( command == "sensor" ){
    Process proc;
    String s =  String( sensor.luminosidade ) + " " +
                String( sensor.pressao      ) + " " +
                String( sensor.temperatura  ) + " " +
                String( sensor.humidade     ) + " " +
                String( sensor.altitude     ) + " " +
                String( sensor.gas          ) + " " +
                String( sensor.eventos      ) ;
    String cmd = "/usr/bin/lua /usr/wer/temperatura.lua " + s;
    proc.runShellCommandAsynchronously(cmd);
    // proc.runShellCommand(cmd);
    while( proc.available() ){
      //char c = proc.read();
      //startString += c;
      delay( 10 );
    }
    proc.close();
  }

  if( command == "sensor2" ){
    Process proc;
    String s =  String( sensor.luminosidade ) + " " +
                String( sensor.pressao      ) + " " +
                String( sensor.temperatura  ) + " " +
                String( sensor.humidade     ) + " " +
                String( sensor.altitude     ) + " " +
                String( sensor.gas          ) + " " +
                String( sensor.eventos      ) ;

    String cmd = "/usr/bin/lua /usr/wer/temperatura.lua " + s;
    proc.runShellCommand(cmd);
    while( proc.running() ){
      delay( 10 );
    };
    proc.close();
  }
}

void loop(){
  BridgeClient client = server.accept();
  //if(client.connected())
  if( client ){
    process(client);
    client.stop();
  }

  if( millis() > lastUpdate ){
    lastUpdate = millis() + interval;

    myLux.powerOn();
    delay(2);
    sensor.luminosidade = myLux.getLux();
    myLux.powerOff();

    bme680.writeCTRLMeas();
    //bme680.readStatus();

    sensor.temperatura  = bme680.readTempC();
    sensor.humidade     = bme680.readHumidity();
    sensor.pressao      = bme680.readPressure();
    sensor.altitude     = bme680.readAltitudeMeter();
    sensor.gas          = bme680.readGas();
    sensor.eventos++;
    Bridge.put("eventos", String(sensor.eventos));
  }
}

Crontab run this script every x minuts:
*/5 * * * * lua /usr/wer/exec.lua

/usr/wer/exec.lua

#!/usr/bin/lua
local agora = os.time() - 10800

print("exe( agora=" .. agora .. " )")

file = io.open("/usr/wer/log/exec.log", "a")

local socket = require("socket")
-- portas 5555, 5700, 6571
local client = socket.connect("127.0.0.1", 5555)
if client then
    print("conectado")
    client:send("sensor2")
    file:write(agora .. "=OK," .. os.date('%Y-%m-%d %H:%M:%S', agora + 10800) .. "\n")
    client:close()
else
    file:write(agora .. "=ERR," .. os.date('%Y-%m-%d %H:%M:%S', agora + 10800) .. "\n")
    print("off line")
end

file:close()
os.exit()

/usr/wer/temperatura.lua

#!/usr/bin/lua
local agora = os.time() - 10800

local luminosidade = tonumber(arg[1]) or 0
local pressao      = tonumber(arg[2]) or 0
local temperatura  = tonumber(arg[3]) or 0
local humidade     = tonumber(arg[4]) or 0
local altitude     = tonumber(arg[5]) or 0
local gas          = tonumber(arg[6]) or 0
local eventos      = tonumber(arg[7]) or 0

print("temperatura( agora=" .. agora .. ", temperatura=" .. temperatura .. " )")

file = io.open("/usr/wer/log/temperatura.log", "a")
file:write(agora .. "=" .. os.date('%Y-%m-%d %H:%M:%S', agora + 10800) .. "," ..luminosidade .. "," .. pressao .. "," .. temperatura .. "," .. humidade .. "," .. altitude .. "," .. gas .. "," .. eventos .. "\n")
file:close()

Maybe I found a solution...

Add some delays...

Arduino is ready earlier than Draguino/Yun.

I thinking this is a problem...

See: https://forum.arduino.cc/index.php?topic=290566.msg2032601#msg2032601

Hello!

Maybe I found a solution...

write this function in your sketch:
String Run_Shell(String c){
Process p;
String s;
p.runShellCommand( c );
while( p.running() ){
delay(2);
};
while( p.available() ){
char c = p.read();
s += c;
}
p.close();
Bridge.put("startString", String( s ) );
return startString;
}

Write this script in /usr/wer/hello.lua

#!/usr/bin/lua
print("HelloYun!")

At end's Setup()... include this...

String test = Run_Shell("/usr/wer/hello.lua");
while( test != "HelloYun!" ){
test = Run_Shell("/usr/wer/hello.lua");
delay(250);
}

This way can verify if Yun is "ready"...
If YES, the setup's loop end.