arduino to android

97 views
Skip to first unread message

manorAstroman

unread,
Sep 1, 2011, 7:09:46 AM9/1/11
to Amarino Toolkit
Hellon im trying to send data from arduino to android.
Ive wrote this code in android

public class ArduinoReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
String data = null;
float[] var= new float[10];


final String address =
intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);


final int dataType =
intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);


if (dataType == AmarinoIntent.STRING_EXTRA)
{
data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);

testview = (TextView)findViewById(R.id.result);
testview.setText(data);

Scanner s = null;
try
{
s = new Scanner(data);
s.useDelimiter(",\\s*");

int i=1;
while (s.hasNext())
{
if (s.hasNextInt())
{
var[i]= s.nextInt();

}
else
{
s.next();

}
i++;
}
}
finally
{
s.close();
testview = (TextView)findViewById(R.id.textView1);
testview.setText(Float.toString(var[1]));
testview = (TextView)findViewById(R.id.textView2);
testview.setText(Float.toString(var[2]));
testview = (TextView)findViewById(R.id.textView3);
testview.setText(Float.toString(var[3]));
testview = (TextView)findViewById(R.id.textView4);
testview.setText(Float.toString(var[4]));
testview = (TextView)findViewById(R.id.textView5);
testview.setText(Float.toString(var[5]));

}

}
}
}

I want to send 5 different values from arduino. When im using this way
Serial.print(18);
Serial.print(1);
Serial.print(2);
Serial.print(3);
Serial.print(4);
Serial.print(5);
Serial.print(19);

i dont get anything in the android,

but if i send them as a string with the meetandroid library
meetAndroid.send("1,2,3,4,5");

the values are appearing ok in the text boxes.

I looked at the meetandroid library and it apears to send the values
like this
void MeetAndroid::send(int n){
Serial.print(startFlag);
Serial.print(n);
Serial.print(ack);
}
which is the same with what im doing..

Can anyone help me cause i have really stuck with this..

Thank you

manorAstroman

unread,
Sep 1, 2011, 7:19:49 AM9/1/11
to Amarino Toolkit
edit:
Serial.print(18);
Serial.print(1);
Serial.print(",");
Serial.print(2);
Serial.print(",");
Serial.print(3);
Serial.print(",");
Serial.print(4);
Serial.print(",");
Serial.print(5);
Serial.print(19);

Brian Farrugia

unread,
Sep 1, 2011, 8:47:38 AM9/1/11
to amarino...@googlegroups.com
Hi,
you have the startFlag and ack missing so Amarino receiver will not know that you are sending data destined to him , my guess, so you need to use meetAndriod.send() to send data to Amarino.

what are serial.print(18) and serial.print(19) for?

why not send the data as string in the first place? 

I use this to send a string with a floating point number in it.

void Get_Temp(byte flag, byte numOfValues)

{

float userTemp;

EEPROM_readAnything(0,userTemp);

char getBuff[6],buffer[20];

  dtostrf(userTemp,3,2,getBuff);

  sprintf(buffer,"settemp,%s",getBuff); //Creates the string I want to output

meetAndroid.send(buffer);

}

Hope it helps.
 

2011/9/1 manorAstroman <teleft...@gmail.com>

manorAstroman

unread,
Sep 1, 2011, 11:37:02 AM9/1/11
to Amarino Toolkit
Thanx for your answer. The serial.print(18) is the startFlag and the
Serial.print(19) is the ack. I just found what was wrong, 18 and 19 is
char's.
so this is the correct
char startFlag = 18;
char ack = 19;

Serial.print(startFlag);
Serial.print(1);
Serial.print(",");
Serial.print(2);
Serial.print(",");
Serial.print(3);
Serial.print(",");
Serial.print(4);
Serial.print(",");
Serial.print(5);
Serial.print(ack);

Another problem that i have now is that after a little time the
android program crashes.. =\
If i put a larger than 400ms delay in the arduino loop it is working
ok, but with smaller delay crashes! Is there anyway to obtain a rate?

On 1 Σεπτ, 15:47, Brian Farrugia <bri...@gmail.com> wrote:
> Hi,
> you have the startFlag and ack missing so Amarino receiver will not know
> that you are sending data destined to him , my guess, so you need to use
> meetAndriod.send() to send data to Amarino.
>
> what are serial.print(18) and serial.print(19) for?
>
> why not send the data as string in the first place?
>
> I use this to send a string with a floating point number in it.
>
> void Get_Temp(byte flag, byte numOfValues)
> {float userTemp;EEPROM_readAnything(0,userTemp);char getBuff[6],buffer[20];
> dtostrf(userTemp,3,2,getBuff);  sprintf(buffer,"settemp,%s",getBuff);
> //Creates the string I want to outputmeetAndroid.send(buffer);}
> Hope it helps.
>
> 2011/9/1 manorAstroman <telefthera...@gmail.com>

manorAstroman

unread,
Sep 5, 2011, 1:26:45 PM9/5/11
to Amarino Toolkit
I find a way to remove the delay from the arduino code. Im now sending
a start and a stop flag from android to arduino in order to start and
stop ardunio sending data. It works almost ok without crashing but its
very slow, i am getting new data every 300-400ms..
Am i doing something wrong? Can anybody please help me? Here is the
code for the android and for the arduino.

#include <MeetAndroid.h>

MeetAndroid meetAndroid;
char startFlag = 18;
char ack = 19;
int FLAG = 0;
void setup()
{
Serial.begin(57600);
meetAndroid.registerFunction(stop1, 'S');
meetAndroid.registerFunction(run, 'R');
Serial.print(startFlag);
Serial.print(5);
Serial.print(ack);
}

void loop(){
meetAndroid.receive();

int randNumber1 = random(10, 20);
int randNumber2 = random(10, 20);
int randNumber3 = random(10, 20);
int randNumber4 = random(10, 20);
int randNumber5 = random(10, 20);
int randNumber6 = random(10, 20);

if(FLAG == 1){
Serial.print(startFlag);
Serial.print(randNumber1);
Serial.print(",");
Serial.print(randNumber2);
Serial.print(",");
Serial.print(randNumber3);
Serial.print(",");
Serial.print(randNumber4);
Serial.print(",");
Serial.print(randNumber5);
Serial.print(",");
Serial.print(randNumber6);
Serial.print(ack);
FLAG = 0;
}

}
void stop1(byte flag, byte numOfValues)
{
if(meetAndroid.getInt() == 0)
{
FLAG = 0;
digitalWrite(13,HIGH);
}

}
void run(byte flag, byte numOfValues)
{
if(meetAndroid.getInt() == 1)
{
FLAG = 1;
digitalWrite(13,LOW);
}

}


public class Am_test extends Activity {
/** Called when the activity is first created. */

private static final String DEVICE_ADDRESS = "00:06:66:42:1F:C8";

private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();

TextView testview;
String num;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
protected void onStart() {
super.onStart();
// in order to receive broadcasted intents we need to register our
receiver
registerReceiver(arduinoReceiver, new
IntentFilter(AmarinoIntent.ACTION_RECEIVED));

// this is how you tell Amarino to connect to a specific BT device
from within your own code
Amarino.connect(this, DEVICE_ADDRESS);
}

@Override
protected void onStop() {
super.onStop();

// if you connect in onStart() you must not forget to disconnect
when your app is closed
Amarino.disconnect(this, DEVICE_ADDRESS);

// do never forget to unregister a registered receiver
unregisterReceiver(arduinoReceiver);
}

public class ArduinoReceiver extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
String data = null;
float[] var= new float[20];



final String address =
intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);


final int dataType =
intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);


if (dataType == AmarinoIntent.STRING_EXTRA)
{
data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);
sendflagstop();
testview = (TextView)findViewById(R.id.result);
testview.setText(data);

Scanner s = null;
try
{
s = new Scanner(data);
// s.useDelimiter(",\\s*");
s.useDelimiter(",|\\r\\n");


int i=1;
while (s.hasNext())
{
if (s.hasNextFloat())
{
var[i]= s.nextFloat();

}
else
{
s.next();

}
i++;
}
}
finally
{
s.close();
sendflagstart();
testview = (TextView)findViewById(R.id.textView1);
testview.setText(Float.toString(var[1]));
testview = (TextView)findViewById(R.id.textView2);
testview.setText(Float.toString(var[2]));
testview = (TextView)findViewById(R.id.textView3);
testview.setText(Float.toString(var[3]));
testview = (TextView)findViewById(R.id.textView4);
testview.setText(Float.toString(var[4]));
testview = (TextView)findViewById(R.id.textView5);
testview.setText(Float.toString(var[5]));

}

}
}
}
private void sendflagstop(){

Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'S', 0);
}
private void sendflagstart(){

Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'R', 1);
}



On 1 Σεπτ, 18:37, manorAstroman <telefthera...@gmail.com> wrote:
> Thanx for your answer. The serial.print(18) is the startFlag and the
> Serial.print(19) is the ack. I just found what was wrong, 18 and 19 is
> char's.
> so this is the correct
> char startFlag = 18;
> char ack = 19;
>
> Serial.print(startFlag);
>   Serial.print(1);
>   Serial.print(",");
>   Serial.print(2);
>   Serial.print(",");
>   Serial.print(3);
>   Serial.print(",");
>   Serial.print(4);
>   Serial.print(",");
>   Serial.print(5);
>   Serial.print(ack);
>
> Another problem that i have now is that after a little time the
> android program crashes.. =\
> If i put a larger than 400ms delay in the arduino loop it is working
> ok, but with smaller delay crashes! Is there anyway to obtain a rate?
>
Reply all
Reply to author
Forward
0 new messages