Communicates with the WiFi - be very careful when using as it is tempremental.
Dependencies: mbed
initiateWifi.cpp@3:b593f43b7251, 2017-05-25 (annotated)
- Committer:
- qdanilc
- Date:
- Thu May 25 10:37:33 2017 +0000
- Revision:
- 3:b593f43b7251
- Parent:
- 2:0b9ca0830cd3
- Child:
- 4:65f657998b7d
Has an automated startup sequence;
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 | 3:b593f43b7251 | 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 | 3:b593f43b7251 | 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 | 3:b593f43b7251 | 16 | char c; |
qdanilc | 3:b593f43b7251 | 17 | char * buffer = (char *)malloc(128); //setup a buffer 128 characters long |
qdanilc | 3:b593f43b7251 | 18 | char * nextBuff; // a pointer to the next character being added to the buffer |
qdanilc | 1:cd1fe330bc2a | 19 | |
qdanilc | 1:cd1fe330bc2a | 20 | char input; //character to store inputs/outputs in communication |
qdanilc | 2:0b9ca0830cd3 | 21 | char* startCommands[10]= { |
qdanilc | 2:0b9ca0830cd3 | 22 | "AT", |
qdanilc | 2:0b9ca0830cd3 | 23 | "AT", |
qdanilc | 2:0b9ca0830cd3 | 24 | "AT", |
qdanilc | 2:0b9ca0830cd3 | 25 | "AT+RST", |
qdanilc | 2:0b9ca0830cd3 | 26 | "AT+CWMODE=1", |
qdanilc | 3:b593f43b7251 | 27 | "AT+CWJAP=\"Hotspot\",\"password\"", //name and password of wifi |
qdanilc | 2:0b9ca0830cd3 | 28 | "AT+CIFSR", |
qdanilc | 2:0b9ca0830cd3 | 29 | "AT+CIPMUX=1", |
qdanilc | 2:0b9ca0830cd3 | 30 | "AT+CIPSERVER=1,8080", |
qdanilc | 2:0b9ca0830cd3 | 31 | "AT+CIPSEND=0," |
qdanilc | 2:0b9ca0830cd3 | 32 | }; |
qdanilc | 1:cd1fe330bc2a | 33 | |
qdanilc | 1:cd1fe330bc2a | 34 | |
qdanilc | 2:0b9ca0830cd3 | 35 | int numberOfCommands; |
qdanilc | 3:b593f43b7251 | 36 | int commandsExecuted; |
qdanilc | 3:b593f43b7251 | 37 | int gotLine = 0; // boolean for whether we have a line |
qdanilc | 2:0b9ca0830cd3 | 38 | |
qdanilc | 2:0b9ca0830cd3 | 39 | |
qdanilc | 3:b593f43b7251 | 40 | void start() |
qdanilc | 3:b593f43b7251 | 41 | { |
qdanilc | 2:0b9ca0830cd3 | 42 | pc.printf("Initialising start sequence\r\n"); |
qdanilc | 3:b593f43b7251 | 43 | |
qdanilc | 3:b593f43b7251 | 44 | |
qdanilc | 2:0b9ca0830cd3 | 45 | //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]); |
qdanilc | 2:0b9ca0830cd3 | 46 | numberOfCommands = 10; |
qdanilc | 3:b593f43b7251 | 47 | |
qdanilc | 3:b593f43b7251 | 48 | commandsExecuted = 0; |
qdanilc | 3:b593f43b7251 | 49 | esp.printf("%s\r\n",startCommands[commandsExecuted]); |
qdanilc | 3:b593f43b7251 | 50 | while (commandsExecuted < numberOfCommands) { //loop until we have executed all the commands |
qdanilc | 3:b593f43b7251 | 51 | if(gotLine) { // if esp signals that it's ready to receive a command, send the next command |
qdanilc | 3:b593f43b7251 | 52 | pc.printf("\r\ncommand %d",commandsExecuted); //identify the command number |
qdanilc | 3:b593f43b7251 | 53 | pc.printf("\r\n"); |
qdanilc | 3:b593f43b7251 | 54 | |
qdanilc | 3:b593f43b7251 | 55 | esp.printf("%s\r\n",startCommands[commandsExecuted]); //send command |
qdanilc | 3:b593f43b7251 | 56 | commandsExecuted ++; //increment the counter |
qdanilc | 3:b593f43b7251 | 57 | gotLine = 0; |
qdanilc | 3:b593f43b7251 | 58 | } |
qdanilc | 2:0b9ca0830cd3 | 59 | } |
qdanilc | 2:0b9ca0830cd3 | 60 | } |
qdanilc | 2:0b9ca0830cd3 | 61 | |
qdanilc | 3:b593f43b7251 | 62 | void reset() //resets the module by turning rst off for 2 seconds then on again |
qdanilc | 3:b593f43b7251 | 63 | { |
qdanilc | 1:cd1fe330bc2a | 64 | rstPin = 0; |
qdanilc | 1:cd1fe330bc2a | 65 | wait(2); |
qdanilc | 1:cd1fe330bc2a | 66 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 67 | pc.printf("Reset complete\r\n"); |
qdanilc | 3:b593f43b7251 | 68 | |
qdanilc | 1:cd1fe330bc2a | 69 | } |
qdanilc | 1:cd1fe330bc2a | 70 | void esp_recv() |
qdanilc | 1:cd1fe330bc2a | 71 | { |
qdanilc | 3:b593f43b7251 | 72 | char d; |
qdanilc | 3:b593f43b7251 | 73 | nextBuff = buffer; //make nextbuff point to beginning fo buffe |
qdanilc | 3:b593f43b7251 | 74 | |
qdanilc | 1:cd1fe330bc2a | 75 | while(esp.readable()) { |
qdanilc | 3:b593f43b7251 | 76 | d = esp.getc(); // get character sent by the esp |
qdanilc | 3:b593f43b7251 | 77 | |
qdanilc | 3:b593f43b7251 | 78 | pc.putc(d); // get character sent by the esp |
qdanilc | 3:b593f43b7251 | 79 | * nextBuff = d; //store in nextBuff |
qdanilc | 3:b593f43b7251 | 80 | nextBuff ++; |
qdanilc | 3:b593f43b7251 | 81 | if (d== '\n' ) { // if our first two characters are return and newline, then we have reached the end of the line |
qdanilc | 3:b593f43b7251 | 82 | pc.putc('!'); |
qdanilc | 3:b593f43b7251 | 83 | nextBuff = buffer; // what does this do????????????/ |
qdanilc | 3:b593f43b7251 | 84 | gotLine = 1; |
qdanilc | 3:b593f43b7251 | 85 | } |
qdanilc | 3:b593f43b7251 | 86 | |
qdanilc | 3:b593f43b7251 | 87 | |
qdanilc | 1:cd1fe330bc2a | 88 | } |
qdanilc | 1:cd1fe330bc2a | 89 | } |
qdanilc | 1:cd1fe330bc2a | 90 | void pc_recv() |
qdanilc | 1:cd1fe330bc2a | 91 | { |
qdanilc | 1:cd1fe330bc2a | 92 | char c; |
qdanilc | 1:cd1fe330bc2a | 93 | greenLED = !greenLED; |
qdanilc | 1:cd1fe330bc2a | 94 | while(pc.readable()) { |
qdanilc | 3:b593f43b7251 | 95 | c=pc.getc(); // get typed input |
qdanilc | 3:b593f43b7251 | 96 | |
qdanilc | 3:b593f43b7251 | 97 | |
qdanilc | 3:b593f43b7251 | 98 | if(c==13) { //send enter |
qdanilc | 3:b593f43b7251 | 99 | |
qdanilc | 2:0b9ca0830cd3 | 100 | esp.putc(10); //send the command by pressing enter |
qdanilc | 1:cd1fe330bc2a | 101 | pc.putc(10); //ie makes enter perform as expected |
qdanilc | 3:b593f43b7251 | 102 | } |
qdanilc | 3:b593f43b7251 | 103 | |
qdanilc | 3:b593f43b7251 | 104 | else if(c=='y') { // send the startup sequence |
qdanilc | 3:b593f43b7251 | 105 | start(); |
qdanilc | 3:b593f43b7251 | 106 | } else { |
qdanilc | 3:b593f43b7251 | 107 | esp.putc(c); |
qdanilc | 1:cd1fe330bc2a | 108 | } |
qdanilc | 1:cd1fe330bc2a | 109 | } |
qdanilc | 1:cd1fe330bc2a | 110 | } |
qdanilc | 1:cd1fe330bc2a | 111 | |
qdanilc | 3:b593f43b7251 | 112 | void initiateWifi() |
qdanilc | 3:b593f43b7251 | 113 | { |
qdanilc | 3:b593f43b7251 | 114 | |
qdanilc | 1:cd1fe330bc2a | 115 | //initialise the pins |
qdanilc | 1:cd1fe330bc2a | 116 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 117 | chpdPin = 1; |
qdanilc | 3:b593f43b7251 | 118 | |
qdanilc | 1:cd1fe330bc2a | 119 | reset(); //perform an initial reset |
qdanilc | 3:b593f43b7251 | 120 | |
qdanilc | 1:cd1fe330bc2a | 121 | blueLED = 1; //turn on test light |
qdanilc | 1:cd1fe330bc2a | 122 | |
qdanilc | 1:cd1fe330bc2a | 123 | pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services |
qdanilc | 1:cd1fe330bc2a | 124 | esp.attach(&esp_recv, Serial::RxIrq); |
qdanilc | 3:b593f43b7251 | 125 | for (char i=10; i>0; i--) { |
qdanilc | 3:b593f43b7251 | 126 | pc.printf("%d\r\n", i); |
qdanilc | 3:b593f43b7251 | 127 | wait(1); |
qdanilc | 3:b593f43b7251 | 128 | } |
qdanilc | 3:b593f43b7251 | 129 | start(); |
qdanilc | 1:cd1fe330bc2a | 130 | |
qdanilc | 1:cd1fe330bc2a | 131 | } |