Communicates with the WiFi - be very careful when using as it is tempremental.
Dependencies: mbed
initiateWifi.cpp
- Committer:
- qdanilc
- Date:
- 2017-05-25
- Revision:
- 3:b593f43b7251
- Parent:
- 2:0b9ca0830cd3
- Child:
- 4:65f657998b7d
File content as of revision 3:b593f43b7251:
#include "mbed.h" #include "RawSerial.h" //setup the other wifi pins DigitalOut rstPin (PTE29); DigitalOut chpdPin (PTE23); RawSerial esp (PTC4, PTC3, 115200); //setup a serial to the (board) Tx = PTC4 and Rx = PTC3 pins at baud rate 115200 Serial pc(USBTX, USBRX, 115200); DigitalOut redLED(LED1); // twp leds DigitalOut greenLED(LED2); // to allow visual check of bidirectional comms DigitalOut blueLED(LED3); //to prove it's working char c; char * buffer = (char *)malloc(128); //setup a buffer 128 characters long char * nextBuff; // a pointer to the next character being added to the buffer char input; //character to store inputs/outputs in communication char* startCommands[10]= { "AT", "AT", "AT", "AT+RST", "AT+CWMODE=1", "AT+CWJAP=\"Hotspot\",\"password\"", //name and password of wifi "AT+CIFSR", "AT+CIPMUX=1", "AT+CIPSERVER=1,8080", "AT+CIPSEND=0," }; int numberOfCommands; int commandsExecuted; int gotLine = 0; // boolean for whether we have a line void start() { pc.printf("Initialising start sequence\r\n"); //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]); numberOfCommands = 10; commandsExecuted = 0; esp.printf("%s\r\n",startCommands[commandsExecuted]); while (commandsExecuted < numberOfCommands) { //loop until we have executed all the commands if(gotLine) { // if esp signals that it's ready to receive a command, send the next command pc.printf("\r\ncommand %d",commandsExecuted); //identify the command number pc.printf("\r\n"); esp.printf("%s\r\n",startCommands[commandsExecuted]); //send command commandsExecuted ++; //increment the counter gotLine = 0; } } } void reset() //resets the module by turning rst off for 2 seconds then on again { rstPin = 0; wait(2); rstPin = 1; pc.printf("Reset complete\r\n"); } void esp_recv() { char d; nextBuff = buffer; //make nextbuff point to beginning fo buffe while(esp.readable()) { d = esp.getc(); // get character sent by the esp pc.putc(d); // get character sent by the esp * nextBuff = d; //store in nextBuff nextBuff ++; if (d== '\n' ) { // if our first two characters are return and newline, then we have reached the end of the line pc.putc('!'); nextBuff = buffer; // what does this do????????????/ gotLine = 1; } } } void pc_recv() { char c; greenLED = !greenLED; while(pc.readable()) { c=pc.getc(); // get typed input if(c==13) { //send enter esp.putc(10); //send the command by pressing enter pc.putc(10); //ie makes enter perform as expected } else if(c=='y') { // send the startup sequence start(); } else { esp.putc(c); } } } void initiateWifi() { //initialise the pins rstPin = 1; chpdPin = 1; reset(); //perform an initial reset blueLED = 1; //turn on test light pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services esp.attach(&esp_recv, Serial::RxIrq); for (char i=10; i>0; i--) { pc.printf("%d\r\n", i); wait(1); } start(); }