Step 4: App Inventor Code - Introduction

Before being able to use the Android application, you need to pair the Bluetooth module with your smartphone.

Power the Arduino board, turn on Bluetooth on the Android phone and search for Bluetooth devices nearby: the JY-MCU module will present itself as HC-06, the pairing password is 1234.

The key component of the Bluetooth Arduino Connection App is the Bluetooth Client while the Arduino board will act as Server: this means that the app will always initiate the connection. When you power the Arduino board, the Bluetooth module red LED starts blinking; push the app "Connect to Device" button and select your module from the list:the red LED light becomes solid and the connection status will change to "Connected".

Again, this has nothing to do with Bluetooth Master / Slave, but still can be confusing: you Bluetooth module needs to be Slave but it (or rather the Arduino sketch) will behave as Server in the Client - Server communication with the Android app.

You can install the Bluetooth Arduino Connection App directly from Android Play Store, or you can import the full app code into your MIT App Inventor projects by downloading the attached file.

Bluetooth Arduino Connection App is developed with MIT App Inventor 2; the following step will provide a detailed explanation.

<p>really well explained but you should addded another part related the the app making</p>
<p>What if I used HC-05?</p>
<p>tell me please how can i download this softwear on my mobile </p><p>and can i use this code for TEMP 35 </p><p>please please tell me</p>
Thank you very much for this great instructable every thing is very clearly mentioned ..<br><br>Why not you make more instructable like this <br>
<p>To illustrate the connection between the Arduino the tablet we will be sending simple text messages back and forth between them. However, since the Arduino doesn&rsquo;t really have a keyboard, we are going to plug the arduino into our pc and talk to it via a serial connection using usb.<br><a href="http://www.examcollection.us/70-488-vce.html" rel="nofollow">70-488 vce</a></p>
<p>can i use dht11 instead of tmp 36 please give me changes in the code</p>
<p>Please any one can give a code for dht 11 sensor instead of tmp36</p>
<p>Hello, The *.ino file takes forever to upload onto my Arduino. What could be the problem?</p>
<p>First unplug the gnd and vcc of bluetooth module. Then upload the code. After than again plug the gnd and vcc. :)</p>
<p>Hi. May I ask do the Bluetooth Arduino Connection app have code for java? Such that I can open in Android studio?</p>
<p>can we replace TMP36 with LM35 with the same Arduino temperature reading code here? what is the changes in ardiuno if we use LM35 in this case ? </p>
Hi Eng HowT,<br>the sketch pinout doesn't change but the code might be a little different. For example you can have a look here: http://arduino.stackexchange.com/questions/6646/temperature-sensors-tmp36-and-lm35-have-temperature-diff-offset-in-the-same-c<br>
<p>very useful ible</p>
<p>The controller app is really smooth and lightweight that is appreciating.</p>
<p>1. When I go out of range the connection is lost (there is no warning attempts to reconnect). <br>2. I move back into range .. must reconnect &hellip; the phone receive the status of the LED, but the new status not update in the app with the current state of the LED (attached printscreen).</p>
<p>this is my project android arduino bluetooth </p><p>The software can be used to control the electrical equipment in your phone to your home.</p><p>It can be used on your design and many designs can be cautious.</p><p>The software has been designed in Goods 10 interfaces required to control.</p><p>The serial send command can be used to control any amount of equipment.</p><p>You can get it with the file contains instructions on how to create the device.</p><p>The need for this basic Arduino code available.</p><p>We wish to provide the next version of the software can control from anywhere in the world.</p><p>But as for your ideas, suggestions please send it to us in the forum by software</p><p>for more information</p><p>http://dcmote.blogspot.com/</p>
<p>Great Instructable! One question though - If I were to make something similar, but just wanted the Arduino to send data to the App, data as in bits - 0 or 1, would Serial.println() or Serial.write() be appropriate?</p><p>Thanks.</p>
<p>this is my circuit </p>
<p>Hi,i woul like your help for a project that i have to tele-controll with android phone and hc-05 a 220v bulb.Actually i am using a 5v reley,a Photoresistor Sensor Module Light Detection Light for Arduino,android phone,arduino uno.Untill now this is the code i have used,is not complete.i am asking your help for this code because there are cases when i type to my android phone a 1 it dot give me always a 49 to serial monitor(arduino) or to the above condition i have specified that if the value i give is equal to 49 the bulb must stay on 2 seconds and off 8seconds,but it does the reverse,how can i solve this problem </p><p>Serial.print(ReadBT);<br> if (ReadBT == 49)<br> {<br> Delay = 8000;<br> Serial.println(&quot;DDDDDDDDDDDDD&quot;);<br> }<br> else<br> { <br> Delay = 2000;<br> Serial.println(&quot;CCCCCCCCCCCC&quot;);<br> }<br> <br> digitalWrite(Relay, HIGH); // turn on relay<br> delay(10000-Delay); // time OFF<br> digitalWrite(Relay, LOW); // turn off relay<br> delay(Delay); // time ON<br><br>}<br><br><br><br>This is the code i have created until now<br><br>///////////////////////////////////////////////////////////////////////////////////////////////////////////<br>///////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br>#include &lt;SoftwareSerial.h&gt;<br>int Relay = 7;<br>int BTtx = 10;<br>int BTrx = 11;<br>int ReadBT;<br>int Delay;<br><br>SoftwareSerial bluetooth(BTtx, BTrx); // TX | RX<br><br>void setup() {<br> <br> // put your setup code here, to run once:<br> <br> Serial.begin(9600); // opens serial port, sets data rate to 9600 bps<br><br> bluetooth.begin(9600);<br> bluetooth.print(&quot;$$$&quot;);<br> delay(100);<br> bluetooth.println(&quot;U,9600,N&quot;);<br> bluetooth.begin(9600);<br> Serial.println(&quot;setup complete.&quot;);<br> pinMode(Relay, OUTPUT);<br>}<br><br>void loop() {<br> // if theres data from the bluetooth module<br> if (bluetooth.available()) {<br> ReadBT = (int)bluetooth.read();<br> Serial.println(&quot;BBBBBBBBBBB&quot;);<br> //echo it to the serial monitor<br> //Serial.print(&quot;bt module said:&quot;);<br> Serial.print(ReadBT);<br> }<br> <br> //if theres data from the serial monitor<br> if (Serial.available()) {<br> ReadBT = (int)Serial.read();<br> Serial.println(&quot;AAAAAAAAA&quot;);<br> //send it to the bluetooth module<br> bluetooth.print(ReadBT);<br> }<br><br> Serial.print(ReadBT);<br> if (ReadBT == 49)<br> {<br> Delay = 8000;<br> Serial.println(&quot;DDDDDDDDDDDDD&quot;);<br> }<br> else<br> { <br> Delay = 2000;<br> Serial.println(&quot;CCCCCCCCCCCC&quot;);<br> }<br> <br> digitalWrite(Relay, HIGH); // turn on relay<br> delay(10000-Delay); // time OFF<br> digitalWrite(Relay, LOW); // turn off relay<br> delay(Delay); // time ON<br><br>}<br><br>2.I have bought this light sensor for my project:http://www.ebay.com/itm/1PC-Photoresistor-Sensor-Module-Light-Detection-Light-for-Arduino/251673864995?_trksid=p2045573.c100033.m2042&amp;_trkparms=aid%3D111001%26algo%3DREC.SEED%26ao%3D1%26asc%3D20131017132637%26meid%3D93d7e24c9d784838bf12ddf7c9ee88f1%26pid%3D100033%26rk%3D1%26rkt%3D4%26mehot%3Dpp%26sd%3D251673864995,can anywone help me with a code how to sens light intensity during day/night(considering even the condition that when it is bad/cloudy time i dont want my llamp to get turned on,i want the bulb to get turned on when the sensor sens the dark(for example 8pm) and to turn off the bulb when it is light(7 pm))</p>
<p>This is what I'm searching for. Thank you for this very informative post! Can you help me with my current project? My problem is I need to compare the Android phone's app (Speedometer) value to the Arduino programmed speed limit. So I need to connect my Android app to Arduino using bluetooth/serial connection. What is the code I need to put in my app? And what is the code I need to put in Arduino? Thanks. I'm a newbie.</p>
<p>Hi! I like your project,its really helpful for me. But i still need help about some details.I will use arduino nano and hc-05 bluetooth module on my own project. But how can i make the work mode as a slave? I know that i have to write a AT code. But how will I write this AT code on the program of Arduino? Thanks again..</p>
<p>This is a really great tutorial, I was looking for this for a while - thanks! But before I make it, I have a quick question: do you know how much memory and processing this takes on the Arduino side of things? I was planning on using this with an unbuffered e-Ink display, which takes a bit over half (according to Adafruit, anyway) of the Arduino's power. I just want to be sure the Arduino can support both at the same time, and if not, buy another ATMega. Speaking of which, I'm planning on taking this out of Arduino into an ATMega 328 - there's nothing in your code which looks like it would require anything an ATMega can't do - am I correct?<br>Thanks again for the great tutorial!</p>
<p>Hi and thanks!</p><p>In reply to your questions: processing is negligible and memory is mostly related to the size of the strings you are using; in addition using program memory can further reduce the code footprint (if you are not familiar with it, this is an excellent tutorial: https://learn.adafruit.com/memories-of-an-arduino/...</p><p>When moving the code to other platforms, including ATMega, I strongly advise to get rid of the serialEvent() function and to move the code at the top of loop().</p><p>It goes like this:</p><p>if (Serial.available()) {</p><p>readBytes = Serial.readBytesUntil('\n', cmd, sizeof(cmd));</p><p>cmd[readBytes] = '\0';</p><p>}</p><p>This way it will work for any Serial port.</p><p>Regards</p>
<p>Also, thanks for the tutorial - it really helped.</p>
<p>Awesome! Thank you so much for your prompt reply!</p>
<p>What kind of range can you get with this?</p>
I've tested the BT module up to 10 mt, with a thin wall in between.<br>The theoretical range is much higher.
<p>This is another nice post on arduino and android interfacing, with trouble shooting techniques</p><p>http://knowledge-cess.com/arduino-android-interfacing-example-code-and-troubleshooting/</p>
<p>how to connect android to arduino use bluetooth automatically without fist pair before ?</p>
You need to. code a native java android app for this: it's not possible with MIT App Inventor.
<p>awesome project</p>
<p>Hi pmosconi - this code is really awfully interesting for me. You have so good comments written into your sketch file for arduino. At least I had it put alltogether manually and understood a lot of your explanations of the code. But one thing I found nowhere on the web. What the hell means the &quot;?&quot; into your code:</p><p><strong>else if (command.equals(&quot;YELLOW&quot;)) { // YELLOW=ON|OFF<br>value.equals(&quot;ON&quot;) ? digitalWrite(led1Pin, HIGH) : digitalWrite(led1Pin, LOW);<br></strong></p><p><strong>stringOK = true;</strong></p><p>I found really no explanation whether in the Arduino - command or syntax - please give me chance - I do not understand it. By no means. Thanks for an answer.</p><p>Peter</p>
<p>Hi Peter, '?' is a standard C++ operator called &quot;ternary operator&quot; (http://www.cprogramming.com/reference/operators/te...</p><p>it's meaning is &lt;condition&gt; ? &lt;execute when true&gt; : &lt;execute when false&gt;;</p><p>For our purposes, it's equivalent to</p><p>if (&lt;condition&gt;) {</p><p> &lt;execute when true&gt;;</p><p>}</p><p>else {</p><p> &lt;execute when false&gt;;</p><p>}</p><p>If you need to know more about the difference between if-then-else and ?, see the language reference.</p><p>Hope this helps.</p>
<p>This is awesome tutorial. Thanks for sharing.</p>
<p>This is awesome tutorial. Thanks for sharing.</p>
<p>I am new to the hardware interfacing and stuffs .. Someoneplease guide on where to type the ARDUINO code .. and also .. incase i wanna PLOT a graph using the value that i get from the kit by using plotly but without an ethernet shield , what am i supposed to do ??? Kinda urgent </p>
<p>I am new to the hardware interfacing and stuffs .. Someoneplease guide on where to type the ARDUINO code .. and also .. incase i wanna PLOT a graph using the value that i get from the kit by using plotly but without an ethernet shield , what am i supposed to do ??? Kinda urgent </p>
<p>I am new to the hardware interfacing and stuffs .. Someoneplease guide on where to type the ARDUINO code .. and also .. incase i wanna PLOT a graph using the value that i get from the kit by using plotly but without an ethernet shield , what am i supposed to do ??? Kinda urgent </p>
Many thanks for your kindness!!
<p>Hi Pmosconi,</p><p>This is exactly what I was looking for. Thanks for instructable.</p><p>I am having some challenges in displaying TEMP value on android phone. In my case it is printing full string. Will you be able to share your android java code? aia file above appears to be binary. </p><p>Appreciate your help. Thanks</p>
Hi sammy011, I developed the Android application with MIT App Inventor (<a href="http://appinventor.mit.edu/explore/" rel="nofollow">http://appinventor.mit.edu/explore/</a>) thus it is not native. From the link you'll find how to manage the aia file, which btw is a zip containing the whole App Inventor project.<br> In any case, what do you mean by printing full string? Maybe I can still help you.<br> <br>
<p>Thanks Pmosconi for reply. </p><p>I resolved the issue with my setup today. The issue was I supposed to use lightweight sdk for nRF8001 BLE. I added it and app worked like charm. </p><p>Thanks for your response and offer to help. :)</p><p>+1 follow</p>
<div> <br><div> <br><div>Dear Pmosconi,<br>I use Arduino Uno for my <br>project, once I try to pair Bluetooth to PC, it's worked and then I try <br>to see my program is working. I use JY-MCU bluetooth module for this <br>project. The outcoming port is shown as COM 11, but when I want to try <br>to see in tools-&gt;Serial Port menu in the Arduino program, there is no <br> com port 11 at all there. I just can see Com port 9, but I need to see <br>COM 11 there to communicate with th PC. Could you please <br>help in this project, I have to solve this problem as soon as I can, so <br>could please get back to me ASAP, that would be very grateful. Many Thanks and Best Regards..</div></div></div>
Hi numancelik34,<br>have a look at this: http://ecno92.blogspot.it/2012/10/installing-and-testing-jy-mcu-bt-chip.html<br>It might help with the PC connection.
<p>Still same issue, the outgoing COM port doesn't appear in tools--&gt; Serial Port, thus I cannot communicate with the PC and send commands from the PC to the Arduino. I don't have any idea what the reason is. Do you have any other solutions to this problem?</p>
If the PC bt port is working with other devices, I have no idea otherwise the problem lies in the PC settings. Sorry, but I don't believe I can offer any additional help
<p>Do you have any idea how can I do the same thing using the wifi module? have two wifi module in my house and want to use wifi rather than the bluetooth.Is it possible?</p><p>Cheers!</p>
Bluetooth in the end is like having a serial cable connecting the 2 devices; wifi is way more complicated: you'll use tcp over it and you'll need at least a web service on the arduino.
<p>really nice job.</p><p>I've in mind project base on Arduino Uno + Sensor DHT11 ==&gt; Moisture + temperature Sensor.</p><p>With DHT11 Sensor, we need to use DHT library.</p><p>hereby a sample squetch :</p><p>#include &quot;DHT.h&quot;</p><p>#define DHTPIN 8</p><p>#define DHTTYPE DHT11 //#define DHTTYPE DHT22 (pour un capteur DHT22)</p><p>DHT dht(DHTPIN, DHTTYPE);</p><p>float h = 0;</p><p>float t = 0;</p><p>void setup(){</p><p>Serial.begin(9600);</p><p>dht.begin();</p><p>Serial.print(&quot;Test Connexion&quot;);</p><p>}</p><p>void loop(){</p><p>h = dht.readHumidity();</p><p>t = dht.readTemperature();</p><p>if (isnan(t) || isnan(h))</p><p>Serial.println( &quot;Lecture impossible !&quot;);</p><p>else{</p><p>Serial.print(&quot;Humidite :&quot;);</p><p>Serial.print(h);</p><p>Serial.print(&quot; %\t&quot;);</p><p>Serial.print(&quot;Temperature :&quot;);</p><p>Serial.print(t);</p><p>Serial.println(&quot; *C&quot;);</p><p>}</p><p>delay(1000);</p><p>}</p><p>I was wondering how it is possible (with few modifications) to adapt your code with this sensor instead of the TMP36?</p><p>there is a few difference with your squetch :</p><p>- Pin definition is Digital instead of Analogic</p><p>- Reading value (temp/moisture) is made inside the loop.</p><p>Thx 4 ur help.</p>

About This Instructable




More by pmosconi:Connect Arduino Uno to Android via Bluetooth 
Add instructable to: