B4R Question [SOLVED] Communication Arduino XBee Shield with PC XBee USB explorer not working

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

have setup a testenvironment with an Arduino XBee Shield and a PC with XBee USB explorer.
1) Connected the Arduino via USB to the PC (COM4), put the XBee Shield in USB Mode (jumper settings), uploaded the B4R program, put the XBee Shield in XBee Mode (jumper settings),

2) Connected the XBee USB explorer with the PC (COM5), compiled the B4J Program and run.

Nothing happens. In the XCTU program COM4 and COM5 are recognized.

Any hints what could be wrong in the setup or code?

B4R Code
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private AStream As AsyncStreams
  Private LED13 As Pin
End Sub

Private Sub AppStart
  Serial1.Initialize(9600)
  LED13.Initialize(13, LED13.MODE_OUTPUT)   
  AStream.Initialize(Serial1.Stream, "AStream_NewData", "AStream_Error")
End Sub

Sub AStream_NewData (Buffer() As Byte)
   Dim ledStatus As Boolean
   If Buffer(0) = 1 Then ledStatus = True Else ledStatus = False
   Log("LED13 Status:", ledStatus)
   LED13.DigitalWrite(ledStatus)
End Sub

Sub AStream_Error
   Log("AStream Error")
End Sub

B4J Code
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private serial As Serial
   Private astream As AsyncStreams
   Private ToggleButton1 As ToggleButton
End Sub

Sub AppStart (Form1 As Form, Args() As String)
  MainForm = Form1
  MainForm.SetFormStyle("UNIFIED")
  MainForm.RootPane.LoadLayout("Main")
  serial.Initialize("serial")
  serial.Open("COM5")
  astream.Initialize(serial.GetInputStream, serial.GetOutputStream, "Astream")
  MainForm.Show
End Sub

Sub ToggleButton1_SelectedChange(Selected As Boolean)
   Dim ledStatus As Byte
   If Selected Then ledStatus = 1 Else ledStatus = 0
   Log($"Send for LED 13 the Status ${ledStatus} to the Arduino."$)
   astream.Write(Array As Byte(ledStatus))
End Sub
 

rwblinn

Well-Known Member
Licensed User
Longtime User
One step further but not ok.
After reconfiguring the XBees = Arduino as Coordinator and the PC as Router communication is functioning between the Arduino (using a sketch) and B4J but not working if using a B4R program
FUNCTIONING
Arduino Sketch
B4X:
void setup() {
  Serial.begin(9600);    
  pinMode(13, OUTPUT);
}

void loop() {
  while(Serial.available()){
    char getData = Serial.read();
    if(getData == 'a'){  
      digitalWrite(13, HIGH);
    }else if(getData == 'b'){
      digitalWrite(13, LOW);
    }
  }
}
B4J Code
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private serial As Serial
    Private astream As AsyncStreams
    Private ToggleButton1 As ToggleButton
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
   serial.Initialize("serial")
   serial.Open("COM5")
   astream.Initialize(serial.GetInputStream, serial.GetOutputStream, "Astream")
   MainForm.Show
End Sub

Sub ToggleButton1_SelectedChange(Selected As Boolean)
    Dim cmd As String
    If Selected Then cmd = "a" Else cmd = "b"
    Dim bytes() As Byte = cmd.GetBytes("ASCII")
    Log("Send Command to Arduino: " & cmd)
    astream.Write(bytes)
End Sub

NOT FUNCTIONING
Nothing happens when toggling the B4J Button. The Arduino serial monitor is constanly updating with log information from async_newdata with character 68?
B4R
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private Astream As AsyncStreams
   Private LED13 As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(9600)
   Log("AppStart")
   LED13.Initialize(13, LED13.MODE_OUTPUT) 
   Astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
End Sub

Sub Astream_NewData (Buffer() As Byte)
    Log("NewData B(0): ", Buffer(0))
    Dim ledStatus As Boolean
    If Buffer(0) = 1 Then ledStatus = True Else ledStatus = False
    LED13.DigitalWrite(ledStatus)
End Sub

Sub Astream_Error
   Log("Astream Error occured.")
End Sub
B4J Code
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private serial As Serial
    Private astream As AsyncStreams
    Private ToggleButton1 As ToggleButton
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.SetFormStyle("UNIFIED")
   MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
   serial.Initialize("serial")
   serial.Open("COM5")
   astream.Initialize(serial.GetInputStream, serial.GetOutputStream, "Astream")
   MainForm.Show
End Sub

Sub ToggleButton1_SelectedChange(Selected As Boolean)
    Dim ledStatus As Byte
    If Selected Then ledStatus = 1 Else ledStatus = 0
    Log($"Send for LED 13 status ${ledStatus} to the Arduino."$)
    astream.Write(Array As Byte(ledStatus))
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Have you implemented AStream_NewData in the B4J program?
Answ: No
Is it raised?
Answ:
After implementing in the B4J program, like below it is NOT raised.
B4X:
Sub Astream_NewData (Buffer() As Byte)
  Log($"${DateTime.Time(DateTime.Now)}: ${BytesToString(Buffer, 0, Buffer.Length, "UTF8")}"$)
End Sub

Make sure that nothing on the PC side connects to the Arduino serial port.
Answ: No active applications using the Srduino serial port.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Implemented on B4R and indeed the LED is turned on.
To go for sure did again with DigitalWrite(False) and the LED remains off, then set to True again and uploaded ... LED is turned on.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Yes have tried the original code - but not working.
Tried again C code on the Arduino receiving a character from B4J application to turn the LED on / off. This is running fine - as mentioned in post #3

There something wrong with the B4R code in receiving data, but what ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You need to configure the serial port after you open it in the B4J program:
B4X:
serial.SetParams(9600, 8, 1, 0)

It will then work.

If you are using a shield similar to the one I'm using then you need to remove the two jumpers when you compile and then put them back in XBEE mode after you see the AppStart log message.

SS-2016-04-14_09.22.51.jpg


I've connected another Arduino to be able to see the logs. This is the code that I ran on the Mega board:

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private astream As AsyncStreams
   Private softserial As SoftwareSerial
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("appstart")
   softserial.Initialize(57600, 10, 11)
   astream.Initialize(softserial.Stream, "astream_newdata", Null)
End Sub


Sub astream_NewData (Buffer() As Byte)
   Log("rcv: ", Buffer)
End Sub

On the Uno with the XBee:
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private Astream As AsyncStreams
  Private LED13 As Pin
  Private softserial As SoftwareSerial
  Private Astream2 As AsyncStreams
  Private bc As ByteConverter
End Sub

Private Sub AppStart
   Serial1.Initialize(9600)
   Log("AppStart")
   LED13.Initialize(13, LED13.MODE_OUTPUT)
   Astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
   softserial.Initialize(57600, 10, 11)
   Astream2.Initialize(softserial.Stream, "astream2_newdata", Null)
   Astream2.Write("test".GetBytes)
End Sub

Sub Astream_NewData (Buffer() As Byte)
  Astream2.Write("data from B4J: ".GetBytes)
   Astream2.Write(bc.HexFromBytes(Buffer).GetBytes)
   LED13.DigitalWrite(Buffer(0) = 1)
End Sub

Sub Astream2_NewData (Buffer() As Byte)
   
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Good News = Its working : UNO LED turning ON/OFF, MEGA Displaying the log, B4J controlling the UNO LED.

Will write this up as a next B4RHowTo which I am building (See here > select B4X > scroll to B4R).

Many THANKS for your help - Iam planning to use XBee Communication for certain sensors for Home Automation (send data to Domoticz & openHAB).
 
Upvote 0
Top