wifi testing

Dependencies:   mbed

Committer:
himanshukaushik
Date:
Tue Mar 16 09:31:32 2021 +0000
Revision:
0:eec7d62631de
wifi testing on march 16

Who changed what in which revision?

UserRevisionLine numberNew contents of line
himanshukaushik 0:eec7d62631de 1 #include "mbed.h"
himanshukaushik 0:eec7d62631de 2 Serial pc(USBTX, USBRX);
himanshukaushik 0:eec7d62631de 3 Serial esp(p9, p10); // tx, rx
himanshukaushik 0:eec7d62631de 4 Timer t;
himanshukaushik 0:eec7d62631de 5 char buf[1024];
himanshukaushik 0:eec7d62631de 6 char snd[255];
himanshukaushik 0:eec7d62631de 7 int count;
himanshukaushik 0:eec7d62631de 8 int timeout,ended;
himanshukaushik 0:eec7d62631de 9 char ssid[32] = "Chaitanya1"; // enter WiFi router ssid inside the quotes
himanshukaushik 0:eec7d62631de 10 char pwd [40] = "9632964359"; // enter WiFi router password inside the quotes
himanshukaushik 0:eec7d62631de 11 void SendCMD(),getreply();
himanshukaushik 0:eec7d62631de 12
himanshukaushik 0:eec7d62631de 13 int main() {
himanshukaushik 0:eec7d62631de 14 pc.baud(9600);
himanshukaushik 0:eec7d62631de 15 esp.baud(9600);
himanshukaushik 0:eec7d62631de 16
himanshukaushik 0:eec7d62631de 17 strcpy(snd,"AT\r\n");
himanshukaushik 0:eec7d62631de 18 SendCMD();
himanshukaushik 0:eec7d62631de 19 timeout=1;
himanshukaushik 0:eec7d62631de 20 getreply();
himanshukaushik 0:eec7d62631de 21 pc.printf(buf);
himanshukaushik 0:eec7d62631de 22 pc.printf("\n---------- Setting Mode ----------\r\n");
himanshukaushik 0:eec7d62631de 23 strcpy(snd, "AT+CWMODE=3\r\n");
himanshukaushik 0:eec7d62631de 24 SendCMD();
himanshukaushik 0:eec7d62631de 25 timeout=3;
himanshukaushik 0:eec7d62631de 26 getreply();
himanshukaushik 0:eec7d62631de 27 pc.printf(buf);
himanshukaushik 0:eec7d62631de 28
himanshukaushik 0:eec7d62631de 29 wait(2);
himanshukaushik 0:eec7d62631de 30 pc.printf("\n---------- Listing Access Points ----------\r\n");
himanshukaushik 0:eec7d62631de 31 strcpy(snd, "AT+CWLAP\r\n");
himanshukaushik 0:eec7d62631de 32 SendCMD();
himanshukaushik 0:eec7d62631de 33 timeout=3;
himanshukaushik 0:eec7d62631de 34 getreply();
himanshukaushik 0:eec7d62631de 35 pc.printf(buf);
himanshukaushik 0:eec7d62631de 36 wait(2);
himanshukaushik 0:eec7d62631de 37
himanshukaushik 0:eec7d62631de 38 pc.printf("\n---------- Connecting to AP ----------\r\n");
himanshukaushik 0:eec7d62631de 39 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
himanshukaushik 0:eec7d62631de 40 strcpy(snd, "AT+CWJAP=\"");
himanshukaushik 0:eec7d62631de 41 strcat(snd, ssid);
himanshukaushik 0:eec7d62631de 42 strcat(snd, "\",\"");
himanshukaushik 0:eec7d62631de 43 strcat(snd, pwd);
himanshukaushik 0:eec7d62631de 44 strcat(snd, "\"\r\n");
himanshukaushik 0:eec7d62631de 45 SendCMD();
himanshukaushik 0:eec7d62631de 46 timeout=3;
himanshukaushik 0:eec7d62631de 47 getreply();
himanshukaushik 0:eec7d62631de 48 pc.printf(buf);
himanshukaushik 0:eec7d62631de 49 wait(5);
himanshukaushik 0:eec7d62631de 50 pc.printf("\n---------- Get IP's ----------\r\n");
himanshukaushik 0:eec7d62631de 51 strcpy(snd, "AT+CIFSR\r\n");
himanshukaushik 0:eec7d62631de 52 SendCMD();
himanshukaushik 0:eec7d62631de 53 timeout=10;
himanshukaushik 0:eec7d62631de 54 getreply();
himanshukaushik 0:eec7d62631de 55 pc.printf(buf);
himanshukaushik 0:eec7d62631de 56 wait(1);
himanshukaushik 0:eec7d62631de 57
himanshukaushik 0:eec7d62631de 58 pc.printf("\n---------- Get Connection Status ----------\r\n");
himanshukaushik 0:eec7d62631de 59 strcpy(snd, "AT+CIPSTATUS\r\n");
himanshukaushik 0:eec7d62631de 60 SendCMD();
himanshukaushik 0:eec7d62631de 61 timeout=5;
himanshukaushik 0:eec7d62631de 62 getreply();
himanshukaushik 0:eec7d62631de 63 pc.printf(buf);
himanshukaushik 0:eec7d62631de 64
himanshukaushik 0:eec7d62631de 65 }
himanshukaushik 0:eec7d62631de 66 void SendCMD()
himanshukaushik 0:eec7d62631de 67 {
himanshukaushik 0:eec7d62631de 68 esp.printf("%s", snd);
himanshukaushik 0:eec7d62631de 69 }
himanshukaushik 0:eec7d62631de 70 void getreply()
himanshukaushik 0:eec7d62631de 71 {
himanshukaushik 0:eec7d62631de 72 memset(buf, '\0', sizeof(buf));
himanshukaushik 0:eec7d62631de 73 t.start();
himanshukaushik 0:eec7d62631de 74 ended=0;
himanshukaushik 0:eec7d62631de 75 count=0;
himanshukaushik 0:eec7d62631de 76 while(!ended) {
himanshukaushik 0:eec7d62631de 77 if(esp.readable()) {
himanshukaushik 0:eec7d62631de 78 buf[count] = esp.getc();
himanshukaushik 0:eec7d62631de 79 count++;
himanshukaushik 0:eec7d62631de 80 }
himanshukaushik 0:eec7d62631de 81
himanshukaushik 0:eec7d62631de 82
himanshukaushik 0:eec7d62631de 83 if(t.read() > timeout) {
himanshukaushik 0:eec7d62631de 84 ended = 1;
himanshukaushik 0:eec7d62631de 85 t.stop();
himanshukaushik 0:eec7d62631de 86 t.reset();
himanshukaushik 0:eec7d62631de 87 }
himanshukaushik 0:eec7d62631de 88 }
himanshukaushik 0:eec7d62631de 89 }