B4J Tutorial B4J Arduino Demo

Here's a really simple demonstration of connecting an Arduino Uno to an app written in B4J.

You will need to modify the code around line 25 to reflect the correct COM Port that your Arduino is connected to. You can test the Arduino code using the Arduino IDE Serial Monitor - by entering the commands "HIGH" or "LOW" (sans quotes) to cycle your Arduino LED on. Set the Serial Monitor to NEWLINE and 9600 Baud.

After you get a successfull test, you should close the Arduino IDE as it may try to maintain the connection with your Arduino - which ties up your COM port and will prevent the B4J app from connecting. It's also possible that without running the Arduino IDE Serial Monitor prior to using the B4J app you will have problems connecting to the Arduino and may have to specify the serial port connection properties in your B4J Project.

Update (02/13/2014):
I added the SetParams function to the AppStart Subroutine below - this will enable the connection from the Java App to the Arduino without requiring the initial connection by the Arduino IDE / Serial Monitor. Contents of the Zip File were updated to reflect the change also.

B4J Program:
B4X:
#Region  Project Attributes

    #MainFormWidth: 330
    #MainFormHeight: 200

#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private txtFromArduino As TextField
    Private btnLEDOn As Button
    Private btnLEDOff As Button
    Private sp As Serial
    Private astream As AsyncStreams
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    sp.Initialize("")
    MainForm.RootPane.LoadLayout("test1") 'Load the layout file.
    MainForm.Show
    MainForm.Title = "B4J Arduino Demo"
    ' SET TO YOUR COM PORT VALUE
    sp.Open("COM3")
    ' CONFIGURE PORT (ADDED 2/13/2014)  
    Dim BaudRate As Int = 9600
    Dim DataBits As Int = 8
    Dim StopBits As Int = 1
    Dim Parity As Int = 0
    sp.SetParams (BaudRate , DataBits , StopBits , Parity )
    astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
    txtFromArduino.Text = "Awaiting Selection"
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim s As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(s)
    If s = "0" Then
    txtFromArduino.Text = "Arduino Replied with a 0 (zero) LED=OFF"
    Else If s = "1" Then
    txtFromArduino.Text = "Arduino Replied with a 1 (one) LED=ON"
    End If
End Sub

Sub Send_To_Arduino(cmd As String)
    Dim bytes() As Byte = (cmd & CRLF).GetBytes("UTF8")
    astream.Write(bytes)
End Sub

Sub btnLEDOff_Action
    Send_To_Arduino("LOW")
End Sub
Sub btnLEDOn_Action
    Send_To_Arduino("HIGH")
End Sub

Sub MainForm_Closed
    sp.Close
    astream.Close
End Sub
Sub AStream_Error
    Log("Error: " & LastException)
    astream.Close
End Sub

Arduino Program:
B4X:
int LED = 13;                  // define pin 13 as led;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);          // set up Serial library at 9600 bps
  pinMode(LED,OUTPUT);          // define pin 13 as output (led);
  Serial.print("Awaiting Input... \n");  // display initial message in Serial Monitor;
}

String txtMsg = "";
char s;

void loop() {
    while (Serial.available() > 0) {
        s=(char)Serial.read();
    
        if (s == '\n') {
            if(txtMsg=="HIGH") {  digitalWrite(LED, HIGH);
              Serial.write("1");          
            }
            if(txtMsg=="LOW")  {  digitalWrite(LED, LOW);
              Serial.write("0");          
            }
        
            txtMsg = "";
        } else {
            txtMsg +=s;
        
        }
    }
}

There's a simple JavaFX Form included in the attached zip file that is used to send the instruction(s) to the Arduino and display the response received from the Arduino.

Here's a link to a short video of the program turning an LED on.

Gaver
 

Attachments

  • B4J_Arduino_V2.zip
    1.5 KB · Views: 982
Last edited:
Top