B4R Question Arduino Nano or MKR 1010 Bluetooth or WiFi Communication to B4A App or B4J App

Guardian17

Active Member
Licensed User
Longtime User
I tried this on the B4A Forum, with no responses. Thought I'd move it here for any Arduino Users in case it was missed over there.

Has anyone made any B4A/B4J application that can receive data and/or talk to one of the more recent Arduino boards, like the Arduino Nano 33 IoT, or MKR WiFi 1010, that use the u-blox NINA-W10? These Arduino boards use the WiFiNINA library for Bluetooth and WiFi.

I'm interested in the Arduino taking garden watering measurements, storing them in an EEPROM-type device, and then transmitting/dumping the memory data upon a request from the B4A App on my smartphone, or to my Windows laptop. I'm already working on the Arduino application to control the garden watering and taking/storing sensor data, but the communications via WiFi or Bluetooth to my smartphone still eludes me.

And I'm interested in talking directly via Bluetooth or WiFi, not via the IoT Cloud.
 
Last edited:

KiloBravo

Active Member
Licensed User
What about this ?

You should be able to port it to something else.
 
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
I tried this on the B4A Forum, with no responses. Thought I'd move it here for any Arduino Users in case it was missed over there.

Has anyone made any B4A application that can receive data and/or talk to one of the more recent Arduino boards, like the Arduino Nano 33 IoT, or MKR WiFi 1010, that use the u-blox NINA-W10? These Arduino boards use the WiFiNINA library for Bluetooth and WiFi.

I'm interested in the Arduino taking garden watering measurements, storing them in an EEPROM-type device, and then transmitting/dumping the memory data upon a request from the B4A App on my smartphone. I'm already working on the Arduino application to control the garden watering and taking/storing sensor data, but the communications via WiFi or Bluetooth to my smartphone still eludes me. If the Arduino can talk to my Windows laptop through Bluetooth or WiFi, that would also be acceptable.

And I'm interested in talking directly via Bluetooth or WiFi, not via the IoT Cloud.
I haven't used them.. those modules probably will work. This days they are developing hardware to talk to phones.

you say "..still elude me."
What is the problem that you have between your hardware and phone in your current system?

In my jarden to see data and control pumps I have a website using the ESP8266 (nothing more)
I'm having issues with the soil sensors - could i ask which one are you using?
 
Last edited:
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
I haven't used them.. those modules probably will work. This days they are developing hardware to talk to phones.

you say "..still elude me."
What is the problem that you have between your hardware and phone in your current system?

In my jarden to see data and control pumps I have a website using the ESP8266 (nothing more)
I'm having issues with the soil sensors - could i ask which one are you using?
My garden project is still getting off the ground, but I believe capacitive sensors are better than resistive sensors. The moisture sensor I am planning to use is made by STEMMA. Here is a link:
Although I have verified that I can obtain readings over i2c, I have not yet placed anything in my garden to see how well they work ... this will be my first year to implement anything like this. If you have tried these STEMMA sensors and have any feedback, please let me know.

In regards to the "...still eludes me", I have only seen examples where one device allows turning an LED ON that's on another device. I am more interested in seeing examples of actual byte/data transfer over Bluetooth or WiFi. My anticipated project has a 32Kx8 FerroRAM i2c device, (which will save data on moisture readings, temperature, control solenoid on/off times (gravity-feed watering), and tank level sensor (time-of-flight sensor)), and I want to be able to dump the memory from time to time to analyze whether any improvements are needed in my watering cycles. Yes, I could arrange to hook up to the USB port to do the memory dump via the Serial Monitor, but it would be less arduous if I can access the data wirelessly.
 
Last edited:
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
What about this ?

You should be able to port it to something else.
Thanks for the feedback. I had seen several of these posts (as well as YouTube videos), but I have yet to look into them extensively, and I think these may be outdated (May of 2016) compared to the Nano 33 IoT and/or MKR WiFi 1010, which use the u-blox NINA-W10 module and the WiFiNINA library for Bluetooth and WiFi. I am also interested in actual byte/data transfer between the Arduino board and my smartphone or laptop, not just turning an LED on/off.
 
Last edited:
Upvote 0

KiloBravo

Active Member
Licensed User
I have code taken from the forum examples that can send data back and forth from an ESP32 or ESP8266 to a PC or Phone. I usually use B4R code and an ESP board to send and receive data to my PC running a B4J program. Two examples I have gotten to work are 1.) use the WIFI network in your home or 2.) use the ESP as a WIFI Server connect your PC to that "server" and then receive the data to the B4j program. Erel's MQTT example also works very well.

Here is link to one of Erel's Projects that works ...

I do not know anything about those boards, but an esp8266 or esp32 can easily be used for I2c comms and TCP/UDP.

I can post a short example of a B4R program and a B4j program that can send and recevieve data in both directions. The B4R program reads a WII Nunchuck over I2c and sends the button and joystick position to the B4J program. The B4J program then changes the data and sends it back. If that would help let me know and I will post the code.
 
Upvote 0

KiloBravo

Active Member
Licensed User
Yesterday I wrote some code for a B4J app to talk back and forth over UDP to an ESP32.
The ESP32 module reads the two positions and the button from a KY-023 Joystick.

Peter Simpson wrote this post on it and I borrowed his code.

B4J is running on Win10 PC it is the Client. ESP32 is the Server. (wifi.StartAccessPoint("esp32_Base")
Fire up B4R and connect to the ESP32 module over USB. <<< this is just to see the messages in the log. It can run standalone from a USB power block.
On your PC disconnect from your router and connect to esp32_Base.
Fire up the B4J Program and then send data back and forth.
 
Upvote 0

KiloBravo

Active Member
Licensed User
B4J_B4R_UDP.gif
 
Upvote 1

KiloBravo

Active Member
Licensed User
B4J code. Just a quick hack cobbled together from examples on the forum.
It should be relatively easy to follow the excellent tutorials on (B4X Pages) making it a B4X cross platform app for B4J, B4A, and B4I.

B4J UDP App:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    '''
    Private usocket As UDPSocket
    Private bc As ByteConverter
    Private serz As B4RSerializator
    Private TextArea1 As B4XView

    Private IPcon As String = "222.111.7.10"
    Private portCon As Int = 55777
    Private Button2 As B4XView

    Private TextArea3 As B4XView
    Private Label1 As B4XView
    Private Label2 As B4XView

    Private Button3 As B4XView
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    usocket.Initialize2("UDP", portCon, 1024, True, False) 'reuse is set to true
    serz.Initialize
End Sub

Sub Button1_Click
'   Send Data To ESP32 Button
    TextArea3.SelectAll
    If TextArea3.SelectionLength = 0 Then
        TextArea3.Text = "Nothing To Send"
    Else
        build_Packet ' Build and Send the Packet over UDP
    End If
End Sub

Sub Button2_Click
'   Clear Recieve Data Button
    TextArea1.Text = ""
End Sub


Private Sub Button3_Click
'   Clear Sent Data Button
    TextArea3.Text = ""
End Sub

Sub build_Packet
'
    Dim mySTR() As String = Regex.Split(" ",TextArea3.Text)
    Dim send_data () As Object = mySTR
    UDP_SendPacket(serz.ConvertArrayToBytes(send_data))
    TextArea3.Text = ""
    TextArea3.Text = "Data Was Sent"
'
'   Old Code just sends a predefined array of data
'
'    Dim pktStart As Object = "<"
'    Dim x As Object = Rnd(0,255)
'    Dim y As Object = Rnd(0,255)
'    Dim some_string As Object = "string"
'    Dim pktEnd As Object = ">"
'    Dim send_data(5) As Object
'    send_data(0) = pktStart
'    send_data(1) = x
'    send_data(2) = y
'    send_data(3) = some_string
'    send_data(4) = pktEnd
'    UDP_SendPacket(serz.ConvertArrayToBytes(send_data))
'    TextArea3.Text = ""
'    TextArea3.Text = "Data Was Sent"
End Sub

Sub UDP_SendPacket (xmit_data() As Byte)
    Dim Packet As UDPPacket
    Packet.Initialize(xmit_data, IPcon, portCon)
    usocket.Send(Packet)
    Log("Packet Sent")
    Log($"IPcon: ${IPcon}"$)
    Log($"portCon: ${portCon}"$)
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
    Log($"Packet.Length: ${Packet.Length}"$)
    Log($"Packet.Data.Length: ${Packet.Data.Length}"$)
    Log("Packet Arrived")
    Log($"Packet IP:  ${Packet.HostAddress}"$)
    Log($"Packet Port:  ${Packet.Port}"$)
    Dim data() As Byte = Packet.Data
    Log($"Packet.Data ${Packet.Data}"$)
    If Packet.Data.Length > Packet.Length Then
        Dim data(Packet.Length) As Byte
        bc.ArrayCopy(Packet.Data, 0, data, 0, Packet.Length)
    End If
    Dim rcvd_data() As Object = serz.ConvertBytesToArray(data)  '''     Private serz As B4RSerializator
    TextArea1.Text = ""
    For Each o As Object In rcvd_data
        Log($"${o}"$)
        TextArea1.Text = TextArea1.Text & o & CRLF
    Next
End Sub
 
Upvote 0

KiloBravo

Active Member
Licensed User
B4R ESP32 Server Code for UDP xmit & receive.

B4R UDP Server App:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private esp As ESP32extras
    Private wifi As ESP8266WiFi
    Private udp As WiFiUDP
    Public bc As ByteConverter
    Public ser As B4RSerializator
    Private js_timer As Timer
    Private btn_timer As Timer
    Public cntr As Int = 0
'''''''''''''''''''''''''''''''''''''''''
    Public myIP As String = "222.111.7.10"   
    Public myGateway As String = "222.111.7.10"   
    Public mySubnet As String = "255.255.255.0"   
    Private Switch, VRx, VRy As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
'*****************************************
    esp.ConfigAP(myIP, myGateway, mySubnet)
    wifi.StartAccessPoint("esp32_Base")
    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)
'*********
    Switch.Initialize(33, Switch.MODE_INPUT_PULLUP) ' ESP32 Pin 33 (Joystick Button)
    VRx.Initialize(35, VRx.MODE_INPUT)                ' ESP32 Pin 35
    VRy.Initialize(34, VRy.MODE_INPUT)                ' ESP32 Pin 34
'
    js_timer.Initialize("ReadJoystick", 500)
    js_timer.Enabled = True 'don't forget to enable it
'
    btn_timer.Initialize("ReadButton", 500)
    btn_timer.Enabled = True 'don't forget to enable it   
'*********   
    Log("Socket Init and Listening")
    udp.Initialize(55777, "udp_PacketArrived")
End Sub

Sub ReadButton
    Dim B_State As Boolean
    B_State = Switch.DigitalRead
    If Not(B_State) Then            ' Button Pushed Down is Zero
    Dim xmit_Data() As Object = Array As Object (0,0,0)
    xmit_Data (0) = "ESP32"
    xmit_Data(1) =  "Test"
    xmit_Data(2) =  "Packet"
    Send_Packet(xmit_Data)   
    End If
End Sub

Sub ReadJoystick
    Dim X As Int = 0
    Dim Y As Int = 0
    Dim B_State As Boolean
    X = VRx.AnalogRead
    Y = VRy.AnalogRead
    B_State = Switch.DigitalRead
'    Log("X: ", X)
'    Log("Y: ", Y)
'    Log("B_State: ", B_State)
    Dim xmit_Data() As Object = Array As Object (0,0,0)
    xmit_Data(0) = B_State
    xmit_Data(1) = X
    xmit_Data(2) = Y
'**********************
'     Check X/Y Values High or Low to make sure Joystick has been moved
    If ((X < 1300) Or (X > 2400)) Then
        Send_Packet(xmit_Data)
    Else If ((Y < 1300) Or  (Y > 2400)) Then
        Send_Packet(xmit_Data)
    End If   
End Sub


Sub Udp_PacketArrived (Buffer() As Byte, Ip() As Byte, Port As UInt)
    Log("PacketArrived: ", Buffer.Length)
    Log("IP: ",Ip(0),".",Ip(1),".",Ip(2),".",Ip(3))
    Log("Port: ",Port)
    If Buffer.Length =  0 Then
        Log("Packet Length 0 ")
        Return 'invalid message
    End If
    If Buffer.Length > 50 Then
        Log("Packet Length > 50")
        Return 'invalid message
    End If
    '********************************************************************************
    Dim be(50) As Object 'used as a storage buffer. Can be a global variable
    Dim Data() As Object = ser.ConvertBytesToArray(Buffer, be)
    '********************************************************************************
    For Each o As Object In Data
    Log("Data: ", o)
    Next
    cntr = cntr + 1
    Log("Counter: ", cntr)
    Data(0) = cntr
End Sub

Sub Send_Packet(xmit_Data()As Object)
'**********************************************************************
'''' IP/Port of Client "222.111.7.100" --- "55777"'''''''''''''''''''''
'**********************************************************************
    Private ip() As Byte = Array As Byte(222,111,7,11)
    Private port As UInt = 55777
    udp.BeginPacket(ip, port)
    udp.Write(ser.ConvertArrayToBytes(xmit_Data))
    udp.SendPacket
    Log("SentPckt: ",xmit_Data(0)," ",xmit_Data(1)," ",xmit_Data(2))
End Sub
 
Upvote 0

KiloBravo

Active Member
Licensed User
I attached a zip of the B4J Project.
The B4R Bin was 33 meg, so I did not zip the B4R Project, but you have the source code and libraries I used.

B4R_Libs.PNG
 

Attachments

  • !B4J_UDP.zip
    115.8 KB · Views: 241
Upvote 0

KiloBravo

Active Member
Licensed User
As I said, this is just a quick hack but the code works and should get you started for whatever you want to do.
I have plenty of LOG statements in both programs, so hopefully the logic/code is easy to follow.

Erel and other's on the forum have posted plenty of great examples of various communication methods.
 
Upvote 0

KiloBravo

Active Member
Licensed User
If you have issues connecting from the PC it might be a firewall issue. You may have to open the ports for UDP comms.
WireShark is great for diagnosing this as well as IP/TCP/UDP and Bluetooth issues.

for I2C I found the port scanner handy.

I hacked the I2C on a WII Nunchuk with code written by Robert Eisele. The URL is listed in my source code in the post below.
 
Upvote 0

KiloBravo

Active Member
Licensed User
I sometimes use an old router (not on the internet) and connect both devices to that for communications.
You can set it up to only allow connections from predefined device MAC Addresses.
 
Upvote 0

Guardian17

Active Member
Licensed User
Longtime User
I sometimes use an old router (not on the internet) and connect both devices to that for communications.
You can set it up to only allow connections from predefined device MAC Addresses.
WOW! I just looked again at this thread and saw all of your posts!! Thank you for all of your responses!! I will probably get to looking at these over this weekend.

THANKS AGAIN!
 
Upvote 0

KiloBravo

Active Member
Licensed User
I am just trying to pay it forward a little. Lots of folks on the forum have been very generous with their time helping me out.

The video tutorials are great as well as the booklets.

I strongly suggest using B4X Pages. The learning curve is a little steeper in the beginning but making a project cross compatible between B4A, B4J, and B4I with 90% code reuse is definitely worth it. IMHO!

If you run into issues start a new post with specific details, any error messages, and a small code sample to reproduce the error.
(Don't forget to use code code tags.)

I will try and help out when and where I can.
 
Upvote 0
Top