Controlling my Pi via bash script on buttonclick



  • Thanks for reading my post. I want to understand how to make a simple application that will run shell script on the local system. For example, I've written a simple bash to control specific pins on my pi.

    @
    while : # Loop forever
    do

    1. Start Pin 12

    2. Stop Pin 12

    3. Start Pin 16

    4. Stop Pin 16

    5. Exit prog
      !

    echo -n " Your choice? : "
    read choice

    All the scripting goes here

    sta_pin12 () {
    echo "12" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio12/direction
    echo "0" > /sys/class/gpio/gpio12/value
    echo "GPIO 12 started"
    echo "12" > /sys/class/gpio/unexport
    }

    sto_pin12 () {
    echo "12" > /sys/class/gpio/export
    echo "1" > /sys/class/gpio/gpio12/value
    echo "GPIO 12 stoped"
    echo "12" > /sys/class/gpio/unexport
    }

    sta_pin16 () {
    echo "16" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio16/direction
    echo "0" > /sys/class/gpio/gpio16/value
    echo "GPIO 16 started"
    echo "16" > /sys/class/gpio/unexport
    }

    sto_pin16 () {
    echo "16" > /sys/class/gpio/export
    echo "1" > /sys/class/gpio/gpio16/value
    echo "GPIO 16 stoped"
    echo "16" > /sys/class/gpio/unexport
    }

    menu order

    case $choice in

    1. sta_pin12 ;;
    2. sto_pin12 ;;
    3. sta_pin16 ;;
    4. sto_pin16 ;;
    5. exit ;;
      *) echo ""$choice" is not valid "; sleep 2 ;;
      esac
      done
      @

    And this works just fine. However I want a simple pushbutton gui, so I created a project in Qt. I created a button, right click, Go to slot and I see this coding:

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {

    }
    @

    ==========================================================================
    What coding do I enter between the brackets above to achieve the following commands below?

    @
    echo "12" > /sys/class/gpio/export
    echo "out" > /sys/class/gpio/gpio12/direction
    echo "0" > /sys/class/gpio/gpio12/value
    echo "12" > /sys/class/gpio/unexport
    @

    I program vb.net for windows and am totally new at Qt, any help is most appreciated!

    [edit: added missing coding tags @ SGaist]


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to write to these files so QFile should be your friend

    Hope it helps



  • Welcome to the forum. Do you want run the shell script from Qt or you want to write the commands to file ? Question heading was different from what you are asking inside the question. If you want to write into file SGaist has already answered it. If you want to run the shell script, look at the QProcess or system call.



  • system call works but not with the syntax I have. I use system to call a bash sript it works fine but if I use

    system("echo “12” > /sys/class/gpio/export");
    system("echo “out” > /sys/class/gpio/gpio12/direction");
    system("echo “0” > /sys/class/gpio/gpio12/value");
    system("echo “12” > /sys/class/gpio/unexport");
    

    I get nothing, is it because of the quotes after echo. I know in vb,net you must double quote, but that didn't work either. :(



  • sorry for formatting

    system(“echo “12” > /sys/class/gpio/export”);
    system(“echo “out” > /sys/class/gpio/gpio12/direction”);
    system(“echo “0” > /sys/class/gpio/gpio12/value”);
    system(“echo “12” > /sys/class/gpio/unexport”);


  • Lifetime Qt Champion

    AFAIK, the formatting of your echo lines are invalid in bash.

    Have a look at QProcess, it should make things easier



  • yup removed quotation from 12 and out, works just fine. I had quotes originally because part of the bash was calling variable from menu. thnx guys


  • Lifetime Qt Champion

    You're welcome :)

    Since you have it running now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.