WiFi test

Dependencies:   mbed

Fork of SerialPassthroughcjsESP8266 by chris stevens

main.cpp

Committer:
danlock10y
Date:
2016-06-09
Revision:
10:56ca30af7a7b
Parent:
9:b1344ca3497d

File content as of revision 10:56ca30af7a7b:

/* Simpler prog based on the serial passthrough code to enable a command line driven test of esp8266
* wifi modules.
* NB this uses the mbed sleep() command to form a low power system but on some MCUs this is a problem
* this works fine on an lpc1768 but not as yet on the KL25Z
*/
#include "mbed.h"

RawSerial  pc(USBTX, USBRX); // serial terminal for the pc connection
RawSerial dev(PTE0,PTE1);  // for KL25Z... asuming one can't use the PTA1 version which is the stdio
//RawSerial  dev(p28,p27); // serial uart for connecting to the esp8266 tx,rx NB mbed tx must connect ot esp rx and vice versa
DigitalOut led1(LED1); // twp leds  
DigitalOut led4(LED4); // to allow visual check of bidirectional comms
DigitalOut rst(PTD1); // single digital pin to drive the esp8266 reset line

// subroutine to run anytime a serial interrupt arrives from the device
// this basically passes everything thatthe device produces on to the pc terminal screen
void dev_recv()
{
    led1 = !led1;
    while(dev.readable()) {
        pc.putc(dev.getc());
        //wait_us(1);
    }
}
// subroutine to service the serial interrupt on the pc connection
// this is a bit more complex - it takes what the use sends on the pc and copies it on to the device 
// the esp should echo these straight back to the the pc if all is well
// this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a)
void pc_recv()
{
    char c;
    led4 = !led4;
    while(pc.readable()) {
        c=pc.getc();
        dev.putc(c);
        pc.putc(c); // echo back
        if(c==13) {dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc
        pc.putc(10);
            }
    }
}

int main()
{
    rst=0;
    wait(1);
    rst=1; // send the esp8266 reset
    wait(1);
    pc.printf("ok off we go....\n\r");
    
    pc.baud(115200); // NB maybe this should go before this
    dev.baud(115200);

    pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
    dev.attach(&dev_recv, Serial::RxIrq);
        
int i=0;
    wait(5);
    pc.printf("Send rst\r\n");
    dev.printf("AT+RST\r\n");
    
    wait(2);
    pc.printf("Set mode\r\n");
    dev.printf("AT+CWMODE=1\r\n");
    
    wait(2);
    pc.printf("\r\n Login\r\n");
    dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
    
    wait(10);
    pc.printf("\r\n Find IP\r\n");
    dev.printf("AT+CIFSR\r\n");
    
    wait(5);
    pc.printf("\r\n MUX\r\n");
    dev.printf("AT+CIPMUX=1\r\n");
    
    wait(2);
    pc.printf("\r\n Open connection\r\n");
    dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.6\",5050\r\n");
    
    wait(2);
    pc.printf("\r\n Send Message\r\n");
    dev.printf("AT+CIPSEND=0,14\r\n");
 wait(2);
    pc.printf("\r\n Send the Message\r\n");
    dev.printf("Hello cruel!\r\n");
    
    while(1) {
        
       (i++)%10; // THIS USED TO BE A SLEEP COMMAND BUT IT WAS CAUSING SOME TROUBLE.
    }
}