wsedfyuji

Dependencies:   ESP8266 mbed

Fork of thingspeak_LEDCONTROL by Karkhana Makerspace

Committer:
pratit911
Date:
Wed May 30 07:18:44 2018 +0000
Revision:
4:a8e100b532ee
Parent:
3:1722a03793c6
qwerghj

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 3:1722a03793c6 3
eduvanceIoT 0:34d3f68b920e 4 Serial pc(USBTX,USBRX);
mrbhatter 3:1722a03793c6 5 DigitalOut rled(LED1);
pratit911 4:a8e100b532ee 6 DigitalOut bled(LED2);
eduvanceIoT 0:34d3f68b920e 7 //wifi UART port and baud rate
mrbhatter 3:1722a03793c6 8 ESP8266 wifi(PTE0, PTE1, 115200); // Class Variable Pin dexlaration for WIFI
pratit911 4:a8e100b532ee 9
eduvanceIoT 0:34d3f68b920e 10 //buffers for wifi library
mrbhatter 3:1722a03793c6 11 char snd[255],resp[9000];
eduvanceIoT 0:34d3f68b920e 12 char http_cmd[300], comm[300];
mrbhatter 3:1722a03793c6 13 char url_response[9000];
eduvanceIoT 0:34d3f68b920e 14
mrbhatter 3:1722a03793c6 15 int timeout = 5000; //timeout for wifi commands
eduvanceIoT 0:34d3f68b920e 16
mrbhatter 3:1722a03793c6 17 //SSID and password for connection
mrbhatter 3:1722a03793c6 18 #define SSID "Karkhana"
mrbhatter 3:1722a03793c6 19 #define PASS "Karkhana2018"
mrbhatter 3:1722a03793c6 20 void control_led(void);
eduvanceIoT 0:34d3f68b920e 21 //Remote IP
pratit911 4:a8e100b532ee 22 #define IP "192.168.88.76" // IP for thingspeak server. Remains same for al codes using thingspeak
mrbhatter 3:1722a03793c6 23 //#define IP "192.168.0.25"
mrbhatter 3:1722a03793c6 24 //waterlevelue global variable
mrbhatter 3:1722a03793c6 25 int ledstatus=0;
SIT2016 1:8ab009672555 26
mrbhatter 3:1722a03793c6 27 //Public and private keys for phant
pratit911 4:a8e100b532ee 28 char* Update_Key = "OPETON9L7UP3FCOP"; // Copy the read key for your channel and paste it here.
mrbhatter 3:1722a03793c6 29 //char* Private_Key = "GPoWnRM60yidrB0e1pXD";
mrbhatter 3:1722a03793c6 30
mrbhatter 3:1722a03793c6 31 //Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above
mrbhatter 3:1722a03793c6 32 void wifi_initialize(void){
mrbhatter 3:1722a03793c6 33
pratit911 4:a8e100b532ee 34 rled=1;
pratit911 4:a8e100b532ee 35 bled=1;
pratit911 4:a8e100b532ee 36
eduvanceIoT 0:34d3f68b920e 37 pc.printf("******** Resetting wifi module ********\r\n");
eduvanceIoT 0:34d3f68b920e 38 wifi.Reset();
mrbhatter 3:1722a03793c6 39
eduvanceIoT 0:34d3f68b920e 40 //wait for 5 seconds for response, else display no response receiveed
mrbhatter 3:1722a03793c6 41 if (wifi.RcvReply(resp, 5000))
mrbhatter 3:1722a03793c6 42 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 43 else
eduvanceIoT 0:34d3f68b920e 44 pc.printf("No response");
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 52
eduvanceIoT 0:34d3f68b920e 53 pc.printf("******** Joining network with SSID and PASS ********\r\n");
mrbhatter 3:1722a03793c6 54 wifi.Join(SSID, PASS);
mrbhatter 3:1722a03793c6 55 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 56 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 57 else
eduvanceIoT 0:34d3f68b920e 58 pc.printf("No response while connecting to network \r\n");
mrbhatter 3:1722a03793c6 59
eduvanceIoT 0:34d3f68b920e 60 pc.printf("******** Getting IP and MAC of module ********\r\n");
mrbhatter 3:1722a03793c6 61 wifi.GetIP(resp);
mrbhatter 3:1722a03793c6 62 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 63 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 64 else
eduvanceIoT 0:34d3f68b920e 65 pc.printf("No response while getting IP \r\n");
mrbhatter 3:1722a03793c6 66
eduvanceIoT 0:34d3f68b920e 67 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
mrbhatter 3:1722a03793c6 68 wifi.setTransparent();
mrbhatter 3:1722a03793c6 69 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 70 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 71 else
eduvanceIoT 0:34d3f68b920e 72 pc.printf("No response while setting wifi passthrough. \r\n");
mrbhatter 3:1722a03793c6 73 wait(1);
mrbhatter 3:1722a03793c6 74
eduvanceIoT 0:34d3f68b920e 75 pc.printf("******** Setting single connection mode ********\r\n");
mrbhatter 3:1722a03793c6 76 wifi.SetSingle();
eduvanceIoT 0:34d3f68b920e 77 wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 78 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 79 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 80 else
eduvanceIoT 0:34d3f68b920e 81 pc.printf("No response while setting single connection \r\n");
eduvanceIoT 0:34d3f68b920e 82 wait(1);
eduvanceIoT 0:34d3f68b920e 83 }
eduvanceIoT 0:34d3f68b920e 84
mrbhatter 3:1722a03793c6 85 void wifi_send(void){
mrbhatter 3:1722a03793c6 86
eduvanceIoT 0:34d3f68b920e 87 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
pratit911 4:a8e100b532ee 88 wifi.startTCPConn(IP,1880); //cipstart
eduvanceIoT 0:34d3f68b920e 89 wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 90 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 95
pratit911 4:a8e100b532ee 96 //create link
pratit911 4:a8e100b532ee 97 sprintf(http_cmd,"/status?"); // Forms the url for transmitting to thingspeak server. the format can be seen on thingspeak site as well
eduvanceIoT 0:34d3f68b920e 98 pc.printf(http_cmd);
mrbhatter 3:1722a03793c6 99
eduvanceIoT 0:34d3f68b920e 100 pc.printf("******** Sending URL to wifi ********\r\n");
eduvanceIoT 0:34d3f68b920e 101 wifi.sendURL(http_cmd, comm); //cipsend and get command
mrbhatter 3:1722a03793c6 102 if (wifi.RcvReply(resp, timeout)){
mrbhatter 3:1722a03793c6 103 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
mrbhatter 2:97dbf542ebee 104 pc.printf("%s",resp);
mrbhatter 3:1722a03793c6 105 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
mrbhatter 3:1722a03793c6 106 control_led();
mrbhatter 3:1722a03793c6 107 }
SIT2016 1:8ab009672555 108 else
SIT2016 1:8ab009672555 109 pc.printf("No response while sending URL \r\n");
mrbhatter 3:1722a03793c6 110
mrbhatter 3:1722a03793c6 111 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
mrbhatter 3:1722a03793c6 112 //wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 113 //pc.printf("%s", resp);
eduvanceIoT 0:34d3f68b920e 114 }
pratit911 4:a8e100b532ee 115
eduvanceIoT 0:34d3f68b920e 116
mrbhatter 3:1722a03793c6 117 void control_led(void)
pratit911 4:a8e100b532ee 118 {
pratit911 4:a8e100b532ee 119 int length=strlen(resp);
pratit911 4:a8e100b532ee 120 pc.printf("%s",resp);
pratit911 4:a8e100b532ee 121 pc.printf("Length of response: %d\r\n",strlen(resp));
pratit911 4:a8e100b532ee 122
pratit911 4:a8e100b532ee 123
pratit911 4:a8e100b532ee 124 /*if(resp[8]=='t')
pratit911 4:a8e100b532ee 125 {
pratit911 4:a8e100b532ee 126 rled=0;
pratit911 4:a8e100b532ee 127 pc.printf("LED1 is ON");
pratit911 4:a8e100b532ee 128 wait(1);
pratit911 4:a8e100b532ee 129 if(resp[20]=='t')
pratit911 4:a8e100b532ee 130 {bled=0;
pratit911 4:a8e100b532ee 131 pc.printf("LED2 is ON");
pratit911 4:a8e100b532ee 132 wait(1);
pratit911 4:a8e100b532ee 133 }
pratit911 4:a8e100b532ee 134 else
pratit911 4:a8e100b532ee 135 bled=1;
pratit911 4:a8e100b532ee 136 pc.printf("LED2 is FALSE");
pratit911 4:a8e100b532ee 137 wait(1);
pratit911 4:a8e100b532ee 138
pratit911 4:a8e100b532ee 139 }
pratit911 4:a8e100b532ee 140 else if(resp[8]=='f')
pratit911 4:a8e100b532ee 141 {
pratit911 4:a8e100b532ee 142
pratit911 4:a8e100b532ee 143 rled=1;
pratit911 4:a8e100b532ee 144 pc.printf("LED1 is OFF");
pratit911 4:a8e100b532ee 145 wait(1);
pratit911 4:a8e100b532ee 146 if(resp[21]=='t')
pratit911 4:a8e100b532ee 147 {bled=0;
pratit911 4:a8e100b532ee 148 pc.printf("LED2 is ON");
pratit911 4:a8e100b532ee 149 wait(1);
pratit911 4:a8e100b532ee 150 }
pratit911 4:a8e100b532ee 151 else
pratit911 4:a8e100b532ee 152 bled=1;
pratit911 4:a8e100b532ee 153 pc.printf("LED2 is OFF");
pratit911 4:a8e100b532ee 154 wait(1);
pratit911 4:a8e100b532ee 155 */
pratit911 4:a8e100b532ee 156
pratit911 4:a8e100b532ee 157 /* ledstatus=int(resp[length-13]);
pratit911 4:a8e100b532ee 158 pc.printf("LED Status is %c\r\n",resp[length-13]);
mrbhatter 3:1722a03793c6 159 pc.printf("LED status is %d\r\n",ledstatus);
mrbhatter 3:1722a03793c6 160 if(resp[length-13]==49){
pratit911 4:a8e100b532ee 161 pc.printf("RED LED ON");
mrbhatter 3:1722a03793c6 162 rled=0;
pratit911 4:a8e100b532ee 163 bled=1;
pratit911 4:a8e100b532ee 164
mrbhatter 3:1722a03793c6 165 }
pratit911 4:a8e100b532ee 166 else if(resp[length-13]==50)
pratit911 4:a8e100b532ee 167
pratit911 4:a8e100b532ee 168 { pc.printf("GREEN LED ON");
mrbhatter 3:1722a03793c6 169 rled=1;
pratit911 4:a8e100b532ee 170 bled=0;
pratit911 4:a8e100b532ee 171
pratit911 4:a8e100b532ee 172 }
pratit911 4:a8e100b532ee 173 else {
pratit911 4:a8e100b532ee 174 pc.printf("waiting");
pratit911 4:a8e100b532ee 175 rled=1;
pratit911 4:a8e100b532ee 176 bled=1;
pratit911 4:a8e100b532ee 177 gled=1;
pratit911 4:a8e100b532ee 178 }*/
pratit911 4:a8e100b532ee 179
pratit911 4:a8e100b532ee 180 }
mrbhatter 3:1722a03793c6 181
mrbhatter 3:1722a03793c6 182 int main () {
mrbhatter 3:1722a03793c6 183
mrbhatter 3:1722a03793c6 184
mrbhatter 3:1722a03793c6 185 wifi_initialize(); // Calls function defined above to initialize the wifi module
mrbhatter 3:1722a03793c6 186
mrbhatter 3:1722a03793c6 187 while (1) {
mrbhatter 3:1722a03793c6 188 wifi_send(); // Calls function defined above to send data to thingspeak
mrbhatter 3:1722a03793c6 189 //wait(5);
eduvanceIoT 0:34d3f68b920e 190 }
eduvanceIoT 0:34d3f68b920e 191 }