I am trying to read some strings off a text file stored in my PC. My GSM Module is SIM900A I am using python to read the following line contained in the text file:
AT+CMGS=\"+9232xxxxxxxx\"\r
This line contains the AT command and the phone number to which I want to send the SMS.
The Python code is as follows:
import serial
import time
arduino = serial.Serial("COM3",9600,timeout = 5)
time.sleep(2)
i = 0
while(i<1):
arduino.flush()
text_file = open("Data.txt","r")
line1 = text_file.readline()
arduino.write(line1)
time.sleep(1)
i = i + 1
exit()
text_file.close()
The Arduino code is as follows:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
int counter = 0;
String msg1;
String numb = "AT+CMGS=\"+9232xxxxxxxx\"\r";
void setup() {
mySerial.begin(9600);
Serial.begin(9600);
delay(100);
}
void loop() {
while (!Serial.available()) {} // wait for data to arrive
// serial read section
while (Serial.available()) {
if(Serial.available()>0) {
msg1 = Serial.readString();
if(counter<1) {
SendMessage();
counter++;
}
}
delay(500);
}
if (mySerial.available()>0) Serial.write(mySerial.read());
}
void SendMessage() {
mySerial.println("AT+CMGF=1");
//Sets the GSM Module in Text Mode
delay(1000);
// Delay of 1000 milli seconds or 1 second
mySerial.println(msg1);
// Replace x with mobile number
delay(1000);
mySerial.println("Hello");
// The SMS text you want to send
delay(100);
mySerial.println((char)26);
// ASCII code of CTRL+Z
delay(1000);
}
mySerial.println("AT+CMGS=\"+9232xxxxxxxx\"\r");
mySerial.println(numb);
mySerial.println(msg1);
If I use the first line, I get the message on my number. Similarly, for option 2, when I pass the string numb that I declared in the Arduino code above, I get the message.
However, for option 3, when I read the aforementioned string from the text file stored on the PC, I do not get a message. Can anyone please guide me what I am doing wrong here?
\n
and you need\r
– Burhan Khalid