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

Dependencies:   mbed

Committer:
qdanilc
Date:
Fri May 26 09:47:27 2017 +0000
Revision:
9:8cf34e4f9ca0
Parent:
8:e09edf050cca
Child:
10:e5eee7bc1a7d
Retreives a correct message about the seconds since the epoch from jsontest.com

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 9:8cf34e4f9ca0 3 #include "string.h"
qdanilc 9:8cf34e4f9ca0 4 #include "stdio.h"
qdanilc 1:cd1fe330bc2a 5
qdanilc 7:bfb2b5142c29 6 //setup the other wifi pins
qdanilc 1:cd1fe330bc2a 7 DigitalOut rstPin (PTE29);
qdanilc 1:cd1fe330bc2a 8 DigitalOut chpdPin (PTE23);
qdanilc 1:cd1fe330bc2a 9
qdanilc 1:cd1fe330bc2a 10
qdanilc 1:cd1fe330bc2a 11 RawSerial esp (PTC4, PTC3, 115200); //setup a serial to the (board) Tx = PTC4 and Rx = PTC3 pins at baud rate 115200
qdanilc 1:cd1fe330bc2a 12
qdanilc 1:cd1fe330bc2a 13 Serial pc(USBTX, USBRX, 115200);
qdanilc 1:cd1fe330bc2a 14
qdanilc 7:bfb2b5142c29 15 DigitalOut redLED(LED1); // twp leds
qdanilc 1:cd1fe330bc2a 16 DigitalOut greenLED(LED2); // to allow visual check of bidirectional comms
qdanilc 1:cd1fe330bc2a 17 DigitalOut blueLED(LED3); //to prove it's working
qdanilc 1:cd1fe330bc2a 18
qdanilc 1:cd1fe330bc2a 19 char input; //character to store inputs/outputs in communication
qdanilc 8:e09edf050cca 20
qdanilc 9:8cf34e4f9ca0 21 //char * getRequest = "GET / HTTP/1.1\r\nHost: time.jsontest.com\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: en-US,en;q=0.8";
qdanilc 9:8cf34e4f9ca0 22 //char * getRequest = "GET / HTTP/1.1\r\nHost: time.jsontest.com\r\n\r\n";
qdanilc 9:8cf34e4f9ca0 23 //char buf[20];
qdanilc 9:8cf34e4f9ca0 24 //sprintf(buf, "AT+CIPSEND=1,%d", sizeof(getRequest));
qdanilc 8:e09edf050cca 25
qdanilc 9:8cf34e4f9ca0 26 char* startCommands[5]= {
qdanilc 2:0b9ca0830cd3 27 "AT+RST",
qdanilc 2:0b9ca0830cd3 28 "AT+CWMODE=1",
qdanilc 6:99841ed0c8a8 29 //"AT+CWJAP=\"Hotspot\",\"password\"", skip signing in for debugging purposes
qdanilc 8:e09edf050cca 30 "AT+CIPMUX=1",
qdanilc 9:8cf34e4f9ca0 31 "AT+CIPSTART=1,\"TCP\",\"time.jsontest.com\",80" //time.jsontest.com
qdanilc 2:0b9ca0830cd3 32 };
qdanilc 1:cd1fe330bc2a 33
qdanilc 1:cd1fe330bc2a 34
qdanilc 9:8cf34e4f9ca0 35 void sendCommand(char* command, int commandNumber)
qdanilc 9:8cf34e4f9ca0 36 {
qdanilc 9:8cf34e4f9ca0 37 pc.printf("\r\ncommand %d",commandNumber); //identify the command number
qdanilc 9:8cf34e4f9ca0 38 pc.printf("\r\n");
qdanilc 9:8cf34e4f9ca0 39
qdanilc 9:8cf34e4f9ca0 40 esp.printf("%s\r\n",command); //send command
qdanilc 9:8cf34e4f9ca0 41
qdanilc 9:8cf34e4f9ca0 42 }
qdanilc 9:8cf34e4f9ca0 43
qdanilc 9:8cf34e4f9ca0 44
qdanilc 9:8cf34e4f9ca0 45 void getTime()
qdanilc 9:8cf34e4f9ca0 46 {
qdanilc 9:8cf34e4f9ca0 47 int * timeDate [2]; //stores time and date
qdanilc 9:8cf34e4f9ca0 48
qdanilc 9:8cf34e4f9ca0 49 pc.printf("Syncing time with internet\r\n");
qdanilc 9:8cf34e4f9ca0 50 char *msg=(char *)malloc(50);
qdanilc 9:8cf34e4f9ca0 51 char *msg2= "GET / HTTP/1.1\r\nhost: time.jsontest.com\r\n\r\n";
qdanilc 9:8cf34e4f9ca0 52
qdanilc 9:8cf34e4f9ca0 53 sprintf(msg,"AT+CIPSEND=1,%d",strlen(msg2));
qdanilc 9:8cf34e4f9ca0 54
qdanilc 9:8cf34e4f9ca0 55 pc.printf("%s\r\n",msg);
qdanilc 9:8cf34e4f9ca0 56 esp.printf("%s\r\n",msg);
qdanilc 9:8cf34e4f9ca0 57 pc.printf("%s\r\n",msg2);
qdanilc 9:8cf34e4f9ca0 58 esp.printf("%s\r\n",msg2);
qdanilc 9:8cf34e4f9ca0 59
qdanilc 9:8cf34e4f9ca0 60
qdanilc 9:8cf34e4f9ca0 61
qdanilc 9:8cf34e4f9ca0 62 }
qdanilc 9:8cf34e4f9ca0 63 char * buffer [128];
qdanilc 9:8cf34e4f9ca0 64 char * nextBuff;
qdanilc 9:8cf34e4f9ca0 65
qdanilc 5:79618eee4d54 66
qdanilc 5:79618eee4d54 67 int k = 0; //counter for comm numb
qdanilc 2:0b9ca0830cd3 68
qdanilc 2:0b9ca0830cd3 69
qdanilc 9:8cf34e4f9ca0 70
qdanilc 9:8cf34e4f9ca0 71
qdanilc 7:bfb2b5142c29 72 void start()
qdanilc 7:bfb2b5142c29 73 {
qdanilc 2:0b9ca0830cd3 74 pc.printf("Initialising start sequence\r\n");
qdanilc 7:bfb2b5142c29 75
qdanilc 9:8cf34e4f9ca0 76 int numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]); // check if this works
qdanilc 9:8cf34e4f9ca0 77 for (int i = 0; i<numberOfCommands; i++) {
qdanilc 7:bfb2b5142c29 78
qdanilc 9:8cf34e4f9ca0 79 sendCommand(startCommands[i],i);
qdanilc 9:8cf34e4f9ca0 80 wait (6); //wait an arbitrary time between each command
qdanilc 7:bfb2b5142c29 81
qdanilc 2:0b9ca0830cd3 82 }
qdanilc 2:0b9ca0830cd3 83 }
qdanilc 2:0b9ca0830cd3 84
qdanilc 7:bfb2b5142c29 85 void reset() //resets the module by turning rst off for 2 seconds then on again
qdanilc 7:bfb2b5142c29 86 {
qdanilc 1:cd1fe330bc2a 87 rstPin = 0;
qdanilc 1:cd1fe330bc2a 88 wait(2);
qdanilc 1:cd1fe330bc2a 89 rstPin = 1;
qdanilc 1:cd1fe330bc2a 90 pc.printf("Reset complete\r\n");
qdanilc 7:bfb2b5142c29 91
qdanilc 1:cd1fe330bc2a 92 }
qdanilc 9:8cf34e4f9ca0 93
qdanilc 1:cd1fe330bc2a 94 void esp_recv()
qdanilc 1:cd1fe330bc2a 95 {
qdanilc 5:79618eee4d54 96 redLED = !redLED;
qdanilc 1:cd1fe330bc2a 97 while(esp.readable()) {
qdanilc 5:79618eee4d54 98 pc.putc(esp.getc());
qdanilc 1:cd1fe330bc2a 99 }
qdanilc 1:cd1fe330bc2a 100 }
qdanilc 9:8cf34e4f9ca0 101
qdanilc 9:8cf34e4f9ca0 102
qdanilc 1:cd1fe330bc2a 103 void pc_recv()
qdanilc 1:cd1fe330bc2a 104 {
qdanilc 1:cd1fe330bc2a 105 char c;
qdanilc 1:cd1fe330bc2a 106 greenLED = !greenLED;
qdanilc 1:cd1fe330bc2a 107 while(pc.readable()) {
qdanilc 5:79618eee4d54 108 c=pc.getc();
qdanilc 5:79618eee4d54 109 esp.putc(c);
qdanilc 7:bfb2b5142c29 110
qdanilc 9:8cf34e4f9ca0 111 /*
qdanilc 9:8cf34e4f9ca0 112 /////////////////////////////////////////////////////////////////////////////////////this section is for debugging purposes only remove when done
qdanilc 9:8cf34e4f9ca0 113 if(c=='x') {
qdanilc 9:8cf34e4f9ca0 114 pc.printf("\r\ncommand %d",k); //identify the command number
qdanilc 9:8cf34e4f9ca0 115 pc.printf("\r\n");
qdanilc 7:bfb2b5142c29 116
qdanilc 9:8cf34e4f9ca0 117 esp.printf("%s\r\n",startCommands[k]); //send command
qdanilc 9:8cf34e4f9ca0 118 pc.putc(10); //ie makes enter perform as expected
qdanilc 7:bfb2b5142c29 119
qdanilc 9:8cf34e4f9ca0 120 if (k <10) {
qdanilc 9:8cf34e4f9ca0 121 k++; //reset k if required
qdanilc 9:8cf34e4f9ca0 122 } else {
qdanilc 9:8cf34e4f9ca0 123 k=0;
qdanilc 9:8cf34e4f9ca0 124 }
qdanilc 9:8cf34e4f9ca0 125 }
qdanilc 9:8cf34e4f9ca0 126 *////////////////////////////////////////////////////////////////////////////////////
qdanilc 7:bfb2b5142c29 127
qdanilc 9:8cf34e4f9ca0 128 if(c==13) {
qdanilc 8:e09edf050cca 129 pc.putc(10); //ie makes enter perform as expected
qdanilc 8:e09edf050cca 130 esp.putc(10);
qdanilc 1:cd1fe330bc2a 131 }
qdanilc 1:cd1fe330bc2a 132 }
qdanilc 1:cd1fe330bc2a 133 }
qdanilc 1:cd1fe330bc2a 134
qdanilc 7:bfb2b5142c29 135 void initiateWifi()
qdanilc 7:bfb2b5142c29 136 {
qdanilc 7:bfb2b5142c29 137
qdanilc 1:cd1fe330bc2a 138 //initialise the pins
qdanilc 1:cd1fe330bc2a 139 rstPin = 1;
qdanilc 1:cd1fe330bc2a 140 chpdPin = 1;
qdanilc 7:bfb2b5142c29 141
qdanilc 1:cd1fe330bc2a 142 reset(); //perform an initial reset
qdanilc 7:bfb2b5142c29 143
qdanilc 1:cd1fe330bc2a 144 blueLED = 1; //turn on test light
qdanilc 1:cd1fe330bc2a 145
qdanilc 1:cd1fe330bc2a 146 pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
qdanilc 1:cd1fe330bc2a 147 esp.attach(&esp_recv, Serial::RxIrq);
qdanilc 7:bfb2b5142c29 148
qdanilc 9:8cf34e4f9ca0 149 wait(10);
qdanilc 9:8cf34e4f9ca0 150 start();
qdanilc 1:cd1fe330bc2a 151
qdanilc 9:8cf34e4f9ca0 152 wait (3);
qdanilc 9:8cf34e4f9ca0 153 getTime();
qdanilc 9:8cf34e4f9ca0 154 /*
qdanilc 9:8cf34e4f9ca0 155 pc.printf("%d",dateTime[0]);
qdanilc 9:8cf34e4f9ca0 156 pc.printf("%d",dateTime[1]);
qdanilc 9:8cf34e4f9ca0 157 */
qdanilc 9:8cf34e4f9ca0 158 }
qdanilc 9:8cf34e4f9ca0 159
qdanilc 9:8cf34e4f9ca0 160
qdanilc 9:8cf34e4f9ca0 161
qdanilc 9:8cf34e4f9ca0 162