Facing difficulty while communicating 800c GSM module with arduino nano at the desired baud rate

Have questions about a SparkFun product or board? This is the place to be.

Moderators: phalanx, TS-Tim

Post Reply
MuhammahHassan
Posts: 1
Joined: Mon Nov 05, 2018 10:38 am

Facing difficulty while communicating 800c GSM module with arduino nano at the desired baud rate

Post by MuhammahHassan » Mon Nov 05, 2018 10:59 am

#include <SoftwareSerial.h>
int rx_pin = 2;
int tx_pin = 3;

SoftwareSerial Serial1(rx_pin, tx_pin);

int count = 0 , bytes_to_read = 0;
char inchr[20], temp;

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
pinMode(LED_BUILTIN , OUTPUT);
}

void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
serial_flush();

while(!initialize_gsm());
delay(2000);
serial_flush();

Serial1.println("AT\r");

while(Serial1.available() == 0);

while(!recieve_char());

serial_flush();
Serial1.println("AT+IPR=9600\r");

while(Serial1.available() == 0);

while(!recieve_char());
while(1)
digitalWrite(LED_BUILTIN, HIGH);

}

void clear_in_array()
{
for(count = 0; count<20; count++)
inchr[count] = '\0';
}

bool initialize_gsm ()
{
bool ok_rec = false;
serial_flush();//clear memory
Serial1.println("AAAT\r");//multiple A's for autobaud sync
Serial.println("AAAT\r");
delay(100);
bytes_to_read = Serial1.available();//check available bytes
if(bytes_to_read < 7)//more than 7 bytes means garbage most probably
{
//next loop is for reading available bytes and storing them
for (count = 0; count < bytes_to_read; count++)
{
inchr[count] = Serial1.read();
if(count>0) if(inchr[count-1] == 'O' && inchr[count] == 'K') ok_rec = true;
}
bytes_to_read = 0;
Serial.println(inchr);
}
clear_in_array();//clear recieve array
delay(1000);
return ok_rec;
}

bool recieve_char()
{
char char_recieved = '\0';
char_recieved = Serial.print(Serial1.read());
if(Serial1.available() == 0) return true;
else return false;
}

void serial_flush()
{
while(Serial1.available() !=0)
temp = Serial1.read();
}

Post Reply