wifi

Dependencies:   ESP8266 mbed

Committer:
prtkmynk
Date:
Thu Jun 07 05:16:45 2018 +0000
Revision:
0:5307b640e09b
wifi; ;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
prtkmynk 0:5307b640e09b 1 #include "mbed.h"
prtkmynk 0:5307b640e09b 2 #include "ESP8266.h"
prtkmynk 0:5307b640e09b 3
prtkmynk 0:5307b640e09b 4 Serial pc(USBTX,USBRX);
prtkmynk 0:5307b640e09b 5
prtkmynk 0:5307b640e09b 6 //POT sensor
prtkmynk 0:5307b640e09b 7 AnalogIn pot(PTB0);
prtkmynk 0:5307b640e09b 8 AnalogIn ldr(PTB1);
prtkmynk 0:5307b640e09b 9 //wifi UART port and baud rate
prtkmynk 0:5307b640e09b 10 ESP8266 wifi(PTE0, PTE1, 115200);
prtkmynk 0:5307b640e09b 11
prtkmynk 0:5307b640e09b 12 //buffers for wifi library
prtkmynk 0:5307b640e09b 13 char snd[255],resp[1000];
prtkmynk 0:5307b640e09b 14 char http_cmd[300], comm[300];
prtkmynk 0:5307b640e09b 15
prtkmynk 0:5307b640e09b 16 int timeout = 8000; //timeout for wifi commands
prtkmynk 0:5307b640e09b 17
prtkmynk 0:5307b640e09b 18 //SSID and password for connection
prtkmynk 0:5307b640e09b 19 #define SSID "Prateek" //"AmeyaHotspot"//"IoTServer" //"AmeyaHotspot" //"Xperia S_f19d"
prtkmynk 0:5307b640e09b 20 #define PASS "prtk9001" //"12345678" //"iotserver" //"12345678" //"profpappu"
prtkmynk 0:5307b640e09b 21
prtkmynk 0:5307b640e09b 22 //Remote IP
prtkmynk 0:5307b640e09b 23 #define IP "184.106.153.149"
prtkmynk 0:5307b640e09b 24 //#define IP "https://api.thingspeak.com"
prtkmynk 0:5307b640e09b 25 //#define IP "192.168.0.25"
prtkmynk 0:5307b640e09b 26 //waterlevelue global variable
prtkmynk 0:5307b640e09b 27 float pot1 = 0,ldr1 = 0;
prtkmynk 0:5307b640e09b 28
prtkmynk 0:5307b640e09b 29 //Public and private keys for phant
prtkmynk 0:5307b640e09b 30 char* Update_Key = "630XFIIPIG55ULJI";
prtkmynk 0:5307b640e09b 31 //char* Private_Key = "GPoWnRM60yidrB0e1pXD";
prtkmynk 0:5307b640e09b 32
prtkmynk 0:5307b640e09b 33 //Wifi init function
prtkmynk 0:5307b640e09b 34 void wifi_initialize(void){
prtkmynk 0:5307b640e09b 35
prtkmynk 0:5307b640e09b 36 pc.printf("******** Resetting wifi module ********\r\n");
prtkmynk 0:5307b640e09b 37 wifi.Reset();
prtkmynk 0:5307b640e09b 38
prtkmynk 0:5307b640e09b 39 //wait for 5 seconds for response, else display no response receiveed
prtkmynk 0:5307b640e09b 40 if (wifi.RcvReply(resp, 8000))
prtkmynk 0:5307b640e09b 41 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 42 else
prtkmynk 0:5307b640e09b 43 pc.printf("No response");
prtkmynk 0:5307b640e09b 44
prtkmynk 0:5307b640e09b 45 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
prtkmynk 0:5307b640e09b 46 wifi.SetMode(1); // set transparent mode
prtkmynk 0:5307b640e09b 47 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
prtkmynk 0:5307b640e09b 48 pc.printf("%s",resp); //Print the response onscreen
prtkmynk 0:5307b640e09b 49 else
prtkmynk 0:5307b640e09b 50 pc.printf("No response while setting mode. \r\n");
prtkmynk 0:5307b640e09b 51
prtkmynk 0:5307b640e09b 52 pc.printf("******** Joining network with SSID and PASS ********\r\n");
prtkmynk 0:5307b640e09b 53 wifi.Join(SSID, PASS);
prtkmynk 0:5307b640e09b 54 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 55 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 56 else
prtkmynk 0:5307b640e09b 57 pc.printf("No response while connecting to network \r\n");
prtkmynk 0:5307b640e09b 58
prtkmynk 0:5307b640e09b 59 pc.printf("******** Getting IP and MAC of module ********\r\n");
prtkmynk 0:5307b640e09b 60 wifi.GetIP(resp);
prtkmynk 0:5307b640e09b 61 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 62 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 63 else
prtkmynk 0:5307b640e09b 64 pc.printf("No response while getting IP \r\n");
prtkmynk 0:5307b640e09b 65
prtkmynk 0:5307b640e09b 66 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
prtkmynk 0:5307b640e09b 67 wifi.setTransparent();
prtkmynk 0:5307b640e09b 68 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 69 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 70 else
prtkmynk 0:5307b640e09b 71 pc.printf("No response while setting wifi passthrough. \r\n");
prtkmynk 0:5307b640e09b 72 wait(1);
prtkmynk 0:5307b640e09b 73
prtkmynk 0:5307b640e09b 74 pc.printf("******** Setting single connection mode ********\r\n");
prtkmynk 0:5307b640e09b 75 wifi.SetSingle();
prtkmynk 0:5307b640e09b 76 wifi.RcvReply(resp, timeout);
prtkmynk 0:5307b640e09b 77 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 78 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 79 else
prtkmynk 0:5307b640e09b 80 pc.printf("No response while setting single connection \r\n");
prtkmynk 0:5307b640e09b 81 wait(1);
prtkmynk 0:5307b640e09b 82 }
prtkmynk 0:5307b640e09b 83
prtkmynk 0:5307b640e09b 84 void wifi_send(void){
prtkmynk 0:5307b640e09b 85
prtkmynk 0:5307b640e09b 86 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
prtkmynk 0:5307b640e09b 87 wifi.startTCPConn(IP,80); //cipstart
prtkmynk 0:5307b640e09b 88 wifi.RcvReply(resp, timeout);
prtkmynk 0:5307b640e09b 89 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 90 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 91 else
prtkmynk 0:5307b640e09b 92 pc.printf("No response while starting TCP connection \r\n");
prtkmynk 0:5307b640e09b 93 wait(1);
prtkmynk 0:5307b640e09b 94
prtkmynk 0:5307b640e09b 95 //create link
prtkmynk 0:5307b640e09b 96 sprintf(http_cmd,"/update?api_key=%s&field1=%f&field2=%f",Update_Key,pot1,ldr1);
prtkmynk 0:5307b640e09b 97 pc.printf(http_cmd);
prtkmynk 0:5307b640e09b 98
prtkmynk 0:5307b640e09b 99 pc.printf("******** Sending URL to wifi ********\r\n");
prtkmynk 0:5307b640e09b 100 wifi.sendURL(http_cmd, comm); //cipsend and get command
prtkmynk 0:5307b640e09b 101 if (wifi.RcvReply(resp, timeout))
prtkmynk 0:5307b640e09b 102 pc.printf("%s",resp);
prtkmynk 0:5307b640e09b 103
prtkmynk 0:5307b640e09b 104 pc.printf("No response while sending URL \r\n");
prtkmynk 0:5307b640e09b 105
prtkmynk 0:5307b640e09b 106 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
prtkmynk 0:5307b640e09b 107 //wifi.RcvReply(resp, timeout);
prtkmynk 0:5307b640e09b 108 //pc.printf("%s", resp);
prtkmynk 0:5307b640e09b 109 }
prtkmynk 0:5307b640e09b 110
prtkmynk 0:5307b640e09b 111 int main () {
prtkmynk 0:5307b640e09b 112
prtkmynk 0:5307b640e09b 113
prtkmynk 0:5307b640e09b 114 wifi_initialize();
prtkmynk 0:5307b640e09b 115
prtkmynk 0:5307b640e09b 116 while (1) {
prtkmynk 0:5307b640e09b 117 pot1 = 255*pot.read();
prtkmynk 0:5307b640e09b 118 ldr1 = 255*ldr.read();
prtkmynk 0:5307b640e09b 119 pc.printf("Current pot reading is = %.3f \r\n Current ldr reading is = %.3f \r\n", pot1,ldr1);
prtkmynk 0:5307b640e09b 120
prtkmynk 0:5307b640e09b 121 wifi_send();
prtkmynk 0:5307b640e09b 122 wait(5);
prtkmynk 0:5307b640e09b 123 }
prtkmynk 0:5307b640e09b 124 }