Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*\
- |*| HM-10 multiple port serial test
- |*|
- |*| Receives from the two software serial ports connected to HM-10's
- |*| sends to the hardware serial port.
- |*|
- |*| In order to listen on a software port, you call port.listen().
- |*| When using two software serial ports, you have to switch ports
- |*| by listen()ing on each one in turn. Pick a logical time to switch
- |*| ports, like the end of an expected transmission, or when the
- |*| buffer is empty. This example switches ports when there is nothing
- |*| more to read from a port
- |*|
- |*| ++tmw:
- |*| Added 2 HM-10 BlueTooth modules to the project
- |*|
- |*| The circuit:
- |*| Two HM-10 BlueTooth devices and ports which communicate serially are needed.
- |*| * First serial device's TX attached to digital pin 10(RX), RX to pin 11(TX)
- |*| * Second serial device's TX attached to digital pin 8(RX), RX to pin 9(TX)
- |*| * First HM-10 TX pin connected to first SoftSerial RX pin(10), RX to SoftSerial TX pin(11)
- |*| * Second HM-10 TX pin connected to second SoftSerial RX pin(8), RX to SoftSerial TX pin(9)
- |*|
- |*| Note:
- |*| Not all pins on the Mega and Mega 2560 support change interrupts,
- |*| so only the following can be used for RX:
- |*| 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
- |*|
- |*| Not all pins on the Leonardo support change interrupts,
- |*| so only the following can be used for RX:
- |*| 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
- |*|
- |*| created 18 Apr. 2011
- |*| modified 19 March 2016
- |*| modified 7 Dec. 2019
- |*| by Trent M. Wyatt for 2 BLE module example
- |*| based on Tom Igoe's multiple serial test
- |*|
- |*| This example code is in the public domain.
- |*|
- |*| Library of HM-10/11 AT commands
- |*|
- |*| Based off of [the datasheet](https://people.ece.cornell.edu/land/courses/ece4760/PIC32/uart/HM10/DSD%20TECH%20HM-10%20datasheet.pdf)
- \*/
- #include <Arduino.h>
- #include <SoftwareSerial.h>
- #include <string.h>
- // Intentionally suppress compiler's "unused variable" warning
- #ifndef UNUSED
- #define UNUSED(A) \
- do { (void)(A); } while (0);
- #endif
- bool connected = false;
- class BlueTooth {
- private:
- // software serial #1: RX = digital pin 10, TX = digital pin 11
- SoftwareSerial *blueToothOne;
- // software serial #2: RX = digital pin 8, TX = digital pin 9
- // on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
- SoftwareSerial *blueToothTwo;
- // keep track of who we are talking to
- SoftwareSerial *talkingTo;
- public:
- BlueTooth() {
- blueToothOne = new SoftwareSerial(10, 11);
- blueToothTwo = new SoftwareSerial(8, 9);
- talkingTo = blueToothOne;
- }
- virtual ~BlueTooth() {
- if (nullptr != blueToothOne) {
- // delete blueToothOne;
- blueToothOne = nullptr;
- }
- if (nullptr != blueToothTwo) {
- // delete blueToothTwo;
- blueToothTwo = nullptr;
- }
- talkingTo = nullptr;
- Serial.println("BlueTooth destructed.");
- }
- void begin(const int baud) {
- // Start each software serial port
- (*blueToothOne).begin(baud);
- (*blueToothTwo).begin(baud);
- if (nullptr == talkingTo) {
- useOne();
- }
- }
- void end() {
- (*blueToothOne).end();
- (*blueToothTwo).end();
- talkingTo = nullptr;
- }
- int available() {
- return (*talkingTo).available();
- }
- char peek() {
- return (*talkingTo).peek();
- }
- char read() {
- return (*talkingTo).read();
- }
- int write(const char c) {
- return (*talkingTo).write(c);
- }
- int write(const char *src, size_t len) {
- size_t count = 0;
- while (len--) {
- count += write(*src++);
- }
- return count;
- }
- int writeAT(const char *src = NULL, size_t len = 0) {
- size_t count = 0;
- count += write("AT", 2);
- if (src != NULL && *src != '\0') {
- if (toupper(src[0]) == 'A' && toupper(src[1]) == 'T') {
- if (src[2] == '+') {
- src += 3;
- } else {
- src += 2;
- }
- }
- count += write('+');
- if (len == 0) {
- len = strlen(src);
- }
- count += write(src, len);
- }
- return count;
- }
- SoftwareSerial &useOne() {
- Serial.println("using: BlueTooth 1");
- talkingTo = blueToothOne;
- (*talkingTo).listen();
- return *talkingTo;
- }
- SoftwareSerial &useTwo() {
- Serial.println("using: BlueTooth 2");
- talkingTo = blueToothTwo;
- (*talkingTo).listen();
- return *talkingTo;
- }
- // toggle who we are talking to
- SoftwareSerial &toggle() {
- if (talkingTo == blueToothOne) {
- useTwo();
- } else {
- useOne();
- }
- return *talkingTo;
- }
- };
- BlueTooth blueTooth;
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- // Open native USB serial communications and wait for port to open:
- Serial.end();
- Serial.begin(115200);
- // wait for serial port to connect. Needed for native USB port only
- while (!Serial) {}
- Serial.flush();
- // Start each software serial port
- blueTooth.begin(9600);
- }
- void loop() {
- static char inByte = ' ';
- static bool newLine = true;
- static unsigned long timer = millis() + 500UL;
- static const char *connStr = "OK+CONN";
- static const char *confStr = "OK+LOST";
- static size_t state = 0;
- if (millis() >= timer) {
- digitalWrite(LED_BUILTIN, connected ? HIGH : !digitalRead(LED_BUILTIN));
- timer = millis() + 500UL;
- }
- // Read from the Serial Monitor and send to the Bluetooth module
- while (Serial.available() > 0) {
- inByte = toupper(Serial.read());
- // The backslash '\' character toggles between the two ports
- if (inByte == '\\') {
- blueTooth.toggle();
- newLine = true;
- } else
- // The '{' runs the factory default and setup function for the master
- if (inByte == '{') {
- setupMaster(blueTooth);
- } else
- // The '}' runs the factory default and setup function for the slave
- if (inByte == '}') {
- setupSlave(blueTooth);
- } else
- // The open bracket character '[' runs the connect by name test
- if (inByte == '[') {
- connect(blueTooth, "PivotControl");
- } else
- // The close bracket character ']' runs the master to slave LED/remote pin control test
- if (inByte == ']') {
- testLed(blueTooth);
- } else {
- if (inByte != '\r' && inByte != '\n') {
- blueTooth.write(inByte);
- }
- // Echo the user input to the main window.
- // If there is a new line print the ">" character.
- if (newLine) {
- Serial.print("\n>");
- newLine = false;
- }
- Serial.write(inByte);
- if (inByte == '\n') {
- newLine = true;
- }
- }
- }
- // Read from the Bluetooth module and send to the Arduino Serial Monitor
- while (blueTooth.available() > 0) {
- char c = blueTooth.read();
- Serial.write(c);
- if (connected) {
- if (c == confStr[state]) {
- state++;
- if (state == strlen(confStr)) {
- connected = false;
- digitalWrite(LED_BUILTIN, LOW);
- state = 0;
- }
- } else {
- state = 0;
- }
- } else {
- if (c == connStr[state]) {
- state++;
- if (state == strlen(connStr)) {
- connected = true;
- digitalWrite(LED_BUILTIN, HIGH);
- state = 0;
- }
- } else {
- state = 0;
- }
- }
- }
- }
- int getByte(BlueTooth &btPort, int timeout) {
- unsigned long timer = millis() + timeout;
- while (millis() < timer) {
- if (btPort.available() > 0) {
- return btPort.read();
- }
- }
- return -1;
- }
- void getBytes(BlueTooth &blueTooth, int len, char *dest) {
- while (blueTooth.available() < len) {}
- for (int i = 0; i < len; i++) {
- dest[i] = blueTooth.read();
- }
- }
- void showResponse(BlueTooth &blueTooth) {
- while (blueTooth.available() > 0) {
- Serial.write(blueTooth.read());
- }
- }
- void writeAtShowResponse(BlueTooth &blueTooth, const char *src = NULL) {
- Serial.print("AT");
- if (src != NULL && *src != '\0') {
- Serial.print('+');
- Serial.print(src);
- }
- blueTooth.writeAT(src);
- delay(100);
- showResponse(blueTooth);
- Serial.print("\n");
- }
- void setupSlaveAndMaster(BlueTooth &blueTooth) {
- setupMaster(blueTooth);
- setupSlave(blueTooth);
- }
- // ==========================
- // set up Central (master):
- void setupMaster(BlueTooth &blueTooth) {
- blueTooth.useTwo();
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "RENEW");
- writeAtShowResponse(blueTooth, "RESET");
- writeAtShowResponse(blueTooth, "NOTI1");
- writeAtShowResponse(blueTooth, "IMME1");
- writeAtShowResponse(blueTooth, "SHOW1");
- writeAtShowResponse(blueTooth, "NAMEPivotGame");
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "RESET");
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "ROLE1");
- writeAtShowResponse(blueTooth);
- }
- // ==========================
- // set up Peripheral (slave):
- void setupSlave(BlueTooth &blueTooth) {
- blueTooth.useOne();
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "RENEW");
- writeAtShowResponse(blueTooth, "RESET");
- writeAtShowResponse(blueTooth, "NOTI1");
- writeAtShowResponse(blueTooth, "MODE1");
- writeAtShowResponse(blueTooth, "NAMEPivotControl");
- writeAtShowResponse(blueTooth, "RESET");
- blueTooth.useTwo();
- }
- void adelay(BlueTooth &blueTooth, int timeout) {
- unsigned long timer = millis() + timeout;
- while (millis() < timer) {
- if (blueTooth.available() > 0) {
- Serial.print(blueTooth.read());
- }
- delay(10);
- }
- }
- void testLed(BlueTooth &blueTooth) {
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "DISC?");
- adelay(blueTooth, 4000);
- Serial.flush();
- while (1) {
- writeAtShowResponse(blueTooth, "CONF83002001B72");
- adelay(blueTooth, 1000);
- for (int i = 0; i < 5; i++) {
- writeAtShowResponse(blueTooth, "PIO21");
- digitalWrite(LED_BUILTIN, HIGH);
- adelay(blueTooth, 1);
- writeAtShowResponse(blueTooth, "PIO20");
- digitalWrite(LED_BUILTIN, LOW);
- }
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth);
- adelay(blueTooth, 10000);
- adelay(blueTooth, 10000);
- adelay(blueTooth, 10000);
- adelay(blueTooth, 10000);
- adelay(blueTooth, 10000);
- adelay(blueTooth, 10000);
- }
- }
- // Discover the available BlueTooth devices and connect to the
- // the named device is available
- //
- // NOTE: Assumes that the passed bluetooth device is in Central (master) mode.
- //
- int connect(BlueTooth &blueTooth, const char *name) {
- UNUSED(name);
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth);
- writeAtShowResponse(blueTooth, "DISC?");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement