Pages: [1] 2 3 ... 7   Go Down
Author Topic: Using android mobile to control arduino  (Read 92624 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my android controlled arduino project. A simple app sends data through SPP over bluetooth and switches an LED on and off.

The circuit is just a bluetooth module, arduino UNO, LED, resistor and some wires.

The LED can be switched on using buttons as well as voice activation (anything that contains the word 'on','off' or 'blink' - so saying 'turn the LED on' works as well as just uttering 'on').
I created the app using MIT app inventor and this project was created pretty much only to show how easy it is to write custom apps using the tool and interface with microcotrollers in this manner. One could use MIT app inventor to add more more features, such as using accelerometer or GPS data to control the LED, switching the state using text messages, web communication, not only send but also receive data, etc.
...well the point is - MIT app inventor is pretty cool and you should check it out


VIDEO OF IT -> http://youtu.be/41MRGjdVIMQ







Here is the app for download -> http://speedy.sh/63Q9N/bluetoothforarduino-app-2.apk

and here is the source, you can upload it to  MIT's app inventor and modify as much as you want to -> http://speedy.sh/mDyAk/bluetoothforarduino-app-1.zip

Here is the arduino code
Code:

const int ledPin = 7;      // the pin that the LED is attached to
  byte serialA;
void setup()
{
  // initialize the serial communication:
  Serial.begin(19200);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {


  
      
      switch (serialA) {
    case 1:
      digitalWrite(ledPin, HIGH);
      break;
    case 2:
      digitalWrite(ledPin, LOW);
      break;
    case 3:digitalWrite(ledPin, HIGH);
      delay(100);
      digitalWrite(ledPin, LOW);
      delay(100);
    
      
  }
}
void serialEvent(){
serialA = Serial.read();
}



* MITappInventor.jpg (117.71 KB, 1019x733 - viewed 1954 times.)

* MITappInventor2.jpg (110.13 KB, 1024x617 - viewed 1021 times.)

* FILE0545.JPG (685.44 KB, 2048x1536 - viewed 765 times.)

* FILE0546.JPG (709.89 KB, 1536x2048 - viewed 412 times.)
« Last Edit: February 16, 2013, 04:01:08 am by kerimil » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could you share the AppInventor project?
Logged

0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the source file -> http://speedy.sh/mDyAk/bluetoothforarduino-app-1.zip


As I mentioned on hackaday it won't be the exact copy as I improved it to react not only to 'BLINK' command but also 'FLASH' and 'CYCLE'... done that just because google voice search usually returns 'pink' or even 'd#ck' instead of BLINK :-D
but apart from that it's the same

If you have any problems with downloading the files just let me know

« Last Edit: February 16, 2013, 03:59:18 am by kerimil » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This might be a noob question but what bluetooth module did you use?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just gave this a try myself and it worked great. Now I am trying to receive data (an integer value) from the arduino instead of sending to it, but cant seem to figure out how...
Logged

0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well it is a bit trickier.

Use a call bluetoothclient.receivetext and set th number of bytes to -1, which reads until delimiter byte is received.
Mmake sure you set the delimiter byte - I used '13' which is carriage return... ohh you might find this useful -> http://www.asciitable.com/index/asciifull.gif

What's more, it's seems there is no serial timout implemented yet and the app freezes if it doesn't receive anything. To solve that I set up the app to first send a 1 byte number to the arduino, when arduino receives that it sends its data.

Here is an app I created to receive data from a 1 wire temperature sensor (ds18b20). I think if you upload it into the MIT app inventor you should be able to work this out and modify it.
http://speedy.sh/qscGe/bluetoothforarduino-app-temperaturereadings-1.zip

and arduino code (temperature sensor is connected to digital pin 2)
Code:
#include <OneWire.h>
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2


  byte serialA;
void setup()
{
  // initialize the serial communication:
  Serial.begin(19200);
  // initialize the ledPin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {

  
 




  
 
    
    if (serialA == 49){float temperature = getTemp();
Serial.println(temperature); serialA = 0;

}


}
  
  
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -100;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad


for (int i = 0; i < 9; i++) {
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float tempRead = ((MSB << 8) | LSB);
float TemperatureSum = tempRead / 16;

return TemperatureSum;}


void serialEvent(){
serialA = Serial.read();
}




The code is a bit messy I admit but it does work
« Last Edit: February 17, 2013, 11:09:41 am by kerimil » Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@kerimil

 Thanks for sharing your code and work! I have now started trying to learn app inventor because of your post! I too wish to send and receive bluetooth data so, I am very interested in that!
Logged


Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


What's more, it's seems there is no serial timout implemented yet and the app freezes if it doesn't receive anything. To solve that I set up the app to first send a 1 byte number to the arduino, when arduino receives that it sends its data.


 I wonder if you have to test for bytes available? In the Arduino it would be a test like  if(Serial.available()){ Serial.read } .

 In the code blocks you have a something that says BluetoothClient1BytesAvailableToReceive.............I think it should be combined with a if > 0 { read the byte }....or something like that.

P.S. I am tring to adapt your code to my purpose and I am learning while making the attempt.  smiley
Logged


0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



 I wonder if you have to test for bytes available? In the Arduino it would be a test like  if(Serial.available()){ Serial.read } .

 In the code blocks you have a something that says BluetoothClient1BytesAvailableToReceive.............I think it should be combined with a if > 0 { read the byte }....or something like that.


Well yeah, but you can't have (Serial.available()){ Serial.read } all by itself - meaning the control structure block has to be attached to something else - ideally it should happen with no user input. The only thing I can think of is to have it inside Clock.timer block and set it to fire every 10 ms or so. That's not ideal but it might work.

I had had some problems when I tried to implement it. Though the idea might be ok. I might have messed up something else - it is possible I sent data more often than the app received them and that freezed the app because of the buffer. Well at least that's my understanding of it
« Last Edit: February 18, 2013, 05:52:10 am by kerimil » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where did you get the bluetooth module?
Logged

0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's a BTM222 module on a custom breakout board - kind of cool because it is a class 1 device - so the max range is not 10m but 100m  smiley-grin
I bought it from a local tinkerer who makes them - so I don't think you'd be able to get it

Though there are lots of such boards all over ebay - though most of them are class 2 (range ~10meter)



Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had had some problems when I tried to implement it. Though the idea might be ok. I might have messed up something else - it is possible I sent data more often than the app received them and that freezed the app because of the buffer. Well at least that's my understanding of it

I kept getting the same thing, its quite frustrating as my phone takes forever to recover! In my arduino code I just had Serial.println(...) in the loop and had my app to receive text every 1 second (with delimiter byte set to 13). I either get some odd values or it just crashes.
Logged

0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it works but only if serial data is received as often as it's sent... so your arduino code should send the data every 1 second
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of all THANKS,
this was the project that I was waiting for.
I do not understand very much the wiring connection, especially the bt connection, my lack.
green wire: pin0 (arduino) - ? (bt)
yellow wire: pin 7 (arduino) - led
black wire: ? (arduino) - resistor - ? (bt)
red wire: ? (arduino) - ? (bt)

and like in a loop I finish with
End of all THANKS
Logged

0
Offline Offline
Full Member
***
Karma: 6
Posts: 229
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh sorry I forgot to post wiring diagrams here. Now it should be a lot easier to understand.




If you can't see the pics just go here -> http://www.instructables.com/id/How-control-arduino-board-using-an-android-phone-a/

« Last Edit: March 10, 2013, 02:07:39 pm by kerimil » Logged

Pages: [1] 2 3 ... 7   Go Up
Jump to: