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