Communicates with the WiFi - be very careful when using as it is tempremental.
Dependencies: mbed
initiateWifi.cpp@1:cd1fe330bc2a, 2017-05-24 (annotated)
- Committer:
- qdanilc
- Date:
- Wed May 24 15:42:41 2017 +0000
- Revision:
- 1:cd1fe330bc2a
- Child:
- 2:0b9ca0830cd3
This wifiReceiver enables user to manually send and receive commands to module;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
qdanilc | 1:cd1fe330bc2a | 1 | #include "mbed.h" |
qdanilc | 1:cd1fe330bc2a | 2 | #include "RawSerial.h" |
qdanilc | 1:cd1fe330bc2a | 3 | |
qdanilc | 1:cd1fe330bc2a | 4 | //setup the other wifi pins |
qdanilc | 1:cd1fe330bc2a | 5 | DigitalOut rstPin (PTE29); |
qdanilc | 1:cd1fe330bc2a | 6 | DigitalOut chpdPin (PTE23); |
qdanilc | 1:cd1fe330bc2a | 7 | |
qdanilc | 1:cd1fe330bc2a | 8 | |
qdanilc | 1:cd1fe330bc2a | 9 | RawSerial esp (PTC4, PTC3, 115200); //setup a serial to the (board) Tx = PTC4 and Rx = PTC3 pins at baud rate 115200 |
qdanilc | 1:cd1fe330bc2a | 10 | |
qdanilc | 1:cd1fe330bc2a | 11 | Serial pc(USBTX, USBRX, 115200); |
qdanilc | 1:cd1fe330bc2a | 12 | |
qdanilc | 1:cd1fe330bc2a | 13 | DigitalOut redLED(LED1); // twp leds |
qdanilc | 1:cd1fe330bc2a | 14 | DigitalOut greenLED(LED2); // to allow visual check of bidirectional comms |
qdanilc | 1:cd1fe330bc2a | 15 | DigitalOut blueLED(LED3); //to prove it's working |
qdanilc | 1:cd1fe330bc2a | 16 | |
qdanilc | 1:cd1fe330bc2a | 17 | char input; //character to store inputs/outputs in communication |
qdanilc | 1:cd1fe330bc2a | 18 | |
qdanilc | 1:cd1fe330bc2a | 19 | |
qdanilc | 1:cd1fe330bc2a | 20 | void reset() { //resets the module by turning rst off for 2 seconds then on again |
qdanilc | 1:cd1fe330bc2a | 21 | rstPin = 0; |
qdanilc | 1:cd1fe330bc2a | 22 | wait(2); |
qdanilc | 1:cd1fe330bc2a | 23 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 24 | pc.printf("Reset complete\r\n"); |
qdanilc | 1:cd1fe330bc2a | 25 | |
qdanilc | 1:cd1fe330bc2a | 26 | } |
qdanilc | 1:cd1fe330bc2a | 27 | void esp_recv() |
qdanilc | 1:cd1fe330bc2a | 28 | { |
qdanilc | 1:cd1fe330bc2a | 29 | redLED = !redLED; |
qdanilc | 1:cd1fe330bc2a | 30 | while(esp.readable()) { |
qdanilc | 1:cd1fe330bc2a | 31 | pc.putc(esp.getc()); |
qdanilc | 1:cd1fe330bc2a | 32 | //wait_us(1); |
qdanilc | 1:cd1fe330bc2a | 33 | } |
qdanilc | 1:cd1fe330bc2a | 34 | } |
qdanilc | 1:cd1fe330bc2a | 35 | void pc_recv() |
qdanilc | 1:cd1fe330bc2a | 36 | { |
qdanilc | 1:cd1fe330bc2a | 37 | char c; |
qdanilc | 1:cd1fe330bc2a | 38 | greenLED = !greenLED; |
qdanilc | 1:cd1fe330bc2a | 39 | while(pc.readable()) { |
qdanilc | 1:cd1fe330bc2a | 40 | c=pc.getc(); |
qdanilc | 1:cd1fe330bc2a | 41 | esp.putc(c); |
qdanilc | 1:cd1fe330bc2a | 42 | //pc.putc(c); // echo back |
qdanilc | 1:cd1fe330bc2a | 43 | if(c==13) { |
qdanilc | 1:cd1fe330bc2a | 44 | esp.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc |
qdanilc | 1:cd1fe330bc2a | 45 | pc.putc(10); //ie makes enter perform as expected |
qdanilc | 1:cd1fe330bc2a | 46 | } |
qdanilc | 1:cd1fe330bc2a | 47 | } |
qdanilc | 1:cd1fe330bc2a | 48 | } |
qdanilc | 1:cd1fe330bc2a | 49 | |
qdanilc | 1:cd1fe330bc2a | 50 | int initiateWifi() { |
qdanilc | 1:cd1fe330bc2a | 51 | |
qdanilc | 1:cd1fe330bc2a | 52 | //initialise the pins |
qdanilc | 1:cd1fe330bc2a | 53 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 54 | chpdPin = 1; |
qdanilc | 1:cd1fe330bc2a | 55 | |
qdanilc | 1:cd1fe330bc2a | 56 | reset(); //perform an initial reset |
qdanilc | 1:cd1fe330bc2a | 57 | |
qdanilc | 1:cd1fe330bc2a | 58 | blueLED = 1; //turn on test light |
qdanilc | 1:cd1fe330bc2a | 59 | |
qdanilc | 1:cd1fe330bc2a | 60 | pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services |
qdanilc | 1:cd1fe330bc2a | 61 | esp.attach(&esp_recv, Serial::RxIrq); |
qdanilc | 1:cd1fe330bc2a | 62 | |
qdanilc | 1:cd1fe330bc2a | 63 | |
qdanilc | 1:cd1fe330bc2a | 64 | } |