ESP32 fail client.connect()

Hi everyone. I have a project with a ESP32-wroom2.
The project use AP+STA succesfully joined (with ip=192.168.1.129) and i am trying to upload json packages to the server ( my notebook with linux on 192.168.1.130)
There is no network problem
Traditional method that works fine ( until 1400 bytes)

        HTTPClient http;
        WiFiClient client;
        char url_full[]="http://192.168.1.130/iot_up.php";
        char http_post[]="{\"data1\":\"1\"}";
        int httpResponseCode             = 0  ;
        http.begin(client, url_full);
        http.addHeader("Content-Type", "application/json"); 
        httpResponseCode = http.POST(http_post); 
        if (httpResponseCode>0) {
          Serial.printf("dbg= http.Response code: %d\n", httpResponseCode);
          strcpy(http_dn_data, http.getString().c_str());
          Serial.printf("dbg=wrx=\"%s\"",http_dn_data);
        //  html_json_in(http_dn_data);
        } else {
        //  Serial.printf("dbg=http.Error code:%d \n",httpResponseCode);
        }

To expand the post data quantity i try to switch to this method but NOT WORK.

        int  post_len=strlen(http_post);
        HTTPClient http;
        WiFiClient client;
        char url_full[]="http://192.168.1.130";
        char http_post[]="{\"data1\":\"1\"}";
        int httpResponseCode             = 0  ;
        char http_header[1000]           =  "";    
        sprintf(http_header,"POST /iot_up.php?mac=11:22:33:44:55:66 HTTP/1.1\r\n" 
          "Host: 192.168.1.130\r\n" 
          "Accept: application/json\r\n" 
          "Content-Type: application/json\r\n" 
          "Content-Length: %d\r\n\r\n", post_len);

        Serial.printf("dbg=host:%s,port:%d\n",host, httpPort);
        //httpResponseCode = client.connect(host, httpPort);
#ifdef ARDUINO_ARCH_ESP8266
        client.setTimeout(2000);
        bool connectSuccess = client.connect("http://192.168.1.130",80);
#elif defined(ARDUINO_ARCH_ESP32)
        bool connectSuccess = client.connect("http://192.168.1.130",80, 2000);
#else
#error "Need platform specific code!!"
#endif

        if( connectSuccess) {
          Serial.printf("dbg=Connected\n");
          //Serial.printf("dbg=%d",httpResponseCode);
          Serial.printf("dbg=hd=%s\n",http_header);
          client.print(http_header);
          Serial.printf("dbg=pd=%s\n",http_post);
          client.print(http_post);
          while(client.connected()){
            //while (client.available()&& status==WL_CONNECTED) {
            while (client.available() && WiFi.status()== WL_CONNECTED) {
                char c = client.read();
                Serial.write(c);
            }
          }
        }
        http.end();

What can be the problem

I'm sure post_len won't hold the correct length of your post data. If that code doesn't generated a compile error you have at least another error in your code. As you failed to post complete code we have no clue what other errors you have in your code.

Dear Pylon.
Thanks for the answer. The complete code is bigger, 12~15 files because the data upload was the last stage of the project. I am in the doubt of create a simplified code or migrate to the AsyncHTTPRequest_Generic.

Thanks you
Christian

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.