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. Sending QJsonArray over socket
QtWS25 Last Chance

Sending QJsonArray over socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 676 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kris Revi
        wrote on last edited by Kris Revi
        #3

        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!
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          K 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

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

            K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #5

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

            qDebug() << QJsonDocument(QJsonObject{{bName, bValue}}).toJson(QJsonDocument::Compact);
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              So what do you get on the esp side?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              K 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                So what do you get on the esp side?

                K Offline
                K Offline
                Kris Revi
                wrote on last edited by
                #7

                @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

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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...)

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  K 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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...)

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on last edited by
                    #9

                    @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!
                    
                    JKSHJ 1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • K Kris Revi

                        @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!
                        
                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #11

                        @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?

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        1 Reply Last reply
                        2

                        • Login

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