runShellcommand not working

Hey!

I have this code but for some reason if i call the runshellcommand() nothing happens in the bridge

but when i copy the shellcommand an paste it directly in the linux terminal this works fine any ideas?
maybe i'm missing something? :roll_eyes:

the idea is that i get a JSONstring and send it over mqtt to server.

thank you in advance! ;D

#define BAUDRATE 115200

#define MSGSIZE             240     //Depends on the size of the JSON string
#define RF95_FREQ           863.2   //Middle frequency

#include <FileIO.h>
#include <Process.h>
#include <Bridge.h>
#include <Console.h>
#include <SPI.h>
#include <RH_RF95.h>


RH_RF95 rf95;

int led = A2;

void SendMqtt()
{
  Process mqtt;                                 
  mqtt.runShellCommand("mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m {\"test12\":\"126\"}");
} 

void setup()
{
  pinMode(led, OUTPUT);
  Bridge.begin(BAUDRATE);
  Console.begin();
  while (!Console) ;                    // Wait for console port to be available
  Console.println("Start Sketch");
  if (!rf95.init())
    Console.println("init failed");

  rf95.setFrequency(RF95_FREQ);        // Setup ISM frequency
  rf95.setTxPower(13);                 // Setup Power,dBm
  rf95.setSpreadingFactor(9);          // Setup Spreading Factor (6 ~ 12)
  rf95.setSignalBandwidth(125000);     // Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000
  rf95.setCodingRate4(5);              // Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8)

  Console.print("Listening on frequency: ");
  Console.println(RF95_FREQ);
}

void loop()
{

  if (rf95.available())
  {
    // Should be a message for us now
    uint8_t buf[MSGSIZE];
    uint8_t len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH);
      Console.println((char*)buf);
      Console.print("RSSI: ");
      Console.println(rf95.lastRssi(), DEC);

      SendMqtt();
     
      digitalWrite(led, LOW);
    }
    else
    {
      Console.println("recv failed");
    }
  }
}

Not sure but your command looks malformed: You should always use absolute paths and you should be carefull when using quotes in a command line since they are parsed before given to the process as arguments:

/path/to/mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m '{\"test12\":\"126\"}'

Note the single-quotes around the JSON.

yeah i also tried putting it in singlequotes like this:

mqtt.runShellCommand("mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m '{\"test12\":\"126\"}'");

yet nothing happens :frowning:

I think you need to escape the single quotes ( ' ).. What value does "runShellCommand" return? Have you tried to run a different command and see if it works?

cmdStatus returns 0

cmdStatus = mqtt.runShellCommand("mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m \'{\"test12\":\"126\"}\'");
  Console.println(cmdStatus);

Have you tried to quote all arguments to the command?

mqtt.runShellCommand("mosquitto_pub -h \"192.168.1.26\" -p \"4238\" -u \"r36fEiQJDQKy0O5EWqwR\" -t \"v1/devices/me/telemetry\" -m \'{\"test12\":\"126\"}\'");

Danois90:
Have you tried to quote all arguments to the command?

mqtt.runShellCommand("mosquitto_pub -h \"192.168.1.26\" -p \"4238\" -u \"r36fEiQJDQKy0O5EWqwR\" -t \"v1/devices/me/telemetry\" -m \'{\"test12\":\"126\"}\'");

yes i did with same result :confused:

I'm out of ideas then.. The exit code "0" suggests success, maybe you should try to wrap the command into a shell (or python) script and try to debug on from there. Create file "/home/username/testmqqt.sh" with content:

#!/bin/sh
mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m '{"test12":"126"}'

Enable execute bit and test script with command:

chmod +x /home/username/testmqqt.sh && /home/username/testmqqt.sh

If the script works, try this on the arduino:

mqtt.runShellCommand("/home/username/testmqqt.sh");

I've deleted your other cross post @duracell666.

Cross posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

Repeated cross posting will result in a suspension from the forum.

In the future, please take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Danois90:
I'm out of ideas then.. The exit code "0" suggests success, maybe you should try to wrap the command into a shell (or python) script and try to debug on from there. Create file "/home/username/testmqqt.sh" with content:

#!/bin/sh

mosquitto_pub -h 192.168.1.26 -p 4238 -u r36fEiQJDQKy0O5EWqwR -t v1/devices/me/telemetry -m '{"test12":"126"}'




Enable execute bit and test script with command:



chmod +x /home/username/testmqqt.sh && /home/username/testmqqt.sh




If the script works, try this on the arduino:



mqtt.runShellCommand("/home/username/testmqqt.sh");

yeah i tried this but i think the problem lays somewhere deeper in one of the buffers of the runShelCommand() because if i shorten my string to like 3 Json objects this works again
if you watch in the headers you can see they call the begin() and addParameter() functions so i switched back using them code now looks something like this

void runCommand(){
  Console.println(messageString);

  Process p;        // Create a process and call it "p"
  p.begin("mosquitto_pub");
  p.addParameter("-h");
  p.addParameter(mqttServerIP);
  p.addParameter("-p");
  p.addParameter(mqttPort);
  p.addParameter("-u");
  p.addParameter(accesToken);
  p.addParameter("-t");
  p.addParameter(topic);
  p.addParameter("-m");
  p.addParameter(messageString);
  p.run();      // Run the process and wait for its termination

  // Print arduino logo over the Serial
  // A process output can be read with the stream methods
  while (p.available() > 0) {
    char c = p.read();
    Console.print(c);
  }
  // Ensure the last bit of data is sent.
  Console.flush();
  //memset(messageString, 0, sizeof(messageString));
  mqttString = "";
  accesToken = "";
}

btw thanks for the reply's :slight_smile: