Connecting to a wifi network
I'm trying to use QProcess to issue a series of system calls to connect to a wifi network after receiving the network name and key - and detect success or not.
Here's part of the code:
QProcess process; process.start("ifconfig wlan0 down"); process.waitForFinished(-1); process.close(); process.start("ifconfig wlan0 up"); process.waitForFinished(-1); process.close(); process.start("iwconfig wlan0 key " + piEnvironment::wifiKey); process.waitForFinished(-1); process.close(); process.start("iwconfig wlan0 essid " + piEnvironment::wifiName); process.waitForFinished(-1); process.close(); process.start("iwconfig wlan0 ap auto"); process.waitForFinished(-1); process.close(); process.start("udhcpc -n -f -q -i wlan0"); process.waitForFinished(-1); QByteArray out = process.readAll(); process.close(); int index = out.indexOf("No lease, failing"); if (index>-1) return false;
The above returns false regardless of whether I set the correct wifiName and wifiKey. The out array actually says that a lease has been obtained? - most confusing!
It would be great if anyone could point out where I'm going wrong.
This line check if out array contain the words "No lease, failing".
int index = out.indexOf("No lease, failing");
I don't know an output of udhcpc. Maybe its output contains these words in both cases.
What is a content of the out array?
Hi, thanks for reply.
I checked the contents of udhcpc output both on success and failure and the phrase 'No lease, failing' only seems to crop up on failure.
It appears that some of the output these commands give when called manually in a terminal does not happen when called from within QProcess. From what I have read so far this could be due to timing. I understand now that process.waitForFinished(-1) might return for some commands before it's complete. I am still investigating and welcome any further comments...
How did your app turn out? I am also using QProcess to call a series of commands (namely, iw, iwlist, wpa_passphrase, wpa_supplicant, and udhcpc). I do have a different way of passing arguments to command lines though, for example:
QStringList args; args << ui ->ifaceLine->text(); args << "scan"; scanProcess.setStandardOutputProcess(&grepProcess); scanProcess.start("iwlist", args); grepProcess.start("grep", QStringList() << "ESSID"); grepProcess.waitForFinished();
Whenever I call udhcpc, I get stuck at "Sending discover". I can't get past this message, so any info you learned can really help a long way.