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