Qt with Wiring PI


26 posts   Page 1 of 2   1, 2
by ember » Sat Feb 16, 2013 12:17 am
Hello.
I'm trying to combine QT with Wiring PI whole day and still no effects.

First of all, I installed Wiring PI on my laptop using build script provided.
Secondly i configured *.pro file with following line:
Code: Select all
LIBS += -L/usr/local/include -lwiringPi

Everything worked fine, until I tried to #include <wiring.h>, which caused:
Code: Select all
wiringPi.h: No such file or directory


So I installed WiringPI on RPI - nothing changed.
Next step was to make new image of Raspbian system including wiringPI (the one that resist on device) and mount it in my Ubuntu instead of old image I used for mounting (btw. I wounder why QT needs it)
I think I'm a one step further:
Nothing's changed when I try to #include <wiringPi.h> (not found), but when I do not include it, i get:
Code: Select all
1. skipping incompatible /home/ember/opt/rasp-pi-rootfs/usr/lib/libwiringPi.so when searching for -lwiringPi
2. skipping incompatible /home/ember/opt/rasp-pi-rootfs/usr/lib/libwiringPi.so when searching for -lwiringPi
3. cannot find -lwiringPi
4. collect2: error: ld returned 1 exit status


Any ideas?
Thanks in advance :)
Posts: 13
Joined: Sat Feb 16, 2013 12:03 am
by bgreat » Sat Feb 16, 2013 2:23 am
You are confusing the library and include sections in the .pro file.

In your .pro:
Code: Select all
# So wiringPi include files can be found during compile
INCLUDEPATH    += /usr/local/include

# To link the wiringPi library when making the executable
LIBS += -L/usr/local/lib -lwiringPi

Enjoy!
Bill
User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm
by ember » Sat Feb 16, 2013 1:39 pm
Thank you.
But I still get this error:
Code: Select all
1. skipping incompatible /home/ember/opt/rasp-pi-rootfs/usr/lib/libwiringPi.so when searching for -lwiringPi
2. skipping incompatible /home/ember/opt/rasp-pi-rootfs/usr/lib/libwiringPi.so when searching for -lwiringPi
3. cannot find -lwiringPi
4. collect2: error: ld returned 1 exit status


I also noticed, that I have following files:
Code: Select all
/usr/lib/libwiringPi.so
/usr/lib/libwiringPi.so.1
/usr/local/lib/libwiringPi.so.1
/usr/local/lib/libwiringPi.so.1.0


It doesn't realy metter if I set
Code: Select all
LIBS += -L/usr/local/lib -lwiringPi

or
Code: Select all
LIBS += -L/usr/lib -lwiringPi

Neither of them is working - incompatible - which is weird, as I compiled it directly on RPi.
Posts: 13
Joined: Sat Feb 16, 2013 12:03 am
by ember » Sat Feb 16, 2013 1:49 pm
Just realized, that I have to link RPi libraries, not Ubuntu libraries:
Code: Select all
INCLUDEPATH    += /home/ember/opt/rasp-pi-rootfs/usr/local/include
LIBS += -L/home/ember/opt/rasp-pi-rootfs/usr/local/lib -lwiringPi


Maybe it's another step further :)
Now I get:
Code: Select all
'wiringPiSetupGpio' was not declared in this scope
Posts: 13
Joined: Sat Feb 16, 2013 12:03 am
by gordon@drogon.net » Sat Feb 16, 2013 1:51 pm
I can't really help with the Qt side of things (as I know nothing about it), but a simple test of wiringPi on your Pi would be to run the gpio command - e.g.

Code: Select all
gpio readall


if that works OK, then it's a good indication that wiringPi is installed correctly. The rest is going to be to do with making the environment your using find the library and header file correctly.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1931
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by bgreat » Sat Feb 16, 2013 2:25 pm
Are you cross compiling with Qt? With multiple copies/versions of WiringPi libraries on the system, I would recommend starting from a fresh OS and Qt install with the corrected build files.

Enjoy!
Bill
User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm
by ember » Sat Feb 16, 2013 3:15 pm
Problem solved!
Turned out, that there were two problems:
First one was, that I had empty /usr/local/lib/libwiringpi.so on my raspberry image. Had to rebuild it twice (don't know why) in order to get work.
Second problem was, that compiler searched for library in
/home/ember/opt/rasp-pi-rootfs/usr/lib
instead of declared in *.pro file
/home/ember/opt/rasp-pi-rootfs/usr/local/lib

/home/ember/opt/rasp-pi-rootfs/usr/local/lib/libwiringpi.so is symlinc to /usr/local/lib/libwiringpi.so , so crosscompiler tried to link it with x86 library.
Had to
Code: Select all
cd ~/opt/rasp-pi-rootfs/usr/lib
sudo mv libwiringPi.so libwiringPi.so_cp
sudo ln -s ../local/lib/libwiringPi.so.1.0 libwiringPi.so


Where of course ~/opt/rasp-pi-rootfs is the mount dir of raspberry image.

And of course you all right - installing wiringPi on my ubuntu was strange thing to do.

Anyway, problem solved. Thank you!
Posts: 13
Joined: Sat Feb 16, 2013 12:03 am
by PS1981 » Wed Feb 27, 2013 8:17 pm
Now I need help please!
I've been using QT for quite some time really for little GUI's and so on, and I've got wiringPi working in the terminal, that is using such things as
Code: Select all
gpio mode 14 out
gpio write 14 1
gpio write 14 0

works to switch an LED on then off again.
My problem is that I'm trying (and this is driving me mad) to combine QTcreator and wiringPi to make GUI's to control hardware with, it's just not working.
To keep it simple I've got a new image, have wiringPi installed and working as written above. QT creator is correctly installed and working. I'm hoping somone out there will be able to tell me what I'm doing wrong. My simple GUI should switch an LED on and off when I click on the buttons.
in my .pro file I have:
Code: Select all
#-------------------------------------------------
#
# Project created by QtCreator 2013-02-27T18:04:21
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = LED_GUI
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

#LIBS += -L/home/pi/wiringPi -lwiringPi
LIBS += -L/usr/local/lib -lwiringPi
INCLUDEPATH += /usr/local/include

and in my mainwindow.h
Code: Select all
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
   
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void ledOn();
    void ledOff();
   
private:
    Ui::MainWindow *ui;


};

#endif // MAINWINDOW_H

main.cpp:
Code: Select all
#include <QApplication>
#include "mainwindow.h"
#include "wiringPi.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    wiringPiSetupSys();
    pinMode(11,OUTPUT);

    return a.exec();
}

and mainwindow.cpp:
Code: Select all
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wiringPi.h"
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"

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

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

void MainWindow::ledOn()
{
    digitalWrite(11,HIGH);
}

void MainWindow::ledOff()
{
    digitalWrite(11,LOW);
}

When I click on the buttons it simply does nothing. No error message, no window closing and sadly no LED on/off.
Maybe someone can help please before I go completely mad!
Thanks in advance.
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by mad-hatter » Wed Feb 27, 2013 9:49 pm
Hello PS1981,

Have you connected the button click signal to your slots.

connect(pushbutton1, SIGNAL(clicked()), this, SLOT(ledOn()));
connect(pushbutton2, SIGNAL(clicked()), this, SLOT(ledOff()));

Regards
User avatar
Posts: 416
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside
by PS1981 » Wed Feb 27, 2013 10:04 pm
Many thanks for the reply mad-hatter. Where should I put this? In my form "mainwindow.ui" I have connected the slots (so graphically if that makes any sense) and this always worked with GUI's before. I never had to write the code as you have described, but maybe I'm missing something. Do you have any working sample code perhaps?
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by PS1981 » Wed Feb 27, 2013 10:30 pm
@mad-hatter: Thinking about your suggestion, I'll try to put some other event into the function, to test if the slots are working.
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by PS1981 » Wed Feb 27, 2013 11:05 pm
I have addes a lineEdit to my GUI, and have changed my mainwindow.cpp to this:
Code: Select all
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "wiringPi.h"
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"

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

}

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

void MainWindow::ledOn()
{
    ui->MainWindow::lineEdit->setText("LED ON");
    digitalWrite(11,HIGH);
}

void MainWindow::ledOff()
{
    ui->MainWindow::lineEdit->setText("LED OFF");
    digitalWrite(11,LOW);
}

Unfortunately I get the text LED ON and LED OFF in the lineEdit, but still no switching LED.
I take this to mean that my slots and buttons are working, but some connection between QT and wiringPi is not working. Very confused. :?
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by mad-hatter » Thu Feb 28, 2013 10:17 am
Hello PS1981,

Is it possible that you have selected/connected the wrong pins?

https://projects.drogon.net/raspberry-pi/wiringpi/pins/

Regards
User avatar
Posts: 416
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside
by PS1981 » Thu Feb 28, 2013 10:38 am
mad-hatter wrote:Hello PS1981,

Is it possible that you have selected/connected the wrong pins?

Regards

I think that I can rule that out as I've tested the gui setting up my LED according to GPIO numbering and according to the wiringPi numbers, but I' wondering if I've linked something wrongly. I'll check this evening when I'm home but since I'm new to Linux I'm thinking that I may have confused the wiringPi.h and the libwiringPi.so and maybe have to check where they are and make some corrections to my .pro file. Just slightly confusing also because qt definately is using the wiringPi.h, and I'm getting no error messages. Will post success or failure. Thanks.
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by PS1981 » Fri Mar 01, 2013 6:34 am
So I got it working. Main problem I think was the lack of root permission. Looking carefully at my code above some may notice that I was using wiringPiSetupSys(), my intention being to be able to make the GUI without being root. My understanding of the wiringPi internet page was that this was possible (with a few extra moves to export pins and so which I did and that worked). Anyway I decided to give up on this, log in as root and quickly make another test gui, much like the above only using wiringPiSetup() instead. This worked on the first try, so I'm going to stick with that for now. Like I said, I'm still quite new to linux, but now I've learned something new and that is not be a bad thing. :mrgreen:
Posts: 446
Joined: Wed Dec 12, 2012 4:11 pm
by Gerard0315 » Fri Oct 31, 2014 1:30 pm
Hi I have been trying to design GUI program on my Pi which includes controling the GPIO pins. I've tried what you guys mentioned in the discussion by QtCreator keeps telling me
WiringPi :Must be root to call wiringPiSetup() Did you forget sudo?

how do I fix that?

many thanks
Gerard
Posts: 7
Joined: Fri Oct 31, 2014 1:27 pm
by mad-hatter » Sat Nov 01, 2014 10:12 am
Hello Gerard0315,

To access the GPIO generally you need root privileges.
Find where your compiled program is.
Open a terminal, cd to your program location.
type:- sudo ./yourprogname.

Regards
User avatar
Posts: 416
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside
by ronaldsnoeck » Sat Mar 05, 2016 11:46 am
The next code is working.

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


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setStyleSheet("background-color: pink;");
ui->switch_on->setStyleSheet("background-color: rgb(255,0,0)");
ui->switch_off->setStyleSheet("background-color: rgb(0,255,0)");

system("gpio mode 0");
system("gpio export 17 out");

wiringPiSetupSys();
}

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


void MainWindow::on_switch_on_pressed()
{
digitalWrite(17,HIGH);
}


void MainWindow::on_switch_off_pressed()
{
digitalWrite(17,LOW);
}
Posts: 1
Joined: Sun Dec 13, 2015 3:10 pm
by chrisoma » Fri Jul 15, 2016 9:24 pm
Guys . I have my project running in one RPI but when I copy the project folder to another RPI the QT ask me "wiringPiSetup: Must Be Root. (Did you forget sudo?)"

the project and the QT are not the problem. I also verify file and folder permissions.. Any advise..?

If I run the project "sudo ./projectname" it runs normally.. the problem is when I want to run from QT (Ctrl+R)

Thanks
Posts: 4
Joined: Mon Feb 08, 2016 5:52 pm
Location: Mexicali, Mexico
by DougieLawson » Fri Jul 15, 2016 10:45 pm
mad-hatter wrote:Hello Gerard0315,

To access the GPIO generally you need root privileges.
Find where your compiled program is.
Open a terminal, cd to your program location.
type:- sudo ./yourprogname.

Regards

Not true any more.

Add
Code: Select all
setenv("WIRINGPI_GPIOMEM", "1", 1);
to your code and it will run without using sudo.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27893
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by gauravsharma0190 » Thu Sep 08, 2016 9:02 am
void MainWindow::on_Camera_clicked()
{

QPixmap p("1.jpg");
using namespace cv;
using namespace std;

wiringPiSetup();
pinMode (0, OUTPUT) ;
digitalWrite (0,1) ;
ui->label->setPixmap(p);
VideoCapture cap(0); // open the default camera

cap.set(CV_CAP_PROP_FRAME_WIDTH,620);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,572);
Mat frame; // keep it out of the loop, so we can save latest img.
int64 t0 = getTickCount(); // start time
for(;;)
{
cap >> frame; // get a new frame from camera
int64 t1 = getTickCount(); // time now
double timeRunning = double(t1-t0)/getTickFrequency();
if (timeRunning >= 1.0) // 2 seconds
break;
}
// write last frame to disk:
imwrite("/home/pi/Desktop/pics/a1.jpg", frame);
delay (1000);
digitalWrite (0, LOW) ;
QPixmap pix("r3.png");
ui->label->setPixmap(pix);

}
in this code my gui window pop up and i am able to press the button but only 1 time only .
if i again press button ,My gui close and a message pop up says
you must only call this once per programe run.this is fatal error.
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am
by gauravsharma0190 » Thu Sep 08, 2016 9:14 am
DougieLawson wrote:
mad-hatter wrote:Hello Gerard0315,

To access the GPIO generally you need root privileges.
Find where your compiled program is.
Open a terminal, cd to your program location.
type:- sudo ./yourprogname.

Regards

Not true any more.

Add
Code: Select all
setenv("WIRINGPI_GPIOMEM", "1", 1);
to your code and it will run without using sudo.


void MainWindow::on_Camera_clicked()
{

QPixmap p("1.jpg");
using namespace cv;
using namespace std;

wiringPiSetup();
pinMode (0, OUTPUT) ;
digitalWrite (0,1) ;
ui->label->setPixmap(p);
VideoCapture cap(0); // open the default camera

cap.set(CV_CAP_PROP_FRAME_WIDTH,620);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,572);
Mat frame; // keep it out of the loop, so we can save latest img.
int64 t0 = getTickCount(); // start time
for(;;)
{
cap >> frame; // get a new frame from camera
int64 t1 = getTickCount(); // time now
double timeRunning = double(t1-t0)/getTickFrequency();
if (timeRunning >= 1.0) // 2 seconds
break;
}
// write last frame to disk:
imwrite("/home/pi/Desktop/pics/a1.jpg", frame);
delay (1000);
digitalWrite (0, LOW) ;
QPixmap pix("r3.png");
ui->label->setPixmap(pix);

}
in this code my gui window pop up and i am able to press the button but only 1 time only .
if i again press button ,My gui close and a message pop up says
you must only call this once per programe run.this is fatal error.

Posts: 93
Joined: Tue Oct 28, 2014 12:06 pm

*
Posts: 103
Joined: Tue Oct 28, 2014 6:36 am
by gordon@drogon.net » Thu Sep 08, 2016 10:58 am
gauravsharma0190 wrote:in this code my gui window pop up and i am able to press the button but only 1 time only .
if i again press button ,My gui close and a message pop up says
you must only call this once per programe run.this is fatal error.

Posts: 93
Joined: Tue Oct 28, 2014 12:06 pm

*


Yes, it is a fatal error to call wiringPiSetup() more than once in a program. You need to move the setup call to outside your main loop.

The reason is that I'm fed-up with people emailing me telling me wiringPi is broken when they repeatedly call the setup code in a loop and their program subsequently runs out of file handles some time later...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1931
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by photomankc » Thu Sep 08, 2016 2:19 pm
gordon@drogon.net wrote:Yes, it is a fatal error to call wiringPiSetup() more than once in a program. You need to move the setup call to outside your main loop.

The reason is that I'm fed-up with people emailing me telling me wiringPi is broken when they repeatedly call the setup code in a loop and their program subsequently runs out of file handles some time later...

-Gordon


LOL - I don't know why but this actually made me laugh out loud. Nothing will prove every assumption you make about how people will use your library wrong faster than it coming in contact with people!
Posts: 74
Joined: Fri Aug 24, 2012 12:58 pm
by DougieLawson » Thu Sep 08, 2016 4:47 pm
gordon@drogon.net wrote:
gauravsharma0190 wrote:in this code my gui window pop up and i am able to press the button but only 1 time only .
if i again press button ,My gui close and a message pop up says
you must only call this once per programe run.this is fatal error.

Posts: 93
Joined: Tue Oct 28, 2014 12:06 pm

*


Yes, it is a fatal error to call wiringPiSetup() more than once in a program. You need to move the setup call to outside your main loop.

The reason is that I'm fed-up with people emailing me telling me wiringPi is broken when they repeatedly call the setup code in a loop and their program subsequently runs out of file handles some time later...

-Gordon


Surely it's not beyond the realms of sensibility to set a flag when wiringPiSetup() is run that you test before doing any initialisation. Second call to wiringPiSetup() effectively becomes a NOP (with a branch to function exit, as the job's already done).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27893
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK