B4R Tutorial Arduino + Raspberry Pi

Discussion in 'B4R Tutorials' started by Erel, Apr 14, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    In this tutorial we will create a currency converter solution that shows the conversion from USD to other currencies on the LCD panel. The user can click on the up and down buttons to switch to different currencies.

    This is a nice example as it shows how B4X tools can be used to program both the Arduino side and the Raspberry Pi side.

    Arduino and Raspberry Pi complete each other nicely. The Arduino is easier to connect to external hardware and the Raspberry Pi is a full and powerful computer.

    The two devices are connected with a USB cable using the Arduino built-in usb to serial converter.

    During development it is convenient to run the RPi side on the PC. Otherwise you need to disconnect the Arduino each time you want to compile and connect it to the PC.

    Once the program is ready we connect B4J to the RPi with B4J-Bridge and run it on the RPi instead of the PC.

    The B4J program will download and parse the currency data from the web service: https://openexchangerates.org

    The B4R program sends the value of the pressed button to the RPi which then sends back the currency name and currency value.

    Note that there is no Log calls in the B4R program as the log messages will be sent to the RPi and interfere with the other commands.

    The B4R program depends on rRandomAccessFile v1.10+ (v1.10 is attached).

    B4R code:
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private astream As AsyncStreams
       
    Private lcd As LiquidCrystal
       
    Private pin0 As Pin
       
    'These are the analog values that identify the LCD shield buttons.
       Private btnValues() As UInt = Array As UInt(50250450650850)
       
    Private const RIGHT = 0, UP = 1, DOWN = 2, LEFT = 3, SELECT_BTN = 4, NONE = 5 As Byte 'ignore
       Private lastValue As Byte = NONE
    End Sub

    Private Sub AppStart
       
    'don't use Log as it sends the data to the serial port
       Serial1.Initialize(115200)
       astream.Initialize(Serial1.Stream, 
    "astream_NewData""astream_Error")
       lcd.Initialize(
    82559Array As Byte (4567))
       lcd.Begin(
    162)
       lcd.Write(
    "waiting for data")
       pin0.Initialize(
    0, pin0.MODE_INPUT)
       
    'CheckButton will be called repeatedly as fast as possible.
       AddLooper("CheckButton")
       
    'Send the first request to the RPi.
       astream.Write(Array As Byte(0, SELECT_BTN))
    End Sub

    Sub CheckButton
       
    'reading the buttons of the LCD keypad shield
       Dim reading As UInt = pin0.AnalogRead
       
    For i = 0 To btnValues.Length - 1
         
    If reading < btnValues(i) Then Exit
       
    Next
       
    If i <> lastValue Then
         lastValue = i
         astream.Write(
    Array As Byte(0, i))
       
    End If
    End Sub


    Sub AStream_NewData (Buffer() As Byte)
       lcd.Clear
       
    If Buffer.Length <> 7 Then
         lcd.Write(
    "error")
         
    Return
       
    End If
       
    Dim raf As RandomAccessFile
       raf.Initialize(Buffer, 
    True)
       lcd.Write(raf.ReadBytes2(
    3, raf.CurrentPosition)) '3 letters currency
       lcd.SetCursor(01)
       lcd.Write(raf.ReadDouble32(raf.CurrentPosition))
    End Sub

    Sub AStream_Error
     
    End Sub
    The B4J program is attached.
     

    Attached Files:

    Last edited: Feb 12, 2018
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice