Bluetooth not sending integers correctly?

Hi all,

I'm trying to use a esp32 to control a pump and solenoid via an app on my phone. The arduino serial monitor is receiving data but the app seems to be sending the wrong data. For example to turn the pump on I expect to receive "11" but the serial monitor reads 49.
I am following this tutorial https://www.youtube.com/watch?v=aM2ktMKAunw&ab_channel=MoThunderz

My esp32 code is as follows:


#include <BluetoothSerial.h>
#define pump 13
#define solenoid 12
BluetoothSerial SerialBT;
int incoming;

void setup() {
  Serial.begin(115200);
  pinMode(pump, OUTPUT);
  pinMode(solenoid, OUTPUT);
  btStart();
  Serial.println("Bluetooth On");
  SerialBT.begin("ESP32_BT");  //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  delay(5000);
}

void loop() {
  if (SerialBT.available()) {

    incoming = SerialBT.read();  //Read what we receive
    Serial.println(incoming);
    // separate button ID from button value -> button ID is 10, 20, 30, etc, value is 1 or 0
    int button = floor(incoming / 10);
    int value = incoming % 10;

    switch (button) {
      case 1:
        Serial.print("Button 1:");
        Serial.println(value);
        digitalWrite(pump, value);
        break;
      case 2:
        Serial.print("Button 2:");
        Serial.println(value);
        digitalWrite(solenoid, value);
        break;
      case 3:
        Serial.print("Button 3:");
        Serial.println(value);
        digitalWrite(pump, value);
        digitalWrite(solenoid, value);
        break;
    }
  }
}

Here are my app blocks

And here is what the serial monitor reads
19:23:46.856 -> Bluetooth On

19:23:47.220 -> The device started, now you can pair it with bluetooth!

19:23:59.871 -> 51

19:23:59.871 -> 49

19:24:05.554 -> 51

19:24:05.554 -> 48

19:24:06.953 -> 51

19:24:06.953 -> 49

19:24:09.139 -> 51

19:24:09.139 -> 48

19:24:10.380 -> 51

19:24:10.380 -> 49

19:24:11.483 -> 51

19:24:11.483 -> 48

You send data as text, and when you receive that data you store it in an integer variable. So when you receive 11 you don't actually get 49, you get 49 49 which is the ascii representation of 11. Use SerialBT.readString(), or send data using send1ByteNumber block.

Cheers brother you're a legend.
I thought it was reading it out as ascII but couldn't figure out how.

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