B4R Question Wifi connection WeMos-Phone with B4a

D

Deleted member 103

Guest
Hi,

if the wireless connection between Wemos and phone is built manually, works with the previous code does the data transfer between the Wemos and Phone.

Now I want the the wifi connection, between Wemos and phone is taken by B4a.
I've tried a few Library, unfortunately without very good success.

The connection is not as stable and does not always work.
Is there somewhere an example that works well?
 
D

Deleted member 103

Guest
A socket-connection between WeMos and phone will only work if the phone in the same Netwerk as the Wemos.
The phone must connect to the Wemos via WiFi.
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

you have to excuse me, I can not so well explained in English.

Here the code I use.
Main-Code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Private pEvent As PhoneEvents
    Public wifiManager As WiFiConnect
End Sub

Sub Globals

    Private edtIp As EditText
    Private btnConnect As Button
    Private lblReading As Label
    Private lblConnectedState As Label
    Private lblLedRed As Label
    Private lblLedYellow As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
   
    wifiManager.Initialize
    pEvent.Initialize("PhoneEvent")

    edtIp.Text = "192.168.4.1"
    lblLedYellow.Color = Colors.Gray
    lblLedRed.Color = Colors.Gray
End Sub

Public Sub StateChanged
    If Starter.connected Then
        lblConnectedState.Color = Colors.Green
    Else
        lblConnectedState.Color = Colors.Gray
    End If
End Sub

Sub Activity_Resume
    StateChanged
End Sub

Sub btnConnect_Click
    If wifiManager.getWifiState <> wifiManager.WIFI_STATE_ENABLED Then
        wifiManager.setWifiEnabled(True)
    End If

    If wifiManager.IsWiFiEnabled Then
        Dim strWifi As String = wifiManager.getSSID
        Log("wifiManager.getSSID = " & strWifi)
        Log("wifiManager.getIpAddress = " & wifiManager.getIpAddress)
        'If strWifi.EqualsIgnoreCase("myWifi") = False Then
        If Not(wifiManager.getIpAddress.StartsWith("192.168.4.")) Then
            'wifiManager.DisconnectCurrentSSID
            'wifiManager.connectToSSID(wifiManager.WIFI_WPA, "myWifi", "12345678")
            wifiManager.SwitchSSID("myWifi", wifiManager.WIFI_WPA, "12345678")
        Else
            CallSub2(Starter, "Connect", edtIp.Text)
        End If
    End If

End Sub

Private Sub PhoneEvent_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
    Log("ConnectivityChanged: " & NetworkType & ", state = " & State)
    If State="CONNECTED" Then

        If NetworkType = "WIFI" Then

            If wifiManager.IsWiFiEnabled Then
                Dim strWifi As String = wifiManager.getSSID

                'If strWifi.EqualsIgnoreCase("myWifi") = False Then
                If Not(wifiManager.getIpAddress.StartsWith("192.168.4.")) Then
                    'wifiManager.DisconnectCurrentSSID
                    'wifiManager.connectToSSID(wifiManager.WIFI_WPA, "myWifi", "12345678")               
                    wifiManager.SwitchSSID("myWifi", wifiManager.WIFI_WPA, "12345678")
                Else
                    Log("WiFiConnect")
                    CallSub2(Starter, "Connect", edtIp.Text)
                End If
            End If

        End If
    End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Starter.socket.Close
End Sub

Starter-Code:
B4X:
Sub Process_Globals
    Public astream As AsyncStreams
    Public socket As Socket
    Public connected As Boolean
    Private bc As ByteConverter
    Private watchdog As Timer
    Private confirmation As Boolean
    Private Port(2) As Int
    Private connection As Int
    Private WeMosIp As String
End Sub

Sub Service_Create
    bc.LittleEndian = True
   
    connection = 0
    Port(0) = 51041
    Port(1) = 51042
   
    confirmation = False
    watchdog.Initialize("watchdog", 1000)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Public Sub Connect(ip As String)
    WeMosIp = ip
    If astream.IsInitialized Then
        astream.Close
    End If
    ConnectToWeMos
End Sub

Private Sub ConnectToWeMos
    socket.Initialize("socket")
    socket.Connect(WeMosIp, Port(connection), 30000)
End Sub

Private Sub Socket_Connected (Successful As Boolean)
    Log("Successful=" & Successful)
    If Successful Then
        connected = True
        astream.Initialize(socket.InputStream, socket.OutputStream, "astream")
        CallSub(Main, "StateChanged")
        watchdog.Enabled = True
    End If
End Sub

Private Sub watchdog_Tick
    If confirmation Then
        watchdog.Enabled = False
    Else
        connection = connection + 1
        If connection > 1 Then connection = 0
        ConnectToWeMos
    End If
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    Dim key As String = bc.StringFromBytes(Buffer, "UTF8")
    Log("Astream_NewData= " & key)
   
    If key = "OK" Then
        confirmation = True   
    End If
   
End Sub

Private Sub Astream_Error
    Log("Disconnected")
    connected = False
    CallSub(Main, "StateChanged")
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm sorry Filippo but I don't understand the question.

In the first post you wrote that there were stability issued. In you second post you wrote that the problem is that the board and Android device need to be in the same network (which is expected if you haven't configured the network properly).

What exactly is the problem?
 
Upvote 0
D

Deleted member 103

Guest
I'm sorry Filippo but I don't understand the question.
I'm sorry Erel.:(
Maybe I need to ask a question differently.

I want to know how to do the following from B4a:
1. Enable Wifi.
2. Connect phone with Wemos over Wifi.

Which Library you can recommend?
 
Upvote 0
D

Deleted member 103

Guest
The code you posted looks correct. Which error did you encounter?
That's the problem!
It's like a switch, goes, do not go.

For example, I make this check
B4X:
If Not(wifiManager.getIpAddress.StartsWith("192.168.4.")) Then
because this test does not work, it always outputs true.
B4X:
If strWifi.EqualsIgnoreCase("myWifi") = False Then
although the log shows: wifiManager.getSSID = Fritz-Box
B4X:
Log("wifiManager.getSSID = " & strWifi)
wifi.JPG
 
Upvote 0
D

Deleted member 103

Guest
Hi Erel,

I came of thing a bit closer.

These lines of code does not work. I think the password is not accepted.
B4X:
wifiManager.connectToSSID(wifiManager.WIFI_WPA, "myWifi", "12345678")
In this mobile phone setting I can not connect.
wifi_02.png


Here are the logs:
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 192.168.178.29
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 0.0.0.0
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 0.0.0.0
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 0.0.0.0
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
btnConnect_Click
wifiManager.getSSID = "myWifi"
wifiManager.getIpAddress = 0.0.0.0
btnConnect_Click
wifiManager.getSSID = "myWifi"
wifiManager.getIpAddress = 0.0.0.0
** Service (smhomeclock) Start **

In this mobile phone setting the connection works.
I once entered the password manually and then change the connection.
wifi_01.png


Here are the logs:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 192.168.178.29
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 192.168.4.2
btnConnect_Click
wifiManager.getSSID = "FRITZ-Box"
wifiManager.getIpAddress = 192.168.4.2
** Service (smhomeclock) Start **
** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
btnConnect_Click
wifiManager.getSSID = "myWifi"
wifiManager.getIpAddress = 192.168.4.2
Successful=true
Astream_NewData= OK

And this is my B4a-Code:
B4X:
Sub btnConnect_Click
    Log("btnConnect_Click")
    If wifiManager.getWifiState <> wifiManager.WIFI_STATE_ENABLED Then
        wifiManager.setWifiEnabled(True)
    End If

    If wifiManager.IsWiFiEnabled Then
        Dim strWifi As String = wifiManager.getSSID
        Log("wifiManager.getSSID = " & strWifi)
        Log("wifiManager.getIpAddress = " & wifiManager.getIpAddress)
        If strWifi.EqualsIgnoreCase($""myWifi""$) = False Or wifiManager.getIpAddress = "0.0.0.0" Then
        'If Not(wifiManager.getIpAddress.StartsWith("192.168.4.")) Then
            wifiManager.DisconnectCurrentSSID
            wifiManager.connectToSSID(wifiManager.WIFI_WPA, "myWifi", "12345678")
            'wifiManager.SwitchSSID("myWifi", wifiManager.WIFI_WPA, "12345678")
        Else
            CallSub2(Starter, "Connect", edtIp.Text)
        End If
    End If

End Sub
 
Upvote 0
Top