Disconnect Callback not triggered

nwuser
Posts: 1
Joined: Tue May 01, 2018 11:10 am

Disconnect Callback not triggered

Postby nwuser » Tue May 01, 2018 11:41 am

Hello,

I am using this simple code to fetch root of http://www.google.com. I get tcp_recv on my serial monitor but the disconnect callback never gets fired. I am compiling this on Arduino IDE using the latest board from github for ESP-12S Adafruit Feather that i purchased last year. I vaguely remember it used to fire but doesnt appear to do on this new installation. Can someone suggest what could be the issue.

I read elsewhere that I needed to run espconn_init() but that errors out for missing espconn_init_lwip2()

I need the disconnect callback to fire so that I can proceed with the next sequential task. Thanks for your help!

Code: Select all

LOCAL void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
{
  struct espconn *pespconn = (espconn *)arg;
    //Serial.println(pusrdata);
  Serial.println("tcp recv !!! \r\n");
}

LOCAL void ICACHE_FLASH_ATTR
user_tcp_sent_cb(void *arg)
{
   //data sent successfully
 
    Serial.println("tcp sent succeed !!! \r\n");
}

LOCAL void ICACHE_FLASH_ATTR
user_tcp_discon_cb(void *arg)
{
   //tcp disconnect successfully
   
    Serial.println("tcp disconnect succeed !!! \r\n");
}

LOCAL void ICACHE_FLASH_ATTR
user_connect(struct espconn *pespconn)
{
    char *pbuf = (char *)os_zalloc(2048);
    Serial.println("Sending 0");
    //key="";
    os_sprintf(pbuf, "GET / HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n","www.google.com");

  espconn_sent(pespconn, (uint8 *)pbuf, os_strlen(pbuf));
  os_free(pbuf);
}

LOCAL void ICACHE_FLASH_ATTR
user_send_data()
{
  char data[]={0x81, 0x83,0xFF, 0xFF, 0xFF, 0xFF, 0xBE, 0xBE, 0xBE};
  espconn_sent(&clientconn, (uint8 *)&data[0],9);
}

LOCAL void ICACHE_FLASH_ATTR
user_tcp_connect_cb(void *arg)
{
    struct espconn *pespconn = (espconn *)arg;
    Serial.println(pespconn->state);
 
    Serial.println("connect succeed !!! \r\n");
 
    espconn_regist_recvcb(pespconn, user_tcp_recv_cb);
    espconn_regist_sentcb(pespconn, user_tcp_sent_cb);
    espconn_regist_disconcb(pespconn, user_tcp_discon_cb);
   
    user_connect(pespconn);
}
 
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("reconnect callback, error code !!!");
    Serial.println(err);
}

 
LOCAL void ICACHE_FLASH_ATTR
user_tcp_discon_cb(void *arg, sint8 err)
{
   //error occured , tcp connection broke. user can try to reconnect here.
   
    Serial.println("Disconnect callback, error code !!!");
    Serial.println(err);
}


LOCAL void ICACHE_FLASH_ATTR send_req(void* arg){
  //espconn_init();
  struct espconn *pespconn = (espconn *) arg;
    Serial.println("Sending req in State 0");
    pespconn->proto.tcp->remote_port = 80;   
 
  pespconn->proto.tcp->remote_ip[0] = 216;
  pespconn->proto.tcp->remote_ip[1] = 58;
  pespconn->proto.tcp->remote_ip[2] = 217;
  pespconn->proto.tcp->remote_ip[3] = 36;
//        tcp_server_ip.addr = ipaddr->addr;
    yield();
  pespconn->proto.tcp->local_port = espconn_port(); //local port of ESP8266
  Serial.println(pespconn->proto.tcp->local_port);
  yield();
  espconn_regist_connectcb(pespconn, user_tcp_connect_cb); // register connect callback
  yield();
  espconn_regist_reconcb(pespconn, user_tcp_recon_cb); // register reconnect callback as error handler
  yield();
  espconn_regist_disconcb(pespconn, user_tcp_discon_cb); // register reconnect callback as error handler
  yield();
  Serial.print(espconn_connect(pespconn)); // tcp connect
  Serial.println(" :Connect initiated");
    Serial.println(pespconn->state);

}

//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());
  Serial.print("Status code from server: ");
  yield();
  _esp_tcp mytcp;
  mytcp.local_port=9000;
  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();
  send_req(&clientconn);


  while(1)
  {
    delay(1000);
    yield();
  }
}



void loop()
{
}

Her Majesty
Posts: 337
Joined: Mon Oct 27, 2014 11:09 am

Re: Disconnect Callback not triggered

Postby Her Majesty » Thu Aug 30, 2018 11:50 am

Maybe it goes to the reconnect callback, you can add some logs to check.

AgentSmithers
Posts: 123
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Disconnect Callback not triggered

Postby AgentSmithers » Tue Sep 25, 2018 3:17 am

NwUser, Did you resolve this issue. If not I can replicate this here by tomorrow if you like and respond with my findings if this is still a problem?

blubb
Posts: 110
Joined: Mon Jun 22, 2015 5:35 am

Re: Disconnect Callback not triggered

Postby blubb » Wed Sep 26, 2018 1:37 am

You need to put "Connection: close\r\n" in your request.

Who is online

Users browsing this forum: Google [Bot] and 11 guests