ESP8266-01 with Arduino UNO and Bascom AVR (as a Client)

   Karena ada yang bertanya mengenai kode program Bascom AVR untuk menggunakan arduino dengan ESP8266-01 saya coba share sedikit yang saya bisa. Namun hal pertama yang harus dipelajari adalah ATCommand dari ESP8266 itu sendiri yang bisa anda pelajari dari postingan saya sebelumnya mengenai ESP8266.

Perlu diingat ATCommand ESP8266 akan selalu diakhiri dengan Chr(13) dan Line Feed pada setiap replynya, sehingga dapat dipergunakan metode syntax input maupun inkey(). Saya menggunakan Arduino UNO sebagai kontrolernya untuk mengirimkan data ADC (yang saya simulasikan dengan Random Number) dengan menggunakan software serial sehingga saya masih dapat melakukan debugging pada hardware serial. Berikut adalah kode program BASCOM AVR nya:

 $regfile = "m328pdef.dat"  
 $crystal = 16000000  
 $baud = 9600  
 $hwstack = 40  
 $swstack = 16  
 $framesize = 32  
 Const Ssid = "Your'e AP"  
 Const Pass = "Your'e AP Pass"  
 Const Ip = "Your'e Destination Host"  
 Declare Sub Wait_for_string(byval R As String)  
 Declare Function Read_string() As String  
 Declare Sub Send  
 Declare Sub Initial  
 Config Portb.5 = Output  
 Portb.5 = 0  
 Open "comb.3:9600,8,n,1" For Output As #1  
 Open "comb.2:9600,8,n,1" For Input As #2  
 Enable Interrupts  
 Dim Check As Bit , Rxstring As String * 100 , V As String * 100  
 Dim A As Integer 

 Wait 5  
 Call Initial  
 Do  
 A = Rnd(255)  
 Call Send  
 Wait 10  
 Loop  
 End  
'====================================================================================
 Sub Initial:  
 Print "ESP8266-01 Module"  
 Waitms 500  
 Print "Reset Module"  
 Waitms 500  
 Print #1 , "AT+RST"  
 Print ""  
 Wait 5  
 Print "Joint into AP"  
 Waitms 500  
 Print #1 , "AT+CWJAP=" ; Chr(34) ; Ssid ; Chr(34) ; Chr(44) ; Chr(34) ; Pass ; Chr(34)  
 Rxstring = Read_string()  
 V = Mid(rxstring , 3 , 10)  
 Do  
 Wait_for_string "OK"  
 Loop Until Check = 1  
 Reset Check  
 Print "Connected to:" ; Ssid  
 Print ""  
 Wait 1  
 Print "SET Multiple connection"  
 Waitms 500  
 Print #1 , "AT+CIPMUX=1="  
 Do  
 Wait_for_string "OK"  
 Loop Until Check = 1  
 Reset Check  
 Print "Multiple Connection Ready"  
 Print ""  
 Wait 1  
 End Sub  
 '===============================================================================  
 Sub Send:  
 Portb.5 = 0  
 Print "Connecting to Host"  
 Print #1 , "AT+CIPSTART=4" ; Chr(44) ; Chr(34) ; "TCP" ; Chr(34) ; Chr(44) ; Chr(34) ; Ip ; Chr(34) ; Chr(44) ; "80"  
 Do  
 Wait_for_string "Linked"  
 Loop Until Check = 1  
 Reset Check  
 Print "Connected to host"  
 Print ""  
 Wait 1  
 Print "Prepare to Send Data"  
 Waitms 500  
 Print #1 , "AT+CIPSEND=4,32"  
 Do  
 Wait_for_string ">"  
 Loop Until Check = 1  
 Reset Check  
 Print "Ready to send"  
 Print ""  
 Wait 1  
 Print "Sending Data"  
 Print #1 , "GET " ; "/save.php?adc=" ; A  
 Print #1 , "HTTP/1.1\r\n"  
 Do  
 Wait_for_string "+IPD"  
 Loop Until Check = 1  
 Reset Check  
 Print "Success Send Data"  
 Print ""  
 Wait 1  
 Close #1  
 Close #2  
 End Sub  
 '===============================================================================  
 Sub Wait_for_string(byval R As String) As String  
   Local Pjng As Byte  
   Local Re As Byte  
    Pjng = 1  
    S1:  
    Re = Inkey(#2)  
    If Re <> Mid(r , Pjng , 1) Then  
      Goto S1  
    Else  
      Incr Pjng  
      If Pjng > Len(r) Then Goto S2  
      Goto S1  
    End If  
   S2:  
   Set Check  
 End Sub  
 '===============================================================================  
 Function Read_string() As String  
   Local Sr As Byte  
   Local Rxd As String * 100  
   Local Panjang As Integer  
   Rxd = ""  
    While Sr <> Chr(13)  
      Sr = Inkey(#2)  
      If Sr = Chr(10) Then Sr = " "  
      Rxd = Rxd + Chr(sr)  
    Wend  
    Panjang = Len(rxd) - 1  
    Read_string = Left(rxd , Panjang)  
 End Function  

Karena saya menggunakan  software serial saya juga dapat melihat proses dari arduino saat mengirimkan data melalui terminal pada Bascom AVR atau serial monitor pada ARDUINO IDE, dan hasilnya seperti pada video yang bisa anda lihat di channel youtube saya.

Anda dapat menggabungkan kode ini dengan metode PHP_GET yang ada pada postingan saya sebelumnya, sehingga anda dapat membuat project IOT untuk mengirimkan data dari sensor ke server dengan menggunakan wifi module. Semoga bermanfaat.


ESP8266-01 with Arduino UNO and Bascom AVR (as a Client) ESP8266-01 with Arduino UNO and Bascom AVR (as a Client) Reviewed by cahyohertanto on July 06, 2015 Rating: 5

No comments:

close