Skip to content

Instantly share code, notes, and snippets.

@danielkwood
Last active March 22, 2023 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkwood/86644e5db8c7d083ca630c2ef67ead3a to your computer and use it in GitHub Desktop.
Save danielkwood/86644e5db8c7d083ca630c2ef67ead3a to your computer and use it in GitHub Desktop.
Arduino - receive string commands via Bluetooth
#include <SoftwareSerial.h>
int led = 2;
SoftwareSerial Bluetooth(7,8); // RX, TX
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Bluetooth.begin(9600);
while (!Bluetooth) {
; // wait for Bluetooth to connect
}
Bluetooth.println("Bluetooth connected");
}
void loop() { // run over and over
String command;
if (Bluetooth.available()) {
// Read command from Bluetooth...
command = Bluetooth.readStringUntil('\n');
delay(10);
// Display the command in the serial monitor
Serial.println(command);
if (command.equals("on")) {
Serial.println("LED turned on");
digitalWrite(led, HIGH);
}
else if (command.equals("off")) {
Serial.println("LED turned off");
digitalWrite(led, LOW);
}
}
if (Serial.available()) {
// You can also send back data to Bluetooth module from the Serial Monitor...
Bluetooth.write(Serial.read());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment