Skip to content

Commit 28eba63

Browse files
changlin.caiclaude
andcommitted
[文件] UDP 範例補上 onDeviceStatus 使用示範
兩個 UDP 範例(同步 + Async)原本只有 onMessage handler,現在補上 onDeviceStatus handler,示範上游設備上下線通知如何接收。 - 印出格式:「Device ▲ ONLINE / ▼ OFFLINE deviceId @ endpoint」 - 註解標清楚:需在 EdgeLink Server 把這台 Arduino 設為 forward target 才會收到事件,且 LOCAL_PORT 需 > 0 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3003f85 commit 28eba63

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

SDK/Arduino/EdgeLink.zip

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:724467f47af27f9820e95db52c6cefeb706e2b772251e32a99383ed30281fa9b
3-
size 10007
2+
oid sha256:621f49148ede9e0cfdea4b0560ece07853a62f2d4a382a88f342e37c4cb692f0
3+
size 10473

SDK/Arduino/EdgeLink/examples/udp_async_sensor/udp_async_sensor.ino

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ const uint16_t LOCAL_PORT = 0;
2828
AsyncUDP asyncUdp;
2929
EdgeLinkAsyncUDP edgelink(asyncUdp);
3030

31-
// LOCAL_PORT > 0 時才會觸發。callback 在 AsyncUDP task 內執行(非 loop()),
32-
// 盡量短、不要呼叫 Serial.print 大量輸出,必要時用 queue 傳回主執行緒處理。
31+
// LOCAL_PORT > 0 時才會觸發。一般業務訊息(已過濾掉 EDGELINK_* 控制訊息)
32+
// callback 在 AsyncUDP task 內執行(非 loop()),盡量短、不要呼叫 Serial.print 大量輸出,
33+
// 必要時用 queue 傳回主執行緒處理。
3334
void onMessage(const String& msg, IPAddress remoteIP, uint16_t remotePort) {
3435
Serial.print("[EdgeLink AsyncUDP] From ");
3536
Serial.print(remoteIP);
@@ -39,6 +40,17 @@ void onMessage(const String& msg, IPAddress remoteIP, uint16_t remotePort) {
3940
Serial.println(msg);
4041
}
4142

43+
// 上游設備在 EdgeLink Server 上下線通知(30s timeout-based)。
44+
// 需在 EdgeLink Server 把這台 ESP32 設成某 UDP port 的 forward target 才會收到。
45+
// 同樣在 AsyncUDP task 內執行。
46+
void onDeviceStatus(bool connected, const String& endpoint, const String& deviceId) {
47+
Serial.print("[EdgeLink AsyncUDP] Device ");
48+
Serial.print(connected ? "▲ ONLINE " : "▼ OFFLINE ");
49+
Serial.print(deviceId);
50+
Serial.print(" @ ");
51+
Serial.println(endpoint);
52+
}
53+
4254
void setup() {
4355
Serial.begin(115200);
4456

@@ -53,6 +65,7 @@ void setup() {
5365
Serial.println(WiFi.localIP());
5466

5567
edgelink.onMessage(onMessage);
68+
edgelink.onDeviceStatus(onDeviceStatus);
5669
if (!edgelink.begin(LOCAL_PORT)) {
5770
Serial.println("AsyncUDP listen failed");
5871
while (true) delay(1000);

SDK/Arduino/EdgeLink/examples/udp_sensor/udp_sensor.ino

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const uint16_t LOCAL_PORT = 0;
2828
WiFiUDP wifiUdp;
2929
EdgeLinkUDP edgelink(wifiUdp);
3030

31-
// LOCAL_PORT > 0 才會觸發。
31+
// LOCAL_PORT > 0 才會觸發。一般業務訊息(已過濾掉 EDGELINK_* 控制訊息)
3232
void onMessage(const String& msg, IPAddress remoteIP, uint16_t remotePort) {
3333
Serial.print("[EdgeLink UDP] From ");
3434
Serial.print(remoteIP);
@@ -38,6 +38,16 @@ void onMessage(const String& msg, IPAddress remoteIP, uint16_t remotePort) {
3838
Serial.println(msg);
3939
}
4040

41+
// 上游設備在 EdgeLink Server 上下線通知(30s timeout-based)。
42+
// 需在 EdgeLink Server 把這台 Arduino 設成某 UDP port 的 forward target 才會收到。
43+
void onDeviceStatus(bool connected, const String& endpoint, const String& deviceId) {
44+
Serial.print("[EdgeLink UDP] Device ");
45+
Serial.print(connected ? "▲ ONLINE " : "▼ OFFLINE ");
46+
Serial.print(deviceId);
47+
Serial.print(" @ ");
48+
Serial.println(endpoint);
49+
}
50+
4151
void setup() {
4252
Serial.begin(115200);
4353

@@ -53,6 +63,7 @@ void setup() {
5363

5464
edgelink.begin(LOCAL_PORT);
5565
edgelink.onMessage(onMessage);
66+
edgelink.onDeviceStatus(onDeviceStatus);
5667
Serial.println("EdgeLink UDP ready");
5768
}
5869

0 commit comments

Comments
 (0)