Having an issue trying to run a QNetworkAccessManager for long periods of time
-
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.
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
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 :). -
@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 showmManager
beingnew
ed,connect
ed tofinished
signal and then immediatelydestroy(mManager)
[whatever yourdestroy
does], so don't know how you get anything to happen. -
@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.
-
@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.
-
get() returns a QNetworkReply, which you can hook up to the errorOccurred signal.
-
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)); }
-
@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.
-
@jinkichi
I have no idea whether this is relevant, but why do you issue aQNetworkAccessManager::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? -
@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.