-3

I want to make a GUI that controls an LED over wifi (using Qt GUI) the same way it is done using web servers. But instead of a URL-based controlling, I want to control it using a Qt GUI. I have searched everywhere and could not find any answers. How can this be done?

EDIT: DETAILS ABOUT THIS PROJECT:

This code is the code that creates a web interface where there is a button to press turn on and a button to press turn off through the local IP address.

If one presses turn on it redirects from 192.168.1.xxx to 192.168.1.xxx/LED=ON (which then turns on the LED). Then if one presses turn off it redirects to 192.168.1.xxx/LED=OFF (which then turns off the LED). All of this can be done over wifi.

I would like to do the same with Qt: have two buttons that say turn on and turn off and control the LED over wifi.

(PLEASE NOTE that I know how to program a Qt GUI already, but I DO NOT know how to connect Qt with my NodeMCU ESP8266). All I am asking is how to create a GUI that controls an LED over wifi instead of using the web UI to do this, (maybe there are some QtWifi library or something) I do hope the details are now clear.

#include <ESP8266WiFi.h>

const char* ssid = "Magesh";
const char* password = "jayakumar";

int ledPin = 13; // GPIO13
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Start the server
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
  // Match the request
  int value = LOW;
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  }
  // Set ledPin according to the request
  //digitalWrite(ledPin, value);
  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");
  client.println("<html>");
  client.print("Led pin is now: ");
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
  client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
  client.println("</html>");
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
}
dda
  • 6,030
  • 2
  • 25
  • 34
user8606643
  • 13
  • 1
  • 7

1 Answers1

0

Here is a basic demo of making HTTP request(GET) with Qt GUI by using QNetworkAccessManager. For further implementation, you can catch reply via QNetworkReply from the WiFiServer.

#include <QApplication>
#include <QVBoxLayout>
#include <QPushButton>
#include <QNetworkAccessManager>
#include <QNetworkRequest>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget* w = new QWidget;
    QVBoxLayout* layout = new QVBoxLayout;
    QPushButton* on = new QPushButton("On");
    QPushButton* off = new QPushButton("Off");
    layout->addWidget(on);
    layout->addWidget(off);
    w->setLayout(layout);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    QObject::connect(on, &QPushButton::clicked,
                     [manager](){
        manager->get(QNetworkRequest(QUrl("http://192.168.1.xxx/LED=ON")));
    });

    QObject::connect(off, &QPushButton::clicked,
                     [manager](){
        manager->get(QNetworkRequest(QUrl("http://192.168.1.xxx/LED=OFF")));
    });

    w->show();

    return a.exec();
}
JustWe
  • 4,250
  • 3
  • 39
  • 90