Android Question [things] Connect Arduino to Raspberry Pi running Android Things

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

followed this (EXITING) thread. Did setup an RPi and ran successfully first simple B4A test apps.

Next wanted to connect an Arduino to the RPi to send data in both ways using SoftwareSerial:
Via Serial Port: According development stage of Android Things, USB Serial Port is not yet supported.
Via SoftwareSerial: Looked at UART, which is already supported.

Question:
Are there plans to extend the B4A things library to support SoftwareSerial (via USB and/or 3-wire connection TX,RX,GND)? and use subsequently the B4XSerializer to exchange data both ways.

EDIT 20170123: Added [things] to the title.
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

thanks for the updated library.
Test Setup Wiring
Arduino UNO Pin 10 = RX to RPi 3 Pin 8 = UART0 TXD
Arduino UNO Pin 11 = TX to RPi 3 Pin 10 = UART0 RXD
Arduino UNO GND to RPi Pin 6 GND

  1. Tested software serial connection between an Arduino UNO and Arduino MEGA using B4R: OK
  2. Tested Arduino UNO to RPi 3 using B4R and B4A: No success, no data arriving (event DataAvailable).


Any further advice appreciated?

B4A Debug Log:
B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
UART Device: UART0
(ArrayList) [BCM12, BCM13, BCM16, BCM17, BCM18, BCM19, BCM20, BCM21, BCM22, BCM23, BCM24, BCM25, BCM26, BCM27, BCM4, BCM5, BCM6]
UART Initialized:true
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy **

The B4A and B4R code attached, but here the B4A code snippet showing UART setup:

B4X:
Sub Process_Globals
    Private pm As PeripheralManager
    Private uart As UartDevice
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
...
        uart.Initialize("UART0", "uartdevice", pm)
        uart.Baudrate = 57600
        uart.StopBits = 1
        uart.Parity = uart.PARITY_NONE
        uart.DataSize = 8
..
    End If
    Activity.LoadLayout("Main")
End Sub

Sub uartdevice_DataAvailable(Buffer() As Byte)
    Log("UART Data is available: "  & Buffer.Length)
    Dim bc As ByteConverter
    Log("UART=" & bc.StringFromBytes(Buffer, "UTF8"))
End Sub
 

Attachments

  • atserialtest.zip
    4.5 KB · Views: 252
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Addition to post #3

Performed another test by sending data from B4A using uart.write to B4R by timer (see code snippets below).
Result: The data send by B4A is received by B4R asyncstreams. So the wiring is OK.

Seems the issue is with the B4A event uart DataAvailable.

B4A Code Snippet
B4X:
Sub timer1_tick
    Dim state As Boolean = pin22.value
    Log("Timer Tick: " & state)
    pin22.value = Not(state)
    Dim b As Byte
    If state Then b = 1 Else b = 0
    uart.Write(Array As Byte(b))
End Sub

B4R Code Snippet
B4X:
Sub astream_NewData (Buffer() As Byte)
    Dim state As Boolean = Buffer(0) = 1
    Log("NewData: State=",state)
    led13.DigitalWrite(Not(state))
End Sub
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Great - Confirmed working.
Created a testproject to display the temperature & humidity in a line chart on the Raspberry Pi from a DHT11 sensor connected to an Arduino UNO.
Attached the B4A and B4R code.
Screenshot - the data is updated every second, max 20 entries, used this B4A chart class (thanks for providing).

EDIT 20170123: Fixed X-Axis datapoints showing correct time.

upload_2017-1-22_11-37-50.png
 

Attachments

  • 7-B4R-Serial-DHT11-20170123.zip
    9.4 KB · Views: 232
Last edited:
Upvote 0

Michael1968

Active Member
Licensed User
Longtime User
Hello Robert,

compiling your example I get this error:
------------------------------------------------------
B4A version: 6.50
Parsing code. (0.02s)
Compiling code. (0.06s)
Compiling layouts code. (0.01s)
Organizing libraries. (0.00s)
Generating R file. (0.09s)
Compiling generated Java code. Error
Cannot find: C:\Anywhere Software\Basic4android\libraries\androidthings.jar
--------------------------------------------------------------------------------
where can I find the missing jar file?

best regards
Michael
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Michael1968

Active Member
Licensed User
Longtime User
Hello Manfred,

I have running a Android Thing Image on the RPI3
running B4A apps works...but...
running B4A apps with the "Things" Lib will get the Error
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

the library was included in the first things library version (0.9) of this post, but missing in the subsequent updates.
Pls find the things library v1.01 incl androidthings.jar.

Looking forward to hear your experience - its fun to see B4A apps running on the RPi communicating to an Arduino with sensors.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks.

Pls note: updated the example in post #6, which fixes X-Axis datapoints showing correct the time (B4A) - did not update the screenshot though.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Working with sensors directly connected to the RPi3 is next on the todo list.
The Android Things peripheral drivers are in progress - look here.

... BUT need to understand how to use in B4A =would be good to have a library "thingsperipherals.jar" containing all available drivers to then (easy) use in B4A.
 
Upvote 0
Top