B4R Question ESP8266 Boards

D

Deleted member 103

Guest
Hi,

I have maybe a stupid question, you can either connect to android phone or iOS phone without a server?
Phone = Server
Board = client
 
D

Deleted member 103

Guest
B4R-Server:
B4X:
#Region Project Attributes
   #AutoFlushLogs: True
   #CheckArrayBounds: True
   #StackBufferSize: 600
#End Region

Sub Process_Globals
   Public Serial1 As Serial
   Public WiFi As ESP8266WiFi
   Private bc As ByteConverter
   
   Private connection As Int
   Private server(2) As WiFiServerSocket
   Private astream(2) As AsyncStreams
   Private timer1 As Timer
   Private pinSensor, pinButton As Pin
   Private pinLED As Pin
   Private pin1 As D1Pins
End Sub

Private Sub AppStart
   Log("AppStart")
   Serial1.Initialize(115200)

   Log("StartAP: ", WiFi.StartAccessPoint2("myWifi", "12345678"))
   Log("My AP ip: ", WiFi.AccessPointIp)
     
   IntiServer
End Sub

#Region "server-Subs"
Public Sub IntiServer
   server(0).Initialize(51041, "Server_NewConnection")
   server(0).Listen

   server(1).Initialize(51042, "Server_NewConnection")
   server(1).Listen

   pinButton.Initialize(pin1.D3, pinButton.MODE_INPUT_PULLUP)
   pinButton.AddListener("pinButton_StateChanged")
   
   pinSensor.Initialize(pin1.D5, pinSensor.MODE_INPUT_PULLUP)
   pinSensor.AddListener("pinSensor_StateChanged")
   
   pinLED.Initialize(pin1.D1, pinLED.MODE_OUTPUT)
   
   timer1.Initialize("timer1_Tick", 1000)
   timer1.Enabled = True
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
   Log("new connection")
   astream(connection).Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
   astream(connection).Write(bc.StringToBytes("OK"))
   connection = connection + 1
   timer1.Enabled = False
End Sub

Sub Timer1_Tick
''   astream.Write(bc.UIntsToBytes(Array As UInt(pin.AnalogRead)))
'   If server.Socket.Connected Then
'     pinLED.DigitalWrite(True)
'     astream.Write("Time here is: ").Write(NumberFormat(Millis, 0, 0))
'   Else
     pinLED.DigitalWrite(Not(pinLED.DigitalRead))
'   End If
End Sub

Sub astream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
   If bc.StringFromBytes(Buffer) = 1 Then
     pinLED.DigitalWrite(True)
   Else
     pinLED.DigitalWrite(False)
   End If
End Sub

Sub astream_Error
   Log("Error.")
   For i = 0 To connection - 1
     server(i).Listen
   Next
   timer1.Enabled = True
End Sub
#End Region

Sub pinButton_StateChanged (State As Boolean)
   Log("pinButton: ", State)
   'state will be False when the button is clicked because of the PULLUP mode.
   If Not(State) Then
     SendValue("a")
     'astream.Write(bc.StringToBytes("0"))
   Else
     SendValue("b")
     'astream.Write(bc.StringToBytes("-0"))
   End If
End Sub

Sub pinSensor_StateChanged (State As Boolean)
   Log("pinSensor: ", State)
   'state will be False when the button is clicked because of the PULLUP mode.
   If Not(State) Then
     SendValue("c")
     'astream.Write(bc.StringToBytes("+"))
   Else
     SendValue("d")
     'astream.Write(bc.StringToBytes("-"))
   End If
End Sub

Sub SendValue(str As String)
   For i = 0 To connection - 1
     astream(i).Write(bc.StringToBytes(str))
   Next
End Sub

B4a-Main:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
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")

    edtIp.Text = "192.168.4.1"
   
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
    CallSub2(Starter, "Connect", edtIp.Text)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnLedOn_Click
    Starter.AStream.Write("1".GetBytes("UTF8")) 'Led ON
End Sub

Sub btnLedOFF_Click
    Starter.AStream.Write("0".GetBytes("UTF8")) 'Led OFF
End Sub

Sub WeMosLedRed(state As Boolean)
    If state Then
        lblLedRed.Color = Colors.Red
    Else
        lblLedRed.Color = Colors.Gray
    End If
End Sub

Sub WeMosLedYellow(state As Boolean)
    If state Then
        lblLedYellow.Color = Colors.Yellow
    Else
        lblLedYellow.Color = Colors.Gray
    End If
End Sub

B4a-Starter:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public astream As AsyncStreams
    Private 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
        ConnectToWeMos
    End If
End Sub

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

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

Private Sub Astream_Terminated
    Astream_Error
End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0
D

Deleted member 103

Guest
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?
 
Upvote 0
Top