potentiometer and LDR sensor Data send to the cloud server.

Dependencies:   mbed ESP8266

Committer:
mrbhatter
Date:
Sun Dec 03 13:10:28 2017 +0000
Revision:
2:97dbf542ebee
Parent:
1:8ab009672555
Child:
3:c3ec6ece92f6
Uploading Two Data To Thingspeak server.

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
eduvanceIoT 0:34d3f68b920e 17 int timeout = 8000; //timeout for wifi commands
eduvanceIoT 0:34d3f68b920e 18
eduvanceIoT 0:34d3f68b920e 19 //SSID and password for connection
mrbhatter 2:97dbf542ebee 20 #define SSID "Tenda_43DA50"
mrbhatter 2:97dbf542ebee 21 #define PASS "Karkhana"
eduvanceIoT 0:34d3f68b920e 22
eduvanceIoT 0:34d3f68b920e 23 //Remote IP
mrbhatter 2:97dbf542ebee 24 #define IP "184.106.153.149" // 52.202.122.57
SIT2016 1:8ab009672555 25 //global variable
mrbhatter 2:97dbf542ebee 26 float potvalue = 0;
mrbhatter 2:97dbf542ebee 27 float litvalue = 0;
SIT2016 1:8ab009672555 28
SIT2016 1:8ab009672555 29 //Update key for thingspeak
mrbhatter 2:97dbf542ebee 30 char* Update_Key = "P2GMSKA9NY7KV0LW";
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 2:97dbf542ebee 102 sprintf(http_cmd,"/update?api_key=%s&field2=%f&field1=%f",Update_Key,litvalue,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) {
SIT2016 1:8ab009672555 118 potvalue = 255*pot.read();
mrbhatter 2:97dbf542ebee 119 litvalue = lit.read();
SIT2016 1:8ab009672555 120 pc.printf("Current potvalue is = %.3f \r\n", potvalue);
mrbhatter 2:97dbf542ebee 121 pc.printf("Current light value is = %.3f \r\n", litvalue);
eduvanceIoT 0:34d3f68b920e 122 wifi_send();
SIT2016 1:8ab009672555 123 wait(30);
eduvanceIoT 0:34d3f68b920e 124 }
eduvanceIoT 0:34d3f68b920e 125 }