Share My Creation B4J App for ESP8266/32

edited: connecting to www was added.
B4J app for testing and setting ESP8266/32 using AT Commands.
AT commands are used to test the module, set baud rate, check version, set RF power etc. The app also connects to a router. List of AT commands can be downloaded from here:
https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
https://docs.espressif.com/projects/esp-at/en/latest/AT_Command_Set/index.html
A new board comes with AT commands firmware, after uploading your code the AT commands are replaced. To have the AT commands back you need to upload the factory bin file.
The module is connected via USB, or via USB to Serial adapter, or via Arduino.
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private sp As Serial
    Private astream As AsyncStreams
    Private cmb1 As ComboBox
    Private Label1 As Label
    Private text1 As TextArea
    Private btnClear As Button
    Private btnSend As Button

    Private rate As Int = 115200
    Private txt1 As TextField
    Private TextField1 As TextField
    Private Button1 As Button
    Private TextField2 As TextField
    Private Button2 As Button
    Private TextField3 As TextField
    Private Button3 As Button
    Private TextField4 As TextField
    Private cmbBaud As ComboBox
    Private btnCmnd As Button
    Private cmbCmnd As ComboBox
    Private TextField5 As TextField
    Private txtIP As TextField
    Private btnWeb As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    sp.Initialize("")
    cmb1.Items.AddAll(sp.ListPorts)

    cmbBaud.Items.AddAll(Array As String("9600", "38400", "115200"))
    cmbCmnd.Items.AddAll(Array As String("AT+RST", "AT+SLEEP", "AT+RFPOWER","AT+UART_CUR","AT+UART_CUR=115200,8,1,0,1", _
    "AT+SYSRAM","AT+SYSADC","AT+CIPSTA_CUR"))
End Sub

Sub cmb1_SelectedIndexChanged(Index As Int, Value As Object)
    Try
        sp.Open(cmb1.Value)
        sp.SetParams(rate,8,1,0)
        astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
        Label1.Text = "Port Opened"
    Catch
        Label1.Text = "Port is busy"
        Log("Port error")
    End Try

End Sub

Sub cmbBaud_SelectedIndexChanged(Index As Int, Value As Object)
    rate=cmbBaud.Value
    cmb1.Enabled=True
End Sub

Sub cmbCmnd_SelectedIndexChanged(Index As Int, Value As Object)
    btnCmnd.Enabled=True
End Sub

Sub btnCmnd_Click
    Dim command As String
    command=cmbCmnd.Value &  Chr(13) & Chr(10)
    astream.Write(command.GetBytes("UTF8"))
End Sub

Sub btnSend_Action
    Dim str As String = txt1.Text.ToUpperCase &  Chr(13) & Chr(10)
    astream.Write(str.GetBytes("UTF8"))
End Sub

Sub Button1_Click
    Dim str1 As String = TextField1.Text.ToUpperCase &  Chr(13) & Chr(10)
    astream.Write(str1.GetBytes("UTF8"))
End Sub

Sub Button2_Click
    Dim str2 As String = TextField2.Text.ToUpperCase &  Chr(13) & Chr(10)
    astream.Write(str2.GetBytes("UTF8"))
End Sub

Sub Button3_Click
    Dim str3 As String  = "AT+CWMODE=1" &  Chr(13) & Chr(10)
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(1000)
    Dim str4 As String = "AT+CWJAP=" & TextField4.Text & "," & TextField5.text & Chr(13) & Chr(10)
    astream.Write(str4.GetBytes("UTF8"))
    Sleep(2000)
    btnWeb.Enabled=True
End Sub

Sub btnWeb_Click
    Dim str3 As String  = "AT+CIPSTART=" & Chr(0x22) & "TCP" & Chr(0x22) & "," & Chr(0x22) & txtIP.text & Chr(0x22) & "," & "80" &  Chr(13) & Chr(10)
    Dim str6 As String = "Host: " & txtIP.text & Chr(13) & Chr(10) & Chr(13) & Chr(10)
    Dim bts As Int = str6.Length + 16
    astream.Write(str3.GetBytes("UTF8"))
    Sleep(1000)
    Dim str4 As String = "AT+CIPSEND=" & bts & Chr(13) & Chr(10)
    astream.Write(str4.GetBytes("UTF8"))
    Sleep(1000)
    Dim str5 As String  = "GET / HTTP/1.1" & Chr(13) & Chr(10)
    astream.Write(str5.GetBytes("UTF8"))
    astream.Write(str6.GetBytes("UTF8"))
 
End Sub

Sub btnClear_Click
    text1.Text=""
End Sub

Sub MainForm_Closed
    'sp.Close
End Sub

Sub AStream_NewData (Buffer() As Byte)
    text1.Text=text1.Text & BytesToString(Buffer,0,Buffer.Length,"UTF-8")
End Sub
 

Attachments

  • esp_test.zip
    4 KB · Views: 261
  • esp_test-4x3.png
    esp_test-4x3.png
    31.4 KB · Views: 1,395
  • esp_test.gif
    esp_test.gif
    30.9 KB · Views: 225
Last edited:

Cableguy

Expert
Licensed User
Longtime User
If I chose a "Busy" com port, the app crashes...
 

Cableguy

Expert
Licensed User
Longtime User
I guess enclosuring it inside a catch/try and deal with the error in some way would do...

I am having a hard time getting AT comand to send (specially since there's no visual indication of it being sent), since I don't get an answer from neither my ESP8266 wemos d1 or my ESP32 dev board.
When I first choose the Baud rate and the Com port, I get a bunch of info from the ESP32, but from the ESP8266 I only get the APPSTART...

I really love the idea of being able to comunicate with the ESP board "out of the box" using AT commands...
 

moty22

Active Member
Licensed User
I guess enclosuring it inside a catch/try and deal with the error in some way would do...

I am having a hard time getting AT comand to send (specially since there's no visual indication of it being sent), since I don't get an answer from neither my ESP8266 wemos d1 or my ESP32 dev board.
When I first choose the Baud rate and the Com port, I get a bunch of info from the ESP32, but from the ESP8266 I only get the APPSTART...

I really love the idea of being able to comunicate with the ESP board "out of the box" using AT commands...
I have to learn about catch/try.
ESP that already has a code embedded don't response to AT commands, I know that after re-uploading the boot bin file it works but it's a long process and you lose the program you wrote to it. I will look at the data sheet to see if there is another way. I have only ESP8266-01
 

Cableguy

Expert
Licensed User
Longtime User
My ESP32 is a new un-used one... my ESP8266 is indeed used.
 

Cableguy

Expert
Licensed User
Longtime User
Sans titre.png

This is my ESP, but I'm struggling to find the right board defenition
 
Top