Pár napja elkezdtem foglalkozni az ESP8266-os wi-fi modullal. Mivel Bascom alatt nem találtam ezzel kapcsolatban topikot, gondoltam megosztom néhány sorban a tapasztalataimat. A terv egy redőnyvezérlő megépítése volt, egy általam nem annyira kedvelt rádiós vezérlés formájában, de jelenleg nincs lehetőségem (nem akarok ) falat vésni, és vezetékezni a szobákban. A vezérlő egy andoidos telefonra/tabletre írt alkalmazás (tcp kliens), ami a wifi modullal (tcp szerver) épít ki egy kapcsolatot, és küldi tovább a parancsokat a kontrollernek. (tehát a redőnytokban lesz elhelyezve az alappanel, ami esetemben egy Atmega328 alapra épülő áramkör, és ez kapcsolódik soros porton az ESP8266 wifi modulhoz)
Saját tapasztalatok a wifi modullal kapcsolatban:
- előre beállított gyári sebesség esetemben 9600
- nem szerette a 5V-t, tehát szintillesztő alkalmazása volt szükséges (vagy 3.3V-n üzemelő kontroller, a végleges majd ez lesz)
- számomra kicsit furcsán kommunikál, néha nehéz volt kihámozni a lényeget a vett üzenetből
Jelenleg teszt üzemmódban van, néhány hetes hibátlan működés után kerül majd a redőny tokjába a végleges nyák, melynek mérete így eléggé limitált lesz (magasság szempontjából). Ebben az esetben a monitorozás már csak szoftveres soros porton fog történni.
Az AC kapcsolását 2 db SSR (2A) végzi (fel/le irányok), remélem bírni fogja az induktív terhelést, ami esetemben ~100W/VA.
Alább a program, mely az első felében a modul tesztelését végzi el (SSID, IP cím, AP üzemmód kiolvasása,TCP port megnyitása), majd jöhet az érdemi rész, vagyis a kommunikáció a klienssel.
Kód: Egész kijelölése
$regfile = "m128def.dat"
$crystal = 16000000
$hwstack = 140
$swstack = 140
$framesize = 140
Config Com1 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Com2 = 38400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "com2:" For Binary As #1
Config Serialin = Buffered , Size = 50
Config Serialin1 = Buffered , Size = 50
Config Watchdog = 2048
Config Portb.1 = Output 'led
Config Portb.2 = Output
Config Portb.3 = Output
Shutter_up Alias Portb.2
Shutter_down Alias Portb.3
Reset Shutter_up
Reset Shutter_down
Config Timer1 = Timer , Prescale = 1024
Timer1 = 49911
On Ovf1 Pulse
Enable Timer1
Start Timer1
Dim S As Byte
Dim Rx_data As String * 50
Dim D As String * 10
Dim Apmode As String * 1
Dim A As Byte
Dim Command As String * 100
Dim Seconds As Byte
Dim Rxtimer As Byte
Dim Status As String * 15
Status = "$Unknown *"
Dim Myipaddress As String * 15
Dim My_access_point As String * 15
Dim Commandlength As Byte
Enable Interrupts
'************************* SETTING WI-FI MODULE ********************
Start Watchdog
Print
Print "Starting...."
Print "Waiting for ESP module wake up" ;
For A = 1 To 2
Print "." ;
Wait 1
Next
Print
Print
'Set Echo on
Print "Set echo on : ";
Print #1 , "ATE1"
Sr0:
Input #1 , Rx_data Noecho
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr0
Print Rx_data
Print
Waitms 500
'Check AP mode
Command = ""
Rx_data = ""
Print #1 , "AT+CWMODE?"
Sr1:
Input #1 , Rx_data Noecho
Command = Command + Rx_data
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr1
Commandlength = Len(command)
Apmode = Mid(command , 21 , 1)
Command = ""
Rx_data = ""
If Apmode = "1" Then Print "AP mode: Standalone"
If Apmode = "2" Then Print "AP mode: Access Point"
If Apmode = "3" Then Print "AP mode: Standalone & Access Point"
Print
Waitms 500
'Check access_point
Command = ""
Rx_data = ""
Print #1 , "AT+CWJAP?"
Sr3:
Input #1 , Rx_data Noecho
Command = Command + Rx_data
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr3
Commandlength = Len(command)
Commandlength = Commandlength - 24
My_access_point = Mid(command , 20 , Commandlength)
Command = ""
Rx_data = ""
Print "Access point : " ; My_access_point
Print
Waitms 500
'Check IP address
Command = ""
Rx_data = ""
Print #1 , "AT+CIFSR"
Sr2:
Input #1 , Rx_data Noecho
Command = Command + Rx_data
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr2
Commandlength = Len(command)
Commandlength = Commandlength - 14
Myipaddress = Mid(command , 11 , Commandlength)
Command = ""
Rx_data = ""
Print "IP address : " ; Myipaddress
Print
Waitms 500
'Set SERVER mode
Print "Setting server mode : " ;
Command = ""
Rx_data = ""
Print #1 , "AT+CIPMUX=1"
Sr4:
Input #1 , Rx_data Noecho
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr4
Command = ""
Rx_data = ""
Print D
Waitms 500
Print
'Open PORT
Print "Open server port : ";
Command = ""
Rx_data = ""
Print #1 , "AT+CIPSERVER=1,2000"
Sr5:
Input #1 , Rx_data Noecho
D = Mid(rx_data , 2 , 2)
Command = ""
Rx_data = ""
If D = "OK" Then Print "Port opened"
If D <> "OK" Then
If D = "no" Then Print "Port opened already"
If D <> "no" Then Goto Sr5
End If
Print
Waitms 500
'Set Echo off
Print "Set echo off : ";
Command = ""
Rx_data = ""
Print #1 , "ATE0"
Sr6:
Input #1 , Rx_data Noecho
D = Mid(rx_data , 2 , 2)
If D <> "OK" Then Goto Sr6
Print Rx_data
Command = ""
Rx_data = ""
Print
Print "Ready.."
Print
'***************** Dialogue with WI-FI MODULE ********************
Do
S = Inkey(#1)
If S <> 0 Then Gosub Received_char
Loop
Received_char:
If S = ":" Then
Command = ""
End If
If S <> 0 Then
If S <> 10 And S <> 13 Then
Rxtimer = 2
If Len(command) > 45 Then Command = ""
Command = Command + Chr(s)
If Command = "Link" Then
Command = ""
Print "Connected"
Waitms 200
Print #1 , "AT+CIPSEND=0,14"
Waitms 200
Print #1 , Status ;
End If
If Command = "Unlink" Then
Print "Disconnected"
Command = ""
End If
If Right(command , 2) = "OK" Then
Commandlength = Len(command)
Commandlength = Commandlength - 3
Command = Mid(command , 2 , Commandlength)
If Command = " SEND " Then
Command = ""
Return
End If
Print "Received Command (" ; Command ; ")"
If Command = "Up" And Shutter_down = 0 Then
Waitms 200
Print #1 , "AT+CIPSEND=0,11"
Waitms 200
Print #1 , "$Moving up*";
Seconds = 40
Waitms 100
Set Shutter_up
Status = "$Moving *"
End If
If Command = "Up" And Shutter_down = 1 Then
Waitms 200
Print #1 , "AT+CIPSEND=0,11"
Waitms 200
Print #1 , "$Stopped *";
Print "Shutter was stopped"
Seconds = 0
Reset Shutter_down
Status = "$Stopped *"
End If
If Command = "Down" And Shutter_up = 0 Then
Waitms 200
Print #1 , "AT+CIPSEND=0,13"
Waitms 200
Print #1 , "$Moving down*";
Seconds = 40
Set Shutter_down
Status = "$Moving *"
End If
If Command = "Down" And Shutter_up = 1 Then
Waitms 200
Print #1 , "AT+CIPSEND=0,11"
Waitms 200
Print #1 , "$Stopped *";
Print "Shutter was stopped"
Seconds = 0
Reset Shutter_up
Status = "$Stopped *"
End If
Command = ""
End If
End If
End If
Return
Pulse:
Reset Watchdog
Timer1 = 49911
Toggle Portb.1
'Print "-sec- : " ; Seconds ; " : " ; Command
If Command <> "" Then Print "Buffer " ; Command
If Len(rx_data) > 45 Then Rx_data = ""
If Len(command) > 45 Then Command = ""
If Rxtimer > 0 Then
Decr Rxtimer
If Rxtimer = 0 Then Command = ""
End If
If Shutter_up = 1 And Shutter_down = 1 Then
Reset Shutter_up
Reset Shutter_down
Print "Controll error!"
End If
If Seconds > 0 Then
Decr Seconds
Print Seconds
If Seconds = 0 And Shutter_up = 1 Then
Reset Shutter_up
Status = "$Shutter_up *"
Print "Shutter has moved up"
Print #1 , "AT+CIPSEND=0,22"
Waitms 500
Print #1 , "$Shutter was moved up*";
End If
If Seconds = 0 And Shutter_down = 1 Then
Reset Shutter_down
Status = "$Shutter_down*"
Print "Shutter has moved down"
Print #1 , "AT+CIPSEND=0,24"
Waitms 500
Print #1 , "$Shutter was moved down*";
End If
End If
Return
End