92 lines
1.9 KiB
C++
92 lines
1.9 KiB
C++
// SerialIn_SerialOut_004
|
|
//
|
|
// Uses hardware serial to talk to the host computer and Software
|
|
// Serial for communication with the bluetooth module
|
|
//
|
|
// What ever is entered in the serial monitor is sent to the connected
|
|
// device
|
|
// Anything received from the connected device is copied to the serial
|
|
// monitor
|
|
//
|
|
// Pins
|
|
// BT VCC to Arduino 5V out.
|
|
// BT GND to GND
|
|
// Arduino D8 (SS RX) - BT TX no need voltage divider
|
|
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
|
|
//
|
|
// Setup procedure
|
|
/*
|
|
* AT
|
|
* AT+ORGL
|
|
* // restart
|
|
* AT
|
|
* AT+NAME=Meeting Clock
|
|
* AT+NAME?
|
|
* AT+PSWD="1812"
|
|
* AT+PSWD?
|
|
* AT+UART=9600,1,0
|
|
* AT+UART?
|
|
*/
|
|
|
|
#include <SoftwareSerial.h>
|
|
SoftwareSerial BTserial(5, 4); // RX, TX
|
|
#define STATE_PIN 3
|
|
|
|
char c = ' ';
|
|
boolean NL = true;
|
|
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
Serial.print("Sketch: "); Serial.println(__FILE__);
|
|
Serial.print("Uploaded: "); Serial.println(__DATE__);
|
|
Serial.println(" ");
|
|
|
|
BTserial.begin(38400); // for at mode
|
|
//BTserial.begin(9600); // for echo mode
|
|
Serial.println("BTserial started at 9600");
|
|
Serial.println(" ");
|
|
|
|
pinMode(STATE_PIN, INPUT);
|
|
}
|
|
|
|
bool state = false;
|
|
void loop()
|
|
{
|
|
if (digitalRead(STATE_PIN) != state) {
|
|
state = digitalRead(STATE_PIN);
|
|
Serial.print("! new BT state: ");
|
|
if (state) {
|
|
Serial.println("Connected");
|
|
} else {
|
|
Serial.println("Disconnected");
|
|
}
|
|
}
|
|
|
|
// Read from the Bluetooth module and send to the Arduino Serial
|
|
// Monitor
|
|
if (BTserial.available())
|
|
{
|
|
c = BTserial.read(); Serial.write(c);
|
|
}
|
|
|
|
|
|
// Read from the Serial Monitor and send to the Bluetooth module
|
|
if (Serial.available())
|
|
{
|
|
c = Serial.read();
|
|
BTserial.write(c);
|
|
|
|
// Echo the user input to the main window. The ">" character
|
|
// indicates the user entered text.
|
|
if (NL) {
|
|
Serial.print(">");
|
|
NL = false;
|
|
}
|
|
Serial.write(c);
|
|
if (c == 10) {
|
|
NL = true;
|
|
}
|
|
}
|
|
}
|