Hi Guys,
I am currently using an Arduino Mega for building a "Smart Home". I would like to control it through BT and SMS. I have both modules connected to the Mega. If I only connect one of the systems everything works fine but both at the same time doesn't work. I made some research and I am pretty sure that the problem is in choosing the correct baud rate but I still cant come up with a solution.
Sorry that some of the comments in the code are in german, but its more for my own understanding since I am new to this.
This is my code:
/////BT////
#include <AltSoftSerial.h>
AltSoftSerial altBtSerial;
///SIM////
#include <SoftwareSerial.h>
SoftwareSerial smsSerial(10, 11); // RX, TX
///SIM////
String msg = String("");
int SmsContentFlag = 0; // EN: Set to 1 when the next GPRS shield message will contains the SMS message
String SIM_PIN_CODE = String( "1305" ); // EN: Code PIN of the SIM card (if applied)
String altBtData;
const int ledPinBlue = 38;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600); //this is the messages that are typed as the normal AT commands
smsSerial.begin(9600); //this serial is for the connection from and to the sim module to type the necessary AT commands
delay(1000);
altBtSerial.println("SMS-Serial available");
delay(1000);
altBtSerial.begin(9600); //this serial is the one for the bluetooth: so all the texts have to be printed here
delay(5000);
altBtSerial.println("alt-bluetooth available");
pinMode(ledPinBlue, OUTPUT);
smsSerial.println("AT+CMGF=1");
smsSerial.write('AT+CMGL');
delay(1000);
altBtSerial.println("Setup is Ready");
}
void loop() {
char SerialInByte;
if (Serial.available()) //wenn eine nachricht im serial auftaucht,
{
smsSerial.print((unsigned char)Serial.read()); //dann soll sie im smsSerial abgeschickt werden => ausführen der AT commands
}
else if (smsSerial.available()) //wenn eine nachricht vom SMS modul kommt
{
char SerialInByte;
SerialInByte = (unsigned char)smsSerial.read(); //speicher diese in der variable SerialInByte
altBtSerial.print(SerialInByte); //schreibe sie im bluetooth serial => das könnte schon reichen um dann von den if bt data available erkannt zu werden, dann muss es nur eine funktion geben
Serial.print(SerialInByte);
if ( SerialInByte == 13 ) { // EN: Store the char into the message buffer
ProcessGprsMsg();
}
if ( SerialInByte == 10 ) { // EN: Skip Line feed
}
else {
msg += String(SerialInByte); // EN: store the current character in the message string buffer
}
}
if (altBtSerial.available()) {
altBtData = altBtSerial.readString();
//////////led/////////
if (altBtData == "on") {
ledOn();
}
if ( altBtData == "off") {
ledOff();
}
}
}
void ProcessSms( String msg ) {
altBtSerial.print( "ProcessSms for [" );
altBtSerial.print( msg );
altBtSerial.println( "]" );
if ( msg.indexOf("OnB") >= 0 ) {
digitalWrite( ledPinBlue, HIGH );
altBtSerial.println( "SMS: Blue LED is on" );
return;
}
if ( msg.indexOf("OffB") >= 0 ) {
digitalWrite( ledPinBlue, LOW );
altBtSerial.println( "SMS: Blue LED is off" );
return;
}
}
void GprsSendPinCode() { // EN: Send the SIM PIN Code to the GPRS shield
if ( SIM_PIN_CODE.indexOf("XXXX") >= 0 ) {
altBtSerial.println( "*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***" );
altBtSerial.println( "*** Please, define the SIM_PIN_CODE variable . ***" );
return;
}
smsSerial.print("AT+CPIN=");
smsSerial.println( SIM_PIN_CODE );
}
void GprsTextModeSMS() {
smsSerial.println( "AT+CMGF=1" ); // EN: Request Text Mode for SMS messaging
altBtSerial.println("Set to SMS text mode");
}
void GprsReadSmsStore( String SmsStorePos ) {
// Serial.print( "GprsReadSmsStore for storePos " );
// Serial.println( SmsStorePos );
smsSerial.print( "AT+CMGR=" );
smsSerial.println( SmsStorePos );
}
void ClearGprsMsg() {
msg = "";
}
void ProcessGprsMsg() {
altBtSerial.println("");
altBtSerial.print( "GPRS Message: [" );
altBtSerial.print( msg );
altBtSerial.println( "]" );
if ( msg.indexOf( "+CPIN: SIM PIN" ) >= 0 ) {
altBtSerial.println( "*** NEED FOR SIM PIN CODE ***" );
altBtSerial.println( "PIN CODE *** WILL BE SEND NOW" );
GprsSendPinCode();
}
if ( msg.indexOf( "Call Ready" ) >= 0 ) {
altBtSerial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
if ( msg.indexOf( "+CMTI" ) >= 0 ) {
altBtSerial.println( "*** SMS Received ***" );
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos + 1 );
altBtSerial.print( "SMS stored at " );
altBtSerial.println( SmsStorePos );
GprsReadSmsStore( SmsStorePos );
}
if ( msg.indexOf( "+CMGR:" ) >= 0 ) {
SmsContentFlag = 1;
ClearGprsMsg();
return;
}
if ( SmsContentFlag == 1 ) {
altBtSerial.println( "*** SMS MESSAGE CONTENT ***" );
altBtSerial.println( msg );
altBtSerial.println( "*** END OF SMS MESSAGE ***" );
ProcessSms( msg );
}
ClearGprsMsg();
SmsContentFlag = 0;
}
/////////////led///////////
void ledOn() {
digitalWrite(ledPinBlue, HIGH);
altBtSerial.println("BT: LED is on");
}
void ledOff () {
digitalWrite(ledPinBlue, LOW);
altBtSerial.println("BT: LED is off");
}
The results in the Bluetooth Serial Monitor look like this:
GPRS Message: [
]
+CMTI: "SM",5
GPR�sage: [
+CMTI: "SM",5]
*** SM�������***
SMS stored at 5
AT+CMGR=5
GPRS Message: [
AT+CMGR=5]
So first of all it doesn't process the message properly. I was sending "OnB" to turn on a LED.
And second is obviously these questionmarks.
I was trying to use different baud rates to overcome the problem. I got my input from here: AltSoftSerial Library, for an extra serial port
So I know that the baud rate of the SoftwareSerial should be like 10 times higher then the one from the altSoftSerial port.
But I cant find a way to make it work.
So I would be really happy for some help of yours!
And since this is my first post I am also open for some feedback!
Thanks already
Bene