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. Having an issue trying to run a QNetworkAccessManager for long periods of time
QtWS25 Last Chance

Having an issue trying to run a QNetworkAccessManager for long periods of time

Scheduled Pinned Locked Moved Unsolved General and Desktop
helpqnetworkaccessqnetworkqnetworkreplynewbie
12 Posts 4 Posters 1.0k 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.
  • J Offline
    J Offline
    jinkichi
    wrote on 10 Apr 2023, 03:42 last edited by
    #1

    Hello I'm having some issues trying to run a QNAccessManager for long periods of time, basically, I'm building a GUI to check the level of water in your water tank using some Arduino serial read, and the part that is causing me a headache is the ESP8266 running a server to access the info of the ultrasonic sensor via wifi, well I'm displaying the sensor info in an html page that is read using QNetworkAccessManager transform into a QByteArray then converted to int and all the calculations are done from there, the problem is I can not keep the reading for long periods of time, sometimes it works for one hour, then for 12 minutes and so on, I think the longest time I had it running was for like 4 hours.QNetwork.PNG
    that is part of the code I find works to read the html info sent by the ESP8266 but after a few minutes it stops working I put this in the ui->setupUi(this) part cause
    I have a void that runs the reading of the serial port so I made the request to the server in the ESP8266 there
    QUrl.PNG
    so as I said before after keep running the GUI for several minutes this part stops working is only this part because the other functionalities of the gui still running the serial keep running I can turn on and off outputs in the ESP8266 is just this part that stops working I do not really know in all of this of programming even less in QT so I'll appreciate any help trying to understand what's going on here or how this code works and try to figure out a solution also sorry for the mix between English and Spanish in the code >.< thank you for taking the time to read my issue :).

    J 2 Replies Last reply 10 Apr 2023, 03:55
    0
    • J jinkichi
      10 Apr 2023, 03:42

      Hello I'm having some issues trying to run a QNAccessManager for long periods of time, basically, I'm building a GUI to check the level of water in your water tank using some Arduino serial read, and the part that is causing me a headache is the ESP8266 running a server to access the info of the ultrasonic sensor via wifi, well I'm displaying the sensor info in an html page that is read using QNetworkAccessManager transform into a QByteArray then converted to int and all the calculations are done from there, the problem is I can not keep the reading for long periods of time, sometimes it works for one hour, then for 12 minutes and so on, I think the longest time I had it running was for like 4 hours.QNetwork.PNG
      that is part of the code I find works to read the html info sent by the ESP8266 but after a few minutes it stops working I put this in the ui->setupUi(this) part cause
      I have a void that runs the reading of the serial port so I made the request to the server in the ESP8266 there
      QUrl.PNG
      so as I said before after keep running the GUI for several minutes this part stops working is only this part because the other functionalities of the gui still running the serial keep running I can turn on and off outputs in the ESP8266 is just this part that stops working I do not really know in all of this of programming even less in QT so I'll appreciate any help trying to understand what's going on here or how this code works and try to figure out a solution also sorry for the mix between English and Spanish in the code >.< thank you for taking the time to read my issue :).

      J Offline
      J Offline
      jinkichi
      wrote on 10 Apr 2023, 03:55 last edited by
      #2

      leave my code here in English in case is need it
      ad6f8549-f0f8-4e67-bc45-52de14f4a776-image.png

      J 1 Reply Last reply 10 Apr 2023, 08:29
      0
      • J jinkichi
        10 Apr 2023, 03:55

        leave my code here in English in case is need it
        ad6f8549-f0f8-4e67-bc45-52de14f4a776-image.png

        J Offline
        J Offline
        JonB
        wrote on 10 Apr 2023, 08:29 last edited by
        #3

        @jinkichi
        For what you show (much better if paste code than screenshot, and why do you indent your code so that you/we cannot tell where the braces match properly?) I am surprised anything works. You show mManager being newed, connected to finished signal and then immediately destroy(mManager) [whatever your destroy does], so don't know how you get anything to happen.

        J 1 Reply Last reply 10 Apr 2023, 18:12
        1
        • J JonB
          10 Apr 2023, 08:29

          @jinkichi
          For what you show (much better if paste code than screenshot, and why do you indent your code so that you/we cannot tell where the braces match properly?) I am surprised anything works. You show mManager being newed, connected to finished signal and then immediately destroy(mManager) [whatever your destroy does], so don't know how you get anything to happen.

          J Offline
          J Offline
          jinkichi
          wrote on 10 Apr 2023, 18:12 last edited by
          #4

          @JonB I guess that's part of the magic xD. Is it ok if I post my code I mean is like 400+ lines so figured out it will be better just to post the parts that I'm having issues with but if you say is ok I can post the complete code. also thank you for your answer.

          J 1 Reply Last reply 10 Apr 2023, 18:16
          0
          • J jinkichi
            10 Apr 2023, 18:12

            @JonB I guess that's part of the magic xD. Is it ok if I post my code I mean is like 400+ lines so figured out it will be better just to post the parts that I'm having issues with but if you say is ok I can post the complete code. also thank you for your answer.

            J Offline
            J Offline
            JonB
            wrote on 10 Apr 2023, 18:16 last edited by JonB 4 Oct 2023, 18:22
            #5

            @jinkichi
            I am not asking you to post 400+ lines of code. Somebody else might look through it, personally I won't. To get help coders should reduce their code to some minimal example which illistrates their problem for others to investigate. Sometimes in producing that they solve their own problem as well.

            The part you have posted seems to show a QNetworkAccessManager being created, connected to and then immediately destroyed, so ....

            What I was saying is whatever code you do post is better posted as pasted code than as screen shots.

            1 Reply Last reply
            1
            • J jinkichi
              10 Apr 2023, 03:42

              Hello I'm having some issues trying to run a QNAccessManager for long periods of time, basically, I'm building a GUI to check the level of water in your water tank using some Arduino serial read, and the part that is causing me a headache is the ESP8266 running a server to access the info of the ultrasonic sensor via wifi, well I'm displaying the sensor info in an html page that is read using QNetworkAccessManager transform into a QByteArray then converted to int and all the calculations are done from there, the problem is I can not keep the reading for long periods of time, sometimes it works for one hour, then for 12 minutes and so on, I think the longest time I had it running was for like 4 hours.QNetwork.PNG
              that is part of the code I find works to read the html info sent by the ESP8266 but after a few minutes it stops working I put this in the ui->setupUi(this) part cause
              I have a void that runs the reading of the serial port so I made the request to the server in the ESP8266 there
              QUrl.PNG
              so as I said before after keep running the GUI for several minutes this part stops working is only this part because the other functionalities of the gui still running the serial keep running I can turn on and off outputs in the ESP8266 is just this part that stops working I do not really know in all of this of programming even less in QT so I'll appreciate any help trying to understand what's going on here or how this code works and try to figure out a solution also sorry for the mix between English and Spanish in the code >.< thank you for taking the time to read my issue :).

              J Offline
              J Offline
              jinkichi
              wrote on 10 Apr 2023, 18:27 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • L Offline
                L Offline
                lorn.potter
                wrote on 10 Apr 2023, 18:31 last edited by
                #7

                get() returns a QNetworkReply, which you can hook up to the errorOccurred signal.

                Freelance Software Engineer, Platform Maintainer QtWebAssembly, Maintainer QtSensors
                Author, Hands-On Mobile and Embedded Development with Qt 5 http://bit.ly/HandsOnMobileEmbedded

                J 1 Reply Last reply 10 Apr 2023, 18:55
                0
                • J Offline
                  J Offline
                  jinkichi
                  wrote on 10 Apr 2023, 18:44 last edited by
                  #8

                  sorry again for the mix between Spanish and English here is the part of the code I hope this helps you to understand what I'm trying to do. sorry if it's all messy as I say before I'm not really a programmer I'm more on the electronic side of things, also yesterday trying some things I remove the delete(mManager) and nothing really changed but now I add some clear connection cache and clear Access cache but no Idea if this is gonna work.

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include "wiringPi.h"
                  #include <QtNetwork>
                  #include <QNetworkAccessManager>
                  #include <QNetworkReply>
                  #include <QNetworkRequest>
                  #include <QCoreApplication>
                  int sensor=0;
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                      , ui(new Ui::MainWindow)
                  
                  {
                      ui->setupUi(this);
                      serial = new QSerialPort(); //starts serial variable
                      arduino_available =false;
                      foreach(const QSerialPortInfo &serial_Info, QSerialPortInfo::availablePorts()){
                         qDebug()<<"puerto: "<<serial_Info.portName();
                         portname=serial_Info.portName();
                         qDebug()<<"Vendor Id: "<<serial_Info.vendorIdentifier();
                         vendorId=serial_Info.vendorIdentifier();
                         qDebug()<<"Producto: "<<serial_Info.productIdentifier();
                         productId=serial_Info.productIdentifier();
                         arduino_available= true;
                  
                      }
                      if (arduino_available){
                          arduino_init();
                      }
                      wiringPiSetup();
                      pinMode(29,OUTPUT);
                  
                          //Part that gets a value sent by the ESP8266 to and html and then convert into int
                  
                          mManager= new QNetworkAccessManager(this);
                              connect(mManager,&QNetworkAccessManager::finished,this,[&](QNetworkReply *answer)
                              {
                                 QByteArray datafromESP=answer->readAll();
                                 qDebug()<<datafromESP;
                                 int first=datafromESP.indexOf("<p>");
                                 int last=datafromESP.lastIndexOf("</p>");
                                 qInfo()<<"Start"<<first<<"End"<<last;
                                 qDebug()<<"you are here";
                                 if(first>=0&&last>=0){
                                 QString heli=datafromESP.mid(first+3,(last-first)-3);
                                 int aos=heli.toInt();
                                 if(aos>0){
                                     sensor=aos;
                                 }
                                 
                  
                                 }
                                 mManager->clearConnectionCache();
                                 mManager->clearAccessCache();
                              });
                  
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  void MainWindow::arduino_init()
                  {
                      
                      serial->setPortName("ttyACM0");
                      qDebug()<<"puerto seleccionado"<<portname;
                      serial->setBaudRate(QSerialPort::Baud9600);
                      serial->setDataBits(QSerialPort::Data8);
                      serial->setParity(QSerialPort::NoParity);
                      serial->setStopBits(QSerialPort::OneStop);
                      serial->setFlowControl(QSerialPort::NoFlowControl);
                      serial->open(QIODevice::ReadWrite);
                      connect(serial,SIGNAL(readyRead()),this,SLOT(serial_read()));
                      qDebug()<<"adios";
                  }
                  
                  
                  void MainWindow::serial_read()
                  {
                     
                      if(serial->isWritable()&&arduino_available){
                          qDebug()<<"reading data...";
                          QByteArray data = serial->readAll();
                          qDebug()<<"read : "<<data.toInt();
                          int hc=110-sensor;
                          int d=data.toInt();
                         
                          }
                          
                         
                      const QUrl conta = QUrl("http://192.168.100.159/Contador");
                      mManager->get(QNetworkRequest(conta));
                      
                  
                  }
                  
                  S 1 Reply Last reply 10 Apr 2023, 18:54
                  0
                  • J jinkichi
                    10 Apr 2023, 18:44

                    sorry again for the mix between Spanish and English here is the part of the code I hope this helps you to understand what I'm trying to do. sorry if it's all messy as I say before I'm not really a programmer I'm more on the electronic side of things, also yesterday trying some things I remove the delete(mManager) and nothing really changed but now I add some clear connection cache and clear Access cache but no Idea if this is gonna work.

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include "wiringPi.h"
                    #include <QtNetwork>
                    #include <QNetworkAccessManager>
                    #include <QNetworkReply>
                    #include <QNetworkRequest>
                    #include <QCoreApplication>
                    int sensor=0;
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    
                    {
                        ui->setupUi(this);
                        serial = new QSerialPort(); //starts serial variable
                        arduino_available =false;
                        foreach(const QSerialPortInfo &serial_Info, QSerialPortInfo::availablePorts()){
                           qDebug()<<"puerto: "<<serial_Info.portName();
                           portname=serial_Info.portName();
                           qDebug()<<"Vendor Id: "<<serial_Info.vendorIdentifier();
                           vendorId=serial_Info.vendorIdentifier();
                           qDebug()<<"Producto: "<<serial_Info.productIdentifier();
                           productId=serial_Info.productIdentifier();
                           arduino_available= true;
                    
                        }
                        if (arduino_available){
                            arduino_init();
                        }
                        wiringPiSetup();
                        pinMode(29,OUTPUT);
                    
                            //Part that gets a value sent by the ESP8266 to and html and then convert into int
                    
                            mManager= new QNetworkAccessManager(this);
                                connect(mManager,&QNetworkAccessManager::finished,this,[&](QNetworkReply *answer)
                                {
                                   QByteArray datafromESP=answer->readAll();
                                   qDebug()<<datafromESP;
                                   int first=datafromESP.indexOf("<p>");
                                   int last=datafromESP.lastIndexOf("</p>");
                                   qInfo()<<"Start"<<first<<"End"<<last;
                                   qDebug()<<"you are here";
                                   if(first>=0&&last>=0){
                                   QString heli=datafromESP.mid(first+3,(last-first)-3);
                                   int aos=heli.toInt();
                                   if(aos>0){
                                       sensor=aos;
                                   }
                                   
                    
                                   }
                                   mManager->clearConnectionCache();
                                   mManager->clearAccessCache();
                                });
                    
                    }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    void MainWindow::arduino_init()
                    {
                        
                        serial->setPortName("ttyACM0");
                        qDebug()<<"puerto seleccionado"<<portname;
                        serial->setBaudRate(QSerialPort::Baud9600);
                        serial->setDataBits(QSerialPort::Data8);
                        serial->setParity(QSerialPort::NoParity);
                        serial->setStopBits(QSerialPort::OneStop);
                        serial->setFlowControl(QSerialPort::NoFlowControl);
                        serial->open(QIODevice::ReadWrite);
                        connect(serial,SIGNAL(readyRead()),this,SLOT(serial_read()));
                        qDebug()<<"adios";
                    }
                    
                    
                    void MainWindow::serial_read()
                    {
                       
                        if(serial->isWritable()&&arduino_available){
                            qDebug()<<"reading data...";
                            QByteArray data = serial->readAll();
                            qDebug()<<"read : "<<data.toInt();
                            int hc=110-sensor;
                            int d=data.toInt();
                           
                            }
                            
                           
                        const QUrl conta = QUrl("http://192.168.100.159/Contador");
                        mManager->get(QNetworkRequest(conta));
                        
                    
                    }
                    
                    S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 10 Apr 2023, 18:54 last edited by
                    #9

                    Hi,

                    You should add error management to your code.
                    Connect the error signals of your QNAM to get information if things get awry.

                    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
                    • L lorn.potter
                      10 Apr 2023, 18:31

                      get() returns a QNetworkReply, which you can hook up to the errorOccurred signal.

                      J Offline
                      J Offline
                      jinkichi
                      wrote on 10 Apr 2023, 18:55 last edited by
                      #10

                      @lorn-potter but I get no errors after some time the mManager stops connecting to the html page in the ESP8266 but everything else in the GUI keeps working.

                      J 1 Reply Last reply 10 Apr 2023, 18:58
                      0
                      • J jinkichi
                        10 Apr 2023, 18:55

                        @lorn-potter but I get no errors after some time the mManager stops connecting to the html page in the ESP8266 but everything else in the GUI keeps working.

                        J Offline
                        J Offline
                        JonB
                        wrote on 10 Apr 2023, 18:58 last edited by JonB 4 Oct 2023, 19:00
                        #11

                        @jinkichi
                        I have no idea whether this is relevant, but why do you issue a QNetworkAccessManager::get() whenever one or more bytes are available to read?

                                QByteArray data = serial->readAll();
                                qDebug()<<"read : "<<data.toInt();
                        

                        Are you aware that, at least theoretically, serial->readAll(); could return anywhere from 1 to any number of bytes here? Are you expecting a particular number of bytes?

                        J 1 Reply Last reply 10 Apr 2023, 19:19
                        0
                        • J JonB
                          10 Apr 2023, 18:58

                          @jinkichi
                          I have no idea whether this is relevant, but why do you issue a QNetworkAccessManager::get() whenever one or more bytes are available to read?

                                  QByteArray data = serial->readAll();
                                  qDebug()<<"read : "<<data.toInt();
                          

                          Are you aware that, at least theoretically, serial->readAll(); could return anywhere from 1 to any number of bytes here? Are you expecting a particular number of bytes?

                          J Offline
                          J Offline
                          jinkichi
                          wrote on 10 Apr 2023, 19:19 last edited by
                          #12

                          @JonB That especific array allows me to read info coming from an arduino connected to my raspberry in where I'm doing all of this so the arduino sents me the dintance from a second sensor it is Just a number for example 11 or 113 or 5 is similar to What I'm trying to do with the ESP8266 but via wifi. The arduino part is always woking no matter how much time I have the gui running so I kinda figured out my mistake must be in the way I get the value from the hmtl page wich brings me to the QNetworkAccessManager.

                          1 Reply Last reply
                          0

                          3/12

                          10 Apr 2023, 08:29

                          topic:navigator.unread, 9
                          • Login

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