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

Dependencies:   mbed

Committer:
qdanilc
Date:
Thu May 25 12:12:09 2017 +0000
Revision:
6:99841ed0c8a8
Parent:
5:79618eee4d54
Child:
7:bfb2b5142c29
sends commands in a manual sequence

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