potentiometer and LDR sensor Data send to the cloud server.

Dependencies:   mbed ESP8266

Committer:
rdverma
Date:
Wed Feb 12 11:18:48 2020 +0000
Revision:
3:c3ec6ece92f6
Parent:
2:97dbf542ebee
potentiometer and ldr data to the cloud 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
rdverma 3:c3ec6ece92f6 20 #define SSID "ssid1"
rdverma 3:c3ec6ece92f6 21 #define PASS "motomoto1"
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
rdverma 3:c3ec6ece92f6 30 char* Update_Key = "AN51JBJDF9WYOLEA";
mrbhatter 2:97dbf542ebee 31
eduvanceIoT 0:34d3f68b920e 32 //Wifi init function
mrbhatter 2:97dbf542ebee 33 void wifi_initialize(void)
mrbhatter 2:97dbf542ebee 34 {
eduvanceIoT 0:34d3f68b920e 35 pc.printf("******** Resetting wifi module ********\r\n");
eduvanceIoT 0:34d3f68b920e 36 wifi.Reset();
mrbhatter 2:97dbf542ebee 37
eduvanceIoT 0:34d3f68b920e 38 //wait for 5 seconds for response, else display no response receiveed
mrbhatter 2:97dbf542ebee 39 if (wifi.RcvReply(resp, 5000))
mrbhatter 2:97dbf542ebee 40 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 41 else
eduvanceIoT 0:34d3f68b920e 42 pc.printf("No response");
mrbhatter 2:97dbf542ebee 43
mrbhatter 2:97dbf542ebee 44
eduvanceIoT 0:34d3f68b920e 45 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
eduvanceIoT 0:34d3f68b920e 46 wifi.SetMode(1); // set transparent mode
eduvanceIoT 0:34d3f68b920e 47 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
eduvanceIoT 0:34d3f68b920e 48 pc.printf("%s",resp); //Print the response onscreen
eduvanceIoT 0:34d3f68b920e 49 else
eduvanceIoT 0:34d3f68b920e 50 pc.printf("No response while setting mode. \r\n");
mrbhatter 2:97dbf542ebee 51
eduvanceIoT 0:34d3f68b920e 52 pc.printf("******** Joining network with SSID and PASS ********\r\n");
mrbhatter 2:97dbf542ebee 53 wifi.Join(SSID, PASS);
mrbhatter 2:97dbf542ebee 54 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 55 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 56 else
eduvanceIoT 0:34d3f68b920e 57 pc.printf("No response while connecting to network \r\n");
mrbhatter 2:97dbf542ebee 58
eduvanceIoT 0:34d3f68b920e 59 pc.printf("******** Getting IP and MAC of module ********\r\n");
mrbhatter 2:97dbf542ebee 60 wifi.GetIP(resp);
mrbhatter 2:97dbf542ebee 61 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 62 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 63 else
eduvanceIoT 0:34d3f68b920e 64 pc.printf("No response while getting IP \r\n");
mrbhatter 2:97dbf542ebee 65
eduvanceIoT 0:34d3f68b920e 66 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
mrbhatter 2:97dbf542ebee 67 wifi.setTransparent();
mrbhatter 2:97dbf542ebee 68 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 69 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 70 else
eduvanceIoT 0:34d3f68b920e 71 pc.printf("No response while setting wifi passthrough. \r\n");
mrbhatter 2:97dbf542ebee 72 wait(1);
mrbhatter 2:97dbf542ebee 73
eduvanceIoT 0:34d3f68b920e 74 pc.printf("******** Setting single connection mode ********\r\n");
mrbhatter 2:97dbf542ebee 75 wifi.SetSingle();
eduvanceIoT 0:34d3f68b920e 76 wifi.RcvReply(resp, timeout);
mrbhatter 2:97dbf542ebee 77 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 78 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 79 else
eduvanceIoT 0:34d3f68b920e 80 pc.printf("No response while setting single connection \r\n");
eduvanceIoT 0:34d3f68b920e 81 wait(1);
eduvanceIoT 0:34d3f68b920e 82 }
eduvanceIoT 0:34d3f68b920e 83
mrbhatter 2:97dbf542ebee 84
mrbhatter 2:97dbf542ebee 85 void wifi_send(void)
mrbhatter 2:97dbf542ebee 86 {
eduvanceIoT 0:34d3f68b920e 87 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
eduvanceIoT 0:34d3f68b920e 88 wifi.startTCPConn(IP,80); //cipstart
eduvanceIoT 0:34d3f68b920e 89 wifi.RcvReply(resp, timeout);
mrbhatter 2:97dbf542ebee 90 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 91 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 92 else
eduvanceIoT 0:34d3f68b920e 93 pc.printf("No response while starting TCP connection \r\n");
eduvanceIoT 0:34d3f68b920e 94 wait(1);
mrbhatter 2:97dbf542ebee 95
mrbhatter 2:97dbf542ebee 96 //create link
mrbhatter 2:97dbf542ebee 97 sprintf(http_cmd,"/update?api_key=%s&field2=%f&field1=%f",Update_Key,litvalue,potvalue);
mrbhatter 2:97dbf542ebee 98
eduvanceIoT 0:34d3f68b920e 99 pc.printf(http_cmd);
mrbhatter 2:97dbf542ebee 100
eduvanceIoT 0:34d3f68b920e 101 pc.printf("******** Sending URL to wifi ********\r\n");
eduvanceIoT 0:34d3f68b920e 102 wifi.sendURL(http_cmd, comm); //cipsend and get command
mrbhatter 2:97dbf542ebee 103 if (wifi.RcvReply(resp, timeout))
mrbhatter 2:97dbf542ebee 104 pc.printf("%s",resp);
SIT2016 1:8ab009672555 105 else
SIT2016 1:8ab009672555 106 pc.printf("No response while sending URL \r\n");
eduvanceIoT 0:34d3f68b920e 107 }
eduvanceIoT 0:34d3f68b920e 108
mrbhatter 2:97dbf542ebee 109 int main ()
mrbhatter 2:97dbf542ebee 110 {
eduvanceIoT 0:34d3f68b920e 111 wifi_initialize();
eduvanceIoT 0:34d3f68b920e 112 while (1) {
SIT2016 1:8ab009672555 113 potvalue = 255*pot.read();
mrbhatter 2:97dbf542ebee 114 litvalue = lit.read();
SIT2016 1:8ab009672555 115 pc.printf("Current potvalue is = %.3f \r\n", potvalue);
mrbhatter 2:97dbf542ebee 116 pc.printf("Current light value is = %.3f \r\n", litvalue);
eduvanceIoT 0:34d3f68b920e 117 wifi_send();
SIT2016 1:8ab009672555 118 wait(30);
eduvanceIoT 0:34d3f68b920e 119 }
eduvanceIoT 0:34d3f68b920e 120 }