Thursday, 22 December 2016

IOT : Control 4 LED dan 8 Channel Relay Module dengan ESP8266 NodeMcu via Local Wifi Router

");
client.println("
");
client.println("");
client.print("Control 4 LED Via Wifi");
client.println("
");
client.println("
"
);
client.println("

"
);

// Return the response LED1
client.print("LED1 is now: ");
// Control LED 1
if (request.indexOf("/LED1=ON") != -1) {
digitalWrite(LED1, HIGH);
value1 = HIGH;
}
if (request.indexOf("/LED1=OFF") != -1) {
digitalWrite(LED1, LOW);
value1 = LOW;
}
if(value1==HIGH){client.print("ON");}
if(value1==LOW){client.print("OFF");}

client.println("

"
);
client.println("");
client.println("
"
);
client.println("");

// Return the response LED2
client.println("

"
);
client.print("LED2 is now: ");
// Control LED 2
if (request.indexOf("/LED2=ON") != -1) {
digitalWrite(LED2, HIGH);
value2 = HIGH;
}
if (request.indexOf("/LED2=OFF") != -1) {
digitalWrite(LED2, LOW);
value2 = LOW;
}
if(value2==HIGH){client.print("ON");}
if(value2==LOW){client.print("OFF");}

client.println("

"
);
client.println("");
client.println("
"
);
client.println("");

// Return the response LED3
client.println("

"
);
client.print("LED3 is now: ");
// Control LED 3
if (request.indexOf("/LED3=ON") != -1) {
digitalWrite(LED3, HIGH);
value3 = HIGH;
}
if (request.indexOf("/LED3=OFF") != -1) {
digitalWrite(LED3, LOW);
value3 = LOW;
}
if(value3==HIGH){client.print("ON");}
if(value3==LOW){client.print("OFF");}

client.println("

"
);
client.println("");
client.println("
"
);
client.println("");

// Return the response LED4
client.println("

"
);
client.print("LED4 is now: ");
// Control LED 4
if (request.indexOf("/LED4=ON") != -1) {
digitalWrite(LED4, HIGH);
value4 = HIGH;
}
if (request.indexOf("/LED4=OFF") != -1) {
digitalWrite(LED4, LOW);
value4 = LOW;
}
if(value4==HIGH){client.print("ON");}
if(value4==LOW){client.print("OFF");}

client.println("

"
);
client.println("");
client.println("
"
);
client.println("");

delay(1);
Serial.println("Client disonnected");
Serial.println("");

}


Pastikan proses Upload Berhasil dan tertera "Done Uploading".
Proses Upload akan memakan waktu lebih lama dibanding upload ke Arduino.

6.Setelah proses Upload selesai, buka Serial Monitor pada Arduino IDE pada boudrate 115200 - Newline.


akan nampak seperti gambar di atas (jika anda pakai coding diatas maka akan menggunakan bahasa inggris).Perhatikan pada IP Address yang tampil pada serial monitor.Itu adalah address ESP yang telah tergabung pada jaringan Wifi anda.

7.Buka Browser (Mozilla,Chrome,IE,UC,Safari,Opera) pada PC/Laptop atau Smartphone anda yang sudah tergabung dalam jaringan Wifi yang sama.Ketikan Address ESP yg ada pada Serial Monitor pada kolom alamat pada browser anda.
pada contoh kali ini alamat ESP saya adalah : 192.168.0.110

8.Anda dapat mengontrol LED1 - LED4 melalui tombol tombol yang tampil pada browser




Kontrol 8 Channel Relay Module melalui wifi menggunakan ESP8266 Nodemcu

Pada Tutorial kali ini kita membutuhkan :
-ESP8266 NodeMCU (4M lua wifi) baik yang pakai Uploader CH340 atau CP2102
-Kabel data Micro USB Untuk programming module ESP Nodemcu
-Relay Module 8 Channel (Low Active)
-Kabel Jumper Dupont Female to Female
-Software Arduino IDE 1.6.xx



Berikut Coding untuk Control Relay secara sederhana menggunakan ESP8266 Nodemcu

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include 

#define ON LOW
#define OFF HIGH

const char* ssid = "RAJACELL";
const char* password = "Rajacell123Rajacell123";

int RELAY1 = 5; //D1 on ESP Board
int RELAY2 = 4; //D2 on ESP Board
int RELAY3 = 0; //D3 on ESP Board
int RELAY4 = 2; //D4 on ESP Board
int RELAY5 = 14; //D5 on ESP Board
int RELAY6 = 12; //D6 on ESP Board
int RELAY7 = 13; //D7 on ESP Board
int RELAY8 = 15; //D8 on ESP Board
int value1 = OFF, value2 = OFF, value3 = OFF, value4 = OFF;
int value5 = OFF, value6 = OFF, value7 = OFF, value8 = OFF;

WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);
pinMode(RELAY1, OUTPUT); digitalWrite(RELAY1, HIGH);
pinMode(RELAY2, OUTPUT); digitalWrite(RELAY2, HIGH);
pinMode(RELAY3, OUTPUT); digitalWrite(RELAY3, HIGH);
pinMode(RELAY4, OUTPUT); digitalWrite(RELAY4, HIGH);
pinMode(RELAY5, OUTPUT); digitalWrite(RELAY5, HIGH);
pinMode(RELAY6, OUTPUT); digitalWrite(RELAY6, HIGH);
pinMode(RELAY7, OUTPUT); digitalWrite(RELAY7, HIGH);
pinMode(RELAY8, OUTPUT); digitalWrite(RELAY8, HIGH);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Menghubungkan ke ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Terhubung");

// Start the server
server.begin();
Serial.println("Memulai Server");

// Print the IP address
Serial.print("Gunakan URL berikut untuk terhubung dengan ESP8266 : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");

}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Set LED according to the request
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("");
client.println("");
client.println("");
client.print("Control 8 Relay Via Wifi");
client.println("
");
client.println("

"
);

// Return the response RELAY1
client.print("RELAY1 dalam kondisi : ");
// Control LED 1
if (request.indexOf("/RELAY1=ON") != -1) {
digitalWrite(RELAY1, ON);
value1 = ON;}
if (request.indexOf("/RELAY1=OFF") != -1) {
digitalWrite(RELAY1, OFF);
value1 = OFF;}
if(value1==ON){client.print("ON");}
if(value1==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY2
client.println("

"
);
client.print("RELAY2 dalam kondisi : ");
// Control LED 2
if (request.indexOf("/RELAY2=ON") != -1) {
digitalWrite(RELAY2, ON);
value2 = ON;}
if (request.indexOf("/RELAY2=OFF") != -1) {
digitalWrite(RELAY2, OFF);
value2 = OFF;}
if(value2==ON){client.print("ON");}
if(value2==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY3
client.println("

"
);
client.print("RELAY3 dalam kondisi : ");
// Control LED 3
if (request.indexOf("/RELAY3=ON") != -1) {
digitalWrite(RELAY3, ON);
value3 = ON;}
if (request.indexOf("/RELAY3=OFF") != -1) {
digitalWrite(RELAY3, OFF);
value3 = OFF;}
if(value3==ON){client.print("ON");}
if(value3==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY4
client.println("

"
);
client.print("RELAY4 dalam kondisi : ");
// Control LED 4
if (request.indexOf("/RELAY4=ON") != -1) {
digitalWrite(RELAY4, ON);
value4 = ON;}
if (request.indexOf("/RELAY4=OFF") != -1) {
digitalWrite(RELAY4, OFF);
value4 = OFF;}
if(value4==ON){client.print("ON");}
if(value4==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY5
client.println("

"
);
client.print("RELAY5 dalam kondisi : ");
if (request.indexOf("/RELAY5=ON") != -1) {
digitalWrite(RELAY5, ON);
value5 = ON;}
if (request.indexOf("/RELAY5=OFF") != -1) {
digitalWrite(RELAY5, OFF);
value5 = OFF;}
if(value5==ON){client.print("ON");}
if(value5==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY6
client.println("

"
);
client.print("RELAY6 dalam kondisi : ");
if (request.indexOf("/RELAY6=ON") != -1) {
digitalWrite(RELAY6, ON);
value6 = ON;}
if (request.indexOf("/RELAY6=OFF") != -1) {
digitalWrite(RELAY6, OFF);
value6 = OFF;}
if(value6==ON){client.print("ON");}
if(value6==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY7
client.println("

"
);
client.print("RELAY7 dalam kondisi : ");
if (request.indexOf("/RELAY7=ON") != -1) {
digitalWrite(RELAY7, ON);
value7 = ON;}
if (request.indexOf("/RELAY7=OFF") != -1) {
digitalWrite(RELAY7, OFF);
value7 = OFF;}
if(value7==ON){client.print("ON");}
if(value7==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("");


// Return the response RELAY8
client.println("

"
);
client.print("RELAY8 dalam kondisi : ");
if (request.indexOf("/RELAY8=ON") != -1) {
digitalWrite(RELAY8, ON);
value8 = ON;}
if (request.indexOf("/RELAY8=OFF") != -1) {
digitalWrite(RELAY8, OFF);
value8 = OFF;}
if(value8==ON){client.print("ON");}
if(value8==OFF){client.print("OFF");}
client.println("

"
);
client.println("");
client.println("
"
);
client.println("

Video yang berkaitan dengan IOT : Control 4 LED dan 8 Channel Relay Module dengan ESP8266 NodeMcu via Local Wifi Router


0 comments:

Post a Comment