Ligne 68 : |
Ligne 68 : |
| ## Il interprète le contenu du message (par l'intermédiaire de la fonction ProcessSms). | | ## Il interprète le contenu du message (par l'intermédiaire de la fonction ProcessSms). |
| | | |
− | == Code Source == | + | == Code Source - Arduino < 1.0 == |
− | Le programme est malheureusement un peu long. | + | Le programme est malheureusement un peu long mais il fonctionne parfaitement. |
| | | |
− | Vous pouvez le télécharger en cliquant sur [http://mchobby.be/data-files/gprsshield/SmsCommand_V0.pde]. | + | Vous pouvez le télécharger en cliquant sur [http://mchobby.be/data-files/gprsshield/SmsCommand_V0.pde SmsCommand_V0.pde]. |
| | | |
| <nowiki> | | <nowiki> |
Ligne 115 : |
Ligne 115 : |
| | | |
| NewSoftSerial mySerial(7, 8); | | NewSoftSerial mySerial(7, 8); |
| + | |
| + | // EN: String buffer for the GPRS shield message |
| + | // FR: Mémoire tampon de type string pour les messages du shield GPRS |
| + | String msg = String(""); |
| + | // EN: Set to 1 when the next GPRS shield message will contains the SMS message |
| + | // FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS |
| + | int SmsContentFlag = 0; |
| + | |
| + | // EN: Pin of the LED to turn ON and OFF depending on the received message |
| + | // FR: Pin de la LED a allumer/éteindre en fonction du message reçu |
| + | int ledPin = 13; |
| + | |
| + | // EN: Code PIN of the SIM card (if applied) |
| + | // FR: Code PIN de la carte SIM (si applicable) |
| + | String SIM_PIN_CODE = String( "XXXX" ); |
| + | |
| + | void setup() |
| + | { |
| + | mySerial.begin(19200); // the GPRS baud rate |
| + | Serial.begin(19200); // the GPRS baud rate |
| + | |
| + | // Initialize la PIN |
| + | pinMode( ledPin, OUTPUT ); |
| + | digitalWrite( ledPin, LOW ); |
| + | } |
| + | |
| + | void loop() |
| + | { |
| + | char SerialInByte; |
| + | |
| + | if(Serial.available()) |
| + | { |
| + | mySerial.print((unsigned char)Serial.read()); |
| + | } |
| + | else if(mySerial.available()) |
| + | { |
| + | char SerialInByte; |
| + | SerialInByte = (unsigned char)mySerial.read(); |
| + | |
| + | // EN: Relay to Arduino IDE Monitor |
| + | // FR: Relayer l'information vers le moniteur Serie Arduino |
| + | Serial.print( SerialInByte ); |
| + | |
| + | // ------------------------------------------------------------------- |
| + | // EN: Program also listen to the GPRS shield message. |
| + | // FR: Le programme écoute également les messages issus du GPRS Shield. |
| + | // ------------------------------------------------------------------- |
| + | |
| + | // EN: If the message ends with <CR> then process the message |
| + | // FR: Si le message se termine par un <CR> alors traiter le message |
| + | if( SerialInByte == 13 ){ |
| + | // EN: Store the char into the message buffer |
| + | // FR: Stocké le caractère dans le buffer de message |
| + | ProcessGprsMsg(); |
| + | } |
| + | if( SerialInByte == 10 ){ |
| + | // EN: Skip Line feed |
| + | // FR: Ignorer les Line Feed |
| + | } |
| + | else { |
| + | // EN: store the current character in the message string buffer |
| + | // FR: stocker le caractère dans la mémoire tampon réservé au message |
| + | msg += String(SerialInByte); |
| + | } |
| + | } |
| + | } |
| + | |
| + | // EN: Make action based on the content of the SMS. |
| + | // Notice than SMS content is the result of the processing of several GPRS shield messages. |
| + | // FR: Execute une action sur base du contenu d'un SMS. |
| + | // Notez que le contenu du SMS est le résultat du traitement de plusieurs messages du shield GPRS. |
| + | void ProcessSms( String sms ){ |
| + | Serial.print( "ProcessSms for [" ); |
| + | Serial.print( sms ); |
| + | Serial.println( "]" ); |
| + | |
| + | if( sms.indexOf("on") >= 0 ){ |
| + | digitalWrite( ledPin, HIGH ); |
| + | Serial.println( "LED IS ON" ); |
| + | return; |
| + | } |
| + | if( sms.indexOf("off") >= 0 ){ |
| + | digitalWrite( ledPin, LOW ); |
| + | Serial.println( "LED IS OFF" ); |
| + | return; |
| + | } |
| + | } |
| + | |
| + | // EN: Send the SIM PIN Code to the GPRS shield |
| + | // FR: Envoyer le code PIN de la carte SIM au shield GRPS |
| + | void GprsSendPinCode(){ |
| + | if( SIM_PIN_CODE.indexOf("XXXX")>=0 ){ |
| + | Serial.println( "*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***" ); |
| + | Serial.println( "*** Please, define the SIM_PIN_CODE variable . ***" ); |
| + | return; |
| + | } |
| + | mySerial.print("AT+CPIN="); |
| + | mySerial.println( SIM_PIN_CODE ); |
| + | } |
| + | |
| + | // EN: Request Text Mode for SMS messaging |
| + | // FR: Demande d'utiliser le mode Text pour la gestion des messages |
| + | void GprsTextModeSMS(){ |
| + | mySerial.println( "AT+CMGF=1" ); |
| + | } |
| + | |
| + | void GprsReadSmsStore( String SmsStorePos ){ |
| + | // Serial.print( "GprsReadSmsStore for storePos " ); |
| + | // Serial.println( SmsStorePos ); |
| + | mySerial.print( "AT+CMGR=" ); |
| + | mySerial.println( SmsStorePos ); |
| + | } |
| + | |
| + | // EN: Clear the GPRS shield message buffer |
| + | // FR: efface le contenu de la mémoire tampon des messages du GPRS shield. |
| + | void ClearGprsMsg(){ |
| + | msg = ""; |
| + | } |
| + | |
| + | // EN: interpret the GPRS shield message and act appropiately |
| + | // FR: interprete le message du GPRS shield et agit en conséquence |
| + | void ProcessGprsMsg() { |
| + | Serial.println(""); |
| + | Serial.print( "GPRS Message: [" ); |
| + | Serial.print( msg ); |
| + | Serial.println( "]" ); |
| + | |
| + | if( msg.indexOf( "+CPIN: SIM PIN" ) >= 0 ){ |
| + | Serial.println( "*** NEED FOR SIM PIN CODE ***" ); |
| + | Serial.println( "PIN CODE *** WILL BE SEND NOW" ); |
| + | GprsSendPinCode(); |
| + | } |
| + | |
| + | if( msg.indexOf( "Call Ready" ) >= 0 ){ |
| + | Serial.println( "*** GPRS Shield registered on Mobile Network ***" ); |
| + | GprsTextModeSMS(); |
| + | } |
| + | |
| + | // EN: unsolicited message received when getting a SMS message |
| + | // FR: Message non sollicité quand un SMS arrive |
| + | if( msg.indexOf( "+CMTI" ) >= 0 ){ |
| + | Serial.println( "*** SMS Received ***" ); |
| + | // EN: Look for the coma in the full message (+CMTI: "SM",6) |
| + | // In the sample, the SMS is stored at position 6 |
| + | // FR: Rechercher la position de la virgule dans le message complet (+CMTI: "SM",6) |
| + | // Dans l'exemple, le SMS est stocké à la position 6 |
| + | int iPos = msg.indexOf( "," ); |
| + | String SmsStorePos = msg.substring( iPos+1 ); |
| + | Serial.print( "SMS stored at " ); |
| + | Serial.println( SmsStorePos ); |
| + | |
| + | // EN: Ask to read the SMS store |
| + | // FR: Demande de lecture du stockage SMS |
| + | GprsReadSmsStore( SmsStorePos ); |
| + | } |
| + | |
| + | // EN: SMS store readed through UART (result of GprsReadSmsStore request) |
| + | // FR: Lecture du stockage SMS via l'UART (résultat de la requete GprsReadSmsStore) |
| + | if( msg.indexOf( "+CMGR:" ) >= 0 ){ |
| + | // EN: Next message will contains the BODY of SMS |
| + | // FR: Le prochain message contiendra le contenu du SMS |
| + | SmsContentFlag = 1; |
| + | // EN: Following lines are essentiel to not clear the flag! |
| + | // FR: Les ligne suivantes sont essentielle pour ne pas effacer le flag! |
| + | ClearGprsMsg(); |
| + | return; |
| + | } |
| + | |
| + | // EN: +CMGR message just before indicate that the following GRPS Shield message |
| + | // (this message) will contains the SMS body |
| + | // FR: le message +CMGR précédent indiquait que le message suivant du Shield GPRS |
| + | // (ce message) contient le corps du SMS |
| + | if( SmsContentFlag == 1 ){ |
| + | Serial.println( "*** SMS MESSAGE CONTENT ***" ); |
| + | Serial.println( msg ); |
| + | Serial.println( "*** END OF SMS MESSAGE ***" ); |
| + | ProcessSms( msg ); |
| + | } |
| + | |
| + | ClearGprsMsg(); |
| + | // EN: Always clear the flag |
| + | // FR: Toujours mettre le flag à 0 |
| + | SmsContentFlag = 0; |
| + | } |
| + | </nowiki> |
| + | |
| + | == Code Source - Arduino >= 1.0 == |
| + | Cette version du code est adaptée pour fonctionner avec Arduino 1.0. |
| + | |
| + | Vous pouvez le télécharger en cliquant sur [http://mchobby.be/data-files/gprsshield/SmsCommand_V0.pde SmsCommand_V0.pde]. |
| + | |
| + | <nowiki>// EN: |
| + | // *** PATCHED for Arduino 1.0 *** |
| + | // SmsCommand - Active Pin 13 On/OFF depending on the received SMS message (on/off). |
| + | // This soft also send the PIN code to SIM card when appropriate. |
| + | // Based Serial Relay. Arduino will still patch a serial link between the |
| + | // computer and the GPRS Shield at 19200 bps 8-N-1. |
| + | // |
| + | // *** Configure Serial Monitor to Carriage Return, 19200 bauds *** |
| + | // |
| + | // *** VERY IMPORTANT!!! *** |
| + | // *** FOR READING SMS, DO NOT FORGET TO CHANGE *** |
| + | // *** THE BUFFER SIZE OF NewSoftSerial to 128 bytes *** |
| + | // *** see line _NewSS_MAX_RX_BUFF in NewSoftSerial.h *** |
| + | // |
| + | // FR: |
| + | // *** Version adaptée for Arduino 1.0 (ou supérieur) *** |
| + | // |
| + | // SmsCommand - Active la pin 13 allumer/éteindre en fonction du message (on/off) |
| + | // reçu par SMS. Ce logiciel envoie également le code PIN si la carte SIM |
| + | // en demande un. |
| + | // Basé Serial Relay. Arduino maintient également une liaison série entre l'ordinateur |
| + | // et le shield GPRS a 19200 bps 8-N-1. |
| + | // |
| + | // *** Configurer le Moniteur Serie sur Carriage Return, 19200 bauds *** |
| + | // |
| + | // *** TRES IMPORTANT!!! |
| + | // *** POUR LA LECTURE DES SMS, NE PAS OUBLIER DE MODIFIER *** |
| + | // *** LA TAILLE DU BUFFER DE RECEPTION DE NewSoftSerial SUR 128 bytes *** |
| + | // *** voir la ligne _NewSS_MAX_RX_BUFF dans NewSoftSerial.h *** |
| + | // |
| + | // Pour plus d'information voir |
| + | // http://mchobby.be/wiki/index.php?title=GeekOnFire_GSM/GPRS_Shield |
| + | // |
| + | // Licence - Share-Alike - CC-BY |
| + | // |
| + | // Toutes toute référence, mention ou extrait de ce contenu doit être explicitement |
| + | // accompagné du texte suivant : « Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » |
| + | // et ce quelque soit le média utilisé. |
| + | // |
| + | // BONNE AMUSEMENT :-) |
| + | // |
| + | #include <SoftwareSerial.h> |
| + | |
| + | SoftwareSerial mySerial(7, 8); |
| | | |
| // EN: String buffer for the GPRS shield message | | // EN: String buffer for the GPRS shield message |