Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sending QJsonArray over socket



  • Code Qt Side

    void MainWindow::selectedImageDisplay(QString img)
    {
        QImage imageObject;    // Make a new imageObject
        imageObject.load(img); // Load the image from path
    
        imageObject = imageObject.convertToFormat(QImage::Format_RGB16);
    
        QJsonArray RGB565;
        for(int y = 0; y < imageObject.height(); y++)
        {
            const quint16 *line = reinterpret_cast<const quint16*>(imageObject.constScanLine(y));
            for(int x = 0; x < imageObject.width(); x++)
            {
                RGB565 << *(line++);
            }
        }
    
        qDebug() << RGB565;
        socket.sendCommandStrip(QString("pixArt"), RGB565);
    }
    

    Code ESP32 Side

    class MatrixSocket
    { 
        public:
            static void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
            {
                Serial.println("Incoming data from socket!");
    
                IPAddress ip = webSocket.remoteIP(num);
    
                const size_t CAPACITY = JSON_ARRAY_SIZE(128); // Json Array Size (128x128 image size)
    
                DynamicJsonDocument inData(CAPACITY);
                StaticJsonDocument<512> outData;
    
                DeserializationError err = deserializeJson(inData, payload);
    
                switch (err.code())
                {
                    case DeserializationError::Ok:
                        Serial.println("Deserialization succeeded");
                        break;
                    case DeserializationError::EmptyInput:
                        Serial.println("Empty input!");
                        break;
                    case DeserializationError::IncompleteInput:
                        Serial.println("Incomplete input!");
                        break;
                    case DeserializationError::InvalidInput:
                        Serial.println("Invalid input!");
                        break;
                    case DeserializationError::NoMemory:
                        Serial.println("Not enough memory");
                        break;
                    case DeserializationError::NotSupported:
                        Serial.println("Not Supported");
                        break;
                    case DeserializationError::TooDeep:
                        Serial.println("Too Deep");
                        break;
                    default:
                        Serial.println("Deserialization failed");
                        break;
                }
    
                String outJsonData;
    
                if (inData["reqDeviceInfo"] && inData["reqDeviceInfo"] == "yes")
                {
                    outData["ledtype"]              = "MATRIX";
                    
                    serializeJson(outData, outJsonData);
                    webSocket.sendTXT(num, outJsonData);
    
                    Serial.println("Sending out data!");
                }
    
                if (inData["pixArt"])
                { 
                    Serial.println("Pixel Data Incoming!");
                    JsonArray src = inData["pixArt"].as<JsonArray>();
                    int i = 0;
                    for (JsonVariant p : src)
                    {
                        if (i >= 256) break; // just to be safe!
                        pixelArt[i++] = p.as<uint16_t>();
                    }
                }
    
                switch (type)
                {
                    case WStype_CONNECTED:
                        Serial.printf("[SOCKET][STATUS][%u] Connected from %d.%d.%d.%d url: %s \n", num, ip[0], ip[1], ip[2], ip[3], payload);
                    break;
    
                    case WStype_DISCONNECTED: 
                        Serial.printf("[SOCKET][STATUS] [%u] Disconnected! \n", num);
                    break;
    
                    case WStype_ERROR:
                        Serial.println("[SOCKET][ERROR] Oh Shit! \n");
                    break;
    
                    /*case WStype_TEXT:
                    case WStype_BIN:
                    case WStype_FRAGMENT_TEXT_START:
                    case WStype_FRAGMENT_BIN_START:
                    case WStype_FRAGMENT:
                    case WStype_FRAGMENT_FIN:*/
    
                    default:
                    break;
                    
                }
            }
    

    Problem
    When printing out the QJsonArray RGB565 after it's done i get this https://pastebin.com/3zgVpfkG big ass array (and that is fine)
    but when it sends it over to my ESP32 all i get is

    Incoming data from socket!
    Empty input!
    [SOCKET][STATUS] [0] Disconnected!
    

    then i get disconnected :S why? what am i doing wrong?


  • Lifetime Qt Champion

    Then print out what you really receive to see if it's what you're sending. Also print out what you really send on the sender side.



  • So i did this

    void Socket::sendCommandStrip(const QString &bName, const QJsonValue bValue)
    {
        m_webSocket.sendTextMessage(QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact));
        qDebug() << QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact);
    }
    

    and got

    "{\"pixArt\":[]}"
    

    now my question is, why? :S

    @Christian-Ehrlicher

    if i do this

    socket.sendCommandStrip(QString("pixArt"), QJsonValue(RGB565).toArray());
    

    i get the correct output (im not copying all 16k + here)

    "{\"pixArt\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,30912,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30912,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,61888,
    

    it ends with this

    0,0,0,0,0,0,0,0,0]}"
    [SOCKET][INFO] We are closing the connection
    

    again it disconnects me after :S

    on the ESP32 side i get this

    Incoming data from socket!
    Empty input!
    [SOCKET][STATUS] [0] Disconnected!
    

  • Lifetime Qt Champion

    I said you should output what you really send, not what you pass to a function you wrote.



  • @Christian-Ehrlicher that is what i send....

    qDebug() << QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact);
    

  • Lifetime Qt Champion

    So what do you get on the esp side?



  • @Christian-Ehrlicher the same as stated above

    Incoming data from socket!
    Empty input!
    [SOCKET][STATUS] [0] Disconnected!
    

    it claims inData['pixArt'] is empty and the disconnects me :S


  • Lifetime Qt Champion

    @Kris-Revi said in Sending QJsonArray over socket:

    the same as stated above

    I don't want processed output but the raw data (for the third time...)



  • @Christian-Ehrlicher
    like this?

                JsonArray test = inData["pixArt"];
    
                Serial.println(test);
                Serial.println(inData["pixArt"].as<JsonArray>());
    

    this just gives me

    Incoming data from socket!
    0
    0
    Empty input!
    [SOCKET][STATUS] [0] Disconnected!
    

  • Lifetime Qt Champion

    I'm giving up... don't know what's so hard to print out the raw incoming data. Good luck.


  • Moderators

    @Kris-Revi said in Sending QJsonArray over socket:

    like this?

    No. Please show us your payload. What does it look like before you passed it into deserializeJson()? (This is what @Christian-Ehrlicher meant by "raw data")

    Also:

    1. What is the value of length in webSocketEvent()?
    2. Converting an image into a QJsonArray is very expensive and inefficient. What is the purpose using QJsonArray here?

Log in to reply