Data Not Transferring Via Bluetooth

Hello. I hope someone can help.
I have an ESP32 connected to an IR sensor. It is coded to connect via bluetooth, which it does using the Serial Bluetooth Terminal app. It is also coded to count items that go by the IR sensor on a conveyor belt, which it does. The data (item count) shows up on the Arduino IDE program on the laptop when the ESP32 is connected via USB. And I can communicate text between the Arduino IDE and the Serial Bluetooth Terminal using the ESP32's bluetooth connection, so I know the 2 way communication via bluetooth is working. But I cannot get the IR sensor count to show up on the Serial Bluetooth Terminal.

Any help would be appreciated. TIA

Here is the code:

#include "BluetoothSerial.h"


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


BluetoothSerial SerialBT;


// Pin configuration
const int sensorPin = 2;  // Connect the infrared sensor's output pin to Arduino pin 2


// Variables
int objectCount = 0;      // Counter to keep track of the number of objects
int sensorState = 0;      // Current state of the sensor (HIGH or LOW)
int lastSensorState = 0;  // Previous state of the sensor


void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(sensorPin, INPUT);  // Set sensor pin as input
Serial.begin(9600);         // Initialize serial communication for debugging
}


void loop() {
  sensorState = digitalRead(sensorPin);  // Read the state of the sensor


  // Check if the sensor state has changed (object detected)
  if (sensorState != lastSensorState) {
    if (sensorState == HIGH) {
      objectCount++;  // Increment the object count when an object is detected
      Serial.print("Object detected! Count: ");
      Serial.println(objectCount);
    }


    // Delay to debounce the sensor (avoid multiple counts for a single object)
    delay(50);
  }


  lastSensorState = sensorState;  // Update the last sensor state for the next iteration


if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}

Please edit your post, select all code and click the <CODE/> button. Next save your post.

This will apply code tags to your code which make it easier to read, easier to copy and the forum software will display it correctly.


Because you nowhere are printing the objectCount over SerialBT; you only print it over Serial.

Hello. Thank you for your reply.
I tried adding BT to make it SerialBT instead of just Serial, but it wouldn't work.

I do not know anything about coding and appear to be stuck.

Please post your updated code in a new reply; don't forget the code tags :wink:

When I edited the post, it made a new post. I'll try again here.

#include "BluetoothSerial.h"


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


BluetoothSerial SerialBT;


// Pin configuration
const int sensorPin = 2;  // Connect the infrared sensor's output pin to Arduino pin 2


// Variables
int objectCount = 0;      // Counter to keep track of the number of objects
int sensorState = 0;      // Current state of the sensor (HIGH or LOW)
int lastSensorState = 0;  // Previous state of the sensor


void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(sensorPin, INPUT);  // Set sensor pin as input
Serial.begin(9600);         // Initialize serial communication for debugging
}


void loop() {
  sensorState = digitalRead(sensorPin);  // Read the state of the sensor


  // Check if the sensor state has changed (object detected)
  if (sensorState != lastSensorState) {
    if (sensorState == HIGH) {
      objectCount++;  // Increment the object count when an object is detected
      Serial.print("Object detected! Count: ");
      Serial.println(objectCount);
    }


    // Delay to debounce the sensor (avoid multiple counts for a single object)
    delay(50);
  }


  lastSensorState = sensorState;  // Update the last sensor state for the next iteration


if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}

Please indicate where in the code of post #5 you try to send the objectCount over SerialBT.

I'm guessing that I don't? I do not know how to code. I have to do this for a final project in an online class, but they never covered how to write code, so I'm a little lost.

I used 2 existing codes from the library and merged them through a bunch of trial and error. If something is missing or in the wrong order, I don't know what, or where it should go. Any guidance would be greatly appreciated. Thank you.

I don't see where.

What did they cover?

Absolutely nothing. That's the problem.

So what course did you follow?


This is a hint. Just like Serial has print() and println() methods, SerialBT also has them. So where you print()/println() the count to Serial, you can also add print()/println() statements to send the count to SerialBT.

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