Amine KHETTAT
/
GSM
Program which allow to send/receive and treat SMS messages by connecting a GSM module to the mbed.
main.cpp@2:a1675893f497, 2012-06-19 (annotated)
- Committer:
- khettat
- Date:
- Tue Jun 19 01:48:29 2012 +0000
- Revision:
- 2:a1675893f497
- Parent:
- 1:e2c09cb36265
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
khettat | 0:b1c6550c1d02 | 1 | //Program d'émission/reception et traitement d'SMS |
khettat | 0:b1c6550c1d02 | 2 | #include "mbed.h" |
khettat | 0:b1c6550c1d02 | 3 | |
khettat | 0:b1c6550c1d02 | 4 | //DigitalOut myled(LED1); |
khettat | 0:b1c6550c1d02 | 5 | //Module GSM connecté sur les pin 13 et 14 |
khettat | 0:b1c6550c1d02 | 6 | Serial GSM(p13,p14); |
khettat | 0:b1c6550c1d02 | 7 | //Declaration de l'interface serie du PC |
khettat | 0:b1c6550c1d02 | 8 | Serial pc(USBTX,USBRX); |
khettat | 0:b1c6550c1d02 | 9 | |
khettat | 0:b1c6550c1d02 | 10 | //Flush serial buffer |
khettat | 0:b1c6550c1d02 | 11 | void FlushGSM(void) { char char1 = 0; while (GSM.readable()) { char1 = GSM.getc(); } return; } |
khettat | 0:b1c6550c1d02 | 12 | |
khettat | 0:b1c6550c1d02 | 13 | int main() { |
khettat | 0:b1c6550c1d02 | 14 | int CPIN = 1234;//Code PIN de la carte SIM |
khettat | 2:a1675893f497 | 15 | char NumTel[] = "0600000000"; |
khettat | 0:b1c6550c1d02 | 16 | unsigned char i = 0; |
khettat | 0:b1c6550c1d02 | 17 | unsigned char j = 0; |
khettat | 0:b1c6550c1d02 | 18 | unsigned char CtrlZ = 0x1A; |
khettat | 0:b1c6550c1d02 | 19 | bool Flag = false; |
khettat | 0:b1c6550c1d02 | 20 | char r[]=""; //Chaine de caractère pour récupérer les réponses du module GSM |
khettat | 0:b1c6550c1d02 | 21 | GSM.baud(115200);//Vitesse de com avec le module GSM à 11520 Bauds |
khettat | 0:b1c6550c1d02 | 22 | GSM.format(8,Serial::None,1); //Format de la trame serie |
khettat | 0:b1c6550c1d02 | 23 | |
khettat | 0:b1c6550c1d02 | 24 | //Envoi du code PIN de la puce |
khettat | 0:b1c6550c1d02 | 25 | GSM.printf("AT+CIND=%d",CPIN); |
khettat | 0:b1c6550c1d02 | 26 | |
khettat | 0:b1c6550c1d02 | 27 | while(!GSM.readable()){;}//Attente de la réponse du module |
khettat | 0:b1c6550c1d02 | 28 | //Récupération de la réponse |
khettat | 0:b1c6550c1d02 | 29 | while(GSM.readable()) |
khettat | 0:b1c6550c1d02 | 30 | { |
khettat | 0:b1c6550c1d02 | 31 | r[i] = GSM.getc(); |
khettat | 0:b1c6550c1d02 | 32 | i++; |
khettat | 0:b1c6550c1d02 | 33 | } |
khettat | 0:b1c6550c1d02 | 34 | //Si le Code pin entré est correcte, la SIM répondra par un OK |
khettat | 0:b1c6550c1d02 | 35 | //Donc, il faut chercher la chaine OK dans la réponse |
khettat | 0:b1c6550c1d02 | 36 | for(j=i-1;j--;0) |
khettat | 0:b1c6550c1d02 | 37 | { |
khettat | 0:b1c6550c1d02 | 38 | if((r[i]=='K')||(r[i]=='O')) |
khettat | 0:b1c6550c1d02 | 39 | { |
khettat | 0:b1c6550c1d02 | 40 | pc.printf("Code PIN correct !!!\r\n"); |
khettat | 0:b1c6550c1d02 | 41 | } |
khettat | 0:b1c6550c1d02 | 42 | else |
khettat | 0:b1c6550c1d02 | 43 | { |
khettat | 0:b1c6550c1d02 | 44 | pc.printf("Erreur code PIN !!!\r\n"); |
khettat | 0:b1c6550c1d02 | 45 | return 0; |
khettat | 0:b1c6550c1d02 | 46 | } |
khettat | 0:b1c6550c1d02 | 47 | } |
khettat | 0:b1c6550c1d02 | 48 | //Configuration des SMS en mode Text |
khettat | 0:b1c6550c1d02 | 49 | GSM.printf("AT+CMGF=1"); |
khettat | 0:b1c6550c1d02 | 50 | FlushGSM();//vider le buffer de reception |
khettat | 0:b1c6550c1d02 | 51 | |
khettat | 0:b1c6550c1d02 | 52 | //Envoi d'un SMS à l'autre système pour lui signaler que le programme tourne |
khettat | 0:b1c6550c1d02 | 53 | GSM.printf("AT+CMGS=%s",NumTel); |
khettat | 0:b1c6550c1d02 | 54 | //Récupération de la réponse et recherche du caractère ">" |
khettat | 0:b1c6550c1d02 | 55 | while(GSM.readable()) |
khettat | 0:b1c6550c1d02 | 56 | { |
khettat | 0:b1c6550c1d02 | 57 | if(GSM.getc()=='>') |
khettat | 0:b1c6550c1d02 | 58 | { |
khettat | 0:b1c6550c1d02 | 59 | Flag = true; |
khettat | 0:b1c6550c1d02 | 60 | } |
khettat | 0:b1c6550c1d02 | 61 | } |
khettat | 0:b1c6550c1d02 | 62 | if(Flag==true) |
khettat | 0:b1c6550c1d02 | 63 | { |
khettat | 0:b1c6550c1d02 | 64 | //Ecriture de l'SMS |
khettat | 0:b1c6550c1d02 | 65 | GSM.printf("Essai !!!"); |
khettat | 0:b1c6550c1d02 | 66 | //Validation de l'envoi avec le caractère Ctrl+Z |
khettat | 0:b1c6550c1d02 | 67 | GSM.printf("%c",CtrlZ); |
khettat | 0:b1c6550c1d02 | 68 | //Attente de la réponse suite à l'envoi |
khettat | 0:b1c6550c1d02 | 69 | while(!GSM.readable()){;} |
khettat | 0:b1c6550c1d02 | 70 | i=0; |
khettat | 0:b1c6550c1d02 | 71 | while(GSM.readable())//Récupération de la réponse dans r[] |
khettat | 0:b1c6550c1d02 | 72 | { |
khettat | 0:b1c6550c1d02 | 73 | r[i]=GSM.getc(); |
khettat | 0:b1c6550c1d02 | 74 | i++; |
khettat | 0:b1c6550c1d02 | 75 | } |
khettat | 0:b1c6550c1d02 | 76 | |
khettat | 0:b1c6550c1d02 | 77 | } |
khettat | 0:b1c6550c1d02 | 78 | else |
khettat | 0:b1c6550c1d02 | 79 | { |
khettat | 0:b1c6550c1d02 | 80 | pc.printf("Terminal error !!!"); |
khettat | 0:b1c6550c1d02 | 81 | return 0; |
khettat | 0:b1c6550c1d02 | 82 | } |
khettat | 0:b1c6550c1d02 | 83 | |
khettat | 0:b1c6550c1d02 | 84 | while(1) { |
khettat | 0:b1c6550c1d02 | 85 | |
khettat | 0:b1c6550c1d02 | 86 | } |
khettat | 0:b1c6550c1d02 | 87 | } |