Программа контроллера сети CAN
Программа контроллера сети CAN
1. CAN Network Controller Program
This article provides a technical description of an ESP32-based controller program for building a local CAN network for smart homes.
Key Features
- Uses the same program for all controllers (Master/Slave distinction determined by device profile)
- The Master device (Local Number 1 + Ethernet) is responsible for communication with the outside (Internet, Mobile)
- Designed so that another device can take over the Master role in the event of a failure
- Stores and manages profiles for all devices in NVS flash memory
- FreeRTOS-based multitasking (uses tasks, queues, and semaphores)
- ESP-IDF v5.4.3, ESP32C6 target
Key Implemented Functions
- CAN (TWAI) communication + network scanning
- Ethernet connection
- MQTT over WebSocket Secure (WSS) client
- Event/scenario-based operation
- Currently supported function types: 0x210 (2-channel relay), 0x0A0 (button/switch)
Program Structure
- `app_main.c`: Main entry point, task creation, initialization
- `ethernet.c`: W5500-based Ethernet driver `app_mqtt_wss.c`: MQTT + WebSocket Secure Client
- `CAN.c`: TWAI driver and message processing
- `TYPE_210.c`, `TYPE_0A0.c`: Drivers for each function type
- Stores device profiles, events, and scenarios in NVS
MQTT Topic Structure (Important)
- `com/...`: For control (not stored by the broker)
- `inf/...`: For information/status (uses retain flag)
- Example: `com/1/210/p/1` → Controls parameter 1 of type 210 of controller 1
2. Summary of W5500's Role
The W5500 is an SPI interface Ethernet controller chip manufactured by Wiznet.
In this program, the role of the W5500 is as follows:
| Item | Content |
|||
| Location | Driver implementation in the `ethernet.c` file |
| Connection Method | Connected to ESP32C6 via SPI |
| Key Function | Provides wired Ethernet communication (TCP/IP offload) |
| Role in Master Device | Connect to router → Obtain IP address → Enable MQTT (WSS) communication |
| Operation | `WORK` LED lights up upon successful connection, turns off upon failure/disconnection |
| Significance within the System | The sole channel connecting the CAN network to the outside world (Internet/Mobile) |
One-line Summary:
> The W5500 is a wired network interface that enables the Master controller to communicate with the Internet/MQTT broker.
> It serves as the basis for Ethernet drivers in the program and is the core hardware through which the Master device connects to the outside world.
=========================================
1. CAN 네트워크 컨트롤러 프로그램
이 글은 스마트 홈용 로컬 CAN 네트워크를 구축하기 위한 ESP32 기반 컨트롤러 프로그램에 대한 기술 설명입니다.
주요 특징
- 모든 컨트롤러에 동일한 프로그램을 사용 (Master/Slave 구분은 장치 프로필로 결정)
- Master 장치(로컬 번호 1 + Ethernet)가 외부(인터넷, 모바일)와의 통신을 담당
- 장애 발생 시 다른 장치가 Master 역할을 대신할 수 있도록 설계
- 모든 장치의 프로필을 NVS 플래시 메모리에 저장·관리
- FreeRTOS 기반 멀티태스킹 (태스크, 큐, 세마포어 사용)
- ESP-IDF v5.4.3, ESP32C6 타겟
구현된 주요 기능
- CAN (TWAI) 통신 + 네트워크 스캔
- Ethernet 연결
- MQTT over WebSocket Secure (WSS) 클라이언트
- 이벤트/시나리오 기반 동작
- 현재 지원 기능 타입: 0x210 (2채널 릴레이), 0x0A0 (버튼/스위치)
프로그램 구조
- `app_main.c`: 메인 진입점, 태스크 생성, 초기화
- `ethernet.c`: W5500 기반 이더넷 드라이버
- `app_mqtt_wss.c`: MQTT + WebSocket Secure 클라이언트
- `CAN.c`: TWAI 드라이버 및 메시지 처리
- `TYPE_210.c`, `TYPE_0A0.c`: 각 기능 타입별 드라이버
- NVS에 장치 프로필, 이벤트, 시나리오 저장
MQTT 토픽 구조 (중요)
- `com/...` : 제어용 (브로커가 저장 안 함)
- `inf/...` : 정보/상태용 (retain 플래그 사용)
- 예: `com/1/210/p/1` → 1번 컨트롤러의 210 타입 1번 파라미터 제어
2. W5500의 역할 요약
W5500은 Wiznet에서 만든 SPI 인터페이스 이더넷 컨트롤러 칩입니다.
이 프로그램에서 W5500의 역할은 다음과 같습니다:
한 줄 요약:
> W5500은 Master 컨트롤러가 인터넷/MQTT 브로커와 통신할 수 있게 해주는 유선 네트워크 인터페이스입니다.
> 프로그램에서 Ethernet 드라이버의 기반이 되며, Master 장치가 외부와 연결되는 핵심 하드웨어입니다.
