The problem: my Bluetooth modules are not being consistent, and one never works with software serial and it's driving me nuts.
Right, so I have two Arduinos with one hm-10 Bluetooth module attached to each. It should be noted that they appear to be the Chinese knock-off cc41a.
One Arduino is a 3.3v pro mini and is wired to it's HM-10 using 4 wires (HM-10 to Arduino):
Tx to pin 4
Rx to pin 3
Vcc to vcc
GND to GND
And when it runs this sketch:
#include <SoftwareSerial.h>
int bluetoothTx = 4; // TX-O pin of bluetooth
int bluetoothRx = 3; // RX-I pin of bluetooth
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.begin(9600); // Start bluetooth serial at 9600
Serial.print("init");
bluetooth.print("AT+RENEW");
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
When I open up the serial monitor, the word "init' appears as expected. And when I type "AT' and send with NO LINE ENDING selected, it get the repose "OK" appear in the serial monitor as expected. However the response "AT+RENEWOK" (in repsonse to "AT+RENEW) only appears sometimes when the arduino is reset. And I cannot work out a correlation with anything.
However......
I have a Moteino Mega (1284p 3.3v arduino) connected to the other HM-10.
When I run this sketch on it
/*
Multple Serial test
Receives from the main serial port, sends to the others.
Receives from serial port 1, sends to the main serial (Serial 0).
This example works only with boards with more than one serial like Arduino Mega, Due, Zero etc
The circuit:
* Any serial device attached to Serial port 1
* Serial monitor open on Serial port 0:
created 30 Dec. 2008
modified 20 May 2012
by Tom Igoe & Jed Roach
modified 27 Nov 2015
by Arturo Guadalupi
This example code is in the public domain.
*/
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
}
I am able to get the "OK" response on the serial monitor as expected but only when "BOTH NL AND CR " is selected. Sometimes it needs AT to be in uppercase, sometimes in lower case, and sometimes it doesn't care. This also appears to be random. And the line end requirements are already different to the other module.
I do realise that the second module is using hardware serial and not software serial, but that is because any sketch I use that communicates with the module over software serial, NEVER works. I can type "AT" in as many different combinations of case and line ending, and I never recieve back "ok"
This is the sketch that never works:
#include <SoftwareSerial.h>
int bluetoothTx = 13; // TX-O pin of bluetooth
int bluetoothRx = 14; // RX-I pin of bluetooth
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.begin(9600); // Start bluetooth serial at 9600
Serial.println("init");
bluetooth.print("AT+RENEW");
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
it does show "init" on the serial monitor as expected.
And I when switching between hardware serial and software serial programs, I of course change the pins that the HM-10's are connected too.
If anyone can help shed some light on this, it will help bring an end to the days of systematic testing I am doing and still getting nowhere.
Cheers