Communicates with the WiFi - be very careful when using as it is tempremental.
Dependencies: mbed
initiateWifi.cpp@7:bfb2b5142c29, 2017-05-25 (annotated)
- Committer:
- qdanilc
- Date:
- Thu May 25 12:12:58 2017 +0000
- Revision:
- 7:bfb2b5142c29
- Parent:
- 6:99841ed0c8a8
- Child:
- 8:e09edf050cca
sends manual messages to connect to json time server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
qdanilc | 1:cd1fe330bc2a | 1 | #include "mbed.h" |
qdanilc | 1:cd1fe330bc2a | 2 | #include "RawSerial.h" |
qdanilc | 1:cd1fe330bc2a | 3 | |
qdanilc | 7:bfb2b5142c29 | 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 | 7:bfb2b5142c29 | 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 | 7:bfb2b5142c29 | 36 | void start() |
qdanilc | 7:bfb2b5142c29 | 37 | { |
qdanilc | 2:0b9ca0830cd3 | 38 | pc.printf("Initialising start sequence\r\n"); |
qdanilc | 7:bfb2b5142c29 | 39 | |
qdanilc | 2:0b9ca0830cd3 | 40 | //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]); |
qdanilc | 2:0b9ca0830cd3 | 41 | numberOfCommands = 10; |
qdanilc | 7:bfb2b5142c29 | 42 | |
qdanilc | 7:bfb2b5142c29 | 43 | for (int i = 0; i<numberOfCommands; i++) { //replace the 9 with sizeOf start commands |
qdanilc | 7:bfb2b5142c29 | 44 | |
qdanilc | 5:79618eee4d54 | 45 | pc.printf("\r\ncommand %d",i); //identify the command number |
qdanilc | 7:bfb2b5142c29 | 46 | pc.printf("\r\n"); |
qdanilc | 7:bfb2b5142c29 | 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 | 7:bfb2b5142c29 | 50 | |
qdanilc | 7:bfb2b5142c29 | 51 | |
qdanilc | 2:0b9ca0830cd3 | 52 | } |
qdanilc | 2:0b9ca0830cd3 | 53 | } |
qdanilc | 2:0b9ca0830cd3 | 54 | |
qdanilc | 7:bfb2b5142c29 | 55 | void reset() //resets the module by turning rst off for 2 seconds then on again |
qdanilc | 7:bfb2b5142c29 | 56 | { |
qdanilc | 1:cd1fe330bc2a | 57 | rstPin = 0; |
qdanilc | 1:cd1fe330bc2a | 58 | wait(2); |
qdanilc | 1:cd1fe330bc2a | 59 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 60 | pc.printf("Reset complete\r\n"); |
qdanilc | 7:bfb2b5142c29 | 61 | |
qdanilc | 1:cd1fe330bc2a | 62 | } |
qdanilc | 1:cd1fe330bc2a | 63 | void esp_recv() |
qdanilc | 1:cd1fe330bc2a | 64 | { |
qdanilc | 5:79618eee4d54 | 65 | redLED = !redLED; |
qdanilc | 1:cd1fe330bc2a | 66 | while(esp.readable()) { |
qdanilc | 5:79618eee4d54 | 67 | pc.putc(esp.getc()); |
qdanilc | 5:79618eee4d54 | 68 | //wait_us(1); |
qdanilc | 1:cd1fe330bc2a | 69 | } |
qdanilc | 1:cd1fe330bc2a | 70 | } |
qdanilc | 1:cd1fe330bc2a | 71 | void pc_recv() |
qdanilc | 1:cd1fe330bc2a | 72 | { |
qdanilc | 1:cd1fe330bc2a | 73 | char c; |
qdanilc | 1:cd1fe330bc2a | 74 | greenLED = !greenLED; |
qdanilc | 1:cd1fe330bc2a | 75 | while(pc.readable()) { |
qdanilc | 5:79618eee4d54 | 76 | c=pc.getc(); |
qdanilc | 5:79618eee4d54 | 77 | esp.putc(c); |
qdanilc | 7:bfb2b5142c29 | 78 | |
qdanilc | 7:bfb2b5142c29 | 79 | |
qdanilc | 5:79618eee4d54 | 80 | if(c==13) { |
qdanilc | 5:79618eee4d54 | 81 | pc.printf("\r\ncommand %d",k); //identify the command number |
qdanilc | 7:bfb2b5142c29 | 82 | pc.printf("\r\n"); |
qdanilc | 7:bfb2b5142c29 | 83 | |
qdanilc | 5:79618eee4d54 | 84 | esp.printf("%s\r\n",startCommands[k]); //send command |
qdanilc | 1:cd1fe330bc2a | 85 | pc.putc(10); //ie makes enter perform as expected |
qdanilc | 7:bfb2b5142c29 | 86 | |
qdanilc | 7:bfb2b5142c29 | 87 | if (k <10) { |
qdanilc | 7:bfb2b5142c29 | 88 | k++; //reset k if required |
qdanilc | 7:bfb2b5142c29 | 89 | } else { |
qdanilc | 7:bfb2b5142c29 | 90 | k=0; |
qdanilc | 7:bfb2b5142c29 | 91 | } |
qdanilc | 1:cd1fe330bc2a | 92 | } |
qdanilc | 1:cd1fe330bc2a | 93 | } |
qdanilc | 1:cd1fe330bc2a | 94 | } |
qdanilc | 1:cd1fe330bc2a | 95 | |
qdanilc | 7:bfb2b5142c29 | 96 | void initiateWifi() |
qdanilc | 7:bfb2b5142c29 | 97 | { |
qdanilc | 7:bfb2b5142c29 | 98 | |
qdanilc | 1:cd1fe330bc2a | 99 | //initialise the pins |
qdanilc | 1:cd1fe330bc2a | 100 | rstPin = 1; |
qdanilc | 1:cd1fe330bc2a | 101 | chpdPin = 1; |
qdanilc | 7:bfb2b5142c29 | 102 | |
qdanilc | 1:cd1fe330bc2a | 103 | reset(); //perform an initial reset |
qdanilc | 7:bfb2b5142c29 | 104 | |
qdanilc | 1:cd1fe330bc2a | 105 | blueLED = 1; //turn on test light |
qdanilc | 1:cd1fe330bc2a | 106 | |
qdanilc | 1:cd1fe330bc2a | 107 | pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services |
qdanilc | 1:cd1fe330bc2a | 108 | esp.attach(&esp_recv, Serial::RxIrq); |
qdanilc | 7:bfb2b5142c29 | 109 | |
qdanilc | 5:79618eee4d54 | 110 | //wait(10); |
qdanilc | 4:65f657998b7d | 111 | //start(); |
qdanilc | 1:cd1fe330bc2a | 112 | |
qdanilc | 1:cd1fe330bc2a | 113 | } |