EL_dev_arduino 2.2.1
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1#include "M5Atom.h"
2#include <WiFi.h>
3#include "EL.h"
4
5#define WIFI_SSID "YOUR_SSID"
6#define WIFI_PASS "YOUR_PASS"
7
8WiFiClient client;
9WiFiUDP elUDP;
10EL echo(elUDP, 0x01, 0x33, 0x01 );
11
12uint8_t state = 0;
13
14void printNetData();
15void toggleSwicth();
16void changeMode(int mode);
17
18void setup()
19{
20 Serial.begin(115200);
21 Serial.println("");
22
23 pinMode(26, OUTPUT);
24 digitalWrite(26, LOW);
25 M5.begin(true, false, true);
26 delay(10);
27
28 M5.dis.drawpix(0, 0x00f000);
29
30 WiFi.begin(WIFI_SSID, WIFI_PASS);
31 while (WiFi.status() != WL_CONNECTED) {
32 Serial.println("wait...");
33 delay(1000);
34 }
36
37 M5.dis.drawpix(0, 0x0000f0);
38 echo.begin(); // EL 起動シーケンス
39
40 echo.details[0x9D] = new byte[3] {0x02, 0x01, 0x80}; // change set property map
41 echo.details[0x9E] = new byte[4] {0x03, 0x02, 0x80, 0xa0}; // change set property map
42 echo.details[0x9f] = new byte[12]{0x0b, 0x0a, 0x80, 0x81, 0x82, 0x83, 0x88, 0x8a, 0x9d, 0x9e, 0x9f, 0xa0}; // get property map
43 echo.details[0x80] = new byte[2] {0x01, 0x31}; // 音発生設定
44 echo.details[0xA0] = new byte[2] {0x01, 0x31}; // 換気風量設定発生設定
45
46 // 自ノードインスタンスリスト通知をINFで出す
47 byte s[] = {
48 0x10, 0x81, 0x00, 0x00, 0x0e, 0xf0, 0x01, 0x0e, 0xf0, 0x01, EL_INF, 0x01, 0xd5, 0x04, 0x01, 0x01, 0x33, 0x01
49 };
50 echo.sendMulti(s, sizeof(s) / sizeof(s[0]));
51}
52
53uint8_t FSM = 0;
54
55int packetSize = 0; // 受信データ量
56byte *pdcedt = nullptr; // テンポラリ
57
58uint8_t pressed = 0;
59
60void loop()
61{
62 if (M5.Btn.wasPressed())
63 {
64 if(state == 0){
65 changeMode(-1);
66 }
67 }
68
69 // パケット貰ったらやる
70 packetSize = 0;
71 pdcedt = nullptr;
72
73 if (0 != (packetSize = echo.read()) ) // 0!=はなくてもよいが,Warning出るのでつけとく
74 { // 受け取った内容読み取り,あったら中へ
75 switch (echo._rBuffer[EL_ESV])
76 {
77 case EL_SETI:
78 case EL_SETC:
79 Serial.println("EL_SETI");
80 switch (echo._rBuffer[EL_EPC])
81 {
82 case 0x80: // 電源
83 if (echo._rBuffer[EL_EDT] == 0x30)
84 { // ON
85 // 動作ON処理
86 changeMode(1);
87 pdcedt = new byte[2]{0x01, 0x30};
88 echo.update(echo._rBuffer[EL_EPC], pdcedt); // ECHONET Liteの状態を変更
89 }
90 else if (echo._rBuffer[EL_EDT] == 0x31)
91 {
92 changeMode(0);
93 pdcedt = new byte[2]{0x01, 0x31};
94 echo.update(echo._rBuffer[EL_EPC], pdcedt); // ECHONET Liteの状態を変更
95 }
96 break;
97
98 case 0xA0: // 換気風量設定
99 break;
100
101 default:
102 break;
103 }
104
105 // pdcedtを使ったらクリア
106 if (pdcedt != nullptr)
107 {
108 delete[] pdcedt;
109 pdcedt = nullptr;
110 }
111
112 if (echo._rBuffer[EL_ESV] == EL_SETC)
113 { // SETCなら返信必要
114 echo.returner();
115 }
116 break; // SETI, SETCここまで
117
118 // -----------------------------------
119 // Get,INF_REQ対応
120 // SETの時にきちんとupdate関数でECHONET Liteの状態変更をライブラリに教えておけばここは簡素になる
121 case EL_GET:
122 case EL_INF_REQ:
123 Serial.println("EL_GET/EL_INF_REQ");
124 // update関数でdetailsに状態が登録されていれば自動で返信する
125 echo.returner();
126 break; // GetとINF_REQここま
127
128 case EL_INF:
129 Serial.println("EL_INF");
130 break;
131
132 default: // 解釈不可能なESV
133 Serial.println(echo._rBuffer[EL_ESV]);
134 break;
135 }
136 }
137
138 delay(100);
139 M5.update();
140}
141
142void changeMode(int mode){
143 switch(mode){
144 case 0: // OFF
145 if(echo.details[0x80][1] == 0x31) return;
146
147 if(echo.details[0xA0][1] == 0x31){
148 toggleSwicth();
149 toggleSwicth();
150 toggleSwicth();
151 } else if(echo.details[0xA0][1] == 0x32){
152 toggleSwicth();
153 toggleSwicth();
154 } else if(echo.details[0xA0][1] == 0x33){
155 toggleSwicth();
156 }
157 M5.dis.drawpix(0, 0x0000f0);
158 break;
159
160 case 1: // ON
161 if(echo.details[0x80][1] == 0x30) return;
162
163 if(echo.details[0xA0][1] == 0x31){
164 toggleSwicth();
165 } else if(echo.details[0xA0][1] == 0x32){
166 toggleSwicth();
167 toggleSwicth();
168 } else if(echo.details[0xA0][1] == 0x33){
169 toggleSwicth();
170 toggleSwicth();
171 toggleSwicth();
172 }
173 M5.dis.drawpix(0, 0xf00000);
174 break;
175
176 default:
177 toggleSwicth();
178 break;
179 }
180}
181
183 digitalWrite(26, HIGH);
184 delay(100);
185 digitalWrite(26, LOW);
186 delay(100);
187}
188
189// debug
191{
192 Serial.println("-----------------------------------");
193
194 // IP
195 // print your WiFi shield's IP address:
196 IPAddress ip = WiFi.localIP();
197 Serial.print("IP Address: ");
198 Serial.println(ip);
199
200 IPAddress dgwip = WiFi.gatewayIP();
201 Serial.print("DGW Address: ");
202 Serial.println(dgwip);
203
204 IPAddress smip = WiFi.subnetMask();
205 Serial.print("SM Address: ");
206 Serial.println(smip);
207
208 Serial.println("-----------------------------------");
209}
ECHONET Lite protocol for Arduino
#define EL_SETC
Definition: EL.h:36
#define EL_GET
Definition: EL.h:37
#define EL_ESV
Definition: EL.h:25
#define EL_INF_REQ
Definition: EL.h:38
#define EL_SETI
Definition: EL.h:35
#define EL_INF
Definition: EL.h:42
#define EL_EPC
Definition: EL.h:27
#define EL_EDT
Definition: EL.h:29
Main class for EL
Definition: EL.h:152
void update(const byte epc, byte pdcedt[])
Definition: EL.cpp:139
void begin(void)
Definition: EL.cpp:72
void sendMulti(byte sBuffer[], int size)
Definition: EL.cpp:187
void returner(void)
Definition: EL.cpp:386
byte _rBuffer[EL_BUFFER_SIZE]
receive buffer
Definition: EL.h:172
ELOBJ details
device object (for simple eoj)
Definition: EL.h:170
int read()
Definition: EL.cpp:374
uint8_t state
Definition: main.cpp:12
void changeMode(int mode)
Definition: main.cpp:142
uint8_t pressed
Definition: main.cpp:58
byte * pdcedt
Definition: main.cpp:56
WiFiClient client
Definition: main.cpp:8
#define WIFI_PASS
Definition: main.cpp:6
void setup()
Definition: main.cpp:18
#define WIFI_SSID
Definition: main.cpp:5
void toggleSwicth()
Definition: main.cpp:182
int packetSize
Definition: main.cpp:55
uint8_t FSM
Definition: main.cpp:53
EL echo(elUDP, 0x01, 0x33, 0x01)
WiFiUDP elUDP
Definition: main.cpp:9
void printNetData()
Definition: main.cpp:190
void loop()
Definition: main.cpp:60