banner image
Sedang Dalam Perbaikan

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



Pada tutorial Project ini kita akan mengendalikan LED dan / atau Relay module melalui jaringan Wifi Router menggunakan ESP8266 Nodemcu-4M.

Pada tutorial kali ini yang kita butuhkan adalah Jaringan Wifi melaului Wifi Router (tidak harus terhubung ke internet).Nantinya ESP8266 akan kita hubungkan ke Jaringan wifi sebagai Client dan akan mendapatkan ip address untuk di Akses.Semua perangkat Client yang terhubung ke router bisa mengakses ESP8266 melalui IP Address ESP.




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
-4pcs LED + 4PCS Resistor 100Ohm
-Relay Module 8 Channel
-Kabel Jumper Dupont Female to Female
-Software Arduino IDE 1.6.xx

Software Arduino IDE harus sudah diinstall Board ESP8266..apabila belum ada silahkan terlebihdulu mengikuki Tutorial awal : Starting ESP8266 Nodemcu with Arduino IDE

Setting Arduino IDE seperti pada gambar berikut:


 Atur Com-port nya sesuai port CH340/CP2102 module ESP8266 pada sistem anda.
Ada baiknya Tes terlebih dulu dengan example sketch BLINK sebelum memulai project ini untuk memastikan koneksi ESP8266 Nodemcu ke Arduino IDE.
Apabila masih agak bingung dengan penggunaan ESP Nodemcu melalui Arduino IDE silahkan baca terlebih dulu Tutorial Sebelumnya.

GPIO Maping ESP8266 Nodemcu pada Arduino IDE

Pada saat melakukan pemrograman ESP8266 menggunakan Arduino IDE yang kita pakai sebagai acuan adalah nomor GPIO dari ESP nya,bukan nama pin pada Board nya sehingga kita membutuhkan GPIO Pin Maping seperti pada gambar dibawah ini :

 

 

Artinya apabila kita ingin menggunakan Pin D0 pada board maka kita harus menuliskan nomor GPIO nya,yaitu '16' saat membuat coding pada Arduino IDE.Berikut contohnya :


1
2
3
4
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

Untuk memudahkan kita membuat coding dengan Arduino IDE perhatikan tabel GPIO maping berikut :




Kontrol 4 LED melalui wifi menggunakan ESP8266 Nodemcu

 Module yang kita butuhkan untuk tutorial kali ini adalah :
-ESP8266 NodeMCU (4M lua wifi) baik yang pakai Uploader CH340 atau CP2102
-Kabel data Micro USB Untuk programming module ESP Nodemcu
-4pcs LED + 4PCS Resistor 100Ohm
-Kabel Jumper Dupont Female to Female
-Software Arduino IDE 1.6.xx

1.Hubungkan masing-masing pin (+) dari led ke Resistor 100Ohm tujuanya untuk menghindari kerusakan pada led karena io esp memiliki tegangan 3.3V

2.Hubungkan ujung-ujung resistor pada ke empat led masing masing pada pin
 LED1 = D7 on ESP Board
 LED2 = D6 on ESP Board
 LED3 = D5 on ESP Board
 LED4 = D4 on ESP Board

3.Satukan seua pin (-) led dan hubungkan ke pin GND board ESP8266

4.Hubungkan ESP8266 Nodemcu ke Arduino IDE,pastikan setingan Board dan Com sudah benar

5.Upload Sketch Coding berikut :


  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
#include <ESP8266WiFi.h>

const char* ssid = "Nama Jaringan Wifi Anda";
const char* password = "Password Wifi Anda";

int LED1 = 13; //D7 on ESP Board
int LED2 = 12; //D6 on ESP Board
int LED3 = 14; //D5 on ESP Board
int LED4 = 2; //D4 on ESP Board
int value1 = LOW;
int value2 = LOW;
int value3 = LOW;
int value4 = LOW;
WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);

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

WiFi.begin(ssid, password);

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

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

// Print the IP address
Serial.print("Use this URL to connect: ");
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("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<fieldset>");
client.println("<font color = redq>");
client.print("Control 4 LED Via Wifi");
client.println("</font>");
client.println("</fieldset>");
client.println("<br><br>");

// 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("<br><br>");
client.println("<a href=\"/LED1=ON\"\"><button>LED1 ON </button></a>");
client.println("<a href=\"/LED1=OFF\"\"><button>LED1 OFF </button></a><br />");
client.println("</html>");

// Return the response LED2
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/LED2=ON\"\"><button>LED2 ON </button></a>");
client.println("<a href=\"/LED2=OFF\"\"><button>LED2 OFF </button></a><br />");
client.println("</html>");

// Return the response LED3
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/LED3=ON\"\"><button>LED3 ON </button></a>");
client.println("<a href=\"/LED3=OFF\"\"><button>LED3 OFF </button></a><br />");
client.println("</html>");

// Return the response LED4
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/LED4=ON\"\"><button>LED4 ON </button></a>");
client.println("<a href=\"/LED4=OFF\"\"><button>LED4 OFF </button></a><br />");
client.println("</html>");

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 <ESP8266WiFi.h>

#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("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<font color = red>");
client.print("Control 8 Relay Via Wifi");
client.println("</font>");
client.println("<br><br>");

// 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("<br><br>");
client.println("<a href=\"/RELAY1=ON\"\"><button>RELAY1 ON </button></a>");
client.println("<a href=\"/RELAY1=OFF\"\"><button>RELAY1 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY2
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY2=ON\"\"><button>RELAY2 ON </button></a>");
client.println("<a href=\"/RELAY2=OFF\"\"><button>RELAY2 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY3
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY3=ON\"\"><button>RELAY3 ON </button></a>");
client.println("<a href=\"/RELAY3=OFF\"\"><button>RELAY3 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY4
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY4=ON\"\"><button>RELAY4 ON </button></a>");
client.println("<a href=\"/RELAY4=OFF\"\"><button>RELAY4 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY5
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY5=ON\"\"><button>RELAY5 ON </button></a>");
client.println("<a href=\"/RELAY5=OFF\"\"><button>RELAY5 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY6
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY6=ON\"\"><button>RELAY6 ON </button></a>");
client.println("<a href=\"/RELAY6=OFF\"\"><button>RELAY6 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY7
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY7=ON\"\"><button>RELAY7 ON </button></a>");
client.println("<a href=\"/RELAY7=OFF\"\"><button>RELAY7 OFF </button></a><br />");
client.println("</html>");


// Return the response RELAY8
client.println("<br><br>");
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("<br><br>");
client.println("<a href=\"/RELAY8=ON\"\"><button>RELAY8 ON </button></a>");
client.println("<a href=\"/RELAY8=OFF\"\"><button>RELAY8 OFF </button></a><br />");
client.println("</html>");


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

}

IOT : Control 4 LED dan 8 Channel Relay Module dengan ESP8266 NodeMcu via Local Wifi Router IOT : Control 4 LED dan 8 Channel Relay Module dengan ESP8266 NodeMcu via Local Wifi Router Reviewed by MCH on July 21, 2016 Rating: 5

No comments:

Powered by Blogger.