2015/04/20

WiFi for everything #2

As I mentioned in my previous post, I tried to transfer data over TCP connection. It succeeded, but it was not so easy. There were difficulties on both sides of the communication.

The plan was to create a client WP 8.1 app for my phone, which connects to the TCP server (hardware with WiFi module), sends a short message, receives a reply message - temperature value measured by the server -, then closes the connection. On the server side I put a DS18B20 temperature sensor to the microcontroller, and modified the code - TCP server, measurement, host functions.

I found a pretty good sample code for TCP client app in the Microsoft Dev Center. It includes simple functions, like TCP Connect, Send, Receive, Close. Using these methodes I wrote a simple app, which establishes a connection and sends the string "temp" in every 2 seconds.

After a few tests I found out, that there is problem with the sending - the connection builds up, the sending succeeds (checking the method return value), but there is no outgoing TCP "temp" packet in Wireshark.

Have you ever heard about the Nagle's algorithm? Neither have I. It is used to improve the efficiency of TCP/IP networks by reducing the number of packets that need to be sent over the network. That means small messages will be sent out in one TCP packet. In my case sending the packet with 4 byte payload was transferred right just before the connection was closed.

I found on the internet a simple solution for this problem. Putting \r\n bytes (0x0D, 0x0A) at the end of the payload makes the network driver to send the small packet immediately out (e.g. HTTP GET requests). I verified it in Wireshark.


After that I changed the server's code for waiting "temp\r\n" request, but it always crashed. The problem was the difference between the AT commands of the WiFi module in the case of incoming TCP and UDP packets. The syntax is different, and there is nothing about it in the datasheet :) So the solution was to fix my library.

Both the client and the server worked finally. First I used the WP emulator of the Visual Studio, then I registered my phone for WP app development, and tried the client app on it.


So what's coming next? I recently bought an electric fish feeder...

3 comments:

  1. Yes mate WiFi is everything it's life man everything seems to just stucked or stooped without wife as it is allowed in every working system. Great share.

    ReplyDelete
  2. You have some really good ideas in this article. I am glad I read this. I agree with much of what you state in this article. Your information is thought-provoking, interesting and well-written. Network drive in Windows 10

    ReplyDelete
  3. Casino games | DrMCD
    Casino games in your browser · Quick 광명 출장샵 Summary · Quick Facts · All 제주도 출장샵 Casino games 공주 출장마사지 in the 천안 출장안마 casino are played in the browser · Quick Facts · All Casino games are played 창원 출장샵

    ReplyDelete