South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« on: January 09, 2013, 09:55:37 am » |
Hi, using arduino uno r3 and sim900 shield to receive sms's and switch on on board L.E.D,with the sketch below,i am able to read all sms's pefectly but i need to capture the data and activate an output,if i sms the word on to the shield,pin13 must go high,please help complete the sketch.thank you. /* * Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version 0.1 * Author: Alejandro Gállego */
int led = 13; int onModulePin = 9; // the pin to switch on the module (without press on button)
int timesToSend = 1; // Numbers of SMS to send int count = 0;
int n_sms,x,sms_start; char data[256];
void switchModule(){ digitalWrite(onModulePin,HIGH); delay(2000); digitalWrite(onModulePin,LOW); }
void setup(){
Serial.begin(19200); // UART baud rate delay(2000); pinMode(led, OUTPUT); pinMode(onModulePin, OUTPUT); switchModule(); // switches the module ON
for (int i=0;i < 5;i++){ delay(5000); }
Serial.println("AT+CMGF=1"); // sets the SMS mode to text delay(1000); }
void loop(){ Serial.println("AT+CMGL"); //Reads the first SMS Serial.println("AT+CMGD=1,4"); Serial.flush(); for (x=0;x < 255;x++){ data[x]='\0'; } x=0; do{ while(Serial.available()==0); data[x]=Serial.read(); x++; if(data[x-1]==0x0D&&data[x-2]=='"'){ x=0; } }while(!(data[x-1]=='K'&&data[x-2]=='O'));
data[x-3]='\0'; //finish the string before the OK
Serial.println(data); //shows the message
delay(5000);
}
|
|
|
|
« Last Edit: January 09, 2013, 11:10:15 am by kevin-se »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #1 on: January 09, 2013, 10:17:45 am » |
Serial.flush(); Why? I'm pretty sure that your code does not look like that. Modify your post. Select the code, delete it. Select the # icon, and re-paste your code. Save the change.
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #2 on: January 09, 2013, 11:14:43 am » |
The sketch seems to be reading sms's and displaying them perfectly,so i didnt want to modify it in any way,not sure about Serial.flush
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #3 on: January 09, 2013, 11:24:53 am » |
Which version of the IDE are you using? If you are using 1.0 or later, Serial.flush() blocks until the last character from the send buffer has been grabbed for sending. Is that really useful or necessary in your application? Are you talking to the phone on the serial port, or the Serial Monitor? You seem to be trying to do both. Serial.println(data); //shows the message Does it? What does the message look like? Parsing the message, if you've collected it correctly, is pretty easy. But, it would help if we saw what the message was.
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #4 on: January 09, 2013, 11:48:15 am » |
i view all sms's using serial monitor, in my serial monitor it shows the contents of the sms received by the shield,when i sms on, the word on shows up on the serial monitor,what i really want to achieve is to switch on the on board led when i sms the word 'on' to the shield and off to turn the led off ,in stand alone mode
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #5 on: January 09, 2013, 11:50:28 am » |
what i really want to achieve is to switch on the on board led when i sms the word 'on' to the shield and off to turn the led off ,in stand alone mode What does "in stand alone mode" mean? Use strcmp() to see if data and "on" are equal. If so, turn the LED on. Use strcmp() to see if data and "off" are equal. If so, turn the LED off.
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #6 on: January 09, 2013, 11:57:29 am » |
sorry for the different use of english,(South Africa),i am using uno R3 and sim900 shield,using this project for switching an appliance by sms,also a beginner in programming,
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #7 on: January 09, 2013, 12:15:49 pm » |
Will have do sum research on using the Use strcmp commands,thanks
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6383
-
|
 |
« Reply #8 on: January 09, 2013, 01:02:05 pm » |
Are you talking to the phone on the serial port, or the Serial Monitor? You seem to be trying to do both.
You may not have realized the significance of this comment from PaulS. You can only have one device at each end of the serial link. Your sketch is using the Arduino's hardware serial port to talk to the GSM shield. You must not connect anything else to the same serial port. The USB/Serial connection to the host PC uses the same serial port so you must not connect the USB lead at the same time as the shield is connected; that would result in both sources trying to drive the same serial Tx/Rx lines which is a bad idea.
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #9 on: January 09, 2013, 02:28:37 pm » |
o yes, now i realize there could be conflict when sharing the tx and rx,maybe thats why some of my previous attempts ended in failure.thanks. i am still trying to figure out how the Strcmp string works and how i would use it in my application to compare sms's.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #10 on: January 09, 2013, 02:30:58 pm » |
i am still trying to figure out how the Strcmp string works and how i would use it if(strcmp(data, "on") == 0) { // Turn the pin on } if(strcmp(data, "off") == 0) { // Turn the pin off }
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #11 on: January 09, 2013, 02:34:30 pm » |
do i need any new variables on the top,sorry,little knowledge of c++
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #12 on: January 09, 2013, 02:36:04 pm » |
do i need any new variables on the top,sorry,little knowledge of c++ Not for that code.
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #13 on: January 09, 2013, 02:39:10 pm » |
ok,thanks,i will try out the new code now,
|
|
|
|
|
Logged
|
|
|
|
|
South Africa
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #14 on: January 09, 2013, 02:52:53 pm » |
OK,final sketch,please let me know if it will work, int led = 13; int onModulePin = 9; // the pin to switch on the module (without press on button)
int timesToSend = 1; // Numbers of SMS to send int count = 0;
int n_sms,x,sms_start; char data[256];
void switchModule(){ digitalWrite(onModulePin,HIGH); delay(2000); digitalWrite(onModulePin,LOW); }
void setup(){
Serial.begin(19200); // UART baud rate delay(2000); pinMode(led, OUTPUT); pinMode(onModulePin, OUTPUT); switchModule(); // switches the module ON
for (int i=0;i < 5;i++){ delay(5000); }
Serial.println("AT+CMGF=1"); // sets the SMS mode to text delay(1000); }
void loop(){ Serial.println("AT+CMGR"); //Reads the first SMS Serial.flush(); for (x=0;x < 255;x++){ data[x]='\0'; } x=0; do{ while(Serial.available()==0); data[x]=Serial.read(); x++; if(data[x-1]==0x0D&&data[x-2]=='"'){ x=0; } }while(!(data[x-1]=='K'&&data[x-2]=='O'));
data[x-3]='\0'; //finish the string before the OK
Serial.println(data); //shows the message
delay(5000); if(strcmp(data, "on") == 0) { digitalWrite(led,HIGH); } if(strcmp(data, "off") == 0) { digitalWrite(led,LOW); // Turn the pin off Serial.println("AT+CMGD=1,4"); } }
|
|
|
|
|
Logged
|
|
|
|
|
|