Communicate to bluetooth devices or wifi per at-commands

Dependents:   Nucleo_bt

Committer:
rainerraul
Date:
Mon Aug 13 20:27:21 2018 +0000
Revision:
0:413f3c13a00a
Child:
1:ce7fb335aa1b
For atcommand Devices

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rainerraul 0:413f3c13a00a 1
rainerraul 0:413f3c13a00a 2 #include "mbed.h"
rainerraul 0:413f3c13a00a 3 #include "at.h"
rainerraul 0:413f3c13a00a 4
rainerraul 0:413f3c13a00a 5 #define TX PA_9
rainerraul 0:413f3c13a00a 6 #define RX PA_10
rainerraul 0:413f3c13a00a 7
rainerraul 0:413f3c13a00a 8 #define TX1 PC_6
rainerraul 0:413f3c13a00a 9 #define RX1 PC_7
rainerraul 0:413f3c13a00a 10
rainerraul 0:413f3c13a00a 11 Serial atserial0(TX, RX); //UART1
rainerraul 0:413f3c13a00a 12 Serial atserial1(TX1, RX1); //UART6
rainerraul 0:413f3c13a00a 13
rainerraul 0:413f3c13a00a 14 atterm at0;
rainerraul 0:413f3c13a00a 15 atterm at1;
rainerraul 0:413f3c13a00a 16
rainerraul 0:413f3c13a00a 17 void CallBack0()
rainerraul 0:413f3c13a00a 18 {
rainerraul 0:413f3c13a00a 19 at0.sign = atserial0.getc();
rainerraul 0:413f3c13a00a 20
rainerraul 0:413f3c13a00a 21
rainerraul 0:413f3c13a00a 22 if(at0.sign != '\r') { //with no linefeed and carriage return, string is written in buffer
rainerraul 0:413f3c13a00a 23 at0.buffer[at0.a] = at0.sign;
rainerraul 0:413f3c13a00a 24 at0.a ++;
rainerraul 0:413f3c13a00a 25 } else { //if user typed in cr and lf, the content of buffer is send to atcommand device
rainerraul 0:413f3c13a00a 26
rainerraul 0:413f3c13a00a 27 if(at0.off == 0) {
rainerraul 0:413f3c13a00a 28 atserial0.printf("Read: %s\r\n", at0.buffer); // via printf formatted as string for debug use
rainerraul 0:413f3c13a00a 29 }
rainerraul 0:413f3c13a00a 30
rainerraul 0:413f3c13a00a 31 }
rainerraul 0:413f3c13a00a 32 }
rainerraul 0:413f3c13a00a 33
rainerraul 0:413f3c13a00a 34 void CallBack1()
rainerraul 0:413f3c13a00a 35 {
rainerraul 0:413f3c13a00a 36 at1.sign = atserial1.getc();
rainerraul 0:413f3c13a00a 37
rainerraul 0:413f3c13a00a 38
rainerraul 0:413f3c13a00a 39 if(at1.sign != '\r') { //with no linefeed and carriage return, string is written in buffer
rainerraul 0:413f3c13a00a 40 at1.buffer[at1.a] = at1.sign;
rainerraul 0:413f3c13a00a 41 at1.a ++;
rainerraul 0:413f3c13a00a 42 } else { //if user typed in cr and lf, the content of buffer is send to atcommand device
rainerraul 0:413f3c13a00a 43
rainerraul 0:413f3c13a00a 44 if(at1.off == 0) {
rainerraul 0:413f3c13a00a 45 atserial1.printf("Read: %s\r\n", at1.buffer); // via printf formatted as string for debug use
rainerraul 0:413f3c13a00a 46 }
rainerraul 0:413f3c13a00a 47
rainerraul 0:413f3c13a00a 48 }
rainerraul 0:413f3c13a00a 49 }
rainerraul 0:413f3c13a00a 50
rainerraul 0:413f3c13a00a 51 void atterm::debug_off(bool l)
rainerraul 0:413f3c13a00a 52 {
rainerraul 0:413f3c13a00a 53 if(l) atterm::off = 1;
rainerraul 0:413f3c13a00a 54 if(!l) atterm::off = 0;
rainerraul 0:413f3c13a00a 55 }
rainerraul 0:413f3c13a00a 56
rainerraul 0:413f3c13a00a 57 void atterm::at_send(char *format, char *buf)
rainerraul 0:413f3c13a00a 58 {
rainerraul 0:413f3c13a00a 59 atserial0.printf(format, buf);
rainerraul 0:413f3c13a00a 60 }
rainerraul 0:413f3c13a00a 61
rainerraul 0:413f3c13a00a 62 void atterm::clear()
rainerraul 0:413f3c13a00a 63 {
rainerraul 0:413f3c13a00a 64 memset(atterm::buffer, 0, sizeof(atterm::buffer)); //set buffer to 0, for next clean read
rainerraul 0:413f3c13a00a 65 atterm::a = 0; //index pointer to zero
rainerraul 0:413f3c13a00a 66
rainerraul 0:413f3c13a00a 67 }
rainerraul 0:413f3c13a00a 68
rainerraul 0:413f3c13a00a 69 void atterm::device_init(unsigned long baud)
rainerraul 0:413f3c13a00a 70 {
rainerraul 0:413f3c13a00a 71 atterm::debug_off(1);
rainerraul 0:413f3c13a00a 72 atserial0.attach(&CallBack0);
rainerraul 0:413f3c13a00a 73 atserial0.baud(baud);
rainerraul 0:413f3c13a00a 74 atserial0.printf("AT\r\n");
rainerraul 0:413f3c13a00a 75
rainerraul 0:413f3c13a00a 76 }
rainerraul 0:413f3c13a00a 77
rainerraul 0:413f3c13a00a 78 void atterm::at_send1(char *format, char *buf)
rainerraul 0:413f3c13a00a 79 {
rainerraul 0:413f3c13a00a 80 atserial1.printf(format, buf);
rainerraul 0:413f3c13a00a 81 }
rainerraul 0:413f3c13a00a 82
rainerraul 0:413f3c13a00a 83
rainerraul 0:413f3c13a00a 84
rainerraul 0:413f3c13a00a 85 void atterm::device_init1(unsigned long baud)
rainerraul 0:413f3c13a00a 86 {
rainerraul 0:413f3c13a00a 87 atterm::debug_off(1);
rainerraul 0:413f3c13a00a 88 atserial1.attach(&CallBack1);
rainerraul 0:413f3c13a00a 89 atserial1.baud(baud);
rainerraul 0:413f3c13a00a 90 atserial1.printf("AT\r\n");
rainerraul 0:413f3c13a00a 91
rainerraul 0:413f3c13a00a 92
rainerraul 0:413f3c13a00a 93 }