Help - Latest SDK not sending TCP GET request as it used to

sgoggin
Posts: 7
Joined: Thu Apr 16, 2015 9:21 pm

Help - Latest SDK not sending TCP GET request as it used to

Postby sgoggin » Sat Apr 18, 2015 8:59 pm

Hello All,

I have an odd issue.

My code, when compiled using esp_iot_sdk_v0.9.3_14_11_21 works correctly, it sends a data sample to Thinksgpeak every 15 seconds (or 60 seconds). In thingspeak I can see the count steadily increasing and when I look at it's os_printf output I see it send the packet very quickly and instantly after the espconn_sent:

Here is a WORKING output

Code: Select all

Waiting for IP...
add 0
aid 6
pm open phy2,type:2 0 0
cnt

connected with LightningStrikesTwice, channel 1
dhcp client start...
ip:10.1.1.164,mask:255.255.255.0,gw:10.1.1.1
UDP connection set
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=0&field2=0&field3=40968 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:49:51 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "34173cb38f07f89ddbebc2ac9128303f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9b3a562c-7355-4f7d-9725-d7fbc959beb2

2
30
0

]TS: TCP disconnect
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=1&field2=0&field3=40968 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:50:06 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "c16a5320fa475530d9583c34fd356ef5"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: d572ac02-c7f0-4544-8d59-e526e5582b06

2
31
0

]TS: TCP disconnect
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=2&field2=0&field3=40968 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:50:21 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "6364d3f0f495b6ab9dcf8d3b5c6e0b01"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 235182d5-120f-4e1a-8e1a-5ef2baccea97

2
32
0

]TS: TCP disconnect
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=3&field2=0&field3=40968 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:50:36 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "182be0c5cdcd5072bb1864cdee4d3d6e"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 5ea17087-044f-4a69-a472-06419b039210

2
33
0

]TS: TCP disconnect
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=4&field2=0&field3=40968 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:50:51 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "e369853df766fa44e1ed0ff613f563bd"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: d531bc3b-3bc0-4e75-9d7b-bfb9aacff8df

2
34
0


You can see that's it's getting a response for every send.

So If I update the SDK for the latest version (Which I want to upgrade to) then the sending becomes slow. In the terminal I can see it's taking a very long time to get the response. And it only sends every second sample. In think speak I can see samples 1,3,5,7 etc. This is exactly the same code - the only difference is I swapped the SDK directories for the latest non BETA version. The os_printf output looks like this:

Code: Select all

Waiting for IP...
ip:10.1.1.164,mask:255.255.255.0,gw:10.1.1.1
UDP connection set
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=0&field2=0&field3=39328 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=1&field2=0&field3=39792 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:57:33 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "72b32a1f754ba1c09b3695e0cb6cde7f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 849ffcea-95e2-40ad-9f51-29b39ff1d5ba

2
57
0

]TS: TCP disconnect
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=2&field2=0&field3=39608 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=3&field2=0&field3=39792 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 12:58:03 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "66f041e16a60928b05a7e228a89c3799"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9051722f-829b-45cd-a426-bcf66ea700ff

2
58
0

]TS: TCP disconnect


In the above - you can see that the first sample (number 0) does not get a response, the second one DOES, and then the third does NOT. It's only sending every second sample.

Is this a bug in the SDK? Or is there something wrong in my code.


(Here is the code that sends the data to Thingspeak - it get's called every 15 seconds in this example).

Code: Select all

#include "c_types.h"
#include "ip_addr.h"
#include "ets_sys.h"
#include "espconn.h"
#include "osapi.h"
#include "mem.h"
#include "gpio.h"
#include "os_type.h"
#include "user_config.h"
#include "user_interface.h"
#include "driver/uart.h"

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1

void ICACHE_FLASH_ATTR network_init();

static esp_tcp global_tcp;                                  // TCP connect var (see espconn.h)
static struct espconn global_tcp_connect;                   // Connection struct (see espconn.h)
static uint8_t tcp_conn_ok = FALSE;                         // Bool to know if tcp connection set


static void ICACHE_FLASH_ATTR ThingSpeakThingSpeaktcpNetworkRecvCb(void *arg, char *data, unsigned short len)
{
   
   os_printf("TS: RECV - [%s]",data);
}

static void ICACHE_FLASH_ATTR ThingSpeaktcpNetworkConnectedCb(void *arg)
{
  struct espconn *tcpconn=(struct espconn *)arg; 
  // Thingspeak sends back data so define a  Call back for this.
  espconn_regist_recvcb(tcpconn, ThingSpeakThingSpeaktcpNetworkRecvCb);
   
  os_printf("TS: TCP connected\n\r");
  tcp_conn_ok = TRUE;
 
}

static void ICACHE_FLASH_ATTR ThingSpeaktcpNetworkReconCb(void *arg, sint8 err)
{
  os_printf("TS: TCP reconnect\n\r");
  tcp_conn_ok = FALSE;
  network_init();
}


static void ICACHE_FLASH_ATTR ThingSpeaktcpNetworkDisconCb(void *arg)
{
  os_printf("TS: TCP disconnect\n\r");
  tcp_conn_ok = FALSE;
  network_init();
}



char* itoa(int i, char b[])  // Convert Integer to ASCII!!
{
  char const digit[] = "0123456789";
  char* p = b;
  if(i<0){
    *p++ = '-';
    i *= -1;
  }
  int shifter = i;
  do{ //Move to where representation ends
    ++p;
    shifter = shifter/10;
  }
  while(shifter);
  *p = '\0';
  do{ //Move back, inserting digits as u go
    *--p = digit[i%10];
    i = i/10;
  }
  while(i);
  return b;
}




void ThingSpeak (int P_count, int P_value)
//
// Executes every MINUTE
// Collection routine to grab the debounced pulses every period
//
{
   
  // Define the Thingspeak feed
  // char WriteAPIKey[]="MWL56OHOIPP1SM8O";  // LIVE
  char WriteAPIKey[]="NIBK61K9A1S5KB92"; // TEST
  char TS_count[10]="100";
  char TS_value[10]="0";
  char data[120];
 
  // Update Server
  global_tcp_connect.type=ESPCONN_TCP;                                    // We want to make a TCP connection
  global_tcp_connect.state=ESPCONN_NONE;                                  // Set default state to none
  global_tcp_connect.proto.tcp=&global_tcp;                               // Give a pointer to our TCP var
  global_tcp_connect.proto.tcp->local_port=espconn_port();                // Ask a free local port to the API
 
  // api.thingspeak.com 184.106.153.149
  global_tcp_connect.proto.tcp->remote_port=80;                         // Set remote port (bcbcostam)
  global_tcp_connect.proto.tcp->remote_ip[0]=184;                          // Your computer IP
  global_tcp_connect.proto.tcp->remote_ip[1]=106;                           // Your computer IP
  global_tcp_connect.proto.tcp->remote_ip[2]=153;                         // Your computer IP
  global_tcp_connect.proto.tcp->remote_ip[3]=149;                         // Your computer IP
 
  espconn_regist_connectcb(&global_tcp_connect, ThingSpeaktcpNetworkConnectedCb);   // Register connect callback
  espconn_regist_disconcb(&global_tcp_connect, ThingSpeaktcpNetworkDisconCb);       // Register disconnect callback
  espconn_regist_reconcb(&global_tcp_connect, ThingSpeaktcpNetworkReconCb);         // Register reconnection function
  espconn_connect(&global_tcp_connect);                                   // Start connection

  // ----


  os_printf("TS: TCP Send to Server\n\r");
  itoa(P_count,TS_count);
  itoa(P_value,TS_value);
   
 
   uint16_t heap=system_get_free_heap_size();
  // uint8_t cpuspeed=system_get_cpu_freq();
  // os_printf(" HEAP=[%u] CPU_FREQ=[%u] \n\r",heap,cpuspeed);
     
   os_sprintf(data, "GET http://api.thingspeak.com/update?api_key=%s&field1=%s&field2=%s&field3=%lu HTTP/1.1\r\nHost: api.thingspeak.com\r\nUser-agent: the best\r\nConnection: close\r\n\r\n",
       WriteAPIKey, TS_count,TS_value,heap);
   os_printf ("TS: Sending this - [%s]",data);
   espconn_sent(&global_tcp_connect, data, strlen(data)); 


 
}


Last edited by sgoggin on Sun Apr 19, 2015 8:18 pm, edited 1 time in total.

sgoggin
Posts: 7
Joined: Thu Apr 16, 2015 9:21 pm

Re: Help - Latest SDK not sending GET request as it used to

Postby sgoggin » Sat Apr 18, 2015 9:30 pm

Tested with esp_iot_sdk_v1.0.1_b2_15_04_10.zip - and still not working correctly

The samples that are not being sent stop here:

Code: Select all

TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=8&field2=0&field3=39600 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: TCP connected

and they do not proceed past this point, until the next sample is sent, and then it continues with:

Code: Select all

0
TS: TCP connected
TS: TCP Send to Server
TS: Sending this - [GET http://api.thingspeak.com/update?api_key=NIBK61K9A1S5KB92&field1=13&field2=0&field3=39784 HTTP/1.1
Host: api.thingspeak.com
User-agent: the best
Connection: close

]UDP Send to Server
0
TS: RECV - [HTTP/1.1 200 OK
Server: nginx/1.7.5
Date: Sat, 18 Apr 2015 13:29:07 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "5ef059938ba799aaa845e1c2e8a762bd"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: b42797f4-66f1-4c5e-bbfe-578e839d38c0

3
118
0

]TS: TCP disconnect




I thought it might work if I send the samples every 2 minutes - but it makes no difference - the send packets seem to get stuck in the send queue and are not sent until the next one comes in, but then it loses the first packet.

tve
Posts: 123
Joined: Sun Feb 15, 2015 4:33 pm

Re: Help - Latest SDK not sending TCP GET request as it used

Postby tve » Sun Apr 19, 2015 10:10 pm

Are you aware of the fact that the semantics of espconn_sent changed between 0.9.3 and 1.0?

sgoggin
Posts: 7
Joined: Thu Apr 16, 2015 9:21 pm

Re: Help - Latest SDK not sending TCP GET request as it used

Postby sgoggin » Mon Apr 20, 2015 7:32 am

ah.. no... thanks some reading to do then.

sgoggin
Posts: 7
Joined: Thu Apr 16, 2015 9:21 pm

Re: Help - Latest SDK not sending TCP GET request as it used

Postby sgoggin » Mon Apr 20, 2015 1:09 pm

thank you!

It's working now - I followed the basic example shown for a tcp client in the documents section of this forum, it's all working now exactly is it should be.

Who is online

Users browsing this forum: No registered users and 151 guests