Android Question How to light on a led on usb port ?

GDO

Member
Licensed User
Hi,
I'ld like to light on/off a led on usb port (on android device).
My first thought was to use an arduino (chip not board) and an usb library but is there a simplier way to do that ?
Thank you for your help.
 

Filippo

Expert
Licensed User
Hello,
Could you tell me what is not working exactly ?

Hi @GDO ,

many thanks for the answer.

The problem lies with usbserial.Initialize2 (...), here the app crashes.

B4X:
            If usbserial.IsInitialized Then usbserial.Close
            'usbserial.Initialize("serial", device, -1)
            usbserial.Initialize2("serial", device, -1, "CDCSerialDevice") 'HIDSerialDevice, CDCSerialDevice, CH34xSerialDevice, CP2102SerialDevice, FTDISerialDevice, PL2303SerialDevice
            usbserial.BaudRate = 9600 '115200
            usbserial.DataBits = usbserial.DATA_BITS_8
 
Upvote 0

GDO

Member
Licensed User
Hi @GDO ,

many thanks for the answer.

The problem lies with usbserial.Initialize2 (...), here the app crashes.

B4X:
            If usbserial.IsInitialized Then usbserial.Close
            'usbserial.Initialize("serial", device, -1)
            usbserial.Initialize2("serial", device, -1, "CDCSerialDevice") 'HIDSerialDevice, CDCSerialDevice, CH34xSerialDevice, CP2102SerialDevice, FTDISerialDevice, PL2303SerialDevice
            usbserial.BaudRate = 9600 '115200
            usbserial.DataBits = usbserial.DATA_BITS_8

Is "my code" working in your program ?

How did you manage DEVICE (Initialize2("serial", DEVICE, -1, "CDCSerialDevice")) before initialize2 ?
 
Upvote 0

Filippo

Expert
Licensed User
Is "my code" working in your program ?

How did you manage DEVICE (Initialize2("serial", DEVICE, -1, "CDCSerialDevice")) before initialize2 ?

Starter-Service:
B4X:
Public Sub InitfelUsb
    felUsb.ConnectUsbButton
End Sub

Class-Modul:
B4X:
Private Sub GetUsbDevice As Int
   If usbmanager.GetDevices.Length > 0 Then
       Return 0
   Else
       Return -1
   End If
End Sub

Public Sub ConnectUsbButton
    Dim indexUsbDevice As Int = GetUsbDevice
    If indexUsbDevice = -1 Then
        'Log("No connected usb devices.")
        ToastMessageShow(Starter.Language.Value("USBNotConnected"), True)
        IsConnected = False
    Else
        Dim device As UsbDevice = usbmanager.GetDevices(indexUsbDevice)
        If usbmanager.HasPermission(device) = False Then
            'ToastMessageShow(Starter.Language.Value("USBAllowConnection"), True)
            usbmanager.RequestPermission(device)
            
            Dim timeout As Int
            Do While Not(usbmanager.HasPermission(device)) And timeout < 30
                Sleep(1000)
                timeout = timeout + 1
                'Log("Sleep(1000)" & timeout)
            Loop
            
            If usbmanager.HasPermission(device) Then
                'LogColor("ConnectUsbButton", Colors.Green)
                ConnectUsbButton
            End If
        Else
            'LogColor("usbserial.Initialize", Colors.red)
            If usbserial.IsInitialized Then usbserial.Close

            ' This line works with Arduino-nano
            'usbserial.Initialize("serial", device, -1)

            ' This line should work for DigisPark, but it will not work.
            usbserial.Initialize2("serial", device, -1, "CDCSerialDevice") 'HIDSerialDevice, CDCSerialDevice, CH34xSerialDevice, CP2102SerialDevice, FTDISerialDevice, PL2303SerialDevice

            usbserial.BaudRate = 9600 '115200
            usbserial.DataBits = usbserial.DATA_BITS_8

            usbserial.StartReading
            
            IsConnected = True
        End If
    End If
    
    CallSubDelayed(Main, "ShowUSBInfo")
End Sub
 
Upvote 0

GDO

Member
Licensed User
Starter-Service:
B4X:
Public Sub InitfelUsb
    felUsb.ConnectUsbButton
End Sub

Class-Modul:
B4X:
Private Sub GetUsbDevice As Int
   If usbmanager.GetDevices.Length > 0 Then
       Return 0
   Else
       Return -1
   End If
End Sub

Public Sub ConnectUsbButton
    Dim indexUsbDevice As Int = GetUsbDevice
    If indexUsbDevice = -1 Then
        'Log("No connected usb devices.")
        ToastMessageShow(Starter.Language.Value("USBNotConnected"), True)
        IsConnected = False
    Else
        Dim device As UsbDevice = usbmanager.GetDevices(indexUsbDevice)
        If usbmanager.HasPermission(device) = False Then
            'ToastMessageShow(Starter.Language.Value("USBAllowConnection"), True)
            usbmanager.RequestPermission(device)
          
            Dim timeout As Int
            Do While Not(usbmanager.HasPermission(device)) And timeout < 30
                Sleep(1000)
                timeout = timeout + 1
                'Log("Sleep(1000)" & timeout)
            Loop
          
            If usbmanager.HasPermission(device) Then
                'LogColor("ConnectUsbButton", Colors.Green)
                ConnectUsbButton
            End If
        Else
            'LogColor("usbserial.Initialize", Colors.red)
            If usbserial.IsInitialized Then usbserial.Close

            ' This line works with Arduino-nano
            'usbserial.Initialize("serial", device, -1)

            ' This line should work for DigisPark, but it will not work.
            usbserial.Initialize2("serial", device, -1, "CDCSerialDevice") 'HIDSerialDevice, CDCSerialDevice, CH34xSerialDevice, CP2102SerialDevice, FTDISerialDevice, PL2303SerialDevice

            usbserial.BaudRate = 9600 '115200
            usbserial.DataBits = usbserial.DATA_BITS_8

            usbserial.StartReading
          
            IsConnected = True
        End If
    End If
  
    CallSubDelayed(Main, "ShowUSBInfo")
End Sub


Could you please add information about device just before initialize2 ?

...

ToastMessageShow(device.VendorId & " - " & device.ProductId, True)

' This line should work for DigisPark, but it will not work.
usbserial.Initialize2("serial", device, -1, "CDCSerialDevice")
...
 
Upvote 0

Filippo

Expert
Licensed User
Could you please add information about device just before initialize2 ?
Yes, I will do this tonight.
 
Upvote 0

Filippo

Expert
Licensed User
So it does not want to work for me.
Here is the code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    Private usbserial As felUsbSerial
    Private manager As UsbManager
    Private bc As ByteConverter
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    If FirstTime Then
        manager.Initialize
    End If
End Sub

Sub btnConnect_Click
    ' Pas de USb ?
    If manager.GetDevices.Length = 0 Then
        ToastMessageShow("Please connect usb device and try again.", True)
    Else
        ' Pas autorisation USB
        Dim device As UsbDevice
        For i=0 To manager.GetDevices.Length-1
            device = manager.GetDevices(i)
            Log("device= " & device)
            ToastMessageShow(i & " : " & device.VendorId & " - " & device.ProductId, True)
            If device.VendorId = 5840 And device.ProductId = 1875 Then        ' Digispark 16d0=5840 et 0753=1875
                'If device.VendorId = 5840 And device.ProductId = 2174 Then        ' Digispark 16d0=5840 et 087E=2174
                If manager.HasPermission(device) = False Then
                    ToastMessageShow("Please allow connection and try again.", True)
                    manager.RequestPermission(device)
                Else
                    Try
                        usbserial.Initialize2("serial", device, -1, "CDCSerialDevice")
                        ToastMessageShow("UsbOk = True", True)
                    Catch
                        Log(LastException.Message)
                    End Try
                End If
            End If
            Exit
        Next
    End If

End Sub

Private Sub serial_DataAvailable (Buffer() As Byte)
    Log("New data: " & bc.HexFromBytes(Buffer))
    EditText1.Text = EditText1.Text & bc.HexFromBytes(Buffer) & CRLF
    EditText1.SelectionStart = EditText1.Text.Length
End Sub


Here are the logs:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
device= (UsbDevice) UsbDevice[mName=/dev/bus/usb/002/002,mVendorId=5840,mProductId=1875,mClass=255,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=null,mSerialNumber=null,mConfigurations=[
UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=50,mInterfaces=[
UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=0,mSubclass=0,mProtocol=0,mEndpoints=[]]]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
device= (UsbDevice) UsbDevice[mName=/dev/bus/usb/002/002,mVendorId=5840,mProductId=1875,mClass=255,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=null,mSerialNumber=null,mConfigurations=[
UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=50,mInterfaces=[
UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=0,mSubclass=0,mProtocol=0,mEndpoints=[]]]
java.lang.ArrayIndexOutOfBoundsException: length=1; index=-1
 
Upvote 0

Filippo

Expert
Licensed User
There are some GPIO ON HC-05. Tou can change their status with a simple AT command. And switch on/off a led very simply. No need for extra hardware
Thanks @tigrot, I know that there are many others, but I need the DigisPark because it is just so small.
 
Upvote 0
Top