Envoi d'un SMS via frdm
Dependencies: mbed
main.cpp@0:5ec65dc8f772, 2016-04-18 (annotated)
- Committer:
- maxlh23
- Date:
- Mon Apr 18 15:03:06 2016 +0000
- Revision:
- 0:5ec65dc8f772
Envoi d'un simple SMS via le microcontrolleur frdm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxlh23 | 0:5ec65dc8f772 | 1 | #include "mbed.h" |
maxlh23 | 0:5ec65dc8f772 | 2 | #include <string> |
maxlh23 | 0:5ec65dc8f772 | 3 | |
maxlh23 | 0:5ec65dc8f772 | 4 | Serial pc(USBTX, USBRX); // pc comunication |
maxlh23 | 0:5ec65dc8f772 | 5 | Serial SIM900(D1, D0); //tx, rx SIM 900 |
maxlh23 | 0:5ec65dc8f772 | 6 | string result; |
maxlh23 | 0:5ec65dc8f772 | 7 | char x; |
maxlh23 | 0:5ec65dc8f772 | 8 | |
maxlh23 | 0:5ec65dc8f772 | 9 | |
maxlh23 | 0:5ec65dc8f772 | 10 | void clearString() |
maxlh23 | 0:5ec65dc8f772 | 11 | { |
maxlh23 | 0:5ec65dc8f772 | 12 | result.clear(); |
maxlh23 | 0:5ec65dc8f772 | 13 | } |
maxlh23 | 0:5ec65dc8f772 | 14 | |
maxlh23 | 0:5ec65dc8f772 | 15 | void callback_rx() { |
maxlh23 | 0:5ec65dc8f772 | 16 | |
maxlh23 | 0:5ec65dc8f772 | 17 | while (SIM900.readable()) { |
maxlh23 | 0:5ec65dc8f772 | 18 | x = SIM900.getc(); |
maxlh23 | 0:5ec65dc8f772 | 19 | result += x; |
maxlh23 | 0:5ec65dc8f772 | 20 | pc.putc(x); // print the answer from SIM900 |
maxlh23 | 0:5ec65dc8f772 | 21 | |
maxlh23 | 0:5ec65dc8f772 | 22 | } |
maxlh23 | 0:5ec65dc8f772 | 23 | } |
maxlh23 | 0:5ec65dc8f772 | 24 | |
maxlh23 | 0:5ec65dc8f772 | 25 | void sendSMS() |
maxlh23 | 0:5ec65dc8f772 | 26 | { |
maxlh23 | 0:5ec65dc8f772 | 27 | |
maxlh23 | 0:5ec65dc8f772 | 28 | clearString(); |
maxlh23 | 0:5ec65dc8f772 | 29 | SIM900.printf("AT+CMGF=1\r"); //at command for send sms |
maxlh23 | 0:5ec65dc8f772 | 30 | wait_ms(1000); |
maxlh23 | 0:5ec65dc8f772 | 31 | clearString(); |
maxlh23 | 0:5ec65dc8f772 | 32 | wait_ms(1000); |
maxlh23 | 0:5ec65dc8f772 | 33 | SIM900.printf("AT+CMGS="); |
maxlh23 | 0:5ec65dc8f772 | 34 | SIM900.putc('"'); |
maxlh23 | 0:5ec65dc8f772 | 35 | SIM900.printf("+33669345069"); |
maxlh23 | 0:5ec65dc8f772 | 36 | SIM900.putc('"'); |
maxlh23 | 0:5ec65dc8f772 | 37 | SIM900.printf("\r"); |
maxlh23 | 0:5ec65dc8f772 | 38 | wait_ms(1000); |
maxlh23 | 0:5ec65dc8f772 | 39 | SIM900.printf("Essai2"); // texte à placer dans le message |
maxlh23 | 0:5ec65dc8f772 | 40 | wait_ms(1000); |
maxlh23 | 0:5ec65dc8f772 | 41 | SIM900.putc(0x1A); |
maxlh23 | 0:5ec65dc8f772 | 42 | wait_ms(5000); |
maxlh23 | 0:5ec65dc8f772 | 43 | } |
maxlh23 | 0:5ec65dc8f772 | 44 | |
maxlh23 | 0:5ec65dc8f772 | 45 | |
maxlh23 | 0:5ec65dc8f772 | 46 | int main() { |
maxlh23 | 0:5ec65dc8f772 | 47 | |
maxlh23 | 0:5ec65dc8f772 | 48 | pc.printf("\r\n GSM 900 TEST\n"); |
maxlh23 | 0:5ec65dc8f772 | 49 | SIM900.attach(&callback_rx); |
maxlh23 | 0:5ec65dc8f772 | 50 | SIM900.baud(9600); // |
maxlh23 | 0:5ec65dc8f772 | 51 | wait_ms(100); |
maxlh23 | 0:5ec65dc8f772 | 52 | |
maxlh23 | 0:5ec65dc8f772 | 53 | sendSMS(); // SEND SMS |
maxlh23 | 0:5ec65dc8f772 | 54 | wait_ms(100); |
maxlh23 | 0:5ec65dc8f772 | 55 | |
maxlh23 | 0:5ec65dc8f772 | 56 | } |
maxlh23 | 0:5ec65dc8f772 | 57 |