I'm working on an app to make a remote-controlled outlet with an HC-05 module, Arduino, and solid-state relay. As a prerequisite, I wanted to make sure I could turn on and off the onboard LED on the Arduino.
I started out following this tutorial:
https://projecthub.arduino.cc/mukeshkp2005/arduino-with-bluetooth-to-control-an-led-a7ad0a
I liked that tutorial because it actually has you making the app instead of downloading one that someone had already made (see "The App" section in above link).
I was able to re-create that app, but when I went into the device listpicker once I built and installed the app, the list of devices came up blank. I figured it was a permissions issue and I found this post, which I incorporated some of the blocks from into my app:
After making that update, it asked me for permission when I opened up the app:
and it shows the list of devices when I click on the listpicker button and I can select my HC-05 module:
When I click on that module address / name, it went back to the main screen, but the ON/OFF buttons had no effect on the on-board LED on the Arduino. I wanted to make sure the module was actually connected in the app, so I tried to add a section to the main screen where it displays the connected device (with a label that says "Connected To:" and another directly below it that is supposed to get populated with the device address (which doesn't show up on the screen preview unless you select it in the Components pane, but it is there)):
When I tried to run this version of the app, it gives me a 503 error after selecting the device from the listpicker:
Is there something wrong I'm doing with my blocks? Here's what they currently look like:
The phone I'm writing this app for is running Android version 12. Before running the app, I make sure the HC-05 module is on and paired to my phone (in my phone's bluetooth settings).
Here's my Arduino code if that's relevant at all:
// Use HC-05 bluetooth module to control onboard LED
// D. Botos
// started 8-18-23 last updated 8-18-23
// see https://projecthub.arduino.cc/mukeshkp2005/arduino-with-bluetooth-to-control-an-led-a7ad0a
// and https://docs.arduino.cc/tutorials/communication/SoftwareSerialExample
// and Blink example (File -> Examples -> 01.Basics -> Blink
// ----- ----- ----- -----
#include <SoftwareSerial.h>
// create SoftwareSerial object
SoftwareSerial BTSerial(3, 2); // Rx to Arduino | Tx from Arduino - see https://docs.arduino.cc/learn/built-in-libraries/software-serial for syntax
// create switchstate variable (has to be char, not int)
char switchstate;
// ----- ----- ----- -----
// setup runs once
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// start serial for bluetooth
BTSerial.begin(38400); // HC-05 default speed
}
// ----- ----- ----- -----
// loop runs repeatedly
void loop() {
while(BTSerial.available()>0){
switchstate = BTSerial.read(); // Serial.read() is to read the value coming from app
if(switchstate == '1'){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
}
else if(switchstate == '0'){
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
}
} // end of while loop
} // end of void loop
// end of sketch
Thanks,
David