element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs MIT App Inventor and Arduino Part 2 - send data from Arduino to Android and display
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 18 Jul 2014 4:06 PM Date Created
  • Views 13661 views
  • Likes 4 likes
  • Comments 53 comments
  • android
  • bluetooth
  • hc06
  • mit
  • arduino_tutorials
  • arduino tutorials
  • appinventor
  • arduino_tutorial
  • arduino tutorial
  • arduino
Related
Recommended

MIT App Inventor and Arduino Part 2 - send data from Arduino to Android and display

neilk
neilk
18 Jul 2014

MIT App Inventor and Arduino Part 2 - send data from Arduino to Android and display


I found numerous links via Google for this example; unfortunately a lot of them were the same tutorial posted in different places by the same person. None of them included the Arduino sketch! However, the sketch tuned out to be entirely trivial

 

I won't bother giving any of the links, because the the whole thing is unbelievably simple!

 

Here is the Design screen for the App:

 

                                                              image

The screen is very similar to the previous app, except the On and Off buttons are replaced by 2 Labels: Label2 has the default text of "Data from Arduino" and Label3 is given the default text of "0", in Red with a larger font.


As in the previous App, there is a  Bluetooth Client and we also need to add a  Clock component to act as a timer.  These are both non-visible on the display and are shown below the bottom of the Android device screen.

 

In the Blocks screen we retain the 2 Blocks relating to selecting and connecting to Bluetooth and the Block responsible for disconnecting  from Bluetooth. We  dispense with the Button Blocks.


We then add the following new Block to receive and display the data:

image

In the Design screen, I left the default timer interval on the Clock at 1000mS because we want to have time to actually read the Android display.

 

The Arduino Sketch turned out to be very simple:

 

/*   Demonstration sketch to send data to an Android App via Bluetooth - essentially simple; this is just serial output of integer data.

     The Bluetooth module is connected across the Arduino Rx and Tx lines as before.

      A 10k pot is connected across +5v and ground; the slider is connected to analog input 0, to supply varying data.

 

     Neil Kenyon

     15 July 2014

*/

int potPin = 0;                                  // Define the Analog input

void setup()

{

  Serial.begin(9600);

  pinMode(potPin, INPUT);

}

void loop()

{

    int value = analogRead(potPin);    // read the value of the Analog Input

    Serial.println(value);                     // Print the value; can also be seen on Serial Monitor

    delay(1000);                                   // Don't send too fast or the Android buffer will flood - this worked for me

}

 

This seems to work well enough to demonstrate the ease with which we can send data from the Arduino to the Android App.


Edited on 10 August 2015........problems can arise if the Arduino Transmission and Android Receive delay times are set identically. This wasn't a problem for my set up, but seems to be for others. If you leave the Arduino delay at 1000mS, then reduce the clock/timer delay in App Inventor to 950mS, it should be good image



There are two attachments; the second one - Data_from_Arduino_A.zip does some extra error checking which some Android devices seem to need more than others.

Attachments:
Data_from_Arduino.zip
Data_from_Arduino - A.zip
  • Sign in to reply

Top Comments

  • e14 Contributor
    e14 Contributor over 11 years ago +2
    Thank you for all your help, I have now (after some playing) successfully able to receive accelerometer data via bluetooth to an android device!!!!! Thanks!
  • e14 Contributor
    e14 Contributor over 11 years ago +2
    I found that the following youtube video was very helpful: https://www.youtube.com/watch?v=DkGGilznfG8
  • e14 Contributor
    e14 Contributor over 11 years ago +2
    Hey everybody... Could You Do Me a Favour? I got problem here when I'm trying to make android app using AppInventor2. I'm going to make android app to showed data "TempValue", "MoistureValue", "ldrvalue…
  • neilk
    neilk over 10 years ago in reply to e14 Contributor

    Hi jefferson.cejas

     

    The problem of sending 2 or more data items from Arduino to Android App and displaying in 2 or more different labels has been addressed in another of my blog posts:

     

    http://www.element14.com/community/people/neilk/blog/2015/02/02/mit-app-inventor-and-arduino-part-4--send-more-than-1-data-item-from-arduino-to-android-and-display

     

    Have a look at that post and reply in that thread if you need anymore help.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 10 years ago

    Sir Neil .... I need help i cant separate the data for humidity and temperature. I want my label3 = Hdata and label4=Tdata image when i run this code humidity and temperature data will display in label3 only image

    imageimage

    #include "DHT.h"

    #include <SoftwareSerial.h>

     

    #define DHTPIN 26     // what pin we're connected to

    #define DHTTYPE DHT11 // DHT 11

     

    DHT dht(DHTPIN, DHTTYPE); //means dht(26,DHT11)

    SoftwareSerial mySerial(10, 11); //means mySerial(Rx,Tx)

    int led = 8;

    int tempmax = 31;

    int tempmin = 30;

     

    void setup()

    {

      Serial.begin(9600);

      mySerial.begin(9600);

      Serial.println("DHTxx test!");

      pinMode(led, OUTPUT);

      dht.begin();

    }

     

    void loop()

      {

        // Reading temperature or humidity takes about 250 milliseconds!

        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)

        float h = dht.readHumidity();

        float t = dht.readTemperature();

      

        // check if returns are valid, if they are NaN (not a number) then something went wrong!

        if (isnan(t) || isnan(h))

        {

          Serial.println("Failed to read");

        }

        else

          {

          //Serial Monitor is available

          

            if (!Serial.available())

            {

              Serial.print("Humidity: ");

              Serial.print(h);

              Serial.print(" %\t");

            

              Serial.print("Temperature: ");

              Serial.print(t);

              Serial.println(" *C");

            }

          

          //Bluetooth Connected to android

            if (!mySerial.available())

            {

              mySerial.println(String(h));

              delay(2000);

              mySerial.println(String(t));

              delay(2000);

            }

          

          }

           // condition for maximum temperature and to ON cooling fan

        if (t == tempmax )

        {

          digitalWrite(led, HIGH);

          delay(1000);

        }

     

        // contition for minimum temperature and to OFF cooling fan

        if (t == tempmin)

          {

            digitalWrite(led, LOW);

            delay(1000);

          }

      }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 11 years ago in reply to e14 Contributor

    Hi vdp1331

     

    I'm sorry you are having trouble with this image. The thread now has so many strands to it, it can be very difficult to follow even for me!

     

    First of all, my original posting was intended to transmit a single item of data - a number, or a string of text to the Android device.

     

    The solution you have been following,  by Kovacs Lajos, is sending 2 numeric data items as a concatenated text string - in fact sort of .csv data. Why his phone needs the leading comma, I don't know - I get the same error as you if I try his code.

     

    So if you are only wanting to transmit a single data item, my original solution should work, but maybe not out of the box - sorry!

     

    I'm pretty sure that the problem you have is probably caused by the transmit and receive both running at the same nominal delay - ie 1000mS. This results in the Android buffer sometimes containing 2 transmissions from the Arduino.

     

    I get the problem sometimes, but not very often!

     

    However, if you reduce the clock/timer time in the App Inventor from 1000mS to 950mS, while leaving the delay in the Arduino at 1000mS, that should fix it. If not, try reducing it to 900mS.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 11 years ago in reply to e14 Contributor

    I am experiencing the exact same issues (first number in extra line). Is this really the only way to fix this? Could you please comment your solution? Do i need all thos commas for one value to be transferred? Did it work for you "out of the box" (without the changes above) on other phones?

    So causes the problem? The phone? The arduino code? Is it an App Inventor Bug?

    Thanks a lot!

    (this behaviour really sucks image i am trying to solve this for days now. With the serial terminal the data is shown correctly)

     

    The workaround with the leading comma (String data=","+String (value)) kind of works. But i'm getting the error "The operation <= cannot accept the arguments [,] [50]" The 50 isn't sent anywher from the arduino and isn't shown in the serial terminal app.

    Any ideas on what could be the reason?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 11 years ago in reply to e14 Contributor

    harekumarbilla@yahoo.com.my Hi . This looks like a pretty complex project, outside of the scope of this thread. Please can you post  as a new QUESTION in the Arduino section of the forum.

     

    I suggest that you should also explain which parts of the project you have already got working.

     

    Thanks

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
<>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube