![](/media/cache/group/mouse.jpg.50x50_q85.jpg)
WIFI
Dependencies: mbed
Fork of SerialPassthroughcjsESP8266 by
main.cpp@10:f7a48cc24892, 2016-06-15 (annotated)
- Committer:
- wadh4587
- Date:
- Wed Jun 15 14:47:19 2016 +0000
- Revision:
- 10:f7a48cc24892
- Parent:
- 9:b1344ca3497d
WIFI ESP8266 & KL25Z
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cstevens | 7:d78ed22a787d | 1 | /* Simpler prog based on the serial passthrough code to enable a command line driven test of esp8266 |
cstevens | 7:d78ed22a787d | 2 | * wifi modules. |
cstevens | 7:d78ed22a787d | 3 | * NB this uses the mbed sleep() command to form a low power system but on some MCUs this is a problem |
cstevens | 7:d78ed22a787d | 4 | * this works fine on an lpc1768 but not as yet on the KL25Z |
cstevens | 7:d78ed22a787d | 5 | */ |
mbedAustin | 0:59bec1fd956e | 6 | #include "mbed.h" |
mbedAustin | 2:a8dcb07a1d00 | 7 | |
wadh4587 | 10:f7a48cc24892 | 8 | Serial pc(USBTX, USBRX); // serial terminal for the pc connection |
wadh4587 | 10:f7a48cc24892 | 9 | Serial dev(PTE0,PTE1); // for KL25Z... asuming one can't use the PTA1 version which is the stdio |
wadh4587 | 10:f7a48cc24892 | 10 | DigitalOut led1(LED1); // twp leds |
wadh4587 | 10:f7a48cc24892 | 11 | DigitalOut led4(LED2); // to allow visual check of bidirectional comms |
wadh4587 | 10:f7a48cc24892 | 12 | DigitalOut rst(PTD7); // single digital pin to drive the esp8266 reset line |
mbedAustin | 2:a8dcb07a1d00 | 13 | |
cstevens | 7:d78ed22a787d | 14 | // subroutine to run anytime a serial interrupt arrives from the device |
cstevens | 7:d78ed22a787d | 15 | // this basically passes everything thatthe device produces on to the pc terminal screen |
sam_grove | 5:96cb82af9996 | 16 | void dev_recv() |
mbedAustin | 2:a8dcb07a1d00 | 17 | { |
sam_grove | 5:96cb82af9996 | 18 | led1 = !led1; |
sam_grove | 5:96cb82af9996 | 19 | while(dev.readable()) { |
sam_grove | 5:96cb82af9996 | 20 | pc.putc(dev.getc()); |
wadh4587 | 10:f7a48cc24892 | 21 | wait_us(1); |
sam_grove | 5:96cb82af9996 | 22 | } |
sam_grove | 5:96cb82af9996 | 23 | } |
cstevens | 7:d78ed22a787d | 24 | // subroutine to service the serial interrupt on the pc connection |
wadh4587 | 10:f7a48cc24892 | 25 | // this is a bit more complex - it takes what the use sends on the pc and copies it on to the device |
cstevens | 7:d78ed22a787d | 26 | // the esp should echo these straight back to the the pc if all is well |
cstevens | 7:d78ed22a787d | 27 | // this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a) |
sam_grove | 5:96cb82af9996 | 28 | void pc_recv() |
sam_grove | 5:96cb82af9996 | 29 | { |
cstevens | 6:dc4c165f6b53 | 30 | char c; |
sam_grove | 5:96cb82af9996 | 31 | led4 = !led4; |
sam_grove | 5:96cb82af9996 | 32 | while(pc.readable()) { |
wadh4587 | 10:f7a48cc24892 | 33 | c = pc.getc(); |
cstevens | 6:dc4c165f6b53 | 34 | dev.putc(c); |
wadh4587 | 10:f7a48cc24892 | 35 | // pc.putc(c); // echo back |
wadh4587 | 10:f7a48cc24892 | 36 | if(c==13) { |
wadh4587 | 10:f7a48cc24892 | 37 | dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc |
wadh4587 | 10:f7a48cc24892 | 38 | pc.putc(10); |
wadh4587 | 10:f7a48cc24892 | 39 | } |
mbedAustin | 0:59bec1fd956e | 40 | } |
mbedAustin | 0:59bec1fd956e | 41 | } |
mbedAustin | 4:ba9100d52e48 | 42 | |
mbedAustin | 4:ba9100d52e48 | 43 | int main() |
mbedAustin | 4:ba9100d52e48 | 44 | { |
wadh4587 | 10:f7a48cc24892 | 45 | pc.baud(115200); |
wadh4587 | 10:f7a48cc24892 | 46 | dev.baud(115200); |
cstevens | 6:dc4c165f6b53 | 47 | rst=0; |
cstevens | 6:dc4c165f6b53 | 48 | wait(1); |
wadh4587 | 10:f7a48cc24892 | 49 | rst = 1; // send the esp8266 reset |
cstevens | 6:dc4c165f6b53 | 50 | wait(1); |
mbedAustin | 4:ba9100d52e48 | 51 | |
cstevens | 8:34cc66421054 | 52 | pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services |
sam_grove | 5:96cb82af9996 | 53 | dev.attach(&dev_recv, Serial::RxIrq); |
wadh4587 | 10:f7a48cc24892 | 54 | pc.printf("ok off we go....\n\r"); |
wadh4587 | 10:f7a48cc24892 | 55 | // wait(10); |
wadh4587 | 10:f7a48cc24892 | 56 | // pc.printf("AT+RST\n\r"); |
wadh4587 | 10:f7a48cc24892 | 57 | // wait(20); |
wadh4587 | 10:f7a48cc24892 | 58 | // pc.printf("AT+CWJAP="CWMWIFI","CWM2016TT""\n\r); |
wadh4587 | 10:f7a48cc24892 | 59 | |
mbedAustin | 4:ba9100d52e48 | 60 | while(1) { |
wadh4587 | 10:f7a48cc24892 | 61 | // (i++)%10; // THIS USED TO BE A SLEEP COMMAND BUT IT WAS CAUSING SOME TROUBLE. |
mbedAustin | 4:ba9100d52e48 | 62 | } |
wadh4587 | 10:f7a48cc24892 | 63 | } |