Steuerkommandos an die Power-Line Steckdose senden
Nachdem die Voraussetzungen für die Power-Line-Kommunikation geschaffen sind, wollen wir nun ein kleines Anwendungsprogramm schreiben, das eine über Power-Line schaltbare Steckdose ein- und ausschaltet. Dies ist eine Standardapplikation in der Home Automation und kann beispielsweise zum Fernsteuern einer Lichtquelle mit JControl verwendet werden.
Der folgende Quelltext zeigt, wie auf Tastendruck ein Steuerkommando an die schaltbare Steckdose über Power-Line verschickt werden kann:
83 | /** |
84 | * Send a power-line UDAT that turns the remote plug on. |
85 | * We assume that the plug is programmed to network address 0x6000. |
86 | */ |
87 | void plugOn() { |
88 | ft12.sendUDAT(new byte[] { L_DATA_req, // link layer command |
89 | 0x0c, // priority |
90 | 0x00, 0x00, // src address |
91 | 0x60, 0x00, // dst address |
92 | (byte)0xF1, // NPCI |
93 | 0x00, // TPCI |
94 | (byte)0x81 } // command 'ON' |
95 | ); |
96 | } |
97 | |
98 | /** |
99 | * Send a power-line UDAT that turns the remote plug off. |
100 | * We assume that the plug is programmed to network address 0x6000. |
101 | */ |
102 | void plugOff() { |
103 | ft12.sendUDAT(new byte[] { L_DATA_req, // link layer command |
104 | 0x0c, // priority |
105 | 0x00, 0x00, // src address |
106 | 0x60, 0x00, // dst address |
107 | (byte)0xF1, // NPCI |
108 | 0x00, // TPCI |
109 | (byte)0x80 } // command 'OFF' |
110 | ); |
111 | } |
112 | |
113 | /** |
114 | * A simple keyboard listener. |
115 | */ |
116 | void userInterface() { |
117 | Display lcd = new Display(); |
118 | lcd.drawString("PowerLineExample", 0, 0); |
119 | lcd.drawString("Press 'UP' to turn plug on",0,20); |
120 | lcd.drawString("Press 'DOWN' to turn plug off",0,30); |
121 | |
122 | Keyboard keys = new Keyboard(); |
123 | for(;;) { |
124 | switch (keys.read()) { |
125 | case 'U': // turn plug on |
126 | plugOn(); |
127 | Buzzer.on((short)1000, (short)100); |
128 | break; |
129 | |
130 | case 'D': // turn plug off |
131 | plugOff(); |
132 | Buzzer.on((short)300, (short)100); |
133 | break; |
134 | |
135 | default: |
136 | } |
137 | } |
138 | } |
Listing 2: Auszug aus PowerLineExample_App.java
Setzt man obigen Quelltext in das Grundgerüst aus dem vorigen Abschnitt ein, ergibt sich das Programm PowerLineExample_App.java. Erzeugen Sie ein entsprechendes JControl/IDE-Projekt und laden Sie das PowerLineExample auf das JControl/PLUI. Schließen Sie zum Beispiel eine Lampe an die schaltbare Steckdose an. Stimmt die Zieladresse in dem Quelltext (hier: 0x6000
) mit der Geräte-Adresse Ihrer schaltbaren Steckdose überein, dann können Sie nun die Lampe mit JControl über Power-Line fernsteuern!