Membuat coding untuk ESP8266 agar dapat digunakan dengan CUBE android apps
Untuk user yang telah mendownload CUBE android apps, kali ini saya akan share cara membuat coding untuk Esp82666 agar dapat dikendalikan dengan menggunakan CUBE android apps. Berikut ini adalah contoh coding yang dapat dipergunakan bersama CUBE android Apps yang telah saya release sebelumnya.
Coding ini sendiri adalah hasil kreasi Kakopappa Aruna Tenakoon yang anda bisa download disini.
Coding ini sendiri adalah hasil kreasi Kakopappa Aruna Tenakoon yang anda bisa download disini.
1: #include <ESP8266WiFi.h>
2: #include <ESP8266WebServer.h>
3: #include <WiFiUdp.h>
4: #include <functional>
5:
6: void prepareIds();
7: boolean connectWifi();
8: boolean connectUDP();
9: void startHttpServer();
10: void turnOnRelay();
11: void turnOffRelay();
12:
13: const char* ssid = "ssid";
14: const char* password = "pass";
15:
16: unsigned int localPort = 1900; // local port to listen on
17:
18: WiFiUDP UDP;
19: boolean udpConnected = false;
20: IPAddress ipMulti(239, 255, 255, 250);
21: unsigned int portMulti = 1900; // local port to listen on
22:
23: ESP8266WebServer HTTP(80);
24:
25: boolean wifiConnected = false;
26:
27: char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
28:
29: String serial;
30: String persistent_uuid;
31: String device_name;
32:
33: const int relayPin = LED_BUILTIN;
34:
35: boolean cannotConnectToWifi = false;
36:
37: void setup() {
38: Serial.begin(115200);
39:
40: // Setup Relay
41: pinMode(relayPin, OUTPUT);
42:
43: prepareIds();
44:
45: // Initialise wifi connection
46: wifiConnected = connectWifi();
47:
48: // only proceed if wifi connection successful
49: if(wifiConnected){
50: udpConnected = connectUDP();
51:
52: if (udpConnected){
53: // initialise pins if needed
54: startHttpServer();
55: }
56: }
57: }
58:
59: void loop() {
60:
61: HTTP.handleClient();
62: delay(1);
63:
64:
65: // if there's data available, read a packet
66: // check if the WiFi and UDP connections were successful
67: if(wifiConnected){
68: if(udpConnected){
69: // if there’s data available, read a packet
70: int packetSize = UDP.parsePacket();
71:
72: if(packetSize) {
73: Serial.println("");
74: Serial.print("Received packet of size ");
75: Serial.println(packetSize);
76: Serial.print("From ");
77: IPAddress remote = UDP.remoteIP();
78:
79: for (int i =0; i < 4; i++) {
80: Serial.print(remote[i], DEC);
81: if (i < 3) {
82: Serial.print(".");
83: }
84: }
85:
86: Serial.print(", port ");
87: Serial.println(UDP.remotePort());
88:
89: int len = UDP.read(packetBuffer, 255);
90:
91: if (len > 0) {
92: packetBuffer[len] = 0;
93: }
94:
95: String request = packetBuffer;
96: //Serial.println("Request:");
97: //Serial.println(request);
98:
99: if(request.indexOf('M-SEARCH') > 0) {
100: if(request.indexOf("urn:Belkin:device:**") > 0) {
101: Serial.println("Responding to search request ...");
102: respondToSearch();
103: }
104: }
105: }
106:
107: delay(10);
108: }
109: } else {
110: // Turn on/off to indicate cannot connect ..
111: }
112: }
113:
114: void prepareIds() {
115: uint32_t chipId = ESP.getChipId();
116: char uuid[64];
117: sprintf_P(uuid, PSTR("38323636-4558-4dda-9188-cda0e6%02x%02x%02x"),
118: (uint16_t) ((chipId >> 16) & 0xff),
119: (uint16_t) ((chipId >> 8) & 0xff),
120: (uint16_t) chipId & 0xff);
121:
122: serial = String(uuid);
123: persistent_uuid = "Socket-1_0-" + serial;
124: device_name = "CUBE-01R";
125: }
126:
127: void respondToSearch() {
128: Serial.println("");
129: Serial.print("Sending response to ");
130: Serial.println(UDP.remoteIP());
131: Serial.print("Port : ");
132: Serial.println(UDP.remotePort());
133:
134: IPAddress localIP = WiFi.localIP();
135: char s[16];
136: sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
137:
138: String response =
139: "HTTP/1.1 200 OK\r\n"
140: "CACHE-CONTROL: max-age=86400\r\n"
141: "DATE: Fri, 15 Apr 2016 04:56:29 GMT\r\n"
142: "EXT:\r\n"
143: "LOCATION: http://" + String(s) + ":80/setup.xml\r\n"
144: "OPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n"
145: "01-NLS: b9200ebb-736d-4b93-bf03-835149d13983\r\n"
146: "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n"
147: "ST: urn:Belkin:device:**\r\n"
148: "USN: uuid:" + persistent_uuid + "::urn:Belkin:device:**\r\n"
149: "X-User-Agent: redsonic\r\n\r\n";
150:
151: UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
152: UDP.write(response.c_str());
153: UDP.endPacket();
154:
155: Serial.println("Response sent !");
156: }
157:
158: void startHttpServer() {
159: HTTP.on("/index.html", HTTP_GET, [](){
160: Serial.println("Got Request index.html ...\n");
161: HTTP.send(200, "text/plain", "Hello World!");
162: });
163:
164: HTTP.on("/upnp/control/basicevent1", HTTP_POST, []() {
165: Serial.println("########## Responding to /upnp/control/basicevent1 ... ##########");
166:
167: //for (int x=0; x <= HTTP.args(); x++) {
168: // Serial.println(HTTP.arg(x));
169: //}
170:
171: String request = HTTP.arg(0);
172: Serial.print("request:");
173: Serial.println(request);
174:
175: if(request.indexOf("
176: <BinaryState>1</BinaryState>") > 0) {
177: Serial.println("Got Turn on request");
178: turnOnRelay();
179: }
180:
181: if(request.indexOf("
182: <BinaryState>0</BinaryState>") > 0) {
183: Serial.println("Got Turn off request");
184: turnOffRelay();
185: }
186:
187: HTTP.send(200, "text/plain", "");
188: });
189:
190: HTTP.on("/eventservice.xml", HTTP_GET, [](){
191: Serial.println(" ########## Responding to eventservice.xml ... ########\n");
192: String eventservice_xml = "
193: <?scpd
194: xmlns=\"urn:Belkin:service-1-0\"?>"
195: "
196: <actionList>"
197: "
198: <action>"
199: "
200: <name>SetBinaryState</name>"
201: "
202: <argumentList>"
203: "
204: <argument>"
205: "
206: <retval/>"
207: "
208: <name>BinaryState</name>"
209: "
210: <relatedStateVariable>BinaryState</relatedStateVariable>"
211: "
212: <direction>in</direction>"
213: "
214: </argument>"
215: "
216: </argumentList>"
217: "
218: <serviceStateTable>"
219: "
220: <stateVariable sendEvents=\"yes\">"
221: "
222: <name>BinaryState</name>"
223: "
224: <dataType>Boolean</dataType>"
225: "
226: <defaultValue>0</defaultValue>"
227: "
228: </stateVariable>"
229: "
230: <stateVariable sendEvents=\"yes\">"
231: "
232: <name>level</name>"
233: "
234: <dataType>string</dataType>"
235: "
236: <defaultValue>0</defaultValue>"
237: "
238: </stateVariable>"
239: "
240: </serviceStateTable>"
241: "
242: </action>"
243: "
244: </scpd>\r\n"
245: "\r\n";
246:
247: HTTP.send(200, "text/plain", eventservice_xml.c_str());
248: });
249:
250: HTTP.on("/setup.xml", HTTP_GET, [](){
251: Serial.println(" ########## Responding to setup.xml ... ########\n");
252:
253: IPAddress localIP = WiFi.localIP();
254: char s[16];
255: sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
256:
257: String setup_xml = "
258: <?xml version=\"1.0\"?>"
259: "
260: <root>"
261: "
262: <device>"
263: "
264: <deviceType>urn:Belkin:device:controllee:1</deviceType>"
265: "
266: <friendlyName>"+ device_name +"</friendlyName>"
267: "
268: <manufacturer>Belkin International Inc.</manufacturer>"
269: "
270: <modelName>Emulated Socket</modelName>"
271: "
272: <modelNumber>3.1415</modelNumber>"
273: "
274: <UDN>uuid:"+ persistent_uuid +"</UDN>"
275: "
276: <serialNumber>221517K0101769</serialNumber>"
277: "
278: <binaryState>0</binaryState>"
279: "
280: <serviceList>"
281: "
282: <service>"
283: "
284: <serviceType>urn:Belkin:service:basicevent:1</serviceType>"
285: "
286: <serviceId>urn:Belkin:serviceId:basicevent1</serviceId>"
287: "
288: <controlURL>/upnp/control/basicevent1</controlURL>"
289: "
290: <eventSubURL>/upnp/event/basicevent1</eventSubURL>"
291: "
292: <SCPDURL>/eventservice.xml</SCPDURL>"
293: "
294: </service>"
295: "
296: </serviceList>"
297: "
298: </device>"
299: "
300: </root>\r\n"
301: "\r\n";
302:
303: HTTP.send(200, "text/xml", setup_xml.c_str());
304:
305: Serial.print("Sending :");
306: Serial.println(setup_xml);
307: });
308:
309: HTTP.begin();
310: Serial.println("HTTP Server started ..");
311: }
312:
313:
314:
315: // connect to wifi – returns true if successful or false if not
316: boolean connectWifi(){
317: boolean state = true;
318: int i = 0;
319:
320: WiFi.mode(WIFI_STA);
321: WiFi.begin(ssid, password);
322: Serial.println("");
323: Serial.println("Connecting to WiFi");
324:
325: // Wait for connection
326: Serial.print("Connecting ...");
327: while (WiFi.status() != WL_CONNECTED) {
328: delay(500);
329: Serial.print(".");
330: if (i > 10){
331: state = false;
332: break;
333: }
334: i++;
335: }
336:
337: if (state){
338: Serial.println("");
339: Serial.print("Connected to ");
340: Serial.println(ssid);
341: Serial.print("IP address: ");
342: Serial.println(WiFi.localIP());
343: }
344: else {
345: Serial.println("");
346: Serial.println("Connection failed.");
347: }
348:
349: return state;
350: }
351:
352: boolean connectUDP(){
353: boolean state = false;
354:
355: Serial.println("");
356: Serial.println("Connecting to UDP");
357:
358: if(UDP.beginMulticast(WiFi.localIP(), ipMulti, portMulti)) {
359: Serial.println("Connection successful");
360: state = true;
361: }
362: else{
363: Serial.println("Connection failed");
364: }
365:
366: return state;
367: }
368:
369: void turnOnRelay() {
370: digitalWrite(relayPin, HIGH); // turn on relay with voltage HIGH
371: }
372:
373: void turnOffRelay() {
374: digitalWrite(relayPin, LOW); // turn off relay with voltage LOW
375: }
376:
Yang perlu anda ganti tentunya adalah ssid dan ssid password disesuaikan dengan ssid anda beserta passwordnya.
Pada CUBE Android Appsnya anda bisa buka page Control dan menekan button beberapa saat hingga tampil dialog box untuk memilih port, pada bagian ini silahkan anda pilih port 80, kemudian tekan save maka akan tampil dialog box untuk mengganti nama button pada aplikasi silahkan anda ganti dengan nama yang anda inginkan, dan proses settingnya telah selesai anda bisa mencobanya dengan menekan tombol button yang telah anda settings dan lihat hasilnya.
Bila tombol ditekan akan menyalakan atau mematikan led internal ESP8266.
Semoga bermanfaat.
Membuat coding untuk ESP8266 agar dapat digunakan dengan CUBE android apps
Reviewed by cahyohertanto
on
June 25, 2018
Rating:
No comments: