n.n

Dependencies:   mbed

Committer:
QuangAnhLe
Date:
Mon Aug 05 17:47:50 2019 +0000
Revision:
0:5fa883d73460
1. Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
QuangAnhLe 0:5fa883d73460 1 #include "mbed.h"
QuangAnhLe 0:5fa883d73460 2 #include <string>
QuangAnhLe 0:5fa883d73460 3
QuangAnhLe 0:5fa883d73460 4 Serial pc(PA_2,PA_3); // pc comunication
QuangAnhLe 0:5fa883d73460 5 Serial SIM900(PA_9, PA_10); //tx, rx SIM 900
QuangAnhLe 0:5fa883d73460 6 string result;
QuangAnhLe 0:5fa883d73460 7 char x;
QuangAnhLe 0:5fa883d73460 8
QuangAnhLe 0:5fa883d73460 9
QuangAnhLe 0:5fa883d73460 10 void clearString()
QuangAnhLe 0:5fa883d73460 11 {
QuangAnhLe 0:5fa883d73460 12 result.clear();
QuangAnhLe 0:5fa883d73460 13 }
QuangAnhLe 0:5fa883d73460 14
QuangAnhLe 0:5fa883d73460 15 void callback_rx() {
QuangAnhLe 0:5fa883d73460 16
QuangAnhLe 0:5fa883d73460 17 while (SIM900.readable()) {
QuangAnhLe 0:5fa883d73460 18 x = SIM900.getc();
QuangAnhLe 0:5fa883d73460 19 result += x;
QuangAnhLe 0:5fa883d73460 20 pc.putc(x); // print the answer from SIM900
QuangAnhLe 0:5fa883d73460 21
QuangAnhLe 0:5fa883d73460 22 }
QuangAnhLe 0:5fa883d73460 23 }
QuangAnhLe 0:5fa883d73460 24
QuangAnhLe 0:5fa883d73460 25 void sendSMS()
QuangAnhLe 0:5fa883d73460 26 {
QuangAnhLe 0:5fa883d73460 27
QuangAnhLe 0:5fa883d73460 28 clearString();
QuangAnhLe 0:5fa883d73460 29 SIM900.printf("AT+CMGF=1\r"); //at command for send sms
QuangAnhLe 0:5fa883d73460 30 wait_ms(1000);
QuangAnhLe 0:5fa883d73460 31 clearString();
QuangAnhLe 0:5fa883d73460 32 wait_ms(1000);
QuangAnhLe 0:5fa883d73460 33 SIM900.printf("AT+CMGS=");
QuangAnhLe 0:5fa883d73460 34 SIM900.putc('"');
QuangAnhLe 0:5fa883d73460 35 SIM900.printf("+4917645651571");
QuangAnhLe 0:5fa883d73460 36 SIM900.putc('"');
QuangAnhLe 0:5fa883d73460 37 SIM900.printf("\r");
QuangAnhLe 0:5fa883d73460 38 wait_ms(1000);
QuangAnhLe 0:5fa883d73460 39 SIM900.printf("test1");
QuangAnhLe 0:5fa883d73460 40 wait_ms(1000);
QuangAnhLe 0:5fa883d73460 41 SIM900.putc(0x1A);
QuangAnhLe 0:5fa883d73460 42 wait_ms(5000);
QuangAnhLe 0:5fa883d73460 43 }
QuangAnhLe 0:5fa883d73460 44
QuangAnhLe 0:5fa883d73460 45
QuangAnhLe 0:5fa883d73460 46 int main() {
QuangAnhLe 0:5fa883d73460 47
QuangAnhLe 0:5fa883d73460 48 pc.printf("\r\n GSM 900 TEST\n");
QuangAnhLe 0:5fa883d73460 49 SIM900.attach(&callback_rx);
QuangAnhLe 0:5fa883d73460 50 SIM900.baud(9600); //
QuangAnhLe 0:5fa883d73460 51 wait_ms(100);
QuangAnhLe 0:5fa883d73460 52
QuangAnhLe 0:5fa883d73460 53
QuangAnhLe 0:5fa883d73460 54
QuangAnhLe 0:5fa883d73460 55 wait_ms(10);
QuangAnhLe 0:5fa883d73460 56 sendSMS(); // SEND SMS
QuangAnhLe 0:5fa883d73460 57 wait_ms(100);
QuangAnhLe 0:5fa883d73460 58
QuangAnhLe 0:5fa883d73460 59
QuangAnhLe 0:5fa883d73460 60
QuangAnhLe 0:5fa883d73460 61 }