# TCPClient as simple logger \[SOLVED\]

**URL:** <https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014>\
**Category:** Firmware\
**Created:** [February 21, 2014, 7:49am UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014 "2014-02-21T07:49:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 7:49am UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/1 "2014-02-21T07:49:40Z")

</div>

**Summary** : For TCPClient to work nicely it is absolutelly required to wait for server to start responding before client.flush/client.stop operations  
**Sample solution** : [https://community.spark.io/t/tcpclient-as-simple-logger/3014/22?u=ryotsuke](https://community.spark.io/t/tcpclient-as-simple-logger/3014/22?u=ryotsuke)

**Below original question**

How should I use TCPClient to use it as simple data logger?  
Currently I have a very simple php backend:

```
<? if("START"==$_SERVER['QUERY_STRING']) {
   file_put_contents("log", "");
 } else { 
   file_put_contents("log", $_SERVER['QUERY_STRING']."\r\n", FILE_APPEND);
 }

```

No content is returned.  
In Spark i use something like this:

```
void setup() {
    client.connect(server, 9606);
}
void loop() {
   if(client.connected()) {
    client.flush(); //we dont need to read anything, flush it
    client.println("GET /l.php?ping"+(String)millis()+" HTTP/1.0");//this is received by server only once :frowning: 
    client.println();    
   
  }
  //do stuff, at least for 0.1 seconds
}

```

Currently similar code has hang my core to factory reset state. I decided that at this point I need an advice how to proceess further.

Parameters of server/port are verified and correct.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 8:06am UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/2 "2014-02-21T08:06:30Z")

</div>

Can you add in the missing lines like client.print("Host: "); and test again? 🙂

> [@Is my web service the problem?](https://community.particle.io/t/is-my-web-service-the-problem/2987/9):
>
> if (myTCP.connect(server, 80)) {  
> [//Serial1.print](https://Serial1.print)("Connected to Server");  
> myTCP.print("GET ");  
> myTCP.print(url);  
> myTCP.println(" HTTP/1.0");  
> myTCP.println("Connection: close");  
> myTCP.print("Host: ");  
> myTCP.println(server);  
> myTCP.println("Accept: text/html, text/plain");  
> myTCP.println();  
> myTCP.flush();  
> }
> 
> }

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 8:09am UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/3 "2014-02-21T08:09:59Z")

</div>

Do I need to tcp.connect tcp.print tcp.flush tcp.stop every request?  
**First** request goes fine without host, IP+port is enough. So issue about constant **repeated** requests.

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 11:37am UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/4 "2014-02-21T11:37:42Z")

</div>

Code below in loop works mostly fine, but produces up to 40 seconds gaps [http://screenshots.ryotsuke.ru/scr\_eac2d6c02184.png](http://screenshots.ryotsuke.ru/scr_eac2d6c02184.png)  
How can I get rid of delay(magicValue)? How can I make “connect” timeout much lower - 1 sec max

```
 void loop() {
    if(client.connect(server, 9606)) {
       client.println("GET /v.php?"+something+" HTTP/1.0");
       client.println();
       delay(50);
       while(client.available()) {
         client.read();
        }
        client.stop();   
    } 
}
```

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 12:43pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/5 "2014-02-21T12:43:37Z")

</div>

Sorry but I can’t figure out what you are logging from the server. :X

The screenshot looks ok except for the few thicker area. Can’t relate how the gap inbetween 2 is a delay…

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 1:00pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/6 "2014-02-21T13:00:30Z")

</div>

Thicker area is area where data was received. X axis is seconds from core start. Thin line is when no data was coming.  
It doesn’t matter what I am logging at all.

Question is what is _correct_ way of **repeated** frequent sending GET requests. Using magic delay value does not seem to be right.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 2:11pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/7 "2014-02-21T14:11:08Z")

</div>

One quick way I can think of is to serial output at some specific points and watch it on the serial terminal.

Might have a clue where’s the delay coming from

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 21, 2014, 3:14pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/8 "2014-02-21T15:14:45Z")

</div>

Hi @ryotsuke,

Make sure you’re making a normal full HTTP request, and also cleaning up your socket when done. You also probably want a delay between when you open a connection to the server, otherwise you’ll be creating and destroying sockets (an expensive operation) very frequently. I had an example function here that also included basic http auth: [https://community.spark.io/t/doorbell-to-send-jsonrpc/2805/2?u=dave](https://community.spark.io/t/doorbell-to-send-jsonrpc/2805/2?u=dave)

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 3:20pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/10 "2014-02-21T15:20:31Z")

</div>

[http://screenshots.ryotsuke.ru/scr\_3d46f175e771.png](http://screenshots.ryotsuke.ru/scr_3d46f175e771.png) This looks ok?  
Also, does client.connect has configurable timeout?

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 21, 2014, 3:35pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/11 "2014-02-21T15:35:55Z")

</div>

I think you want String(voltage1) and not (String)voltage1, yeah?

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 3:41pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/12 "2014-02-21T15:41:26Z")

</div>

For some reason this

> [@ryotsuke](#):
>
> ```
> delay(50);
> while(client.available()) {
> client.read();
> }
> client.stop();
> 
> ```

Is working more stable(sendin more frequently) than new version: [http://screenshots.ryotsuke.ru/scr\_3d46f175e771.png](http://screenshots.ryotsuke.ru/scr_3d46f175e771.png)  
How do you think, what can be the reason?

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 3:44pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/13 "2014-02-21T15:44:01Z")

</div>

Maybe like what @Dave said, the server is being hit with too many request at one go.

Imagine the core repeatedly sending GET requests within a short span of time. Insufficient time to react.

You server logs might give you some clue!

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 3:52pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/14 "2014-02-21T15:52:53Z")

</div>

server is in local lan and would be capable of handling over 100 requests per sec. so as fast as spark can send, server will be able to respond

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 3:56pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/15 "2014-02-21T15:56:10Z")

</div>

How about you add some serial output previously mentioned and have a visual on where is the problem?

And what does your server log say? logging 100 requests per seconds? I guess not since previously you mentioned the data has a 40 seconds gap

We can solve this together faster with more details 😄

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 4:04pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/16 "2014-02-21T16:04:00Z")

</div>

40 seconds gaps are when spark is sending requests. When I send from other PC in a loop I’m able to get over 100 log entries per sec.

Please, I want to know internal firmware difference between client.flush and while(client.available) { client.read } and only guys understanding firmware code can answer this.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 21, 2014, 4:05pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/17 "2014-02-21T16:05:17Z")

</div>

Interestingly enough, @finsprings is also working on streaming data via http from the core rapidly here: [https://community.spark.io/t/non-cyan-flash-offline-core/3012/25](https://community.spark.io/t/non-cyan-flash-offline-core/3012/25) maybe we should join forces? 😄

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 4:14pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/18 "2014-02-21T16:14:30Z")

</div>

One more question - is timeouts for connect function are configurable?

As if not I feed it would be more reasonable to use a TCPServer and wait for requests instead of sending to server. I don’t want to have long blocks if WiFi connection drops.

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [February 21, 2014, 4:26pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/19 "2014-02-21T16:26:58Z")

</div>

Hmm. I am not sure about the connection timeout for TCPClient. I’m asking our firmware guys about it, but it looks like it’s not easily exposed at the moment.

---

<div class="post-metadata">

**Author:** ![ryotsuke](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ryotsuke/32/1902_2.png) [@ryotsuke](https://community.particle.io/u/ryotsuke)\
**Post date:** [February 21, 2014, 4:43pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/20 "2014-02-21T16:43:02Z")

</div>

Hmm, interesting. It appears Spark is breaking connection too early. So actually requests are ~500 micros  
Apparently when sending from other PC i am waiting till request works, and in spark not. I guess PC can do multiple socket requests and because of that it is working much faster.  
Addining a delay(50) **before** client.flush, client.stop makes things much more reliable. I still don’t like idea of using magic delay values. I would like to have a method to actually wait for server to completely respond.  
I have one idea to test… Will report in a while

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [February 21, 2014, 4:45pm UTC](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014/21 "2014-02-21T16:45:58Z")

</div>

Got my Spark core stucked in a loop and not being able to flash firmware using Spark Cloud.

hahaha! We might want to have that fix in future 😄

[Next page](https://community.particle.io/t/tcpclient-as-simple-logger-solved/3014.md?page=2)
