Friday, 4 March 2016

Step to the Next Level with Arduino (Using ESP8266 Modul) #Part1

Introduction

Selanjutnya, kita akan melangkah ke level yang sedikit lebih advance, yaitu membuat board arduino dapat berkomunikasi dengan perangkat lain dengan menambahkan sebuah modul komunikasi, yaitu modul ESP8266 (wifi). Parameter keberhasilan dari proyek ini adalah membuat modul ESP8266 dapat terkoneksi dengan perangkat lain seperti PC, smartphone, dan lain-lain.

 

Intermezzo

Modul ESP8266 memiliki banyak model/tipe. Sejauh saya ketahui ada sekitar enam belas tipe berdasarkan website resmi ESP8266. Pada dasarnya secara fungsional semua tipe sama saja, yang membedakan hanyalah pada jumlah pin, ukuran, dan konteks penggunaan. 

Yang paling sering digunakan untuk board arduino dan juga banyak referensinya di internet adalah modul ESP8266 tipe-01. Untuk proyek ini, asisten perkuliahan memang tidak menyebutkan secara eksplisit tipe mana yang harus digunakan (sedangkan saya saat itu belum mengetahui kalau modul ini memiliki banyak tipe), jadinya saya secara "tidak sengaja" membeli modul ESP8266 tipe-12. Berikut penampakan dari modul ESP8266 tipe-12 dan tabel perbandingan setiap tipe.



What We Will Need

  1. Arduino atau Genuino Board
  2. Modul ESP8266
  3. Bread Board
  4. Kabel Jumper

Build The Circuit

Karena proyek ini menggunakan modul ESP8266 tipe-12 maka ada sedikit penyesuaian pada skema yang digunakan.



Start Coding

Kode berikut diambil dari sini. Kode ini bertujuan untuk menjadikan arduino dan modul ESP8266 sebagai webserver. Namun, karena terkendala pada modifikasi dari tipe-01 ke tipe-12, kode ini belum berhasil diimplementasikan pada proyek ini.

1:  #include <SoftwareSerial.h>  
2:  #define DEBUG true  
3:  SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.  
4:                 // This means that you need to connect the TX line from the esp to the Arduino's pin 2  
5:                 // and the RX line from the esp to the Arduino's pin 3  
6:  void setup()  
7:  {  
8:   Serial.begin(9600);  
9:   esp8266.begin(9600); // your esp's baud rate might be different  
10:   sendData("AT+RST\r\n",2000,DEBUG); // reset module  
11:   sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point  
12:   sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address  
13:   sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections  
14:   sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80  
15:  }  
16:  void loop()  
17:  {  
18:   if(esp8266.available()) // check if the esp is sending a message   
19:   {  
20:    /*  
21:    while(esp8266.available())  
22:    {  
23:     // The esp has data so display its output to the serial window   
24:     char c = esp8266.read(); // read the next character.  
25:     Serial.write(c);  
26:    } */  
27:    if(esp8266.find("+IPD,"))  
28:    {  
29:     delay(1000);  
30:     int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns   
31:                        // the ASCII decimal value and 0 (the first decimal number) starts at 48  
32:     String webpage = "<h1>Hello</h1>&lth2>World!</h2><button>LED1</button>";  
33:     String cipSend = "AT+CIPSEND=";  
34:     cipSend += connectionId;  
35:     cipSend += ",";  
36:     cipSend +=webpage.length();  
37:     cipSend +="\r\n";  
38:     sendData(cipsend,1000,DEBUG);  
39:     sendData(webpage,1000,DEBUG);  
40:     webpage="<button>LED2</button>";  
41:     cipSend = "AT+CIPSEND=";  
42:     cipSend += connectionId;  
43:     cipSend += ",";  
44:     cipSend +=webpage.length();  
45:     cipSend +="\r\n";  
46:     sendData(cipsend,1000,DEBUG);  
47:     sendData(webpage,1000,DEBUG);  
48:     String closeCommand = "AT+CIPCLOSE=";   
49:     closeCommand+=connectionId; // append connection id  
50:     closeCommand+="\r\n";  
51:     sendData(closeCommand,3000,DEBUG);  
52:    }  
53:   }  
54:  }  
55:  String sendData(String command, const int timeout, boolean debug)  
56:  {  
57:    String response = "";  
58:    esp8266.print(command); // send the read character to the esp8266  
59:    long int time = millis();  
60:    while( (time+timeout) > millis())  
61:    {  
62:     while(esp8266.available())  
63:     {  
64:      // The esp has data so display its output to the serial window   
65:      char c = esp8266.read(); // read the next character.  
66:      response+=c;  
67:     }   
68:    }  
69:    if(debug)  
70:    {  
71:     Serial.print(response);  
72:    }  
73:    return response;  
74:  }  

The Result

Berikut hasil dari langkah-langkah di atas yang sudah dikerjakan:



















   
Terima Kasih ;3




No comments :

Post a Comment

Views

Designed By Blogger Templates