element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • 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
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • 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
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs MIT App Inventor and Arduino Part 4 - send more than 1 data item from Arduino to Android and display
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 2 Feb 2015 12:21 PM Date Created
  • Views 2263 views
  • Likes 2 likes
  • Comments 30 comments
  • android
  • bluetooth
  • hc06
  • mit_app_inventor
  • arduino
Related
Recommended

MIT App Inventor and Arduino Part 4 - send more than 1 data item from Arduino to Android and display

neilk
neilk
2 Feb 2015

This is a follow on from an earlier posting:


http://www.element14.com/community/people/neilk/blog/2014/07/18/app-inventor-and-arduino-part-2--send-data-from-arduino-to-android-app

 

I have written this in response to a request from kennethbuenaventura to show how to send 2 or more data items from an Arduino, via Bluetooth and display them on an Android device.


A helpful link to a video by "planetleak" was posted by cams2908:

 

    https://www.youtube.com/watch?v=DkGGilznfG8    but this solution proved to be a little confusing.


In essence, this solution "codes" the readings from 2 analog inputs so that one still reads 0 - 1023 and the other reads 2000 -3023. These coded data items are then transmitted over Bluetooth, to the Android device where they are decoded and displayed separately.


This works, but is confusing because in the Arduino sketch the coding is done by mapping the data, including unnecessarily mapping the first data item as 0, 1023, 0, 1023. Clearly this has no effect.


Thus, in my Arduino sketch I have removed the mapping, leaving the first data item alone and simply adding 2000 to the second data item:


// simulate transmission of 2 changing data items for Android demonstration

const int pot = A0;

void setup()
{
  pinMode(pot, INPUT);
  Serial.begin(9600);
}

void loop()
{
  int potVal1 = analogRead(pot);
  int potVal2 = potVal1/2;              // just to give a different value
  potVal2 = potVal2 + 2000;             //Second value shifted 2000 and 3023
  Serial.println(potVal1);
  delay(500);   
  Serial.println(potVal2);
  delay(500);         
}

 

In the Android App, there is more confusion because of the way the decoding is handled.


Firstly, I have simplified, and modified the Android display. I have also added a Bluetooth disconnect function block to the app.


image

 

Ignoring the Bluetooth connection and disconnection blocks, which have been show elsewhere, my data processing block looks like this:

 

image

I have used a variable, rather than labels, for the data decoding. The variable is defined, globally, outside of the main code block and initialised to 0.


Once the incoming data string has been captured  to the variable "incomingData", it is tested to make sure it is numeric - it will be, most of the time, but there are sometimes glitches in the serial comms or the Bluetooth, which can result in run-time errors.

 

Assuming that "incomingData"  is numeric, it is tested for size: less than 1024 is displayed in Label2; between 2000 and 3023, subtract 2000 and the result is dispalyed in Label3.


Leaving in the test for <= 3023 allows for easy expansion of this solution to more than 2 data items


When I looked at kennethbuenaventura's problem in more depth I appreciated that there was another way of approaching the problem which more closely matched his requirement.


If the data output to serial on the Arduino includes labels, then the Android app merely needs to identify the label and on the basis of the label, display the data appropriately.


If the Arduino sends something like:


              LED = 135                    assume each data string is followed by CR/LF

              LDR = 245

              LED = 178

              LDR = 465          etc


Then all we need to do in the Android app is look for the sub-strings "LED" and "LDR" within each incoming data string. We can then display the whole data string as text on the appropriate label


Here is a simple Arduino sketch to simulate the suggested serial output:


// Simulate 2 labelled data items and output to serial for Android demonstration

const int pot = A0;

void setup() {
  pinMode(pot, INPUT);
  Serial.begin(9600);
}

void loop() {
  int potVal1 = analogRead(pot);
  int potVal2 = potVal1/2;          // just to give a different value
  Serial.print("LED State = " );
  Serial.println(potVal1);
  delay(600);       
  Serial.print("LDR Value = " );
  Serial.println(potVal2); 
  delay(600);             
}



Here is the screen design from App inventor and below that are the code blocks  to process the data strings:

image


image

Again, the incoming data string is captured to the variable, which is initialised to "0", but this time we are handling it as text and so there is no need to test to see if it is a number - indeed, it isn't!!

 

The data string is first tested to see if it contains the sub-string "LED". If so, it is displayed in Label2 and processing ceases. If "LED" is NOT found (else if), then the data string is tested for "LDR", and if found, displayed in Label3. Processing then ceases.

 

I hope that both of these examples are clear.


I have attached the .aia files to load into App Inventor for both of the example Arduino apps

Attachments:
DatafromArduino2.zip
  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 8 years ago +1
    thank you very much i solved my problem thanks
  • Former Member
    Former Member over 7 years ago in reply to neilk +1
    Ya Thnku
  • Former Member
    Former Member over 7 years ago in reply to neilk +1
    Thank You so much for replying, it helps a lot.. my main issue of not getting some data from the analog pins because i was using android version 4.0, the recommended for mit app inv2 is 4.4, hehehehehehe…
  • xxkao
    xxkao over 6 years ago in reply to neilk

    is it already posted? i have the same project like this but i cant get what value im getting from the bluetooth. thank you!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 6 years ago

    Now that I have looked at your I can see that you are sending a lot of data to the serial monitor and also accepting keyboard commands.

     

    To transfer ALL of that data via Bluetooth and display on an Android device using AppInventor could become a significant, but far from impossible, project.

     

    A very simple approach would be to connect your HC-05 to the Rx & Tx pins of the Arduino , remembering to use a potential divider on the Arduino Tx to HC-05 Rx line, to drop the voltage seen by the HC-05 input to about 3V.

     

    It would then be very straight forward to read the Bluetooth input on AppInventor and display each data set received from the Arduino by setting the text value of a label. Of course, you should still see the same data displayed in the Serial Monitor.

     

    See other comments in this blog and in my other blogs on Arduino/AppInventor data transfer via Bluetooth and make sure that you are confident that you have Bluetooth working by reproducing one of the simple examples I have used..

     

    However, if you want to interpret the data within AppInventor and do something with it, the task becomes quite a bit more complicated.

     

    Can I suggest that you "have a go" and see how far you can get. If you get completely stuck, then it would be a good idea to post what you have done as a NEW question in the Arduino group.

     

    Hope this helps

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jhek2017
    jhek2017 over 6 years ago in reply to neilk

    I was thinking how can i transfer the result from arduino to mit app. Or my other option is to create a serial monitor application in bluetooth wherein I can display the results

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jhek2017
    jhek2017 over 6 years ago in reply to neilk

    //Bluetooth (HC-05 JY-MCU) State pin on pin 5 of Arduino
      const int BTState = 5;
    //tcs3200 pins connected to arduino
    const int s0 = 8;  
    const int s1 = 9; 
    const int s2 = 12;  
    const int s3 = 11;  
    const int out = 10;
    
    
    //LED sensor  
     const int lights  = 13;
    
     //sensor variable
     int i=0;
      int j=0;
      int state;
       
    // LED pins connected to Arduino
    int orangeLed = 2;  
    int greenLed = 3;  
    int blueLed = 4;
    
    
    
    
    // Variables  
    int orange = 0;  
    int green = 0;  
    int blue = 0;  
    int pink = 0;
    
    
    
    
    
    
      
    
    
    void setup()   
    {
    
    
      pinMode(lights, OUTPUT);
      pinMode(BTState, INPUT);
    //------------------------sensor---------------------------  
      Serial.begin(9600); 
      pinMode(s0, OUTPUT);  
      pinMode(s1, OUTPUT);  
      pinMode(s2, OUTPUT);  
      pinMode(s3, OUTPUT);  
      pinMode(out, INPUT);  
      pinMode(orangeLed, OUTPUT);  
      pinMode(greenLed, OUTPUT);  
      pinMode(blueLed, OUTPUT);  
      digitalWrite(s0, HIGH);  
      digitalWrite(s1, HIGH);  
    
    
    
    
    }  
        
    void loop() 
    {  
      if(Serial.available() > 0){     
          state = Serial.read();   
        }
    
    
         /************************Lights sensor*****************************/
      //If state is equal with letter 'W', turn leds on or of off
        else if (state == 'W') {
          if (i==0){  
             digitalWrite(lights, HIGH); 
             i=1;
          }
          else if (i==1){
             digitalWrite(lights, LOW); 
             i=0;
          }
          state='n';
        }
      //----------------------------sensor-----------
      color(); 
    
    
      Serial.print(" Intensity : ");  
      Serial.print(orange, DEC);  
      //Serial.println();  
    
    
      if (orange < blue && orange < green && orange < 20)
      {  
       Serial.println(" - (orange Color)K TEST = ADEQUATE");  
       digitalWrite(orangeLed, HIGH); // Turn RED LED ON 
       digitalWrite(greenLed, LOW);  
       digitalWrite(blueLed, LOW);  
      }
    
    
       if (blue < orange && blue < green)   
      {  
       Serial.println(" - (Blue Color)P TEST = SUFFICIENT!!");  
       digitalWrite(orangeLed, LOW);  
       digitalWrite(greenLed, LOW);  
       digitalWrite(blueLed, HIGH); // Turn BLUE LED ON  
      }  
    
    
      else if(pink <green && blue < orange){
           Serial.println(" - (Pink Color) N TEST = SURPLUS!!");  
       digitalWrite(orangeLed, HIGH);// Turn PINK LED ON  
       digitalWrite(greenLed, LOW);  
       digitalWrite(blueLed, LOW);
      }
    
    
      else if (green < orange && green < blue)  
      {  
       Serial.println(" - (Green Color)Ph value not available!");  
       digitalWrite(orangeLed, LOW);  
       digitalWrite(greenLed, HIGH); // Turn GREEN LED ON 
       digitalWrite(blueLed, LOW);  
      }  
    
      
      else{
      Serial.println();  
      }
      delay(500);   
      digitalWrite(orangeLed, LOW);  
      digitalWrite(greenLed, LOW);  
      digitalWrite(blueLed, LOW);
      
    
    
    }   
    void color()  
    {    
      digitalWrite(s2, LOW);  
      digitalWrite(s3, LOW);  
      //count OUT, porange, orange 
     orange = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
      digitalWrite(s3, HIGH);  
      //count OUT, pBLUE, BLUE  
      blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
      digitalWrite(s2, HIGH);  
      //count OUT, pGreen, GREEN  
      green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
       //count OUT, pPink, PINK  
      pink = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 6 years ago in reply to jhek2017

    So far so good with the schematic; unfortunately the link does not take me to your Arduino sketch.....it is a link back to email.

     

    If you press the >> at the top right of the reply window, then Syntax Highlighting and then C++, it will open a sub window into which you can paste your code.

    • 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 © 2023 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube