TNN Smart Control
Two-tier LAN relay control by @anhtn1980 (developed in Vietnam). ESP32+W5500 web UI bridges TCP to an Arduino MEGA2560+W5500 driving 16 RS485 relays.
원본은 베트남에서 베트남어로 개발·작성되었습니다. 아래 내용은 베트남어 원문 README를 한국어·영어로 요약·정리한 것입니다.
프로젝트가 하는 일
TNN Smart Control은 LAN(유선 네트워크)을 통해 릴레이를 제어하는 2계층 스마트 제어 시스템입니다. 웹 브라우저에서 버튼을 누르면, 그 명령이 유선 이더넷과 RS485를 거쳐 최대 16채널의 릴레이를 켜고 끕니다.
- ESP32 + W5500 (프론트엔드 계층): 포트 80에서 웹 서버를 구동해 릴레이 1~16 제어용 Web UI/API를 제공합니다. 사용자의 조작을 텍스트 기반 TCP 명령으로 변환해 MEGA로 전달하고, MEGA가 돌려주는 릴레이 상태로 UI를 갱신합니다.
- Arduino MEGA2560 + W5500 + MAX485 (백엔드 계층): 포트 9000에서 TCP 서버를 구동해 명령을 파싱하고, 대응하는 RS485 프레임으로 릴레이 보드를 제어합니다. 입출력 상태를 주기적으로 동기화하고, 하드웨어 버튼(SCENE_PIN)을 통한 scene 전환도 지원합니다.
언어는 C++ 100%(Arduino 스케치 2개)이며, 동작은 "웹 버튼 클릭 → ESP32가 TCP 명령 전송 → MEGA가 RS485로 릴레이 토글 → 상태 스냅샷 갱신 → ESP32가 /status 폴링으로 UI 반영" 순서입니다.
WIZnet이 들어가는 위치
이 프로젝트에서 사용되는 WIZnet 제품은 W5500이며, 특이하게 두 계층 모두에 들어갑니다.
- ESP32 측 W5500: 웹 UI/API를 유선 Ethernet으로 안정적으로 서비스하고, 백엔드(MEGA)와 TCP로 통신.
- MEGA2560 측 W5500: 포트 9000에서 TCP 서버를 열어 명령 수신.
즉 ESP32와 MEGA는 W5500 ↔ W5500 유선 TCP 링크로 연결됩니다. 릴레이 제어처럼 항상 동작해야 하는 고정 설치 시스템에서 Wi-Fi의 끊김·지연은 치명적인데, W5500은 하드웨어 TCP/IP 스택, SPI 인터페이스, 8개 독립 socket, 32KB 내부 buffer를 제공하므로 MCU가 네트워크 프로토콜 처리 부담을 덜고 릴레이/RS485 로직에 집중할 수 있습니다. 두 노드 모두 정적 IP를 사용해 주소가 고정되는 점도 산업/상업 설비에 적합합니다.
네트워크 기본값(배포 전 실제 LAN에 맞게 조정 권장):
- ESP32 — IP
192.168.1.180, GW192.168.1.1, Subnet255.255.255.0, DNS8.8.8.8, Web 포트80, TCP 대상192.168.1.178:9000 - MEGA2560 — IP
192.168.1.178, GW192.168.1.1, Subnet255.255.255.0, TCP 서버 포트9000
구현 메모
핀 매핑 (현재 코드 기준)
- ESP32 (W5500):
CS=5,RST=4,SCK=18,MISO=19,MOSI=23 - MEGA2560:
DE_PIN=22(MAX485 TX/RX 제어),RS485=Serial1,SCENE_PIN=30(INPUT_PULLUP)
Arduino 라이브러리
- ESP32 스케치:
SPI,Ethernet - MEGA 스케치:
SPI,Ethernet2 - 두 스케치가 서로 다른 Ethernet 라이브러리를 쓰므로, 충돌이 없도록 각각 맞는 라이브러리를 설치해야 합니다.
명령/엔드포인트 요약
- ESP32 HTTP:
GET /(Web UI),GET /cmd?c=<command>(TCP 명령 직전달),GET /set?relay=<n>&value=<true|false|1|0>,GET /status(예:L1=0,L2=1,...),GET /api/status(JSON) - MEGA TCP 텍스트 명령:
set /relay/<1..16>/toggle,set /relay/<1..16>/state true|false,set /system/all off,get /relay/all
위 핀맵·IP·명령은 README 사양을 요약한 것입니다. 실제 스케치(.ino)에 맞춰 확인 후 사용하세요.
실전 팁 / 주의점
- ESP32는 연결이 끊기면 약 3초마다 MEGA로 TCP 재연결을 시도합니다.
- MEGA의
mirrorBlockUntil은 릴레이 조작 직후 발생할 수 있는 교차 응답을 막아줍니다. - UI의 AC 버튼은
set /ac/x/pulse를 보내는데, MEGA에/ac/파서가 없으면 동작하지 않습니다. - 두 노드 모두 정적 IP이므로, 배포 전 실제 라우터/LAN 대역에 맞게 IP를 조정하세요.
- 릴레이가 고전압/주전원 부하를 제어한다면 감전·화재 위험이 있으니 절연·보호와 전기 규정 준수가 필수입니다.
유사 프로젝트
https://maker.wiznet.io/Benjamin/projects/smart-device-control-using-wiznet-w5500-and-stm32/
두 프로젝트는 모두 W5500 유선 Ethernet으로 웹/네트워크에서 기기를 제어하는 시스템이라는 점에서 유사합니다. 다만 TNN Smart Control은 ESP32(웹 게이트웨이) + MEGA2560(RS485 릴레이 컨트롤러)의 2계층 구조로 16채널 릴레이를 제어하는 반면, Benjamin의 프로젝트는 단일 MCU(STM32) + W5500 구성에 가깝습니다.
| 항목 | TNN Smart Control | Smart Device Control (W5500 + STM32) |
|---|---|---|
| 구조 | 2계층(ESP32 게이트웨이 + MEGA 컨트롤러) | 단일 MCU |
| 제어 대상 | RS485 16채널 릴레이 | LED/기기 + 센서 |
| 통신 경로 | HTTP → TCP(9000) → RS485 | W5500 웹 인터페이스 직접 |
| MCU | ESP32 + Arduino MEGA2560 | STM32F103C8T6 |
| W5500 사용 | 양쪽 계층 모두 사용 | 단일 W5500 |
FAQ
Q: TNN Smart Control은 무엇을 하나요? A: 웹에서 LAN을 통해 최대 16채널 릴레이를 제어하는 2계층(ESP32 게이트웨이 + MEGA2560 RS485 컨트롤러) 시스템입니다.
Q: W5500은 어떻게 쓰이나요? A: ESP32와 MEGA2560 양쪽 모두 W5500 유선 Ethernet을 사용하며, 두 노드는 W5500 간 TCP(포트 9000)로 통신합니다.
Q: 왜 Wi-Fi가 아니라 유선 Ethernet인가요? A: 릴레이 제어처럼 항상 동작해야 하는 설비에서 Wi-Fi 끊김/지연을 피하기 위해서입니다.
Q: 릴레이는 어떻게 제어되나요? A: ESP32의 TCP 명령을 MEGA가 받아 RS485 프레임으로 변환하고, MAX485를 거쳐 16채널 RS485 릴레이 보드를 제어합니다.
About this project This project was created and shared by @anhtn1980, and was originally developed and documented in Vietnam (in Vietnamese). The content below is a summarized English adaptation of the original Vietnamese README.
What the Project Does
TNN Smart Control is a two-tier smart control system that controls relays over a LAN (wired network). Press a button in a web browser, and the command travels over wired Ethernet and RS485 to switch up to 16 relay channels on/off.
- ESP32 + W5500 (front-end tier): runs a web server on port 80 providing a Web UI/API to control relays 1–16. It converts user actions into text-based TCP commands sent to the MEGA, and updates the UI with the relay status the MEGA returns.
- Arduino MEGA2560 + W5500 + MAX485 (back-end tier): runs a TCP server on port 9000 that parses commands, builds the corresponding RS485 frames, and drives the relay board. It periodically reads and synchronizes I/O state and supports scene switching via a hardware button (SCENE_PIN).
It is written in 100% C++ (two Arduino sketches). Operating flow: web button click → ESP32 sends a TCP command → MEGA toggles the relay over RS485 → updates an I/O snapshot → ESP32 polls /status to reflect the real state in the UI.
Where WIZnet Fits
The WIZnet product used in this project is the W5500, and notably it appears on both tiers.
- W5500 on the ESP32 side: serves the Web UI/API reliably over wired Ethernet and communicates with the back-end (MEGA) over TCP.
- W5500 on the MEGA2560 side: opens a TCP server on port 9000 to receive commands.
In other words, the ESP32 and MEGA are connected by a W5500 ↔ W5500 wired TCP link. For fixed-installation systems that must always work, such as relay control, Wi-Fi dropouts/latency are critical. The W5500 provides a hardware TCP/IP stack, an SPI interface, eight independent sockets, and a 32 KB internal buffer, so the MCUs can offload network protocol processing and focus on relay/RS485 logic. Both nodes use static IPs, which suits industrial/commercial installations.
Default network settings (adjust to your real LAN before deployment):
- ESP32 — IP
192.168.1.180, GW192.168.1.1, Subnet255.255.255.0, DNS8.8.8.8, web port80, TCP target192.168.1.178:9000 - MEGA2560 — IP
192.168.1.178, GW192.168.1.1, Subnet255.255.255.0, TCP server port9000
Implementation Notes
Pin mapping (per current code)
- ESP32 (W5500):
CS=5,RST=4,SCK=18,MISO=19,MOSI=23 - MEGA2560:
DE_PIN=22(MAX485 TX/RX control),RS485=Serial1,SCENE_PIN=30(INPUT_PULLUP)
Arduino libraries
- ESP32 sketch:
SPI,Ethernet - MEGA sketch:
SPI,Ethernet2 - The two sketches use different Ethernet libraries, so install the correct one for each to avoid conflicts.
Command/endpoint summary
- ESP32 HTTP:
GET /(Web UI),GET /cmd?c=<command>(forward TCP command),GET /set?relay=<n>&value=<true|false|1|0>,GET /status(e.g.,L1=0,L2=1,...),GET /api/status(JSON) - MEGA TCP text commands:
set /relay/<1..16>/toggle,set /relay/<1..16>/state true|false,set /system/all off,get /relay/all
The pin map, IPs, and commands above summarize the spec documented in the README. Verify the values against the actual sketches (.ino) before use.
Practical Tips / Pitfalls
- The ESP32 retries the TCP connection to the MEGA roughly every 3 seconds when disconnected.
- The MEGA's
mirrorBlockUntilmechanism prevents cross-responses that can occur right after a relay action. - The UI's AC button sends a
set /ac/x/pulsecommand; if the MEGA has not implemented an/ac/parser yet, this command has no effect. - Both nodes use static IPs, so adjust the addresses to your actual router/LAN range before deployment.
- If the relay loads are high-voltage/mains, there is real shock/fire risk — proper insulation, protection, and compliance with electrical codes are essential.
Similar Project
https://maker.wiznet.io/Benjamin/projects/smart-device-control-using-wiznet-w5500-and-stm32/
Both projects are similar in that they are systems that control devices from the web/network over W5500 wired Ethernet. However, TNN Smart Control uses a two-tier structure (ESP32 web gateway + MEGA2560 RS485 relay controller) to drive a 16-channel relay board, while Benjamin's project is closer to a single-MCU (STM32) + W5500 device control/monitoring setup.
| Item | TNN Smart Control | Smart Device Control (W5500 + STM32) |
|---|---|---|
| Structure | Two-tier (ESP32 gateway + MEGA controller) | Single-MCU |
| Control target | RS485 16-channel relay board | LEDs/devices + sensors |
| Comms path | HTTP → TCP(9000) → RS485 | W5500 web interface directly |
| MCU | ESP32 + Arduino MEGA2560 | STM32F103C8T6 |
| Scalability | Multi-channel relays via RS485 | Board-channel centric |
| W5500 usage | W5500 on both tiers | Single W5500 |
FAQ
Q: What does TNN Smart Control do? A: A two-tier smart control system (ESP32 gateway + MEGA2560 RS485 controller) that controls up to 16 relay channels over a LAN from a web browser.
Q: How is the W5500 used? A: Both the ESP32 and the MEGA2560 use W5500 wired Ethernet, and the two nodes communicate over a W5500-to-W5500 TCP link (port 9000).
Q: Why wired Ethernet instead of Wi-Fi? A: To avoid Wi-Fi dropouts/latency in installations that must always work, like relay control. The W5500's hardware TCP/IP ensures stable communication.
Q: How are the relays physically controlled? A: The MEGA receives the ESP32's TCP command, converts it into an RS485 frame, and drives a 16-channel RS485 relay board through the MAX485.


