Uploading esp8266

Dependencies:   mbed ESP8266

Committer:
mrbhatter
Date:
Fri Sep 07 11:19:36 2018 +0000
Revision:
3:a19e84b2585e
Parent:
2:97dbf542ebee
ABIT

Who changed what in which revision?

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