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

Dependencies:   mbed

Committer:
qdanilc
Date:
Thu May 25 15:06:50 2017 +0000
Revision:
8:e09edf050cca
Parent:
7:bfb2b5142c29
Child:
9:8cf34e4f9ca0
Receives data from a time website, including a timestamp which can be used to set 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 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 8:e09edf050cca 18
qdanilc 8:e09edf050cca 19 char * getRequest = "GET /HTTP/1.1\r\nHost:172.217.23.19\r\n\r\n";
qdanilc 8:e09edf050cca 20
qdanilc 2:0b9ca0830cd3 21 char* startCommands[10]= {
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 8:e09edf050cca 25 "AT+CIPMUX=1",
qdanilc 8:e09edf050cca 26 "AT+CIPSTART=1,\"TCP\",\"172.217.23.19\",80", // change to 172.217.23.19\", \"80\"",
qdanilc 8:e09edf050cca 27 "AT+CIPSEND=1,45",
qdanilc 8:e09edf050cca 28 getRequest
qdanilc 2:0b9ca0830cd3 29 };
qdanilc 1:cd1fe330bc2a 30
qdanilc 1:cd1fe330bc2a 31
qdanilc 2:0b9ca0830cd3 32 int numberOfCommands;
qdanilc 5:79618eee4d54 33
qdanilc 5:79618eee4d54 34 int k = 0; //counter for comm numb
qdanilc 2:0b9ca0830cd3 35
qdanilc 2:0b9ca0830cd3 36
qdanilc 7:bfb2b5142c29 37 void start()
qdanilc 7:bfb2b5142c29 38 {
qdanilc 2:0b9ca0830cd3 39 pc.printf("Initialising start sequence\r\n");
qdanilc 7:bfb2b5142c29 40
qdanilc 2:0b9ca0830cd3 41 //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]);
qdanilc 2:0b9ca0830cd3 42 numberOfCommands = 10;
qdanilc 7:bfb2b5142c29 43
qdanilc 7:bfb2b5142c29 44 for (int i = 0; i<numberOfCommands; i++) { //replace the 9 with sizeOf start commands
qdanilc 7:bfb2b5142c29 45
qdanilc 5:79618eee4d54 46 pc.printf("\r\ncommand %d",i); //identify the command number
qdanilc 7:bfb2b5142c29 47 pc.printf("\r\n");
qdanilc 7:bfb2b5142c29 48
qdanilc 5:79618eee4d54 49 esp.printf("%s\r\n",startCommands[i]); //send command
qdanilc 5:79618eee4d54 50 esp.putc(10); //send the command by pressing enter
qdanilc 7:bfb2b5142c29 51
qdanilc 7:bfb2b5142c29 52
qdanilc 2:0b9ca0830cd3 53 }
qdanilc 2:0b9ca0830cd3 54 }
qdanilc 2:0b9ca0830cd3 55
qdanilc 7:bfb2b5142c29 56 void reset() //resets the module by turning rst off for 2 seconds then on again
qdanilc 7:bfb2b5142c29 57 {
qdanilc 1:cd1fe330bc2a 58 rstPin = 0;
qdanilc 1:cd1fe330bc2a 59 wait(2);
qdanilc 1:cd1fe330bc2a 60 rstPin = 1;
qdanilc 1:cd1fe330bc2a 61 pc.printf("Reset complete\r\n");
qdanilc 7:bfb2b5142c29 62
qdanilc 1:cd1fe330bc2a 63 }
qdanilc 1:cd1fe330bc2a 64 void esp_recv()
qdanilc 1:cd1fe330bc2a 65 {
qdanilc 5:79618eee4d54 66 redLED = !redLED;
qdanilc 1:cd1fe330bc2a 67 while(esp.readable()) {
qdanilc 5:79618eee4d54 68 pc.putc(esp.getc());
qdanilc 5:79618eee4d54 69 //wait_us(1);
qdanilc 1:cd1fe330bc2a 70 }
qdanilc 1:cd1fe330bc2a 71 }
qdanilc 1:cd1fe330bc2a 72 void pc_recv()
qdanilc 1:cd1fe330bc2a 73 {
qdanilc 1:cd1fe330bc2a 74 char c;
qdanilc 1:cd1fe330bc2a 75 greenLED = !greenLED;
qdanilc 1:cd1fe330bc2a 76 while(pc.readable()) {
qdanilc 5:79618eee4d54 77 c=pc.getc();
qdanilc 5:79618eee4d54 78 esp.putc(c);
qdanilc 7:bfb2b5142c29 79
qdanilc 7:bfb2b5142c29 80
qdanilc 8:e09edf050cca 81 if(c=='x') {
qdanilc 5:79618eee4d54 82 pc.printf("\r\ncommand %d",k); //identify the command number
qdanilc 7:bfb2b5142c29 83 pc.printf("\r\n");
qdanilc 7:bfb2b5142c29 84
qdanilc 5:79618eee4d54 85 esp.printf("%s\r\n",startCommands[k]); //send command
qdanilc 1:cd1fe330bc2a 86 pc.putc(10); //ie makes enter perform as expected
qdanilc 7:bfb2b5142c29 87
qdanilc 7:bfb2b5142c29 88 if (k <10) {
qdanilc 7:bfb2b5142c29 89 k++; //reset k if required
qdanilc 7:bfb2b5142c29 90 } else {
qdanilc 7:bfb2b5142c29 91 k=0;
qdanilc 7:bfb2b5142c29 92 }
qdanilc 8:e09edf050cca 93 } else if(c==13) {
qdanilc 8:e09edf050cca 94 pc.putc(10); //ie makes enter perform as expected
qdanilc 8:e09edf050cca 95 esp.putc(10);
qdanilc 1:cd1fe330bc2a 96 }
qdanilc 1:cd1fe330bc2a 97 }
qdanilc 1:cd1fe330bc2a 98 }
qdanilc 1:cd1fe330bc2a 99
qdanilc 7:bfb2b5142c29 100 void initiateWifi()
qdanilc 7:bfb2b5142c29 101 {
qdanilc 7:bfb2b5142c29 102
qdanilc 1:cd1fe330bc2a 103 //initialise the pins
qdanilc 1:cd1fe330bc2a 104 rstPin = 1;
qdanilc 1:cd1fe330bc2a 105 chpdPin = 1;
qdanilc 7:bfb2b5142c29 106
qdanilc 1:cd1fe330bc2a 107 reset(); //perform an initial reset
qdanilc 7:bfb2b5142c29 108
qdanilc 1:cd1fe330bc2a 109 blueLED = 1; //turn on test light
qdanilc 1:cd1fe330bc2a 110
qdanilc 1:cd1fe330bc2a 111 pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
qdanilc 1:cd1fe330bc2a 112 esp.attach(&esp_recv, Serial::RxIrq);
qdanilc 7:bfb2b5142c29 113
qdanilc 5:79618eee4d54 114 //wait(10);
qdanilc 4:65f657998b7d 115 //start();
qdanilc 1:cd1fe330bc2a 116
qdanilc 1:cd1fe330bc2a 117 }