Control over LAN

Last Updated on : 2023-12-19 08:36:58

A local area network (LAN) is formed using two or more interconnected computers. A home network is a small-scale LAN that connects computers, mobile phones, smart devices, and other devices to the same router. The LAN in this topic refers to the wireless network to which both mobile phones and Wi-Fi devices are connected.

Feature description

The process of device control over LAN:

  • Discover the device.

    1. After being connected to the router, the device regularly broadcasts its presence as a client using the user datagram protocol (UDP).
    2. After the mobile app connects to the router and enters the control panel of the target device, it acts as a server listening for broadcast packets.
    3. If the mobile app receives a packet with the same device information as the target device, it means that the target device is found.
  • Complete connection

    1. The mobile app sends a TCP connection request to the device found on the local area network.
    2. The device accepts the connection request after it receives the packet.
    3. A device on the LAN can connect to a limited number of mobile apps simultaneously, which is three by default. When the limit is reached, the device will stop broadcasting UDP packets.
  • Data exchange

    1. The LAN is the preferred channel for the mobile app to send DP commands to the device.
    2. The device reports the current DP status over LAN when the status changes.

How it works

AppDeviceConnect to the router.Enter the control panelof the target device.Listen for UDPbroadcast packets.Send UDP broadcast packetsregularly.Listen for TCPconnections.Receive thebroadcastpacket from thetarget device.Initiate TCP connection.Complete LAN connection.Stop broadcastingUDP packets.opt[Theconnectionlimit isreached.]Send controlcommands.Send TCP data.Return an ACK message.The device statuschanges.Send TCP data.Return an ACK message.Update the displayeddevice status.AppDevice

Development guide

Runtime environment

Open tuya_iot_config.h and check if the following macro is defined.

#define ENABLE_TUYA_LAN 1

How to

The framework automatically manages the LAN process after the activated device is connected to the router. You do not need to care about the LAN process unless you have special requirements.

API description

Disable LAN

The framework enables the LAN feature by default. If you do not need LAN, you can manually disable it by using this API.

/**
 * @brief Disable LAN service
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_svc_lan_disable(VOID);

Enable LAN

After you disable LAN, you can use this API to enable it again.

/**
 * @brief Enable LAN service
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */
OPERATE_RET tuya_svc_lan_enable(VOID);

FAQs

What is the idle timeout for LAN connection?

When the device is inactive for 30 seconds, the LAN socket will be disconnected.

What is the maximum number of clients that a device can connect to?

Up to three clients.