RITVIK DAVE
/
IOT_LAB_WiFi_ldr_2Channels
IOT_LAB_WiFi_ldr_2Channels
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "ESP8266.h" 00003 00004 Serial pc(USBTX,USBRX); 00005 00006 //POT sensor 00007 AnalogIn pot(PTB1); 00008 00009 //wifi UART port and baud rate 00010 ESP8266 wifi(PTE0, PTE1, 115200); 00011 00012 //buffers for wifi library 00013 char snd[255],resp[1000]; 00014 char http_cmd[300], comm[300]; 00015 00016 int timeout = 8000; //timeout for wifi commands 00017 00018 //SSID and password for connection 00019 #define SSID "me" //"AmeyaHotspot"//"IoTServer" //"AmeyaHotspot" //"Xperia S_f19d" 00020 #define PASS "123123123" //"12345678" //"iotserver" //"12345678" //"profpappu" 00021 00022 //Remote IP 00023 #define IP "184.106.153.149" 00024 //#define IP "https://api.thingspeak.com" 00025 //#define IP "192.168.0.25" 00026 //waterlevelue global variable 00027 float waterlevel = 0; 00028 00029 //Public and private keys for phant 00030 char* Update_Key1 = "KW85FFJJPQ5CW3KE"; 00031 char* Update_Key2 = "6A0KQQKFBPJKLAFT"; 00032 00033 //char* Private_Key = "GPoWnRM60yidrB0e1pXD"; 00034 00035 //Wifi init function 00036 void wifi_initialize(void){ 00037 00038 pc.printf("******** Resetting wifi module ********\r\n"); 00039 wifi.Reset(); 00040 00041 //wait for 5 seconds for response, else display no response receiveed 00042 if (wifi.RcvReply(resp, 5000)) 00043 pc.printf("%s",resp); 00044 else 00045 pc.printf("No response"); 00046 00047 pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); 00048 wifi.SetMode(1); // set transparent mode 00049 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP 00050 pc.printf("%s",resp); //Print the response onscreen 00051 else 00052 pc.printf("No response while setting mode. \r\n"); 00053 00054 pc.printf("******** Joining network with SSID and PASS ********\r\n"); 00055 wifi.Join(SSID, PASS); 00056 if (wifi.RcvReply(resp, timeout)) 00057 pc.printf("%s",resp); 00058 else 00059 pc.printf("No response while connecting to network \r\n"); 00060 00061 pc.printf("******** Getting IP and MAC of module ********\r\n"); 00062 wifi.GetIP(resp); 00063 if (wifi.RcvReply(resp, timeout)) 00064 pc.printf("%s",resp); 00065 else 00066 pc.printf("No response while getting IP \r\n"); 00067 00068 pc.printf("******** Setting WIFI UART passthrough ********\r\n"); 00069 wifi.setTransparent(); 00070 if (wifi.RcvReply(resp, timeout)) 00071 pc.printf("%s",resp); 00072 else 00073 pc.printf("No response while setting wifi passthrough. \r\n"); 00074 wait(1); 00075 00076 pc.printf("******** Setting single connection mode ********\r\n"); 00077 wifi.SetSingle(); 00078 wifi.RcvReply(resp, timeout); 00079 if (wifi.RcvReply(resp, timeout)) 00080 pc.printf("%s",resp); 00081 else 00082 pc.printf("No response while setting single connection \r\n"); 00083 wait(1); 00084 } 00085 00086 void wifi_send(void){ 00087 00088 pc.printf("******** Starting TCP connection on IP and port ********\r\n"); 00089 wifi.startTCPConn(IP,80); //cipstart 00090 wifi.RcvReply(resp, timeout); 00091 if (wifi.RcvReply(resp, timeout)) 00092 pc.printf("%s",resp); 00093 else 00094 pc.printf("No response while starting TCP connection \r\n"); 00095 wait(1); 00096 00097 //create link 00098 if(waterlevel>127) 00099 sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key1,waterlevel); 00100 else 00101 sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key2,waterlevel); 00102 pc.printf(http_cmd); 00103 00104 pc.printf("******** Sending URL to wifi ********\r\n"); 00105 wifi.sendURL(http_cmd, comm); //cipsend and get command 00106 if (wifi.RcvReply(resp, timeout)) 00107 pc.printf("%s",resp); 00108 00109 pc.printf("No response while sending URL \r\n"); 00110 00111 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server 00112 //wifi.RcvReply(resp, timeout); 00113 //pc.printf("%s", resp); 00114 } 00115 00116 int main () { 00117 00118 00119 wifi_initialize(); 00120 00121 while (1) { 00122 waterlevel = 255*pot.read(); 00123 pc.printf("Current waterlevel is = %.3f \r\n", waterlevel); 00124 00125 wifi_send(); 00126 wait(2); 00127 } 00128 } 00129
Generated on Sat Jul 30 2022 11:45:32 by 1.7.2