Skip to content

Instantly share code, notes, and snippets.

@hunandy14
Last active April 8, 2016 03:18
Show Gist options
  • Save hunandy14/1762cc895eb2f707161a to your computer and use it in GitHub Desktop.
Save hunandy14/1762cc895eb2f707161a to your computer and use it in GitHub Desktop.
Arduino
/*
Blue_ATCommand
DATA:20150423
Ver3:20150425_12
NAME:Charlotte.HonG
*/
#include <SoftwareSerial.h>
#include "Timer.h"
#define BT_RX 2
#define BT_TX 3
#define Vcc 4
#define Key 5
SoftwareSerial BT(BT_TX, BT_RX);
Timer T;
Timer T2;
char str[128]="";
/*===========================================*/
void setup() {
Serial.begin(9600);
Serial.println("Welcome Blueteeth_ATCommand");
Help();
Serial.println("");
Serial.println("Now key is LOW.");
BT.begin(38400);
pinMode(Key, OUTPUT);
pinMode(Vcc, OUTPUT);
PowHigh();
}
void loop() {
T.update();
ScanSerialStr();
BT_Read();
}
/*===========================================*/
void Ctrl(){
if(strncmp(str,"/STA",3) == 0){
Static();
}
if(strncmp(str,"/HELP",4) == 0){
Help();
}
if(strncmp(str,"/ATM",4) == 0){
Serial.println("Enter AT mode");
AT_Mode();
}
if(strncmp(str,"/AL",3) == 0){
Serial.println("Auto link mode");
PowLow();
KeyLow();
T.after(1000,PowHigh);
}
if(strncmp(str,"/RE",3) == 0){
Serial.println("Now vcc reset");
PowLow();
T.after(1000,PowHigh);
}
if(strncmp(str,"/RK",3) == 0){
Serial.println("Now key reset");
KeyLow();
T.after(1000,KeyHigh);
}
if(strncmp(str,"/KL",3) == 0){
Serial.println("Now key is LOW");
KeyLow();
}
if(strncmp(str,"/KH",3) == 0){
Serial.println("Now key is HIGH");
KeyHigh();
}
if(strncmp(str,"/PL",3) == 0){
Serial.println("Now vcc is LOW");
PowLow();
}
if(strncmp(str,"/PH",3) == 0){
Serial.println("NOW vcc is HIGH");
PowHigh();
}
}
void Static(){
Serial.print("Key static = ");
Serial.println(digitalRead(Key));
Serial.print("Vcc static= ");
Serial.println(digitalRead(Vcc));
}
void AT_Mode(){
KeyHigh();
PowLow();
T.after(1000,PowHigh);
}
char val;
void BT_Read(){
if (BT.available()) {
val = BT.read();
Serial.print(val);
}
}
void ScanSerialStr(){
if(Serial.available()) {
int strnum=0;
memset( str, 0, strlen(str) );
while (Serial.available() > 0){
val = Serial.read();
str[strnum++] = val;
if(strncmp(str,"/",1) != 0){
BT.print(val);
}
delay(3);
}
// Serial.print("ScanSerialGet:");
// Serial.println(str);
Ctrl();
}
}
/*===========================================*/
void KeyHigh(){
digitalWrite(Key, HIGH);
}
void KeyLow(){
digitalWrite(Key, LOW);
}
void PowHigh(){
digitalWrite(Vcc, HIGH);
}
void PowLow(){
digitalWrite(Vcc, LOW);
}
void Help(){
Serial.println(" Command:");
Serial.println(" /ATM AT Command Mode");
Serial.println(" /RE Vcc Reset");
Serial.println(" /AL Auto Link Mode");
Serial.println(" /KL Key Power OFF");
Serial.println(" /KH Key Power ON");
Serial.println(" /PL Vcc OFF");
Serial.println(" /PH Vcc ON");
Serial.println(" /HELP Return Help");
Serial.println(" /STA Return Key and Vcc now static");
Serial.println("");
Serial.println(" By:Charlotte.HonG");
}
/*===========================================*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment