Skip to first unread message

Angel Zirnovalis

unread,
Aug 9, 2018, 7:11:58 PM8/9/18
to MIT App Inventor Forum
Hi,
 i am trying to send the coordinates of a Map Marker in MIT App Invetor to arduino using BT connection.
 Multiplying the float to get an integer does not solve the problem, because of the many descimals. 
I can't thik of any other way to do it, so im searching here for help.

Chris Ward

unread,
Aug 9, 2018, 8:09:39 PM8/9/18
to MIT App Inventor Forum
Hello Angel

Why can't you send the coordinates as floats (or maybe as a string of chars)? I know Arduino does not perform floating point math natively, but you presumably are not going to perform any math on the coordinates?

Angel Zir

unread,
Aug 10, 2018, 2:34:25 AM8/10/18
to MIT App Inventor Forum
The error i am getting is: Error 510: Could not decode "40.979898" as an integer. 
Any example would be much appreciated, because my programming skills are beginner-ish, and my english is not very good neither...
At the arduino side, i will use them as integers (eg 40979898). 

Juan Antonio

unread,
Aug 10, 2018, 6:14:43 AM8/10/18
to MIT App Inventor Forum
As Chris wrote, you can send the coordinates as String and Arduino convert to Float or Int

Chris Ward

unread,
Aug 10, 2018, 6:54:51 AM8/10/18
to mitappinv...@googlegroups.com
Hi Angel

The App Inventor side. You obviously don't need a calculator-stye GUI, this is just an easy way to check the conversion code works:

Convert Float GUI.png  


Convert Float Blocks.png



FloatConversion.aia

Angel Zir

unread,
Aug 10, 2018, 9:03:21 AM8/10/18
to MIT App Inventor Forum
Thank you for your fast reply, The conversion code sure works. And on top of that, i learned something about the building blocks you used.
But i can't use the map marker value in the initialization. Maybee i have to duplicate somehow the marker value and use it in the initialization.

Chris Ward

unread,
Aug 10, 2018, 9:37:06 AM8/10/18
to MIT App Inventor Forum
Hi Angel

In the real world, the Button_Convert Block could be a Clock Timer Block, depending on how and how often data is sent to the Arduino. The float value would be in that Block too.

Chris Ward

unread,
Aug 10, 2018, 10:10:02 AM8/10/18
to MIT App Inventor Forum
...you will I think need the App Inventor BLE extension:

Message has been deleted

Chris Ward

unread,
Aug 10, 2018, 10:48:19 AM8/10/18
to MIT App Inventor Forum
Hi Angel

I have not used Bluetooth with Arduino - I'm sure one of the experts here such as Juan or ABG will step in to help more if you have specific questions.

Code below is to show you what I mean about a Clock Timer Block, but the BT stuff is incomplete and of course of no use at all if you actually need BLE. Timer intervals are important, if you send data too fast Arduino is not going to like you.......

So, the attached Project file is untested but might help a little - experiment!

Values to Ardunino via BT.png



There is very likely to be some useful videos on YouTube too - a lot of enthusiasts for App Inventor and Arduino.




CordinatesToArduino.aia

Angel Zir

unread,
Aug 10, 2018, 12:06:27 PM8/10/18
to mitappinv...@googlegroups.com
Thanks to your help, i have done it. The do it result is exactly what i was expecting. Now i have to transfer the number to arduino via BT. 
I think i have to use a long, to store the number to arduino. By doing it, i get a Serial.print of
1
115
233
246

for the do it number 41150966. Now i am lost again. 
I will read more tutorials about app inventor and arduino and i hope i will mannage to get it right. Thank you very much for your time and effort.

Juan Antonio

unread,
Aug 10, 2018, 1:53:18 PM8/10/18
to MIT App Inventor Forum
You can send Text:

bluetooth_lat_lon2.png

Arduino get String and convert to Float

// Juan Antonio Villalpando
// kio4.com

char caracter;
String datos;
String latitud;
String longitud;
float latitud_f;
float longitud_f;

int k1;
int k2;

void setup()
{
  Serial.begin(9600);
}

void loop() {
  
  if(Serial.available())
  {
    caracter = Serial.read();  
    datos = datos + caracter; 

if (caracter == '*') {

      Serial.println(datos);
      k1 = datos.indexOf(',');
      latitud = datos.substring(0, k1);
      k2 = datos.indexOf(',', k1+1);
      longitud = datos.substring(k1+1, k2);
      
      Serial.print("Latitud = ");
      Serial.println(latitud); 
      Serial.print("Longitud = ");
      Serial.println(longitud);
      datos = "";
      latitud_f = latitud.toFloat();
      longitud_f = longitud.toFloat();
      Serial.println(latitud_f,6); // 6 decimals
      Serial.println(longitud_f,6); // 6 decimals
  }
      }
}
Regards.
p9J3_latitud_longitud.aia

Angel Zir

unread,
Aug 10, 2018, 2:45:31 PM8/10/18
to MIT App Inventor Forum
Thank you all for all your help, but im am realy stuck. It is realy almost the last parameter of my app, and i can't finish it...  If someone can add the building blocks and code to arduino it would be very much help for me. As you see i am using the cmd/param method for my other button actions, so it is realy difficult for me to put somewhere the code examples you sudgest.
Enter code here...
#include "MSP.h"


byte cmd;  // Stores the next byte of incoming data, which is a "command" to do something 
byte param; // Stores the 2nd byte, which is the command parameter



#define AUX1P 3        
#define AUX2P 4 
#define AUX3P 5
#define AUX4P 6
uint16_t AUX1=1000;
uint16_t AUX2=1000;
uint16_t AUX3=1000;
uint16_t AUX4=1000;
uint16_t SIGN[8]={0,0,0,0};
//uint16_t AUX[4]={1000,1000,1000,1000};
uint16_t ROLL=1500, PITCH=1500, TH=1000, YAW=1500;
uint16_t aT=1000;
uint16_t aR=1000;
uint16_t aP=1000;
uint16_t aY=1000;

MSP msp;


void setup(){


  Serial.begin(115200);
  Serial1.begin(115200);   //MSP
  Serial3.begin(57600);   //BT
  msp.begin(Serial1);

pinMode(AUX1P, INPUT);
pinMode(AUX2P, INPUT);
pinMode(AUX3P, INPUT);
pinMode(AUX4P, INPUT);

digitalWrite(AUX1P, LOW );
digitalWrite(AUX2P, LOW );
digitalWrite(AUX3P, LOW );
digitalWrite(AUX4P, LOW );

}

void loop(){


if ( Serial3.available() )       // if data is available to read
  {
    cmd = Serial3.read();         // read it and store it in 'cmd'
    //Serial.println(cmd);
    //delay(20);
    // Data format is byte 1 = command, byte 2 = parameter
  };
  
  switch ( cmd ) {
    case 1:
      // First byte contains a generic "command" byte. We arbitrarily defined '1' as the command to then check the 2nd parameter byte
      // User can additional commands by adding case 2, 3, 4, etc
      {
         // read the parameter byte
         param = Serial3.read();
         
         switch (param)
         {
              case 1: 
              break;
             
              case 2:
                      
              break;
              case 3:             

              break;
              case 4:
              
              break;
              case 5:
              
              break;

              case 6:
              
              break;

              case 7:
              digitalWrite(AUX1P, HIGH );
              //delay(20);
              break;
              
              case 8:
              digitalWrite(AUX1P, 0 );
              //delay(20);
              break;
              
              case 9:
              digitalWrite(AUX2P, HIGH );
              //delay(20);
              break;
              
              case 10:
              digitalWrite(AUX2P, 0 );
             // delay(20);
              break;
              
              case 11:
              digitalWrite(AUX3P, HIGH );
              //delay(20);
              break;
              
              case 12:
              digitalWrite(AUX3P, 0 );
              //delay(20);
              break;
              
              case 13:
              digitalWrite(AUX4P, HIGH );
              //delay(20);
              break;
              
              case 14:
              digitalWrite(AUX4P, 0 );
              //delay(20);
              break;
           default: break; // do nothing
         } // switch (param)
      } // switch (cmd) case 1
      default: break; // do nothing
  } // switch (cmd)
   

  Serial3.print(rawGps.numSat);
  Serial3.print("|");
  Serial3.print(Comp.distanceToHome);
  Serial3.print("|");  
  Serial3.print(rawGps.groundSpeed*0.036, 1);
  Serial3.print("|");
  Serial3.print(rawGps.lat*0.0000001, 7); 
  Serial3.print("|");
  Serial3.print(rawGps.lon*0.0000001, 7);
  delay(50);

  }

   
  
   



 
}







  
  


varkaki108theleidouleiaallaok_checkpoint1.aia

Angel Zir

unread,
Aug 10, 2018, 2:47:50 PM8/10/18
to MIT App Inventor Forum
and here is the picture of my blocks

blocks (1).png

Angel Zir

unread,
Aug 11, 2018, 11:18:46 AM8/11/18
to mitappinv...@googlegroups.com





void targetcoordinates(){

if(Serial3.available())
  {
    caracter = Serial3.read();  
    //Serial.println(caracter);
    datos = datos + caracter; 

if (caracter == '*') {

      Serial.println(datos);
      k1 = datos.indexOf(',');
      latitud = datos.substring(0, k1);
      k2 = datos.indexOf(',', k1+1);
      longitud = datos.substring(k1+1, k2);
      
      Serial.print("Latitud = ");
      Serial.println(latitud); 
      Serial.print("Longitud = ");
      Serial.println(longitud);
      datos = "";
      latitud_f = latitud.toFloat();
      longitud_f = longitud.toFloat();
      Serial.println(latitud_f,6); // 6 decimals
      Serial.println(longitud_f,6); // 6 decimals
    //  digitalWrite(30, LOW);
      //Serial.println(digitalRead(30));
      //void loop();
  }
      }
}
   

i used the above code from Antonio in Arduino.
My do it result is

Do It Result: 40575022,23942871,*
---
 and my Serial monitor shows

4552,3481*
Latitud = 4552
Longitud = 3481*
4552.000000
3481.000000

The string is that arrives is not the same that is sent..




i removed the decimal point from the string, should i change something in the arduino code?

Angel Zir

unread,
Aug 11, 2018, 1:02:56 PM8/11/18
to MIT App Inventor Forum
and i just found the patern, but i cant explain it

Do It Result:     40575022,23942871,*
string arrived:   4  5  5  2  ,  3  4  8  1*
i have a missing character every second character!

Juan Antonio

unread,
Aug 11, 2018, 5:14:25 PM8/11/18
to MIT App Inventor Forum
Try:
BluetoothClient1.SendText: 36.12345,-6.98765,*

Arduino
Serial.begin(9600);
Does this application work for you? 

p9J3_latitud_longitud.aia
2 KB     Descargar

Angel Zir

unread,
Aug 12, 2018, 5:54:02 PM8/12/18
to MIT App Inventor Forum
The problem was that i had 2 x if(Serial3.available()) in my loop. I think that explains the missing part.
I had to reconfigure the whole bt send. I used your method, and insted of only the '*' char, i am using a few more for my Arduino pins.
After that, i am getting my coordinates right.
I am using 57600 for my BT serial, because i cant get it into AT mode anymore.
I want to thank you all for your kind help.
Without it, i would never find the solution to my problem.
Reply all
Reply to author
Forward
0 new messages