I am working on SMS communication with my phone and Arduino. My intension is simple. When somebody send an SMS, the Arduino should read a text file located on my server and forward the content to the same number.
The problem here is, when I send an SMS to my Arduino, which is waiting for my message, it does not read my message. But when I separately run this example code, I see my SMS is received. It means my SMS is already present in the modem memory. But why does this happen? How to solve this? Here is my code:
// libraries
#include <GSM.h>
// PIN Number
#define PINNUMBER ""
// APN data
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSM_SMS sms;
// URL, path & port (for example: arduino.cc)
char server[] = "yourdomain.com";
char path[] = "/current.txt";
int port = 80; // port 80 is the default for HTTP
char k[30];
char z = '*';
int s=0;
String place;
char remoteNum[20]; // telephone number to send sms
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("SMS connected");
// connection state
boolean notConnected = true;
while(notConnected) {
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GPRS connected...");
Serial.println("Server Connecting.");
// if you get a connection, report back via serial:
serverr();
}
void loop() {
if (sms.available()) {
Serial.println("Message received from:");
sms.remoteNumber(remoteNum, 20);
Serial.println(remoteNum);
sms.flush();
Serial.println("MESSAGE DELETED");
}
}
void smss() {
// I just want phone number
if (client.available()) {
char c = client.read();
//***************************************************
// Filtering a specific message
//**************************************************
if (z==c) {
s=1;
}
if(s == 1) {
int i=0;
k[i] = c;
i=i+1;
// Serial.print(k);
}
place +=k;
//Serial.print(place);
}
//***************************************************
if (!client.available() && !client.connected()) {
sms.beginSMS(remoteNum);
Serial.print("Place =");
Serial.print(place);
sms.print(place);
sms.endSMS();
sms.flush();
Serial.println("MESSAGE DELETED");
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;)
;
}
}
// if you didn't get a connection to the server:
// Serial.print(server);
void serverr() {
if (client.connect(server, port)) {
client.print("GET /current.txt");
Serial.print("GET /current.txt");
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: www.yourdomain.com");
Serial.println("Host: www.yourdomain.com");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Accept: text/html");
client.println("Connection: close");
Serial.println("Connection: close");
client.println();
Serial.println();
Serial.println("\nCOMPLETE!\n");
//client.stop();
} else {
Serial.println("connection failed");
Serial.println("\n FAILED!\n");
}
}
smss()function, so that we can see how you read the SMS and then delete it.remoteNumberis defined aschar remoteNumber[20];? Finally, I wonder if you canflush()an SMS without reading its content first.smss()function is never called. The global structure is just a mess as you did not break down behavior into different functions; instead some functions do a mix of unrelated actions.