Communication between devices that doesn’t require a server (TCP code examples)

TCP packet communication

Instead of implementing a “full” web server, I decided to go to a simpler TCP server – client connection. This eliminates overhead in the protocol but as well limits the usage. It is not possible to just use a browser (Chrome, Firefox, …) to send commands to a device. But I was not planing to use browsers anyway.

TCP client (control device) example for ESP8266

To be able to send a packet over TCP it is only necessary to start a WiFiClient, connect to the server (here on port 9998) and send the packet.

TCP server (sensor/actuator device) example for ESP8266

To run the TCP server part, we define a WiFiServer and start the listening to port 9998 in setup(). In loop() we check if a client has established a connection. If there is a connection, we receive the payload (with a 3 seconds timeout) and then parse the payload.

In this example I parse only for 2 commands, a simple toggle for the debug output and a command to reset the device.


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Link Directory