python - Reading Text from file and sending to GSM Module through Arduino -
i trying read strings off text file stored in pc. gsm module sim900a using python read following line contained in text file:
at+cmgs=\"+9232xxxxxxxx\"\r
this line contains @ command , phone number want send sms.
the python code follows:
import serial import time arduino = serial.serial("com3",9600,timeout = 5) time.sleep(2) = 0 while(i<1): arduino.flush() text_file = open("data.txt","r") line1 = text_file.readline() arduino.write(line1) time.sleep(1) = + 1 exit() text_file.close()
the arduino code 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 data 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 gsm module in text mode delay(1000); // delay of 1000 milli seconds or 1 second myserial.println(msg1); // replace x mobile number delay(1000); myserial.println("hello"); // sms text want 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 use first line, message on number. similarly, option 2, when pass string numb declared in arduino code above, message.
however, option 3, when read aforementioned string text file stored on pc, not message. can please guide me doing wrong here?
if file contents at+cmgs=\"+9232xxxxxxxx\"\r
posted, remove escaping file. should contents of file:
at+cmgs="+9232xxxxxxxx"
the quotes read without attempted escaping. when read contents, if need \r
add (assuming there's no new-line character @ end of line):
line1 = text_file.readline() + '\r'
Comments
Post a Comment