Skip to content

Instantly share code, notes, and snippets.

@maximgladkov
Last active June 27, 2018 14:39
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 maximgladkov/cbd7841377d4f17b0c92463972a556cc to your computer and use it in GitHub Desktop.
Save maximgladkov/cbd7841377d4f17b0c92463972a556cc to your computer and use it in GitHub Desktop.
#include <ParticleSoftSerial.h>
#define SENDER Serial1
#define RECEIVER SoftSer
#define PROTOCOL SERIAL_8N1
const uint32_t baud = 19200;
#if (SYSTEM_VERSION >= 0x00060000)
SerialLogHandler logHandler;
#endif
#define PSS_RX A1
#define PSS_TX A0
ParticleSoftSerial SoftSer(PSS_RX, PSS_TX);
#define MASTER 1
char recv_str[100];
void setup()
{
Serial.begin(115200);
SENDER.begin(baud, PROTOCOL);
RECEIVER.begin(baud, PROTOCOL);
Serial.println("\r\nPower on!!");
if(setupBlueToothConnection() != 0) while(1) { Particle.process(); };
//this block is waiting for connection was established.
while(1)
{
if(recvMsg(1000) == 0)
{
if(strcmp((char *)recv_str, (char *)"OK+CONB") == 0)
{
Serial.println("connected\r\n");
break;
}
}
Particle.process();
delay(200);
}
}
void loop()
{
#if MASTER //central role
//in master mode, the bluetooth send message periodically.
delay(400);
Serial.println("send: hi");
sendBlueToothCommand("hi");
delay(100);
//get any message to print
if(recvMsg(1000) == 0)
{
Serial.print("recv: ");
Serial.print((char *)recv_str);
Serial.println("");
}
#else //peripheral role
delay(200);
//the slave role only send message when received one.
if(recvMsg(1000) == 0)
{
Serial.print("recv: ");
Serial.print((char *)recv_str);
Serial.println("");
Serial.println("send: hello");
sendBlueToothCommand("hello");
}
#endif
}
//used for compare two string, return 0 if one equals to each other
int strcmp(char *a, char *b)
{
unsigned int ptr = 0;
while(a[ptr] != '\0')
{
if(a[ptr] != b[ptr]) return -1;
ptr++;
}
return 0;
}
//configure the Bluetooth through AT commands
int setupBlueToothConnection()
{
#if MASTER
Serial.println("this is MASTER\r\n");
#else
Serial.println("this is SLAVE\r\n");
#endif
Serial.print("Setting up Bluetooth link\r\n");
delay(3500);//wait for module restart
//send command to module in different baud rate
// while(1)
// {
// delay(500);
// SENDER.begin(9600, PROTOCOL);
// // RECEIVER.begin(9600, PROTOCOL);
// delay(500);
// Serial.print("try 9600\r\n");
// if(sendBlueToothCommand("AT") == 0)
// break;
// delay(500);
// SENDER.begin(57600, PROTOCOL);
// //RECEIVER.begin(57600, PROTOCOL);
// delay(500);
// Serial.print("try 57600 (old 115200)\r\n");
// if(sendBlueToothCommand("AT") == 0)
// break;
// Particle.process();
// }
//we have to set the baud rate to 9600, since the soft serial is not stable at 115200
sendBlueToothCommand("AT+RENEW");//restore factory configurations
sendBlueToothCommand("AT+BAUD2");//reset the module's baud rate
sendBlueToothCommand("AT+AUTH1");//enable authentication
sendBlueToothCommand("AT+RESET");//restart module to take effect
// blueToothSerial.begin(9600);//reset the Arduino's baud rate
delay(3500);//wait for module restart
//configure parameters of the module
sendBlueToothCommand("AT+VERS?");//get firmware version
sendBlueToothCommand("AT+ADDE?");//get EDR MAC
sendBlueToothCommand("AT+ADDB?");//get BLE MAC
sendBlueToothCommand("AT+NAMEHM-13-EDR");//set EDR name
sendBlueToothCommand("AT+NAMBHM-13-BLE");//set BLE name
sendBlueToothCommand("AT+PINE123451");//set EDR password
sendBlueToothCommand("AT+PINB123451");//set BLE password
sendBlueToothCommand("AT+SCAN0");//set module visible
sendBlueToothCommand("AT+NOTI1");//enable connect notifications
//sendBlueToothCommand("AT+NOTP1");//enable address notifications
sendBlueToothCommand("AT+PIO01");//enable key function
#if MASTER
sendBlueToothCommand("AT+ROLB1");//set to master mode
#else
sendBlueToothCommand("AT+ROLB0");//set to slave mode
#endif
sendBlueToothCommand("AT+RESET");//restart module to take effect
delay(3500);//wait for module restart
if(sendBlueToothCommand("AT") != 0) return -1;//detect if the module exists
Serial.print("Setup complete\r\n\r\n");
return 0;
}
//send command to Bluetooth and return if there is a response
int sendBlueToothCommand(char command[])
{
int len = 0;
Serial.print("send: ");
Serial.print(command);
Serial.println("");
len = strlen(command) + 1;
SENDER.write((uint8_t*)command, len);
delay(200);
if(recvMsg(200) != 0) return -1;
Serial.print("recv: ");
Serial.print(recv_str);
Serial.println("");
return 0;
}
//receive message from Bluetooth with time out
int recvMsg(unsigned int timeout)
{
//wait for feedback
unsigned int time = 0;
unsigned char num;
unsigned char i;
//waiting for the first character with time out
i = 0;
while(1)
{
delay(50);
if(RECEIVER.available())
{
recv_str[i] = char(RECEIVER.read());
i++;
break;
}
time++;
if(time > (timeout / 50)) return -1;
}
//read other characters from uart buffer to string
while(RECEIVER.available() && (i < 100))
{
recv_str[i] = char(RECEIVER.read());
i++;
}
recv_str[i] = '\0';
Serial.println(recv_str);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment