Created
January 19, 2022 13:03
-
-
Save anuzcan/b98b20abc0e9fcc0edce54fb7de871df to your computer and use it in GitHub Desktop.
JDY-30 bluetooth Module Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(3, 4); // RX, TX | |
void setup() { | |
// put your setup code here, to run once: | |
mySerial.begin(9600); | |
Serial.begin(9600); | |
pinMode(LED_BUILTIN, OUTPUT); | |
digitalWrite(LED_BUILTIN, LOW); | |
sendCommand("AT"); | |
//sendCommand("AT+HELP"); | |
//sendCommand("AT+ROLE0"); | |
sendCommand("AT+NAMEgisbluetooh"); | |
} | |
void sendCommand(const char * command){ | |
Serial.print("Command send :"); | |
Serial.println(command); | |
mySerial.println(command); | |
//wait some time | |
delay(100); | |
char reply[50]; | |
int i = 0; | |
while (mySerial.available()) { | |
reply[i] = mySerial.read(); | |
i += 1; | |
} | |
//end the string | |
reply[i] = '\0'; | |
Serial.print(reply); | |
Serial.println("Reply end"); | |
} | |
void readSerial(){ | |
char reply[50]; | |
int i = 0; | |
while (mySerial.available()) { | |
reply[i] = mySerial.read(); | |
i += 1; | |
} | |
//end the string | |
reply[i] = '\0'; | |
if(strlen(reply) > 0){ | |
Serial.println(reply); | |
Serial.println("We have just read some data"); | |
int state=reply[0]; | |
if (state == '0') { | |
digitalWrite(LED_BUILTIN, LOW); // Turn LED OFF | |
Serial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON" | |
state = 0; | |
} | |
else if (state == '1') { | |
digitalWrite(LED_BUILTIN, HIGH); | |
Serial.println("LED: ON"); | |
state = 0; | |
} | |
} | |
} | |
void loop() { | |
char reply[50]; | |
int i = 0; | |
if (Serial.available()){ | |
while (Serial.available()) { | |
reply[i] = Serial.read(); | |
i += 1; | |
} | |
//end the string | |
reply[i] = '\0'; | |
sendCommand(reply); | |
} | |
readSerial(); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment