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

Dependencies:   mbed

Committer:
qdanilc
Date:
Wed May 24 16:17:04 2017 +0000
Revision:
2:0b9ca0830cd3
Parent:
1:cd1fe330bc2a
Child:
3:b593f43b7251
Sends commands on enter press, from a predefined set. Now to find how to download time;

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 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 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 2:0b9ca0830cd3 24 "AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"",
qdanilc 2:0b9ca0830cd3 25 "AT+CIFSR",
qdanilc 2:0b9ca0830cd3 26 "AT+CIPMUX=1",
qdanilc 2:0b9ca0830cd3 27 "AT+CIPSERVER=1,8080",
qdanilc 2:0b9ca0830cd3 28 "AT+CIPSEND=0,"
qdanilc 2:0b9ca0830cd3 29 };
qdanilc 1:cd1fe330bc2a 30
qdanilc 1:cd1fe330bc2a 31
qdanilc 2:0b9ca0830cd3 32 int numberOfCommands;
qdanilc 2:0b9ca0830cd3 33
qdanilc 2:0b9ca0830cd3 34 int k = 0; //counter for comm numb
qdanilc 2:0b9ca0830cd3 35
qdanilc 2:0b9ca0830cd3 36
qdanilc 2:0b9ca0830cd3 37 void start() {
qdanilc 2:0b9ca0830cd3 38 pc.printf("Initialising start sequence\r\n");
qdanilc 2:0b9ca0830cd3 39
qdanilc 2:0b9ca0830cd3 40 //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]);
qdanilc 2:0b9ca0830cd3 41 numberOfCommands = 10;
qdanilc 2:0b9ca0830cd3 42
qdanilc 2:0b9ca0830cd3 43 for (int i = 0; i<numberOfCommands; i++) { //replace the 9 with sizeOf start commands
qdanilc 2:0b9ca0830cd3 44
qdanilc 2:0b9ca0830cd3 45 pc.printf("\r\ncommand %d",i); //identify the command number
qdanilc 2:0b9ca0830cd3 46 pc.printf("\r\n");
qdanilc 2:0b9ca0830cd3 47
qdanilc 2:0b9ca0830cd3 48 esp.printf("%s\r\n",startCommands[i]); //send command
qdanilc 2:0b9ca0830cd3 49 esp.putc(10); //send the command by pressing enter
qdanilc 2:0b9ca0830cd3 50
qdanilc 2:0b9ca0830cd3 51
qdanilc 2:0b9ca0830cd3 52 }
qdanilc 2:0b9ca0830cd3 53 }
qdanilc 2:0b9ca0830cd3 54
qdanilc 1:cd1fe330bc2a 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 1:cd1fe330bc2a 60
qdanilc 1:cd1fe330bc2a 61 }
qdanilc 1:cd1fe330bc2a 62 void esp_recv()
qdanilc 1:cd1fe330bc2a 63 {
qdanilc 1:cd1fe330bc2a 64 redLED = !redLED;
qdanilc 1:cd1fe330bc2a 65 while(esp.readable()) {
qdanilc 1:cd1fe330bc2a 66 pc.putc(esp.getc());
qdanilc 1:cd1fe330bc2a 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 1:cd1fe330bc2a 75 c=pc.getc();
qdanilc 1:cd1fe330bc2a 76 esp.putc(c);
qdanilc 2:0b9ca0830cd3 77
qdanilc 2:0b9ca0830cd3 78
qdanilc 1:cd1fe330bc2a 79 if(c==13) {
qdanilc 2:0b9ca0830cd3 80 pc.printf("\r\ncommand %d",k); //identify the command number
qdanilc 2:0b9ca0830cd3 81 pc.printf("\r\n");
qdanilc 2:0b9ca0830cd3 82
qdanilc 2:0b9ca0830cd3 83 esp.printf("%s\r\n",startCommands[k]); //send command
qdanilc 2:0b9ca0830cd3 84 esp.putc(10); //send the command by pressing enter
qdanilc 1:cd1fe330bc2a 85 pc.putc(10); //ie makes enter perform as expected
qdanilc 2:0b9ca0830cd3 86
qdanilc 2:0b9ca0830cd3 87 if (k <10) { k++;} else {k=0;} //reset k if required
qdanilc 1:cd1fe330bc2a 88 }
qdanilc 1:cd1fe330bc2a 89 }
qdanilc 1:cd1fe330bc2a 90 }
qdanilc 2:0b9ca0830cd3 91 void sendCommand() {
qdanilc 2:0b9ca0830cd3 92 char c;
qdanilc 2:0b9ca0830cd3 93 while(pc.readable()) {
qdanilc 2:0b9ca0830cd3 94 c=pc.getc();
qdanilc 2:0b9ca0830cd3 95 //pc.putc(c); // echo back
qdanilc 2:0b9ca0830cd3 96 int k = 0;
qdanilc 2:0b9ca0830cd3 97 if(c==13) {
qdanilc 2:0b9ca0830cd3 98
qdanilc 2:0b9ca0830cd3 99 pc.printf("\r\ncommand %d",k); //identify the command number
qdanilc 2:0b9ca0830cd3 100 pc.printf("\r\n");
qdanilc 2:0b9ca0830cd3 101
qdanilc 2:0b9ca0830cd3 102 esp.printf("%s\r\n",startCommands[k]); //send command
qdanilc 2:0b9ca0830cd3 103 esp.putc(10); //send the command by pressing enter
qdanilc 2:0b9ca0830cd3 104 pc.putc(10); //ie makes enter perform as expected
qdanilc 2:0b9ca0830cd3 105
qdanilc 2:0b9ca0830cd3 106 if (k <10) { k ++;} else {k=0;} //reset k if required
qdanilc 2:0b9ca0830cd3 107 }
qdanilc 2:0b9ca0830cd3 108 }
qdanilc 2:0b9ca0830cd3 109
qdanilc 2:0b9ca0830cd3 110 }
qdanilc 1:cd1fe330bc2a 111
qdanilc 2:0b9ca0830cd3 112 void initiateWifi() {
qdanilc 1:cd1fe330bc2a 113
qdanilc 1:cd1fe330bc2a 114 //initialise the pins
qdanilc 1:cd1fe330bc2a 115 rstPin = 1;
qdanilc 1:cd1fe330bc2a 116 chpdPin = 1;
qdanilc 1:cd1fe330bc2a 117
qdanilc 1:cd1fe330bc2a 118 reset(); //perform an initial reset
qdanilc 1:cd1fe330bc2a 119
qdanilc 1:cd1fe330bc2a 120 blueLED = 1; //turn on test light
qdanilc 1:cd1fe330bc2a 121
qdanilc 1:cd1fe330bc2a 122 pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
qdanilc 1:cd1fe330bc2a 123 esp.attach(&esp_recv, Serial::RxIrq);
qdanilc 1:cd1fe330bc2a 124
qdanilc 2:0b9ca0830cd3 125 //wait(10);
qdanilc 2:0b9ca0830cd3 126 //start();
qdanilc 1:cd1fe330bc2a 127
qdanilc 1:cd1fe330bc2a 128 }