Hello, I'm completely new to the arduino world. I have a little project and i want so use BLE to controll a few things. I want to send the numbers 1 to 4 from my phone to the arduino and for every number, the arduino is supposed to put out a PWM signal on one specific output. I have zero knowlege about BLE an tried to write some code by looking up code from others, who did something similar. Here is my code:
The connection with my phone works fine, but as soon as I'm connected, the serial monitor shows that the arduino receives the number "-1" infinite times. If i try to send a number from my phone it doesen't show up on the serial monitor and i also can't measure a current output.
I tried the same using the serial monitor as a control unit and everything works fine. So please help me get this right using BLE.
Thanks for your tips, i managed to figure it out. There are still some minor problems, but it's doing what it's soppused to do. Here is the code, so somone who wants to programm a similar application can look at my results.
#include <ArduinoBLE.h>
#define PIN1 3
#define PIN2 5
#define PIN3 6
#define PIN4 9
#define DAUER_AUSGABE 1500
BLEService myService("1234");
BLECharacteristic rxCharacteristic("5678", BLEWrite | BLEWriteWithoutResponse | BLERead, 20);
void setup() {
Serial.begin(115200);
while (!Serial);
if (!BLE.begin()) {
Serial.println("Failed to initialize BLE!");
while (1);
}
BLE.setLocalName("ESP32-BLE-Receiver");
BLE.setDeviceName("Test");
BLE.setAdvertisedService(myService);
myService.addCharacteristic(rxCharacteristic);
BLE.addService(myService);
BLE.advertise();
Serial.println("BLE Ready.");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to: ");
Serial.println(central.address());
while (central.connected()) {
if (rxCharacteristic.written()) {
int len = rxCharacteristic.valueLength();
char buffer[len + 1];
memcpy(buffer, rxCharacteristic.value(), len);
buffer[len] = '\0';
String receivedData = String(buffer);
Serial.print("Received: ");
Serial.println(receivedData);
int command = receivedData.toInt();
switch (command)
{
case 1:
analogWrite(PIN1, 255);
Serial.println("Pin 1 HIGH");
delay(DAUER_AUSGABE);
analogWrite(PIN1, 0);
Serial.println("Pin 1 LOW");
break;
case 2:
analogWrite(PIN2, 255);
Serial.println("Pin 2 HIGH");
delay(DAUER_AUSGABE);
analogWrite(PIN2, 0);
Serial.println("Pin 2 LOW");
break;
case 3:
analogWrite(PIN3, 255);
Serial.println("Pin 3 HIGH");
delay(DAUER_AUSGABE);
analogWrite(PIN3, 0);
Serial.println("Pin 3 LOW");
break;
case 4:
analogWrite(PIN4, 255);
Serial.println("Pin 4 HIGH");
delay(DAUER_AUSGABE);
analogWrite(PIN4, 0);
Serial.println("Pin 4 LOW");
break;
default:
Serial.println("Ungültige Zahl empfangen.");
break;
}
}
}
Serial.println("Disconnected.");
}
}
I'm glad you got it working. As a side note, you take a very long way around to get to your switch statement. You receive a string like "3", copy it to buffer, convert it to a String object, convert it to an integer and then switch on that.
You could simplify that by just printing out buffer and then switch on the first element of buffer and change your case statements to
case '1':
...
case '2':
...
since there is no need to convert a single digit to a number as your case values can be a character.