Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Controlling my Pi via bash script on buttonclick

Controlling my Pi via bash script on buttonclick

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WDTG
    wrote on last edited by
    #1

    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]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • W Offline
          W Offline
          WDTG
          wrote on last edited by
          #4

          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. :(

          1 Reply Last reply
          0
          • W Offline
            W Offline
            WDTG
            wrote on last edited by
            #5

            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”);

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Have a look at QProcess, it should make things easier

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • W Offline
                W Offline
                WDTG
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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 :)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved