B4X to support (almost) everything

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4R - New development tool for Arduino. It was not yet released.

I wanted to demonstrate the usage of rMQTT and rWire libraries. Eventually I've also added an Android, iPad and Raspberry Pi to the solution.
Why? Because it is simple :)

Make sure to see it in HD mode:

The Arduino code is made of two projects:

Master - Uno with ethernet shield:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
   Private ServerIp() As Byte = Array As Byte(192, 168, 0, 6)
   Private eth As Ethernet
   Private ethSocket As EthernetSocket
   Private mqtt As MqttClient
   Private const SlaveAddress As Byte = 8
   Private master As WireMaster
   Private btnTimer As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   If eth.InitializeDHCP(MacAddress) = False Then
     Log("Failed to get IP address")
     Return
   End If
   mqtt.Initialize(ethSocket.Stream, ServerIp, 51042, "arduino1", "Mqtt_MessageArrived", _
     "Mqtt_Disconnected")
   btnTimer.Initialize("BtnTimer_Tick", 20)
   master.Initialize
   Connect (0)
   
End Sub

Sub Connect (Tag As Byte)
   Log("Trying to connect")
   If mqtt.Connect = False Then
     'try to connect again after a second
     CallSubPlus("Connect", 1000, 0)
     Return
   End If
   Log("Connected!")
   mqtt.Subscribe("arduino", 0)
   btnTimer.Enabled = True
End Sub

Sub Mqtt_Disconnected
   Log("disconnected")
   btnTimer.Enabled = False
   Connect(0)
   
End Sub

Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
   Log("Message arrived, ", Payload)
   master.WriteTo(SlaveAddress, Payload)
End Sub

Sub BtnTimer_Tick
   Dim b() As Byte = master.RequestFrom(SlaveAddress, 1)
   If b.Length > 0 And b(0) <> 0 Then
     'send the message (pressed button) to the android
     mqtt.Publish("mobile", b)
   End If
End Sub

Slave - Mega with LCD shield:

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private slave As WireSlave
   Private lcd As LiquidCrystal
   Private Timer1 As Timer
   Private const UP = 1, DOWN = 2, LEFT = 3, RIGHT = 4 As Byte
   Private pin0 As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   slave.Initialize(8, "Slave_NewData")
   lcd.Initialize(8, 255, 9, Array As Byte(4, 5, 6, 7))
   lcd.Begin(16, 2)
   lcd.Clear
   lcd.Write("Waiting for data")
   Timer1.Initialize("CheckForButtons_Tick", 20)
   Timer1.Enabled = True
   pin0.Initialize(0, pin0.MODE_INPUT)
End Sub

Sub Slave_NewData (Data() As Byte)
   Log("received: ", Data)
   lcd.Clear
   lcd.Write(Data)
End Sub

Sub CheckForButtons_Tick
   'Reading the button from the keypad shield
   Dim i As UInt = pin0.AnalogRead
   Dim btnValue As Byte = 0
   Select True
     Case i < 50
       btnValue = RIGHT
     Case i < 250
       btnValue = UP
     Case i < 450
       btnValue = DOWN
     Case i < 650
       btnValue = LEFT
   End Select
   'prepare it for the master reading.
   slave.SetDataForMaster(Array As Byte(btnValue))
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
I think it is time to get my arduino starter kit ready :)
I bought it a long time ago but never went beyond blinking led example :D
I have a UNO that was never connected! (Hope it works)
 

wonder

Expert
Licensed User
Longtime User
Fantastic Erel! I think the next big step (2018?) would be unifying everything in one IDE... :)
 

Beja

Expert
Licensed User
Longtime User
Great work and a new milestone in this wonderful set of tools.

Erel (Eventually I've also added an Android, iPad and Raspberry Pi to the solution.) does that mean B4A is integrated into B4R so we don't need to
upgrade B4A by itself again?
 
Top