Send data from Arduino to Android

I am working on a device that will send data from Arduino to Android and not sure what is the best way of doing such task. Is this the right way or there is some better/more efficient approach?

    void setup() 
    {
      Serial.begin(115200);  
    }
    void loop()
    {
      float tachometerValue = analogRead(A0);
      float voltageValue = analogRead(A2);

      int value1 = 10;
      int value2 = 20;
    
      tachometerValue = ((tachometerValue / 10) * 10) * (10.0 / 1023.0);

      float vout = (voltageValue * 5.00) / 1024.00;
      float voltValue = vout / (1000.00 / (10000.00 + 1000.00));
      
      if(voltValue < 0.09)
      {
        voltValue = 0.00;
      } 
    
      char buffer[50];
    
      char voltValue_temp[5];
      char tachometerValue_temp[5];

      dtostrf(voltValue, 4, 1, voltValue_temp);
      dtostrf(tachometerValue, 4, 1, tachometerValue_temp);
      
      sprintf(buffer, "%d;%s;%s;%d", value1, tachometerValue_temp, voltValue_temp, value2);
      
      Serial.print(buffer); 
    }

I am using Xamarin forms and Bluetooth to receive data on the other end.

I am getting data in this format for some reason sometimes:

;2.17;0.00;06;2.17;0

This is not what has been sent.
Here is what I see in my PC serial otput:

10;9.64;0.00;20
10;9.64;0.00;20
10;9.64;0.00;20
10;9.64;0.00;20
10;9.65;0.00;20
10;9.64;0.00;20

Please, post complete skecth.

1 Like

I doubt you are seeing that with the code you posted. Serial.print does not add a carriage return or newline at the end of the text, so everything should be on a single line. It is also very odd that you are specifying a single digit after the decimal point in dtostrf but the output is producing two digits.

There really is no need to use dtostrf and sprintf, a series of print statements would work just as well.

1 Like

You are right, I just added Serial.print('\n'); just for convenience in console output. To see the result more clear.

There really is no need to use dtostrf and sprintf, a series of print statements would work just as well.

How do I separate them for example with ; ?

Serial.print(value1);
Serial.print(';');
Serial.print(tachometerValue);
Serial.print(';');
Serial.print(voltValue);
Serial.print(';');
Serial.print(value2);
Serial.println(); //Serial.print('\n');
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.