Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Android2Arduino

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Sun Jul 21, 2013 9:32 am    Post subject: Android2Arduino Reply with quote

Connecting an Android to an Arduino Mega and exchanging information through the USB.
The Arduino Mega should be fitted with an Atmega8u2 or Atmega16u2, FTDI chips will not work.

This does the trick, a OTG-cable


A small program for the Android. Written in Basic4Android.
You need two extra libraries, RandomAccessFile (I have used version 1.50) and USBSerial (I have used version 1.00).

Here a screen shot of the Android program. 4 buttons and when you have a connection pushing one of the 4 buttons will return a reaction from the Arduino.



Here the hardware setup



Basic4Android code:

Code:
'Android2Arduino Small example made by Ben Zijlstra - 2013

'You need the library: RandomAccessFile. I have used version 1.50 and USBSerial. I used version 1.00

Sub Process_Globals
        Dim usb As UsbSerial
        Dim astreams As AsyncStreams
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
        Activity.LoadLayout("1")
End Sub

Sub btnOpen_Click
        If usb.Open(9600) Then
                ToastMessageShow("Connected succesfully",False)
                astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
        Else
                ToastMessageShow("Error opening USB port",False)
        End If
End Sub

Sub Astreams_Newdata(buffer() As Byte)
        ToastMessageShow(BytesToString(buffer,0,buffer.Length,"UTF8") , False)
End Sub


Sub btnClose_Click
        astreams.Close
End Sub

Sub btnSend0_Click
        astreams.Write("1".GetBytes("UTF8"))
End Sub

Sub btnSend1_Click
        astreams.Write("2".GetBytes("UTF8"))
End Sub

Sub btnSend2_Click
        astreams.Write("3".GetBytes("UTF8"))
End Sub

Sub btnSend3_Click
        astreams.Write("4".GetBytes("UTF8"))
End Sub

Sub AStreams_Error
        astreams.Close
End Sub
Sub Astreams_Terminated
        ToastMessageShow("Terminated",False)
        astreams.Close
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 


And the Bascom-AVR-code:

Code:
'Android2Arduino

'Connecting an Arduino to an Android

'Arduino receives commands from the Android using the main serial port,
'reacts on commands and returns status information via the same port.

'To be recognized by the Android the Arduino device must be fitted with either
'an Atmega16U2 or Atmega8U2 programmed as an USB-to-serial converter.
'The Android software will not work with FTDI or Prolofic serial based boards.

'Android 3.1 or above attached via USB cable to a suitable Arduino Uno or Mega

'Idea from G.White (Westy53) - Nov. 2012

'Converted to Bascom-AVR by:
'Ben Zijlstra - July 2013.

$regfile = "m2560def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 64
$framesize = 64
$baud = 9600

Open "Com1:" For Binary As #1

Dim A As Byte , S As String * 2

Do
  A = Ischarwaiting(#1)
  If A = 1 Then                                             'we got something
     A = Waitkey(#1)
     Select Case A
            Case 49 : Print #1 , "A";
            Case 50 : Print #1 , "B";
            Case 51 : Print #1 , "C";
            Case 52 : Print #1 , "D";
     End Select
  End If
Loop

End
 


Just a simple example.

Have fun
Ben Zijlstra


Last edited by bzijlstra on Sun Jul 21, 2013 7:39 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
Bojan

Bascom Member



Joined: 15 Feb 2005
Posts: 83
Location: Slovenia

slovenia.gif
PostPosted: Sun Jul 21, 2013 1:57 pm    Post subject: Reply with quote

Thanks Ben!
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4747
Location: Holland

blank.gif
PostPosted: Mon Jul 22, 2013 9:59 am    Post subject: Reply with quote

hi Ben

Had no idea about OTG before your post. Thanks for your research and sharing with us.
Your sample that demonstrates how simple it can be. Well done!

_________________
Mark
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 840
Location: France

france.gif
PostPosted: Mon Aug 26, 2013 11:25 am    Post subject: Reply with quote

Hi Ben,
You said
Quote:


'To be recognized by the Android the Arduino device must be fitted with either
'an Atmega16U2 or Atmega8U2 programmed as an USB-to-serial converter.


but how to do that ?

I would like to use a tablet programmed with V4android and a ATMEGA644 (Bascom). the tablet is a nice method to have a big full color screen. I'm looking for the best interface :
USB or BlueTooth.
For BT, I don't know witch module used ?

Thanks for your help

JP
Back to top
View user's profile Visit poster's website
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Mon Aug 26, 2013 11:43 am    Post subject: atmega8u2 or atmega16u2 Reply with quote

It is either a 'normal' Arduino with a USB Host shield, or a modern Arduino with an atmega8u2 or atmega16u2 to communicate to the outside world.

The last one works without any modification. You could connect an atmega8u2 or atmega16u2 to your atmega644 to get the same.

Source for atmega8u2 or atmega16u2: all open software...

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
techknight

Bascom Member



Joined: 21 Apr 2008
Posts: 222

usa.gif
PostPosted: Thu Aug 29, 2013 4:26 am    Post subject: Reply with quote

You can use the FTDI chips as well, you have to add the correct code in the manifest editor to call the arduino library alongside the usb library inside B4A. Erel has the steps posted over on the B4A forum.
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Aug 29, 2013 7:28 am    Post subject: B4A: usbserial-library-2-0-supports-more-devices Reply with quote

Have found some info about other serial connections on the Arduino:

http://www.basic4ppc.com/android/forum/threads/usbserial-library-2-0-supports-more-devices.28176/

@JP: perhaps a solution for you Wink

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 840
Location: France

france.gif
PostPosted: Thu Aug 29, 2013 8:38 am    Post subject: Reply with quote

Merci Ben !

JP
Back to top
View user's profile Visit poster's website
dawid3143

Bascom Member



Joined: 06 May 2014
Posts: 1

PostPosted: Tue May 06, 2014 4:09 pm    Post subject: cp2102 Reply with quote

Trying to connect to the CP2102. I need UsbSerial library version 2.3.
http://www.basic4ppc.com/android/forum/threads/usbserial-library-2-0-supports-more-devices.28176/
Can m someone help me?
Back to top
View user's profile
Meister

Bascom Member



Joined: 27 May 2010
Posts: 313

PostPosted: Tue May 06, 2014 10:00 pm    Post subject: Reply with quote

Quote:
For BT, I don't know witch module used ?

I am using BTM222A or B from Datasphere. Those are class 1 (long range). Also I use HC05 module (class2, ~10m). The BTM is more easy to set-up whereas the HC-05 (05 is important) requires a switch from one pin to 3V to enter programming mode. The advantage with this is that it draws less current. With the bigger BTM I had problems with interference when running from the same power supply as the AVR. BTM222B and HC-05 ca be used up to 921600 baud.
Both BTMs and the HC-05 are listed for 3.3V. However, I am using the BTMs with up to 5.4V (4xNiMH) for years now (not steadily, but quite often). They don't get hot... This is quite comfortable since no level shift is needed. The HC-05 I guess should be operated with 3.3V.
On the Tablet side I also use B4A (full version). However, it took me quite long to get used to it.
If you want to have a large display you might use a PC screen. For PCs running Windows or Linux there is B4J from the same company as B4A and many libraries are compatible. The B4J is free!. You just have to register to the forum. To me, the B4J was easier to learn. With B4J you can very easily communicate with any hardware that creates a virtual USB port. I used FTDI2xx or CP2102 and an I2C-USB interface from ELV or a cheap BT dongle on the PC for wireless, pairing with the BTM or HC-05. Via a TXRX-USB coverter you can directly communicate with any AVR and create your own I"C or SPI interfaces or whatever.
Regards, Meister
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Tue May 06, 2014 10:08 pm    Post subject: Thanks for the extra info Reply with quote

Meister

Thanks for the extra info.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum