Uploading POT value to local phant server using ESP8266 Wifi module.

Dependencies:   ESP8266 mbed

Committer:
SIT2016
Date:
Sun Jun 26 10:19:24 2016 +0000
Revision:
2:817794b2f733
Parent:
1:2012c31aee1b
Modified initialize and send functions. Printing http command on screen

Who changed what in which revision?

UserRevisionLine numberNew 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);
SIT2016 1:2012c31aee1b 8
SIT2016 1:2012c31aee1b 9 //wifi UART port and baud rate
SIT2016 1:2012c31aee1b 10 ESP8266 wifi(PTE0, PTE1, 115200);
SIT2016 1:2012c31aee1b 11
SIT2016 1:2012c31aee1b 12 //buffers for wifi library
SIT2016 1:2012c31aee1b 13 char snd[255],resp[1000];
SIT2016 1:2012c31aee1b 14 char http_cmd[300], comm[300];
SIT2016 1:2012c31aee1b 15
SIT2016 1:2012c31aee1b 16 int timeout = 3000; //timeout for wifi commands
SIT2016 1:2012c31aee1b 17
SIT2016 1:2012c31aee1b 18 //SSID and password for connection
SIT2016 1:2012c31aee1b 19 #define SSID "Eduvance_WiFi"
SIT2016 1:2012c31aee1b 20 #define PASS "eduvance"
SIT2016 1:2012c31aee1b 21
SIT2016 1:2012c31aee1b 22 //Remote IP
SIT2016 2:817794b2f733 23 #define IP "192.168.0.2"
SIT2016 1:2012c31aee1b 24
SIT2016 1:2012c31aee1b 25 //ldrvalue global variable
SIT2016 1:2012c31aee1b 26 float ldrval = 0;
SIT2016 1:2012c31aee1b 27
SIT2016 1:2012c31aee1b 28 //Public and private keys for phant
SIT2016 2:817794b2f733 29 char* Public_Key = "eaWkxgdlQZszLEDKpy6OFeDeO8x";
SIT2016 2:817794b2f733 30 char* Private_Key = "25xqpAeBmzSml4Yjpr05Hj3jOBq";
SIT2016 0:8ca71ccb52db 31
SIT2016 1:2012c31aee1b 32 //Wifi init function
SIT2016 1:2012c31aee1b 33 void wifi_initialize(void){
SIT2016 1:2012c31aee1b 34
SIT2016 1:2012c31aee1b 35 pc.printf("******** Resetting wifi module ********\r\n");
SIT2016 1:2012c31aee1b 36 wifi.Reset();
SIT2016 1:2012c31aee1b 37
SIT2016 1:2012c31aee1b 38 //wait for 5 seconds for response, else display no response receiveed
SIT2016 1:2012c31aee1b 39 if (wifi.RcvReply(resp, 5000))
SIT2016 1:2012c31aee1b 40 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 41 else
SIT2016 1:2012c31aee1b 42 pc.printf("No response");
SIT2016 1:2012c31aee1b 43
SIT2016 1:2012c31aee1b 44 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
SIT2016 1:2012c31aee1b 45 wifi.SetMode(1); // set transparent mode
SIT2016 1:2012c31aee1b 46 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
SIT2016 1:2012c31aee1b 47 pc.printf("%s",resp); //Print the response onscreen
SIT2016 1:2012c31aee1b 48 else
SIT2016 1:2012c31aee1b 49 pc.printf("No response while setting mode. \r\n");
SIT2016 1:2012c31aee1b 50
SIT2016 1:2012c31aee1b 51 pc.printf("******** Joining network with SSID and PASS ********\r\n");
SIT2016 1:2012c31aee1b 52 wifi.Join(SSID, PASS);
SIT2016 1:2012c31aee1b 53 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 54 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 55 else
SIT2016 1:2012c31aee1b 56 pc.printf("No response while connecting to network \r\n");
SIT2016 1:2012c31aee1b 57
SIT2016 1:2012c31aee1b 58 pc.printf("******** Getting IP and MAC of module ********\r\n");
SIT2016 1:2012c31aee1b 59 wifi.GetIP(resp);
SIT2016 1:2012c31aee1b 60 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 61 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 62 else
SIT2016 1:2012c31aee1b 63 pc.printf("No response while getting IP \r\n");
SIT2016 1:2012c31aee1b 64
SIT2016 1:2012c31aee1b 65 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
SIT2016 1:2012c31aee1b 66 wifi.setTransparent();
SIT2016 1:2012c31aee1b 67 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 68 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 69 else
SIT2016 1:2012c31aee1b 70 pc.printf("No response while setting wifi passthrough. \r\n");
SIT2016 2:817794b2f733 71 wait(1);
SIT2016 1:2012c31aee1b 72
SIT2016 1:2012c31aee1b 73 pc.printf("******** Setting single connection mode ********\r\n");
SIT2016 1:2012c31aee1b 74 wifi.SetSingle();
SIT2016 1:2012c31aee1b 75 wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 76 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 77 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 78 else
SIT2016 1:2012c31aee1b 79 pc.printf("No response while setting single connection \r\n");
SIT2016 1:2012c31aee1b 80 wait(1);
SIT2016 2:817794b2f733 81 }
SIT2016 2:817794b2f733 82
SIT2016 2:817794b2f733 83 void wifi_send(void){
SIT2016 1:2012c31aee1b 84
SIT2016 1:2012c31aee1b 85 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
SIT2016 1:2012c31aee1b 86 wifi.startTCPConn(IP, 8080); //cipstart
SIT2016 1:2012c31aee1b 87 wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 88 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 89 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 90 else
SIT2016 1:2012c31aee1b 91 pc.printf("No response while starting TCP connection \r\n");
SIT2016 1:2012c31aee1b 92 wait(1);
SIT2016 1:2012c31aee1b 93
SIT2016 1:2012c31aee1b 94 //create link
SIT2016 1:2012c31aee1b 95 sprintf(http_cmd,"/input/%s?private_key=%s&val=%0.2f",Public_Key,Private_Key,ldrval);
SIT2016 2:817794b2f733 96 pc.printf(http_cmd);
SIT2016 1:2012c31aee1b 97
SIT2016 1:2012c31aee1b 98 pc.printf("******** Sending URL to wifi ********\r\n");
SIT2016 1:2012c31aee1b 99 wifi.sendURL(http_cmd, comm); //cipsend and get command
SIT2016 1:2012c31aee1b 100 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 101 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 102 else
SIT2016 1:2012c31aee1b 103 pc.printf("No response while sending URL \r\n");
SIT2016 1:2012c31aee1b 104
SIT2016 1:2012c31aee1b 105 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
SIT2016 1:2012c31aee1b 106 //wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 107 //pc.printf("%s", resp);
SIT2016 1:2012c31aee1b 108 }
SIT2016 1:2012c31aee1b 109
SIT2016 0:8ca71ccb52db 110 int main () {
SIT2016 0:8ca71ccb52db 111
SIT2016 1:2012c31aee1b 112
SIT2016 1:2012c31aee1b 113 wifi_initialize();
SIT2016 1:2012c31aee1b 114
SIT2016 0:8ca71ccb52db 115 while (1) {
SIT2016 1:2012c31aee1b 116 ldrval = pot.read();
SIT2016 1:2012c31aee1b 117 pc.printf("Potentiometer ldrvalue=%.3f \r\n", ldrval);
SIT2016 1:2012c31aee1b 118
SIT2016 1:2012c31aee1b 119 wifi_send();
SIT2016 1:2012c31aee1b 120 wait(3);
SIT2016 0:8ca71ccb52db 121 }
SIT2016 0:8ca71ccb52db 122 }
SIT2016 0:8ca71ccb52db 123
SIT2016 0:8ca71ccb52db 124