Skip to content

Instantly share code, notes, and snippets.

@e-Gizmo
Created March 21, 2017 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-Gizmo/dc55f1426d70c404abf01dd302a1cca4 to your computer and use it in GitHub Desktop.
Save e-Gizmo/dc55f1426d70c404abf01dd302a1cca4 to your computer and use it in GitHub Desktop.
/*
GSM Send Sketch for Arduino
Initializes GSM Module and sends an SMS to recipient
The circuit:
*Arduino pin 0 (RX) - GSM Module (TX)
*Arduino pin 1 (TX) - GSM Module (RX)
Created 2010
by Meann Zabanal
Modified
by John for GSM Shield testing
Modified August 20, 2015
by Amoree for GSM Shield testing Serial Monitor
*/
char Rx_data[150];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
#define DEBUG true
void setup()
{
Serial.begin(9600);
Serial1.begin(19200);
init_GSM();
send_msg("222", "BAL");
}
void loop()
{
if (Serial1.available())
Serial.write(Serial1.read());
if (Serial.available())
Serial1.write(Serial.read());
}
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){
Serial1.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(200);
readSerialString(Rx_data);
Serial.write(Rx_data);
stat = strstr(Rx_data, "OK");
Serial.println("Success");
delay(50000);
}
if (clr){
clearString(Rx_data);
delay(200);
stat = '\0';
}
}
void init_GSM(){
sendData("AT\r\n",1000,DEBUG); // AT
sendData("AT+CMGF=1\r\n",1000,DEBUG); //AT+CMGF=1
sendData("AT+CMGD=1\r\n",1000,DEBUG); //AT+CMGD=1
delay(1000);
delay(1000);
delay(1000);
}
void readSerialString (char *strArray) {
if(!Serial1.available()) {
return;
}
while(Serial1.available()) {
strArray[i] = Serial1.read();
i++;
}
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
Serial1.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial1.available())
{
char c = Serial1.read();
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment