Controlling Two LED's using Node-Red and ESp8266

Dependencies:   ESP8266 mbed

Fork of thingspeak_LEDCONTROL by Karkhana Makerspace

Committer:
animesh44
Date:
Fri Jun 01 07:32:09 2018 +0000
Revision:
4:a4fecf27c726
Parent:
3:1722a03793c6
Controlling Two LED's using Node-Red and ESP8266

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);
animesh44 4:a4fecf27c726 6 DigitalOut gled(LED2);
eduvanceIoT 0:34d3f68b920e 7
eduvanceIoT 0:34d3f68b920e 8 //wifi UART port and baud rate
mrbhatter 3:1722a03793c6 9 ESP8266 wifi(PTE0, PTE1, 115200); // Class Variable Pin dexlaration for WIFI
eduvanceIoT 0:34d3f68b920e 10
eduvanceIoT 0:34d3f68b920e 11 //buffers for wifi library
mrbhatter 3:1722a03793c6 12 char snd[255],resp[9000];
eduvanceIoT 0:34d3f68b920e 13 char http_cmd[300], comm[300];
mrbhatter 3:1722a03793c6 14 char url_response[9000];
eduvanceIoT 0:34d3f68b920e 15
mrbhatter 3:1722a03793c6 16 int timeout = 5000; //timeout for wifi commands
eduvanceIoT 0:34d3f68b920e 17
mrbhatter 3:1722a03793c6 18 //SSID and password for connection
animesh44 4:a4fecf27c726 19 #define SSID "Redmi Lucifer"
animesh44 4:a4fecf27c726 20 #define PASS "bhulgaya"
mrbhatter 3:1722a03793c6 21 void control_led(void);
eduvanceIoT 0:34d3f68b920e 22 //Remote IP
animesh44 4:a4fecf27c726 23 #define IP "192.168.43.73" // IP for thingspeak server. Remains same for al codes using thingspeak
mrbhatter 3:1722a03793c6 24 //#define IP "192.168.0.25"
mrbhatter 3:1722a03793c6 25 //waterlevelue global variable
mrbhatter 3:1722a03793c6 26 int ledstatus=0;
animesh44 4:a4fecf27c726 27 int ledstatus1=0;
SIT2016 1:8ab009672555 28
mrbhatter 3:1722a03793c6 29 //Public and private keys for phant
animesh44 4:a4fecf27c726 30 //char* Update_Key = "QRCE7DOPVY3I49YN"; // Copy the read key for your channel and paste it here.
mrbhatter 3:1722a03793c6 31 //char* Private_Key = "GPoWnRM60yidrB0e1pXD";
mrbhatter 3:1722a03793c6 32
mrbhatter 3:1722a03793c6 33 //Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above
mrbhatter 3:1722a03793c6 34 void wifi_initialize(void){
mrbhatter 3:1722a03793c6 35
eduvanceIoT 0:34d3f68b920e 36 pc.printf("******** Resetting wifi module ********\r\n");
eduvanceIoT 0:34d3f68b920e 37 wifi.Reset();
mrbhatter 3:1722a03793c6 38
eduvanceIoT 0:34d3f68b920e 39 //wait for 5 seconds for response, else display no response receiveed
mrbhatter 3:1722a03793c6 40 if (wifi.RcvReply(resp, 5000))
mrbhatter 3:1722a03793c6 41 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 42 else
eduvanceIoT 0:34d3f68b920e 43 pc.printf("No response");
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 51
eduvanceIoT 0:34d3f68b920e 52 pc.printf("******** Joining network with SSID and PASS ********\r\n");
mrbhatter 3:1722a03793c6 53 wifi.Join(SSID, PASS);
mrbhatter 3:1722a03793c6 54 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 58
eduvanceIoT 0:34d3f68b920e 59 pc.printf("******** Getting IP and MAC of module ********\r\n");
mrbhatter 3:1722a03793c6 60 wifi.GetIP(resp);
mrbhatter 3:1722a03793c6 61 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 62 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 63 else
eduvanceIoT 0:34d3f68b920e 64 pc.printf("No response while getting IP \r\n");
mrbhatter 3:1722a03793c6 65
eduvanceIoT 0:34d3f68b920e 66 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
mrbhatter 3:1722a03793c6 67 wifi.setTransparent();
mrbhatter 3:1722a03793c6 68 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 72 wait(1);
mrbhatter 3:1722a03793c6 73
eduvanceIoT 0:34d3f68b920e 74 pc.printf("******** Setting single connection mode ********\r\n");
mrbhatter 3:1722a03793c6 75 wifi.SetSingle();
eduvanceIoT 0:34d3f68b920e 76 wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 77 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 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 3:1722a03793c6 84 void wifi_send(void){
mrbhatter 3:1722a03793c6 85
eduvanceIoT 0:34d3f68b920e 86 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
animesh44 4:a4fecf27c726 87 wifi.startTCPConn(IP,1880); //cipstart
eduvanceIoT 0:34d3f68b920e 88 wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 89 if (wifi.RcvReply(resp, timeout))
mrbhatter 3:1722a03793c6 90 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 91 else
eduvanceIoT 0:34d3f68b920e 92 pc.printf("No response while starting TCP connection \r\n");
eduvanceIoT 0:34d3f68b920e 93 wait(1);
mrbhatter 3:1722a03793c6 94
mrbhatter 3:1722a03793c6 95 //create link
animesh44 4:a4fecf27c726 96 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 97 pc.printf(http_cmd);
mrbhatter 3:1722a03793c6 98
eduvanceIoT 0:34d3f68b920e 99 pc.printf("******** Sending URL to wifi ********\r\n");
eduvanceIoT 0:34d3f68b920e 100 wifi.sendURL(http_cmd, comm); //cipsend and get command
mrbhatter 3:1722a03793c6 101 if (wifi.RcvReply(resp, timeout)){
mrbhatter 3:1722a03793c6 102 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
mrbhatter 2:97dbf542ebee 103 pc.printf("%s",resp);
mrbhatter 3:1722a03793c6 104 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
mrbhatter 3:1722a03793c6 105 control_led();
mrbhatter 3:1722a03793c6 106 }
SIT2016 1:8ab009672555 107 else
SIT2016 1:8ab009672555 108 pc.printf("No response while sending URL \r\n");
mrbhatter 3:1722a03793c6 109
mrbhatter 3:1722a03793c6 110 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
mrbhatter 3:1722a03793c6 111 //wifi.RcvReply(resp, timeout);
mrbhatter 3:1722a03793c6 112 //pc.printf("%s", resp);
eduvanceIoT 0:34d3f68b920e 113 }
eduvanceIoT 0:34d3f68b920e 114
mrbhatter 3:1722a03793c6 115 void control_led(void)
mrbhatter 2:97dbf542ebee 116 {
mrbhatter 3:1722a03793c6 117 int length=strlen(resp);
mrbhatter 3:1722a03793c6 118 pc.printf("Length of response: %d\r\n",strlen(resp));
animesh44 4:a4fecf27c726 119 ledstatus=int(resp[length-26]);
animesh44 4:a4fecf27c726 120 ledstatus1=int(resp[length-13]);
animesh44 4:a4fecf27c726 121 pc.printf("%s",resp);
animesh44 4:a4fecf27c726 122 pc.printf("LED Status is %c\r\n",resp[length-26]);
animesh44 4:a4fecf27c726 123 pc.printf("LED2 Status is %c\r\n",resp[length-13]);
mrbhatter 3:1722a03793c6 124 pc.printf("LED status is %d\r\n",ledstatus);
animesh44 4:a4fecf27c726 125 pc.printf("LED2 status is %d\r\n",ledstatus1);
animesh44 4:a4fecf27c726 126 if(resp[length-26]==116 && resp[length-13]==97){
mrbhatter 3:1722a03793c6 127 pc.printf("LED ON");
mrbhatter 3:1722a03793c6 128 rled=0;
animesh44 4:a4fecf27c726 129 gled=1;
mrbhatter 3:1722a03793c6 130 }
animesh44 4:a4fecf27c726 131 else if(resp[length-26]==102 && resp[length-13]==116){
mrbhatter 3:1722a03793c6 132 pc.printf("LED OFF");
animesh44 4:a4fecf27c726 133 gled=0;
mrbhatter 3:1722a03793c6 134 rled=1;
mrbhatter 3:1722a03793c6 135 }
mrbhatter 3:1722a03793c6 136 }
mrbhatter 3:1722a03793c6 137
mrbhatter 3:1722a03793c6 138
mrbhatter 3:1722a03793c6 139 int main () {
mrbhatter 3:1722a03793c6 140
mrbhatter 3:1722a03793c6 141
mrbhatter 3:1722a03793c6 142 wifi_initialize(); // Calls function defined above to initialize the wifi module
mrbhatter 3:1722a03793c6 143
mrbhatter 3:1722a03793c6 144 while (1) {
mrbhatter 3:1722a03793c6 145 wifi_send(); // Calls function defined above to send data to thingspeak
mrbhatter 3:1722a03793c6 146 //wait(5);
eduvanceIoT 0:34d3f68b920e 147 }
eduvanceIoT 0:34d3f68b920e 148 }