-->
Page 1 of 1

Disconnect callback not being triggered

PostPosted: Thu May 03, 2018 8:48 am
by nwuser
Hello all,

I am trying a simple webpage pull from my local server based off the espressif sample code and the Arduino-Huzzah combo. I am getting it to connect, send request and receive data but the disconnect callback never gets fired. I do need the disconnect callback to fire for future programming needs. I tried espconn_init() hoping that might be the issue but that throws and error for missing espconn_init_lwip2() which is defined in lwip2 lib. Can someone suggest why the disconnect doesnt fire at the end of the pull in the below code? Thanks!

The Huzzah I am using is this https://www.adafruit.com/product/2821 that I purchased, I think, a year back :)

Code: Select all    const char* TLSHEAD = "GET / HTTP/1.1\r\nHost: %d.%d.%d.%d\r\nConnection: keep-alive\r\n\r\n";

    LOCAL os_timer_t test_timer;
    LOCAL struct espconn user_tcp_conn, clientconn;
    LOCAL struct _esp_tcp user_tcp;
    ip_addr_t tcp_server_ip;
     
    /******************************************************************************
     * FunctionName : user_tcp_recv_cb
     * Description  : receive callback.
     * Parameters   : arg -- Additional argument to pass to the callback function
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
    {
       //received some data from tcp connection
       Serial.println ("Received data");
        os_printf("Received data string: %s \r\n", pusrdata);
         
    }
    /******************************************************************************
     * FunctionName : user_tcp_sent_cb
     * Description  : data sent callback.
     * Parameters   : arg -- Additional argument to pass to the callback function
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_tcp_sent_cb(void *arg)
    {
       //data sent successfully
      Serial.println("Sent data");
        os_printf("Sent callback: data sent successfully.\r\n");
    }
    /******************************************************************************
     * FunctionName : user_tcp_discon_cb
     * Description  : disconnect callback.
     * Parameters   : arg -- Additional argument to pass to the callback function
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg)
    {
       //tcp disconnect successfully
        Serial.println("Disconnected");
        os_printf("Disconnected from server.\r\n");
    }
    /******************************************************************************
     * FunctionName : user_esp_platform_sent
     * Description  : Processing the application data and sending it to the host
     * Parameters   : pespconn -- the espconn used to connetion with the host
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_send_data(struct espconn *pespconn)
    {
       char *pbuf = (char *)os_zalloc(packet_size);
      Serial.println("Sending data");
       os_sprintf(pbuf, TLSHEAD,
        pespconn->proto.tcp->remote_ip[0], pespconn->proto.tcp->remote_ip[1],
        pespconn->proto.tcp->remote_ip[2], pespconn->proto.tcp->remote_ip[3]);
     
       espconn_send(pespconn, (uint8 *)pbuf, os_strlen(pbuf));
         
       os_free(pbuf);
     
    }
     
    /******************************************************************************
     * FunctionName : user_tcp_connect_cb
     * Description  : A new incoming tcp connection has been connected.
     * Parameters   : arg -- Additional argument to pass to the callback function
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg)
    {
        struct espconn *pespconn = (espconn *)arg;
        Serial.println("Connected");
        os_printf("Connected to server...\r\n");
     
        espconn_regist_recvcb(pespconn, user_tcp_recv_cb);
        espconn_regist_sentcb(pespconn, user_tcp_sent_cb);
         
        user_send_data(pespconn);
    }
     
    /******************************************************************************
     * FunctionName : user_tcp_recon_cb
     * Description  : reconnect callback, error occured in TCP connection.
     * Parameters   : arg -- Additional argument to pass to the callback function
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 err)
    {
       //error occured , tcp connection broke. user can try to reconnect here.
       Serial.println("Reconnecting");
        os_printf("Reconnect callback called, error code: %d !!! \r\n",err);
    }
    /******************************************************************************
     * FunctionName : user_dns_found
     * Description  : dns found callback
     * Parameters   : name -- pointer to the name that was looked up.
     *                ipaddr -- pointer to an ip_addr_t containing the IP address of
     *                the hostname, or NULL if the name could not be found (or on any
     *                other error).
     *                callback_arg -- a user-specified callback argument passed to
     *                dns_gethostbyname
     * Returns      : none
    *******************************************************************************/
    LOCAL void ICACHE_FLASH_ATTR user_dns_found(void *arg)
    {
      Serial.println("Starting connection");
        struct espconn *pespconn = (struct espconn *)arg;
     
      pespconn->proto.tcp->remote_ip[0] = 192;
      pespconn->proto.tcp->remote_ip[1] = 168;
      pespconn->proto.tcp->remote_ip[2] = 1;
      pespconn->proto.tcp->remote_ip[3] = 164;
     
      pespconn->proto.tcp->remote_port = 8000; // remote port of tcp server
     
      pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266

      espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback
      espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler
      espconn_regist_disconcb(pespconn, user_tcp_discon_cb);  // register disconnect callback here,
       
      espconn_connect(pespconn); // tcp connect
    }
     


    //Setup
    void setup() {
     
      //bounce = 0;
      pinMode(0, OUTPUT);
      digitalWrite(0,HIGH);
      Serial.begin(9600);
      // Connect via DHCP
      Serial.println("connect to network");
      //client.dhcp();
      randomSeed(ESP.getCycleCount());
      // Connect to WiFi
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
      currentState=0;
      // Print the IP address
      Serial.println(WiFi.localIP());
      //response = "";
      //int statusCode = client.get("/", &response);
      Serial.print("Status code from server: ");
      //Serial.println(statusCode);
      //Serial.print("Response body from server: ");
      yield();
      //Serial.println(response);
      yield();
      _esp_tcp mytcp;
      mytcp.local_ip[0]=192;
      mytcp.local_ip[1]=168;
      mytcp.local_ip[2]=1;
      mytcp.local_ip[3]=165;
      yield();
      clientconn.type=ESPCONN_TCP;
      clientconn.state = ESPCONN_NONE;
      clientconn.proto.tcp=&mytcp;
      yield();
      user_dns_found(&clientconn);
      while(1)
      {
        yield();
        delay(1000);
      }
    }



    void loop()
    {
    }