Wifi data logging on local phant using Node-red
Fork of ESP8266_LocalPhant_KL25Z by
main.cpp@3:5ba1cb5e6613, 2017-03-30 (annotated)
- Committer:
- rushabkarani
- Date:
- Thu Mar 30 12:31:19 2017 +0000
- Revision:
- 3:5ba1cb5e6613
- Parent:
- 2:817794b2f733
Esp8266 With NODE-RED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SIT2016 | 0:8ca71ccb52db | 1 | #include "mbed.h" |
SIT2016 | 0:8ca71ccb52db | 2 | #include "ESP8266.h" |
SIT2016 | 0:8ca71ccb52db | 3 | |
SIT2016 | 0:8ca71ccb52db | 4 | Serial pc(USBTX,USBRX); |
SIT2016 | 1:2012c31aee1b | 5 | |
SIT2016 | 1:2012c31aee1b | 6 | //POT sensor |
SIT2016 | 1:2012c31aee1b | 7 | AnalogIn pot(PTB0); |
rushabkarani | 3:5ba1cb5e6613 | 8 | AnalogIn ldr(PTB1); |
SIT2016 | 1:2012c31aee1b | 9 | |
SIT2016 | 1:2012c31aee1b | 10 | //wifi UART port and baud rate |
SIT2016 | 1:2012c31aee1b | 11 | ESP8266 wifi(PTE0, PTE1, 115200); |
SIT2016 | 1:2012c31aee1b | 12 | |
SIT2016 | 1:2012c31aee1b | 13 | //buffers for wifi library |
SIT2016 | 1:2012c31aee1b | 14 | char snd[255],resp[1000]; |
SIT2016 | 1:2012c31aee1b | 15 | char http_cmd[300], comm[300]; |
SIT2016 | 1:2012c31aee1b | 16 | |
rushabkarani | 3:5ba1cb5e6613 | 17 | int timeout = 5000; //timeout for wifi commands |
SIT2016 | 1:2012c31aee1b | 18 | |
SIT2016 | 1:2012c31aee1b | 19 | //SSID and password for connection |
rushabkarani | 3:5ba1cb5e6613 | 20 | #define SSID "eduvanceAP" |
rushabkarani | 3:5ba1cb5e6613 | 21 | #define PASS "winteriscoming" |
SIT2016 | 1:2012c31aee1b | 22 | |
SIT2016 | 1:2012c31aee1b | 23 | //Remote IP |
rushabkarani | 3:5ba1cb5e6613 | 24 | #define IP "192.168.0.22" |
SIT2016 | 1:2012c31aee1b | 25 | |
SIT2016 | 1:2012c31aee1b | 26 | //ldrvalue global variable |
rushabkarani | 3:5ba1cb5e6613 | 27 | float ldrval = 0; |
rushabkarani | 3:5ba1cb5e6613 | 28 | float potval = 0; |
SIT2016 | 1:2012c31aee1b | 29 | |
SIT2016 | 1:2012c31aee1b | 30 | //Public and private keys for phant |
rushabkarani | 3:5ba1cb5e6613 | 31 | //char* Public_Key = "eaWkxgdlQZszLEDKpy6OFeDeO8x"; |
rushabkarani | 3:5ba1cb5e6613 | 32 | //char* Private_Key = "25xqpAeBmzSml4Yjpr05Hj3jOBq"; |
SIT2016 | 0:8ca71ccb52db | 33 | |
SIT2016 | 1:2012c31aee1b | 34 | //Wifi init function |
SIT2016 | 1:2012c31aee1b | 35 | void wifi_initialize(void){ |
SIT2016 | 1:2012c31aee1b | 36 | |
SIT2016 | 1:2012c31aee1b | 37 | pc.printf("******** Resetting wifi module ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 38 | wifi.Reset(); |
SIT2016 | 1:2012c31aee1b | 39 | |
SIT2016 | 1:2012c31aee1b | 40 | //wait for 5 seconds for response, else display no response receiveed |
rushabkarani | 3:5ba1cb5e6613 | 41 | if (wifi.RcvReply(resp, 8000)) |
SIT2016 | 1:2012c31aee1b | 42 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 43 | else |
SIT2016 | 1:2012c31aee1b | 44 | pc.printf("No response"); |
SIT2016 | 1:2012c31aee1b | 45 | |
SIT2016 | 1:2012c31aee1b | 46 | pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 47 | wifi.SetMode(1); // set transparent mode |
SIT2016 | 1:2012c31aee1b | 48 | if (wifi.RcvReply(resp, timeout)) //receive a response from ESP |
SIT2016 | 1:2012c31aee1b | 49 | pc.printf("%s",resp); //Print the response onscreen |
SIT2016 | 1:2012c31aee1b | 50 | else |
SIT2016 | 1:2012c31aee1b | 51 | pc.printf("No response while setting mode. \r\n"); |
SIT2016 | 1:2012c31aee1b | 52 | |
SIT2016 | 1:2012c31aee1b | 53 | pc.printf("******** Joining network with SSID and PASS ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 54 | wifi.Join(SSID, PASS); |
SIT2016 | 1:2012c31aee1b | 55 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 56 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 57 | else |
SIT2016 | 1:2012c31aee1b | 58 | pc.printf("No response while connecting to network \r\n"); |
SIT2016 | 1:2012c31aee1b | 59 | |
SIT2016 | 1:2012c31aee1b | 60 | pc.printf("******** Getting IP and MAC of module ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 61 | wifi.GetIP(resp); |
SIT2016 | 1:2012c31aee1b | 62 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 63 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 64 | else |
SIT2016 | 1:2012c31aee1b | 65 | pc.printf("No response while getting IP \r\n"); |
SIT2016 | 1:2012c31aee1b | 66 | |
SIT2016 | 1:2012c31aee1b | 67 | pc.printf("******** Setting WIFI UART passthrough ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 68 | wifi.setTransparent(); |
SIT2016 | 1:2012c31aee1b | 69 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 70 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 71 | else |
SIT2016 | 1:2012c31aee1b | 72 | pc.printf("No response while setting wifi passthrough. \r\n"); |
SIT2016 | 2:817794b2f733 | 73 | wait(1); |
SIT2016 | 1:2012c31aee1b | 74 | |
SIT2016 | 1:2012c31aee1b | 75 | pc.printf("******** Setting single connection mode ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 76 | wifi.SetSingle(); |
SIT2016 | 1:2012c31aee1b | 77 | wifi.RcvReply(resp, timeout); |
SIT2016 | 1:2012c31aee1b | 78 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 79 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 80 | else |
SIT2016 | 1:2012c31aee1b | 81 | pc.printf("No response while setting single connection \r\n"); |
SIT2016 | 1:2012c31aee1b | 82 | wait(1); |
SIT2016 | 2:817794b2f733 | 83 | } |
SIT2016 | 2:817794b2f733 | 84 | |
SIT2016 | 2:817794b2f733 | 85 | void wifi_send(void){ |
SIT2016 | 1:2012c31aee1b | 86 | |
SIT2016 | 1:2012c31aee1b | 87 | pc.printf("******** Starting TCP connection on IP and port ********\r\n"); |
rushabkarani | 3:5ba1cb5e6613 | 88 | wifi.startTCPConn(IP, 1880); //cipstart |
SIT2016 | 1:2012c31aee1b | 89 | wifi.RcvReply(resp, timeout); |
SIT2016 | 1:2012c31aee1b | 90 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 91 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 92 | else |
SIT2016 | 1:2012c31aee1b | 93 | pc.printf("No response while starting TCP connection \r\n"); |
SIT2016 | 1:2012c31aee1b | 94 | wait(1); |
SIT2016 | 1:2012c31aee1b | 95 | |
SIT2016 | 1:2012c31aee1b | 96 | //create link |
rushabkarani | 3:5ba1cb5e6613 | 97 | sprintf(http_cmd,"/log?pot=%0.2f&ldr=%0.2f",potval,ldrval); |
SIT2016 | 2:817794b2f733 | 98 | pc.printf(http_cmd); |
SIT2016 | 1:2012c31aee1b | 99 | |
SIT2016 | 1:2012c31aee1b | 100 | pc.printf("******** Sending URL to wifi ********\r\n"); |
SIT2016 | 1:2012c31aee1b | 101 | wifi.sendURL(http_cmd, comm); //cipsend and get command |
SIT2016 | 1:2012c31aee1b | 102 | if (wifi.RcvReply(resp, timeout)) |
SIT2016 | 1:2012c31aee1b | 103 | pc.printf("%s",resp); |
SIT2016 | 1:2012c31aee1b | 104 | else |
SIT2016 | 1:2012c31aee1b | 105 | pc.printf("No response while sending URL \r\n"); |
SIT2016 | 1:2012c31aee1b | 106 | |
SIT2016 | 1:2012c31aee1b | 107 | //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server |
SIT2016 | 1:2012c31aee1b | 108 | //wifi.RcvReply(resp, timeout); |
SIT2016 | 1:2012c31aee1b | 109 | //pc.printf("%s", resp); |
SIT2016 | 1:2012c31aee1b | 110 | } |
SIT2016 | 1:2012c31aee1b | 111 | |
SIT2016 | 0:8ca71ccb52db | 112 | int main () { |
SIT2016 | 0:8ca71ccb52db | 113 | |
SIT2016 | 1:2012c31aee1b | 114 | |
SIT2016 | 1:2012c31aee1b | 115 | wifi_initialize(); |
SIT2016 | 1:2012c31aee1b | 116 | |
SIT2016 | 0:8ca71ccb52db | 117 | while (1) { |
rushabkarani | 3:5ba1cb5e6613 | 118 | potval = pot.read(); |
rushabkarani | 3:5ba1cb5e6613 | 119 | ldrval = ldr.read(); |
rushabkarani | 3:5ba1cb5e6613 | 120 | pc.printf("Potentiometer potvalue=%.3f & ldrvalue=%.3f\r\n",potval,ldrval); |
SIT2016 | 1:2012c31aee1b | 121 | |
SIT2016 | 1:2012c31aee1b | 122 | wifi_send(); |
SIT2016 | 1:2012c31aee1b | 123 | wait(3); |
SIT2016 | 0:8ca71ccb52db | 124 | } |
SIT2016 | 0:8ca71ccb52db | 125 | } |
SIT2016 | 0:8ca71ccb52db | 126 | |
SIT2016 | 0:8ca71ccb52db | 127 |