Introduction
The search for communication between an embedded system and the outside world is always exciting. Being able to control the environment from the palm has been an interesting topic. In this paper is to explain simply and shortly as Android System communicate with a microcontroller mounted on a card called Arduino.
The project we are working together with a friend electronics expert (Julio-is reserved and not yet part of the forums-) and I (no expert but a little good at programming) and headed to operate this pileup . We share it with you.
Target
Material
Software
Android Code
The code used until now for Android in Basic4Android is:
The initial idea of the code I got from the post created by the user Diego:
http://www.b4x.com/forum/basic4android-updates-questions/18292-arduino-uno-usb-host.html#post105928
Arduino Code
Before compiling the code the libraries should be placed in the Libraries folder Arduino Compiler. But the code will not compile and mark errors. The code used until now for Arduino:
Conclusions
To date September 13 has managed to send a string ("Hola") to Arduino which can be viewed via the Serial monitor. Also been sent from Arduino to Android the same chain and can be seen in a Label from Android. Thanks to the data type AnsyncString sending and receiving of data can be simultaneously (or so it appears to the end user) and the system will not lock -Correct me if I'm wrong-.
Other comments and Earrings
This code is not yet finalized and is in development, so it is not optimized, maybe you can make a better way, for now it works this way. Lack implement appropriate communication protocol (I'm thinking of using the same Google uses in its Demokit). As I said, I will upload progress to the page.
Acknowledgements
Erel. For providing the correction to obtain permits for accessory
Jerel.For providing the correction in the code to write and send data
Brief bibliography and links
Connect via USB Sockets
AsyncStreams Tutorial
USB Library information
Sorry for the spelling, I'm from mexico and I have little knowledge of the English language, If you have any mistake or any comments, please let me know, this will be welcome.
Regards.
The search for communication between an embedded system and the outside world is always exciting. Being able to control the environment from the palm has been an interesting topic. In this paper is to explain simply and shortly as Android System communicate with a microcontroller mounted on a card called Arduino.
The project we are working together with a friend electronics expert (Julio-is reserved and not yet part of the forums-) and I (no expert but a little good at programming) and headed to operate this pileup . We share it with you.
Target
- Send control signals to the digital outputs from Android to Arduino board. This may serve to: turn on or off a led up to a motor on or off (with the appropriate electronic settlement, which will not be addressed in this draft).
- Wanted monitor temperature and humidity data that are provided by sensors. That is, send data from Arduino to Android.
- Controlling a servomotor from Android.
Material
- Arduino Mega Adk o some variant:
- Leds
- Temperature and Humidity Sensor (still to be defined, maybe a LM35)
- Servomotor (still to be defined, maybe a Dynamixel)
Software
- Basic4Android (obviously)
- Compilador de Arduino
- Two libraries for Arduino: USB_Host_Shield and AndroidAccesory
Link: Arduino compiler with libraries
Android Code
The code used until now for Android in Basic4Android is:
B4X:
Sub Process_Globals
Dim Accesorio As UsbAccessory
Dim Manejador As UsbManager
Dim AStreams As AsyncStreams
End Sub
Sub Globals
Dim Button1 As Button
Dim Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Escritorio")
If FirstTime = True Then
EncuentraAccesorio
End If
End Sub
Sub Activity_Resume
EncuentraAccesorio
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub EncuentraAccesorio
Manejador.Initialize
Dim ListaAccesorios () As UsbAccessory
ListaAccesorios = Manejador.GetAccessories
For i = 0 To ListaAccesorios.Length - 1
If ListaAccesorios(i).Description = "Prueba" Then
Accesorio = ListaAccesorios(i)
Log(Accesorio.Description)
Log(Accesorio.Manufacturer)
Log(Accesorio.Serial)
End If
Next
If Accesorio <> Null Then
Dim Permiso As Boolean
Dim r As Reflector
r.Target = Accesorio
Permiso= Manejador.HasAccessoryPermission(r.GetField("accessory"))
If Permiso==True Then
Manejador.OpenAccessory(Accesorio)
Log (AStreams.IsInitialized)
If AStreams.IsInitialized==False Then
Try
AStreams.Initialize(Accesorio.InputStream,Accesorio.OutputStream,"AStreams")
Catch
Log("No se inicializo AncyStreams")
End Try
End If
Else
Manejador.RequestAccessoryPermission(r.GetField("accessory"))
Permiso= Manejador.HasAccessoryPermission(r.GetField("accessory"))
If Permiso Then
Manejador.OpenAccessory(Accesorio)
End If
If AStreams.IsInitialized==False Then
Try
'AStreams.Initialize(Accesorio.InputStream,Accesorio.OutputStream,"AStreams")
AStreams.InitializePrefix(Accesorio.InputStream,False,Accesorio.OutputStream , "AStreams")
Catch
Log("No se inicializo AncyStreams")
End Try
End If
End If
Else
Log("No hay accesorio reconocido")
End If
End Sub
'Funcion especial de los AStreams////////////////////////////////////
Sub AStreams_NewData (Buffer() As Byte)
Dim msg As String
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Label1.Text=msg
'ToastMessageShow(msg, False)
Log(msg)
End Sub
Sub AStreams_Error
ToastMessageShow(LastException.Message, True)
End Sub
'///////////////////////////////////////////////////////////////////
Sub Button1_Click
If AStreams.IsInitialized=False Then Return
Dim Cadena As String
Cadena="Hola"
AStreams.Write(Cadena.GetBytes("UTF8"))
End Sub
http://www.b4x.com/forum/basic4android-updates-questions/18292-arduino-uno-usb-host.html#post105928
Arduino Code
Before compiling the code the libraries should be placed in the Libraries folder Arduino Compiler. But the code will not compile and mark errors. The code used until now for Arduino:
B4X:
#include <Max3421e.h>
#include <Usb.h>
#include <AndroidAccessory.h>
#define ARRAY_SIZE 25
AndroidAccessory acc("Google, Inc.",
"Prueba",
"Prueba",
"1.0",
"http://www.android.com",
"0000000012345678");
char hello[ARRAY_SIZE] = {'H','o','l','a',' ',
'M','u','n','d','o',' ', 'd', 'e', 's', 'd', 'e ',' ',
'A', 'r', 'd', 'u', 'i', 'n', 'o', '!'};
byte rcvmsg[255];
byte sntmsg[ARRAY_SIZE];
void setup()
{
Serial.begin(115200);
acc.powerOn();
}
void loop() {
if (acc.isConnected()) {
//read the sent text message into the byte array
int len = acc.read(rcvmsg, sizeof(rcvmsg), 1);
if (len > 0)
{
for(int x = 0; x < 4; x++)
{
Serial.print((char)rcvmsg[x]);
delay(250);
}
Serial.println();
delay(250);
}
for(int x = 0; x < ARRAY_SIZE; x++)
{
sntmsg[x] = hello[x];
}
acc.write(sntmsg, ARRAY_SIZE);
delay(250);
}//Fin del if que verifica si el accesorio esta conectado
}
Conclusions
To date September 13 has managed to send a string ("Hola") to Arduino which can be viewed via the Serial monitor. Also been sent from Arduino to Android the same chain and can be seen in a Label from Android. Thanks to the data type AnsyncString sending and receiving of data can be simultaneously (or so it appears to the end user) and the system will not lock -Correct me if I'm wrong-.
Other comments and Earrings
This code is not yet finalized and is in development, so it is not optimized, maybe you can make a better way, for now it works this way. Lack implement appropriate communication protocol (I'm thinking of using the same Google uses in its Demokit). As I said, I will upload progress to the page.
Acknowledgements
Erel. For providing the correction to obtain permits for accessory
Jerel.For providing the correction in the code to write and send data
Brief bibliography and links
Connect via USB Sockets
AsyncStreams Tutorial
USB Library information
Sorry for the spelling, I'm from mexico and I have little knowledge of the English language, If you have any mistake or any comments, please let me know, this will be welcome.
Regards.
Last edited: