B4R Question Configure esp8266 through the B4A application

Cesar_Morisco

Active Member
Hey guys
All good
Could anyone let me know or have an example I could start with.
How can I configure the esp8266 through my B4A app to not use the ESPConfigurator B4J Thanks in advance
 

Cesar_Morisco

Active Member
There is a problem with the WiFi SSID.
On my simple wi-fi network I can connect the normal "repeated" works fine.
Now on my main wifi I can't connect will it be the spaces between the characters
"Help-Info-Central Tec 8.00 "can't connect
Thank you already
 
Upvote 0

candide

Active Member
Licensed User
you are right, "Space" in html input is received in "+" char in code.

now correction is done in the code and "Space" can be used !
 

Attachments

  • ESP_ConfigV1.2.zip
    4 KB · Views: 173
Upvote 0

Cesar_Morisco

Active Member
you are right, "Space" in html input is received in "+" char in code.

now correction is done in the code and "Space" can be used !
Hello Candide how are you?
Thank you very much
Now I'm connected to my main network that was it Very good
This also works for the other characters like % @ () etc..
thank you very happy
 
Upvote 0

candide

Active Member
Licensed User
the last one with an improvement on sub "Astream_NewData" to avoid duplicate command from html page
 

Attachments

  • ESP_Config1.3.zip
    4 KB · Views: 172
Upvote 0

Cesar_Morisco

Active Member
the last one with an improvement on sub "Astream_NewData" to avoid duplicate command from html page
Hi Candido, how are you?
I have no words to thank you Thank you very much.
I made some changes in your code now You can see your
available wifi networks and copy to the ssid field
Thank you again
Astream.Initialize:
Dim numberofnetwork As Byte = wifi.Scan
    For i = 0 To numberofnetwork - 1
        Astream.Write(" <div style='text-align:left;color:#0000FF;'>")
        Astream.Write("Rede: ").Write(Main.wifi.ScannedSSID(i)).Write("")
        Astream.Write("</div>")
    Next
Reset:
Public Sub ClearStoredDataLength
    Dim header() As Byte = eeprom.ReadBytes(0, 2)
    If header(0) = MAGIC_EEPROM Then
        header(1) = 0
        eeprom.WriteBytes(header,0)   
        Log("Eeprom Apagada")
    End If
    GStore1.Slot0(" ")
    GStore1.Slot1(" ")
    Delay(100)
    esp.restart
End Sub
 

Attachments

  • Captura da Web_7-8-2023_8334_10.0.0.133.jpeg
    Captura da Web_7-8-2023_8334_10.0.0.133.jpeg
    36.7 KB · Views: 130
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
How can I configure the esp8266 through my B4A app
I've made a working project (RFID Wi-Fi time keeping device) one of its components is B4A configuration tool, to configure ESP32 by B4A you need to:

1- Run ESP32 in AP mode, then connect mobile device to it, put your settings on screen then send it to ESP32
2- We send messages between ESP and B4A by serialized object, they should have the same construction on both ESP and B4A

2- You need also to read saved parameters on ESP32
This post explains the ESP32 part, in my next post I will explain the B4A part

declare configuration type according to your needs, and IP's arrays:
B4R:
Type DeviceConfig(DeviceID As String, DeviceIP (4) As Byte , GateWayIP(4) As Byte, SubnetMask(4) As Byte , ServerIP(4) As Byte, _
            ServerPort As UInt, IsStatic As Boolean,NetworkSSID As String, NetworkPassword As String,LCDcontrast As Byte, LCDbacklightLevel As Byte)

'our postman object, send information from/to B4A, it should include the function name you want to run on ESP or B4A when receiving these parameters,
'you may not use all of them, it just can hold up to 16 parameters
Type Message (Function As String, Parameter1 As String, _
                    Parameter2 As String, _
                    Parameter3 As String, _
                    Parameter4 As String, _
                    Parameter5 As String, _
                    Parameter6 As String, _
                    Parameter7 As String, _
                    Parameter8 As String, _
                    Parameter9 As String, _
                    Parameter10 As String, _
                    Parameter11 As String, _
                    Parameter12 As String, _
                    Parameter13 As String, _
                    Parameter14 As String, _
                    Parameter15 As String, _
                    Parameter16 As String) 


Private const SYS_AP_PORT As UInt = 51042 'access point mode port and IP
Private const SYS_AP_IP() As Byte = Array As Byte (192,168,4,1)
Private const SYS_AP_SUBNET() As Byte = Array As Byte (255,255,255,0)
Private Ser As B4RSerializator

At startup, if user pressed setup button then switch to AP mode, otherwise start in STA mode
B4R:
Private Sub AppStart
    Delay(1000)
    Serial1.Initialize(115200)   
    Log("AppStart")   
   
    Dim ST_AP As Boolean = Shift_AP.DigitalRead 'read pin
    If Not(ST_AP) Then  ' if button pressed while startingup, then start wifi in Ap mode, otherwise start normal in STA mode
        WiFiMode = SYS_WIFI_MODE_AP 'this is our setup mode
    Else
        WiFiMode = SYS_WIFI_MODE_ST
    End If
CallSubPlus("Connect",100,0)

After deciding which mode we are going to startup with, we need to make connection, also display some info on LCD
B4R:
Private Sub Connect(Unused As Byte)
    StartWiFi
    If WiFiMode = SYS_WIFI_MODE_AP Then
        NLCD.Clear
        NLCD.PutString(0,0,"CONFG MODE",NLCD.Font8x16,False,True)
        NLCD.PutString(0,20,WiFi.AccessPointIp,NLCD.Font5x8,False,True)
        NLCD.PutString(0,32,JoinStrings(Array As String("Port:",SYS_AP_PORT)),NLCD.Font5x8,False,True)
        
        NLCD.Update
        WiFiServer.Initialize(SYS_AP_PORT, "server_NewConnection")
        WiFiServer.Listen
    Else
        WebSocket.Initialize("WebSocket_NewMessage", "Websocket_Disconnected")
        ConnectWebSocketTimer.Enabled = True
    End If
End Sub

B4R:
private Sub StartWiFi()
    Dim IPall (12) As Byte
    Dim IP(4) As Byte
    Dim GW(4) As Byte
    Dim SM(4) As Byte
    
    Select WiFiMode
        Case SYS_WIFI_MODE_AP            
            IP = SYS_AP_IP
            GW = SYS_AP_IP 'gateway is the same as ip
            SM = SYS_AP_SUBNET
         
            IPall(0) = IP(0)
            IPall(1) = IP(1)
            IPall(2) = IP(2)
            IPall(3) = IP(3)
    
            IPall(4) = GW(0)
            IPall(5) = GW(1)
            IPall(6) = GW(2)
            IPall(7) = GW(3)
    
            IPall(8) = SM(0)
            IPall(9) = SM(1)
            IPall(10) =SM(2)
            IPall(11) = SM(3)           
            RunNative( "StartAP" , IPall )
            ...
            ...
            .....

C++:
void StartAP(B4R::Object* o) {
B4R::Array* b = (B4R::Array*)B4R::Object::toPointer(o);
  const char *ssid = "SSID"; // put your ssid here
  const char *password = "12345678"; //and wifi password as well
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  IPAddress ip(ByteFromArray(b, 0),ByteFromArray(b, 1) , ByteFromArray(b, 2), ByteFromArray(b, 3)); 
  IPAddress gateway(ByteFromArray(b, 4),ByteFromArray(b, 5) , ByteFromArray(b, 6), ByteFromArray(b, 7));
  IPAddress subnet(ByteFromArray(b, 8),ByteFromArray(b, 9) , ByteFromArray(b, 10), ByteFromArray(b, 11));
  WiFi.softAPConfig(ip, gateway, subnet);
  return; //you may not need this
}


Now you need to respond to commands received from your B4A application
B4R:
Sub WiFiStream_NewData (Buffer() As Byte) 'AP mode, debug or set config
    Dim be(20) As Object 'used as a storage buffer, number of items , not bytes
    Dim Objects() As Object = Ser.ConvertBytesToArray(Buffer, be)   'deserialize object received from B4A
    Dim Msg As Message
    Msg.Function = Objects(0)
    Msg.Parameter1 = Objects(1)
    Msg.Parameter2 = Objects(2)
    Msg.Parameter3 = Objects(3)
    Msg.Parameter4 = Objects(4)
    Msg.Parameter5 = Objects(5)
    Msg.Parameter6 = Objects(6)
    Msg.Parameter7 = Objects(7)
    Msg.Parameter8 = Objects(8)
    Msg.Parameter9 = Objects(9)
    Msg.Parameter10 = Objects(10)
    Msg.Parameter11 = Objects(11)
    Msg.Parameter12 = Objects(12)
    Msg.Parameter13 = Objects(13)
    Msg.Parameter14 = Objects(14)
    Msg.Parameter15 = Objects(15)
    Msg.Parameter16 = Objects(16)
    
    Dim FunctionName As String = Msg.Function 'get function name
    Select FunctionName
        Case FUNCTION_SET_DEVICE_CONFIG       ' this is our configuration function
            Dim sConfig As DeviceConfig
            sConfig.DeviceID = Msg.Parameter1
            sConfig.ServerIP = SysTools.IPstrToArray(Msg.Parameter2) 'convert IP string to array of bytes
            sConfig.ServerPort = Msg.Parameter3
            sConfig.IsStatic = SysTools.StrToBoolean(Msg.Parameter4)
            sConfig.DeviceIP = SysTools.IPstrToArray(Msg.Parameter5)
            sConfig.GateWayIP = SysTools.IPstrToArray(Msg.Parameter6)
            sConfig.SubnetMask = SysTools.IPstrToArray(Msg.Parameter7)
            sConfig.NetworkSSID = Msg.Parameter8
            sConfig.NetworkPassword = Msg.Parameter9
            sConfig.LCDcontrast = Msg.Parameter10
            sConfig.LCDbacklightLevel = Msg.Parameter11
            
            SaveConfigs(sConfig)   'save parameters 
case FUNCTION_X
.......
case FUNCTION_Y
......
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
message type declaration, same construction as in ESP32
B4A:
Sub Process_Globals
    Type Message (FUNCTION As String, Parameter1 As String, _
                    Parameter2 As String, _
                    Parameter3 As String, _
                    Parameter4 As String, _
                    Parameter5 As String, _
                    Parameter6 As String, _
                    Parameter7 As String, _
                    Parameter8 As String, _
                    Parameter9 As String, _
                    Parameter10 As String, _
                    Parameter11 As String, _
                    Parameter12 As String, _
                    Parameter13 As String, _
                    Parameter14 As String, _
                    Parameter15 As String, _
                    Parameter16 As String)
Private Ser As B4RSerializator
End Sub

upload config parameters to ESP
B4A:
Private Sub btnUpload_Click
    Dim Msg As Message = CreateEmptyMessage
    Msg.Function = FUNCTION_SET_DEVICE_CONFIG
    Msg.Parameter1 = txtDeviceName.Text
    Msg.Parameter2 = txtServerIP.text
    Msg.Parameter3 = txtPort.text
    Msg.Parameter4 = St.BooleanToStr(swtchDHCP.Value)
    Msg.Parameter5 = txtDeviceIP.text
    Msg.Parameter6 = txtDeviceGateway.text
    Msg.Parameter7 = txtDeviceSubnetMask.Text
    Msg.Parameter8 = txtNetworkSSID.text
    Msg.Parameter9 = txtNetworkPassword.text
    Msg.Parameter10 = pmLCDcontrast.SelectedValue
    Msg.Parameter11 = pmLCDbacklight.SelectedValue
    SendMessage(Msg)
End Sub

initialize message object
B4A:
private Sub CreateEmptyMessage () As Message
    Dim Msg As Message
    Msg.Initialize
    Return Msg
End Sub

serialize object and send it
B4A:
private Sub SendMessage(Msg As Message)
    Try
        If WiFiCon.connected Then 'wificon is a service module
            CallSub2(WiFiCon, "SendData", Ser.ConvertArrayToBytes(MessageToObjects(Msg)))
            ToastMessageShow("Message Sent",False)
        End If
    Catch
        MsgboxAsync(LastException,"")
    End Try
End Sub

B4A:
Public Sub SendData (data() As Byte)
    If Connected Then
        Astream.Write(data)
    End If
End Sub
 

Attachments

  • config_1.jpg
    config_1.jpg
    74.6 KB · Views: 130
Upvote 0

Cesar_Morisco

Active Member
message type declaration, same construction as in ESP32
B4A:
Sub Process_Globals
    Type Message (FUNCTION As String, Parameter1 As String, _
                    Parameter2 As String, _
                    Parameter3 As String, _
                    Parameter4 As String, _
                    Parameter5 As String, _
                    Parameter6 As String, _
                    Parameter7 As String, _
                    Parameter8 As String, _
                    Parameter9 As String, _
                    Parameter10 As String, _
                    Parameter11 As String, _
                    Parameter12 As String, _
                    Parameter13 As String, _
                    Parameter14 As String, _
                    Parameter15 As String, _
                    Parameter16 As String)
Private Ser As B4RSerializator
End Sub

upload config parameters to ESP
B4A:
Private Sub btnUpload_Click
    Dim Msg As Message = CreateEmptyMessage
    Msg.Function = FUNCTION_SET_DEVICE_CONFIG
    Msg.Parameter1 = txtDeviceName.Text
    Msg.Parameter2 = txtServerIP.text
    Msg.Parameter3 = txtPort.text
    Msg.Parameter4 = St.BooleanToStr(swtchDHCP.Value)
    Msg.Parameter5 = txtDeviceIP.text
    Msg.Parameter6 = txtDeviceGateway.text
    Msg.Parameter7 = txtDeviceSubnetMask.Text
    Msg.Parameter8 = txtNetworkSSID.text
    Msg.Parameter9 = txtNetworkPassword.text
    Msg.Parameter10 = pmLCDcontrast.SelectedValue
    Msg.Parameter11 = pmLCDbacklight.SelectedValue
    SendMessage(Msg)
End Sub

initialize message object
B4A:
private Sub CreateEmptyMessage () As Message
    Dim Msg As Message
    Msg.Initialize
    Return Msg
End Sub

serialize object and send it
B4A:
private Sub SendMessage(Msg As Message)
    Try
        If WiFiCon.connected Then 'wificon is a service module
            CallSub2(WiFiCon, "SendData", Ser.ConvertArrayToBytes(MessageToObjects(Msg)))
            ToastMessageShow("Message Sent",False)
        End If
    Catch
        MsgboxAsync(LastException,"")
    End Try
End Sub

B4A:
Public Sub SendData (data() As Byte)
    If Connected Then
        Astream.Write(data)
    End If
End Sub
hello mostez all
well thank you for your help i am not so collied for your
I don't understand much about B4R and it's still giving some errors
I don't know if some libraries are missing
It's a nice code
Thanks a lot for your help too
 

Attachments

  • Sem título.png
    Sem título.png
    110.4 KB · Views: 116
Upvote 0

Cesar_Morisco

Active Member
it's not the full code, I just wanted to explain how a similar-project to your idea works, I'm so sorry I can't post the full code, however I will try to post a simple test
hello mostez all
well thank you
I've already saved it Example of code at the end of samema I'm going to analyze the code
Thank you from the heart
 
Upvote 0
Top