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