Communicates with the WiFi - be very careful when using as it is tempremental.

Dependencies:   mbed

Committer:
qdanilc
Date:
Thu May 25 11:41:21 2017 +0000
Revision:
5:79618eee4d54
Parent:
4:65f657998b7d
Child:
6:99841ed0c8a8
Manually sends a series of commands when you press enter;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
qdanilc 1:cd1fe330bc2a 1 #include "mbed.h"
qdanilc 1:cd1fe330bc2a 2 #include "RawSerial.h"
qdanilc 1:cd1fe330bc2a 3
qdanilc 5:79618eee4d54 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 5:79618eee4d54 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 2:0b9ca0830cd3 18 char* startCommands[10]= {
qdanilc 2:0b9ca0830cd3 19 "AT",
qdanilc 2:0b9ca0830cd3 20 "AT",
qdanilc 2:0b9ca0830cd3 21 "AT",
qdanilc 2:0b9ca0830cd3 22 "AT+RST",
qdanilc 2:0b9ca0830cd3 23 "AT+CWMODE=1",
qdanilc 5:79618eee4d54 24 "AT+CWJAP=\"Hotspot\",\"password\"",
qdanilc 2:0b9ca0830cd3 25 "AT+CIFSR",
qdanilc 2:0b9ca0830cd3 26 "AT+CIPMUX=1",
qdanilc 5:79618eee4d54 27 "AT+CIPSERVER=1,8080",
qdanilc 5:79618eee4d54 28 "AT+CIPSEND=0,"
qdanilc 2:0b9ca0830cd3 29 };
qdanilc 1:cd1fe330bc2a 30
qdanilc 1:cd1fe330bc2a 31
qdanilc 2:0b9ca0830cd3 32 int numberOfCommands;
qdanilc 5:79618eee4d54 33
qdanilc 5:79618eee4d54 34 int k = 0; //counter for comm numb
qdanilc 2:0b9ca0830cd3 35
qdanilc 2:0b9ca0830cd3 36
qdanilc 5:79618eee4d54 37 void start() {
qdanilc 2:0b9ca0830cd3 38 pc.printf("Initialising start sequence\r\n");
qdanilc 5:79618eee4d54 39
qdanilc 2:0b9ca0830cd3 40 //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]);
qdanilc 2:0b9ca0830cd3 41 numberOfCommands = 10;
qdanilc 5:79618eee4d54 42
qdanilc 5:79618eee4d54 43 for (int i = 0; i<numberOfCommands; i++) { //replace the 9 with sizeOf start commands
qdanilc 5:79618eee4d54 44
qdanilc 5:79618eee4d54 45 pc.printf("\r\ncommand %d",i); //identify the command number
qdanilc 5:79618eee4d54 46 pc.printf("\r\n");
qdanilc 5:79618eee4d54 47
qdanilc 5:79618eee4d54 48 esp.printf("%s\r\n",startCommands[i]); //send command
qdanilc 5:79618eee4d54 49 esp.putc(10); //send the command by pressing enter
qdanilc 5:79618eee4d54 50
qdanilc 5:79618eee4d54 51
qdanilc 2:0b9ca0830cd3 52 }
qdanilc 2:0b9ca0830cd3 53 }
qdanilc 2:0b9ca0830cd3 54
qdanilc 5:79618eee4d54 55 void reset() { //resets the module by turning rst off for 2 seconds then on again
qdanilc 1:cd1fe330bc2a 56 rstPin = 0;
qdanilc 1:cd1fe330bc2a 57 wait(2);
qdanilc 1:cd1fe330bc2a 58 rstPin = 1;
qdanilc 1:cd1fe330bc2a 59 pc.printf("Reset complete\r\n");
qdanilc 5:79618eee4d54 60
qdanilc 1:cd1fe330bc2a 61 }
qdanilc 1:cd1fe330bc2a 62 void esp_recv()
qdanilc 1:cd1fe330bc2a 63 {
qdanilc 5:79618eee4d54 64 redLED = !redLED;
qdanilc 1:cd1fe330bc2a 65 while(esp.readable()) {
qdanilc 5:79618eee4d54 66 pc.putc(esp.getc());
qdanilc 5:79618eee4d54 67 //wait_us(1);
qdanilc 1:cd1fe330bc2a 68 }
qdanilc 1:cd1fe330bc2a 69 }
qdanilc 1:cd1fe330bc2a 70 void pc_recv()
qdanilc 1:cd1fe330bc2a 71 {
qdanilc 1:cd1fe330bc2a 72 char c;
qdanilc 1:cd1fe330bc2a 73 greenLED = !greenLED;
qdanilc 1:cd1fe330bc2a 74 while(pc.readable()) {
qdanilc 5:79618eee4d54 75 c=pc.getc();
qdanilc 5:79618eee4d54 76 esp.putc(c);
qdanilc 5:79618eee4d54 77
qdanilc 5:79618eee4d54 78
qdanilc 5:79618eee4d54 79 if(c==13) {
qdanilc 5:79618eee4d54 80 pc.printf("\r\ncommand %d",k); //identify the command number
qdanilc 5:79618eee4d54 81 pc.printf("\r\n");
qdanilc 5:79618eee4d54 82
qdanilc 5:79618eee4d54 83 esp.printf("%s\r\n",startCommands[k]); //send command
qdanilc 1:cd1fe330bc2a 84 pc.putc(10); //ie makes enter perform as expected
qdanilc 5:79618eee4d54 85
qdanilc 5:79618eee4d54 86 if (k <10) { k++;} else {k=0;} //reset k if required
qdanilc 1:cd1fe330bc2a 87 }
qdanilc 1:cd1fe330bc2a 88 }
qdanilc 1:cd1fe330bc2a 89 }
qdanilc 1:cd1fe330bc2a 90
qdanilc 5:79618eee4d54 91 void initiateWifi() {
qdanilc 5:79618eee4d54 92
qdanilc 1:cd1fe330bc2a 93 //initialise the pins
qdanilc 1:cd1fe330bc2a 94 rstPin = 1;
qdanilc 1:cd1fe330bc2a 95 chpdPin = 1;
qdanilc 5:79618eee4d54 96
qdanilc 1:cd1fe330bc2a 97 reset(); //perform an initial reset
qdanilc 5:79618eee4d54 98
qdanilc 1:cd1fe330bc2a 99 blueLED = 1; //turn on test light
qdanilc 1:cd1fe330bc2a 100
qdanilc 1:cd1fe330bc2a 101 pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
qdanilc 1:cd1fe330bc2a 102 esp.attach(&esp_recv, Serial::RxIrq);
qdanilc 5:79618eee4d54 103
qdanilc 5:79618eee4d54 104 //wait(10);
qdanilc 4:65f657998b7d 105 //start();
qdanilc 1:cd1fe330bc2a 106
qdanilc 1:cd1fe330bc2a 107 }