Hi, I have this kind of arduino code:
String readString;
char charb[12];
char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
void setup(){
Serial.begin(38400);
}
void loop() {
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
//do stuff
Serial.println(readString); //prints string to serial port out
readString.toCharArray(charb, 12);
initGSM();
send_msg(charb, "OK!");
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
void loopGSM() {
//none
}
void send_msg(char *number, char *msg)
{
char at_cmgs_cmd[30] = {'\0'};
char msg1[160] = {'\0'};
char ctl_z = 0x1A;
sprintf(msg1, "%s%c", msg, ctl_z);
sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
sendGSM(at_cmgs_cmd);
delay(100);
delay(100);
delay(100);
sendGSM(msg1);
delay(100);
}
void sendGSM(char *string){
Serial.write(string);
delay(90);
}
void clearString(char *strArray) {
int j;
for (j = 100; j > 0; j--)
strArray[j] = 0x00;
}
void send_cmd(char *at_cmd, char clr){
char *stat = '\0';
while(!stat){
sendGSM(at_cmd);
delay(90);
readSerialString(Rx_data);
stat = strstr(Rx_data, "OK");
}
if (clr){
clearString(Rx_data);
delay(200);
stat = '\0';
}
}
void initGSM(){
send_cmd("AT\r\n",1);
// send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module
send_cmd("AT+CMGF=1\r\n",1); // Set message format to text mode
//Sucess
Serial.println("Success");
delay(1000);
delay(1000);
delay(1000);
}
void readSerialString (char *strArray) {
if(!Serial.available()) {
return;
}
while(Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}
My problem is that, to be able for me to input a string on the serial monitor, I have to switch the GSM shield to USUART and then next switch the shield into UART to run/start sending the SMS. Is there any way or some modifications needed in my code to get rid of the switching? I want the switch to be fixed because I'm putting it in a casing and switching the shield could be a great hassle and untidy. I need some suggestions, Thanks in advance!