B4R Question From BLE to WiFi connection

Filippo

Expert
Licensed User
Longtime User
Hi,

I want to send the hotspot data from an Android phone to a “XIAO ESP32-C3” board via a BLE connection.
The hotspot is set up by Android using this B4a code.
The XIAO ESP32-C3 should then establish a WiFi connection to the Android phone, and from that moment on, data should only be exchanged via WiFi.

Question: is this even possible?

I have been using this B4a code for B4a and this code below for B4r.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private client As WiFiSocket
    Private astream As AsyncStreams
    Private timer1 As Timer
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 30) 'PC
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 29)  'Samsung-S4
    Private serverIp() As Byte = Array As Byte(192,168,178,96)  'Nokia G20
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'ScanNetworks
    If wifi.Connect2("ssid","password") Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
    Else
        Log("Failed to connect.")
        Return
    End If
    timer1.Initialize("timer1_Tick", 1000)
    timer1.Enabled = True
    Connect(0)
End Sub

Sub Timer1_Tick
   If client.Connected Then
     astream.Write("Time here is: ").Write(NumberFormat(Millis, 0, 0))
   End If
End Sub

Private Sub Connect(u As Byte)
   If client.ConnectIP(serverIp, 51042) Then
     Log("Connected to server.")
     astream.Initialize(client.Stream, "astream_NewData", "astream_Error")
   Else
     Log("Failed to connect to server")
     CallSubPlus("Connect", 1000, 0)
   End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
End Sub

Sub AStream_Error
   Log("Error")
   CallSubPlus("Connect", 1000, 0)
End Sub

Private Sub ScanNetworks 'ignore
   Dim numberOfNetworks As Byte = wifi.Scan
   Log("Found: ", numberOfNetworks, " networks.")
   For i = 0 To numberOfNetworks - 1
     Log(wifi.ScannedSSID(i))
   Next
End Sub
So far, I haven't even been able to connect to Hotpost.
The error message is always: Failed to connect.

Thank you very much.
Filippo
 

hatzisn

Expert
Licensed User
Longtime User
Hi,

I want to send the hotspot data from an Android phone to a “XIAO ESP32-C3” board via a BLE connection.
The hotspot is set up by Android using this B4a code.
The XIAO ESP32-C3 should then establish a WiFi connection to the Android phone, and from that moment on, data should only be exchanged via WiFi.

Question: is this even possible?

I have been using this B4a code for B4a and this code below for B4r.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private client As WiFiSocket
    Private astream As AsyncStreams
    Private timer1 As Timer
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 30) 'PC
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 29)  'Samsung-S4
    Private serverIp() As Byte = Array As Byte(192,168,178,96)  'Nokia G20
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'ScanNetworks
    If wifi.Connect2("ssid","password") Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
    Else
        Log("Failed to connect.")
        Return
    End If
    timer1.Initialize("timer1_Tick", 1000)
    timer1.Enabled = True
    Connect(0)
End Sub

Sub Timer1_Tick
   If client.Connected Then
     astream.Write("Time here is: ").Write(NumberFormat(Millis, 0, 0))
   End If
End Sub

Private Sub Connect(u As Byte)
   If client.ConnectIP(serverIp, 51042) Then
     Log("Connected to server.")
     astream.Initialize(client.Stream, "astream_NewData", "astream_Error")
   Else
     Log("Failed to connect to server")
     CallSubPlus("Connect", 1000, 0)
   End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
End Sub

Sub AStream_Error
   Log("Error")
   CallSubPlus("Connect", 1000, 0)
End Sub

Private Sub ScanNetworks 'ignore
   Dim numberOfNetworks As Byte = wifi.Scan
   Log("Found: ", numberOfNetworks, " networks.")
   For i = 0 To numberOfNetworks - 1
     Log(wifi.ScannedSSID(i))
   Next
End Sub
So far, I haven't even been able to connect to Hotpost.
The error message is always: Failed to connect.

Thank you very much.
Filippo

I do not have a lot of experience with the ESP32 as until recently I was using ESP8266 in my projects but I saw a video from DroneBotWorkshop channel in YouTube who said that using the latest BoardManager (v3.0+) for ESP32 it is possible to use both bluetooth and WiFi. All you have to do is check the video and find some examples.

 
Last edited:
Upvote 0

pixet

Active Member
Licensed User
Longtime User
Using WiFi and BLE together on the same device (speaking of AP) is possible, but it's not recommended. The WiFi receiver could be saturated and become unresponsive or even break. If not in AP mode, it's easier because you can control the various radio transmitters so as not to saturate the receiver (this also applies to BT/BLE).

But an easier alternative is to use the ESP32 in AP mode to connect from Android. You configure it as you like, and then restart it with the new settings.

Obviously, as soon as you reset it, it will use the new settings.

For convenience, you could add a very small (not visible) button to a pin (with a pullup resistor). This button is pressed within a short initial time and for a certain period of time. If pressed within the predefined time, it erases the WiFi configuration and restarts it in AP mode upon reboot.

Be careful with the pin you use. Some pins, if found in certain conditions at boot, put the device in programming mode or in preset situations.

Unfortunately, I'm out of the office right now and can't give you 100% tested code to try, but I'll try to post it on the forum as soon as I can.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Hi,

I want to send the hotspot data from an Android phone to a “XIAO ESP32-C3” board via a BLE connection.
The hotspot is set up by Android using this B4a code.
The XIAO ESP32-C3 should then establish a WiFi connection to the Android phone, and from that moment on, data should only be exchanged via WiFi.

Question: is this even possible?

I have been using this B4a code for B4a and this code below for B4r.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private client As WiFiSocket
    Private astream As AsyncStreams
    Private timer1 As Timer
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 30) 'PC
    'Private serverIp() As Byte = Array As Byte(192, 168, 178, 29)  'Samsung-S4
    Private serverIp() As Byte = Array As Byte(192,168,178,96)  'Nokia G20
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'ScanNetworks
    If wifi.Connect2("ssid","password") Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
    Else
        Log("Failed to connect.")
        Return
    End If
    timer1.Initialize("timer1_Tick", 1000)
    timer1.Enabled = True
    Connect(0)
End Sub

Sub Timer1_Tick
   If client.Connected Then
     astream.Write("Time here is: ").Write(NumberFormat(Millis, 0, 0))
   End If
End Sub

Private Sub Connect(u As Byte)
   If client.ConnectIP(serverIp, 51042) Then
     Log("Connected to server.")
     astream.Initialize(client.Stream, "astream_NewData", "astream_Error")
   Else
     Log("Failed to connect to server")
     CallSubPlus("Connect", 1000, 0)
   End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Log("Received: ", Buffer)
End Sub

Sub AStream_Error
   Log("Error")
   CallSubPlus("Connect", 1000, 0)
End Sub

Private Sub ScanNetworks 'ignore
   Dim numberOfNetworks As Byte = wifi.Scan
   Log("Found: ", numberOfNetworks, " networks.")
   For i = 0 To numberOfNetworks - 1
     Log(wifi.ScannedSSID(i))
   Next
End Sub
So far, I haven't even been able to connect to Hotpost.
The error message is always: Failed to connect.

Thank you very much.
Filippo

What you can do also, is write a JSON with connection information in the EEPROM with a byte (f.e. in position 0) set to 1 if there is connection data and 0 if there is not. When you will receive the connection data JSON you write it to eeprom with the length of it also (f.e. as a second byte). When you will write the JSON you reboot and in AppStart it checks if the byte in position 0 is 1 and then reads the second byte with the length of the JSON and you read it in a string and parse it. When it is read you connect directly to wifi and do not use the blue tooth connection. It is important that there is a reset button which writes in eeprom in byte position 0 the value 0 and reboots in order to start again using the bluetooth connection if needed.
 
Upvote 0
Top