Pratit Nayak
/
multipleLEDIOT
wasghujkl
Fork of thingspeak_LEDCONTROL by
main.cpp@4:00c227a97ba9, 2018-05-30 (annotated)
- Committer:
- pratit911
- Date:
- Wed May 30 07:19:36 2018 +0000
- Revision:
- 4:00c227a97ba9
- Parent:
- 3:1722a03793c6
sdftyuio
Who changed what in which revision?
User | Revision | Line number | New 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:00c227a97ba9 | 6 | DigitalOut bled(LED2); |
pratit911 | 4:00c227a97ba9 | 7 | DigitalOut gled(LED3); |
eduvanceIoT | 0:34d3f68b920e | 8 | |
pratit911 | 4:00c227a97ba9 | 9 | |
eduvanceIoT | 0:34d3f68b920e | 10 | //wifi UART port and baud rate |
mrbhatter | 3:1722a03793c6 | 11 | ESP8266 wifi(PTE0, PTE1, 115200); // Class Variable Pin dexlaration for WIFI |
eduvanceIoT | 0:34d3f68b920e | 12 | |
eduvanceIoT | 0:34d3f68b920e | 13 | //buffers for wifi library |
mrbhatter | 3:1722a03793c6 | 14 | char snd[255],resp[9000]; |
eduvanceIoT | 0:34d3f68b920e | 15 | char http_cmd[300], comm[300]; |
mrbhatter | 3:1722a03793c6 | 16 | char url_response[9000]; |
eduvanceIoT | 0:34d3f68b920e | 17 | |
mrbhatter | 3:1722a03793c6 | 18 | int timeout = 5000; //timeout for wifi commands |
eduvanceIoT | 0:34d3f68b920e | 19 | |
mrbhatter | 3:1722a03793c6 | 20 | //SSID and password for connection |
mrbhatter | 3:1722a03793c6 | 21 | #define SSID "Karkhana" |
mrbhatter | 3:1722a03793c6 | 22 | #define PASS "Karkhana2018" |
mrbhatter | 3:1722a03793c6 | 23 | void control_led(void); |
eduvanceIoT | 0:34d3f68b920e | 24 | //Remote IP |
mrbhatter | 3:1722a03793c6 | 25 | #define IP "184.106.153.149" // IP for thingspeak server. Remains same for al codes using thingspeak |
mrbhatter | 3:1722a03793c6 | 26 | //#define IP "192.168.0.25" |
mrbhatter | 3:1722a03793c6 | 27 | //waterlevelue global variable |
mrbhatter | 3:1722a03793c6 | 28 | int ledstatus=0; |
SIT2016 | 1:8ab009672555 | 29 | |
mrbhatter | 3:1722a03793c6 | 30 | //Public and private keys for phant |
pratit911 | 4:00c227a97ba9 | 31 | char* Update_Key = "OPETON9L7UP3FCOP"; // Copy the read key for your channel and paste it here. |
mrbhatter | 3:1722a03793c6 | 32 | //char* Private_Key = "GPoWnRM60yidrB0e1pXD"; |
mrbhatter | 3:1722a03793c6 | 33 | |
mrbhatter | 3:1722a03793c6 | 34 | //Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above |
mrbhatter | 3:1722a03793c6 | 35 | void wifi_initialize(void){ |
mrbhatter | 3:1722a03793c6 | 36 | |
pratit911 | 4:00c227a97ba9 | 37 | rled=1; |
pratit911 | 4:00c227a97ba9 | 38 | bled=1; |
pratit911 | 4:00c227a97ba9 | 39 | gled=1; |
pratit911 | 4:00c227a97ba9 | 40 | |
eduvanceIoT | 0:34d3f68b920e | 41 | pc.printf("******** Resetting wifi module ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 42 | wifi.Reset(); |
mrbhatter | 3:1722a03793c6 | 43 | |
eduvanceIoT | 0:34d3f68b920e | 44 | //wait for 5 seconds for response, else display no response receiveed |
mrbhatter | 3:1722a03793c6 | 45 | if (wifi.RcvReply(resp, 5000)) |
mrbhatter | 3:1722a03793c6 | 46 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 47 | else |
eduvanceIoT | 0:34d3f68b920e | 48 | pc.printf("No response"); |
mrbhatter | 3:1722a03793c6 | 49 | |
eduvanceIoT | 0:34d3f68b920e | 50 | pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 51 | wifi.SetMode(1); // set transparent mode |
eduvanceIoT | 0:34d3f68b920e | 52 | if (wifi.RcvReply(resp, timeout)) //receive a response from ESP |
eduvanceIoT | 0:34d3f68b920e | 53 | pc.printf("%s",resp); //Print the response onscreen |
eduvanceIoT | 0:34d3f68b920e | 54 | else |
eduvanceIoT | 0:34d3f68b920e | 55 | pc.printf("No response while setting mode. \r\n"); |
mrbhatter | 3:1722a03793c6 | 56 | |
eduvanceIoT | 0:34d3f68b920e | 57 | pc.printf("******** Joining network with SSID and PASS ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 58 | wifi.Join(SSID, PASS); |
mrbhatter | 3:1722a03793c6 | 59 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 60 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 61 | else |
eduvanceIoT | 0:34d3f68b920e | 62 | pc.printf("No response while connecting to network \r\n"); |
mrbhatter | 3:1722a03793c6 | 63 | |
eduvanceIoT | 0:34d3f68b920e | 64 | pc.printf("******** Getting IP and MAC of module ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 65 | wifi.GetIP(resp); |
mrbhatter | 3:1722a03793c6 | 66 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 67 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 68 | else |
eduvanceIoT | 0:34d3f68b920e | 69 | pc.printf("No response while getting IP \r\n"); |
mrbhatter | 3:1722a03793c6 | 70 | |
eduvanceIoT | 0:34d3f68b920e | 71 | pc.printf("******** Setting WIFI UART passthrough ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 72 | wifi.setTransparent(); |
mrbhatter | 3:1722a03793c6 | 73 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 74 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 75 | else |
eduvanceIoT | 0:34d3f68b920e | 76 | pc.printf("No response while setting wifi passthrough. \r\n"); |
mrbhatter | 3:1722a03793c6 | 77 | wait(1); |
mrbhatter | 3:1722a03793c6 | 78 | |
eduvanceIoT | 0:34d3f68b920e | 79 | pc.printf("******** Setting single connection mode ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 80 | wifi.SetSingle(); |
eduvanceIoT | 0:34d3f68b920e | 81 | wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 82 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 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 | 3:1722a03793c6 | 89 | void wifi_send(void){ |
mrbhatter | 3:1722a03793c6 | 90 | |
eduvanceIoT | 0:34d3f68b920e | 91 | pc.printf("******** Starting TCP connection on IP and port ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 92 | wifi.startTCPConn(IP,80); //cipstart |
eduvanceIoT | 0:34d3f68b920e | 93 | wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 94 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 95 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 96 | else |
eduvanceIoT | 0:34d3f68b920e | 97 | pc.printf("No response while starting TCP connection \r\n"); |
eduvanceIoT | 0:34d3f68b920e | 98 | wait(1); |
mrbhatter | 3:1722a03793c6 | 99 | |
pratit911 | 4:00c227a97ba9 | 100 | //create link |
pratit911 | 4:00c227a97ba9 | 101 | sprintf(http_cmd,"/channels/500979/fields/1.json?api_key=%s&results=2",Update_Key); // Forms the url for transmitting to thingspeak server. the format can be seen on thingspeak site as well |
eduvanceIoT | 0:34d3f68b920e | 102 | pc.printf(http_cmd); |
mrbhatter | 3:1722a03793c6 | 103 | |
eduvanceIoT | 0:34d3f68b920e | 104 | pc.printf("******** Sending URL to wifi ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 105 | wifi.sendURL(http_cmd, comm); //cipsend and get command |
mrbhatter | 3:1722a03793c6 | 106 | if (wifi.RcvReply(resp, timeout)){ |
mrbhatter | 3:1722a03793c6 | 107 | pc.printf("--------------------------Request Response -------------------------------------------\r\n"); |
mrbhatter | 2:97dbf542ebee | 108 | pc.printf("%s",resp); |
mrbhatter | 3:1722a03793c6 | 109 | pc.printf("--------------------------Request Response -------------------------------------------\r\n"); |
mrbhatter | 3:1722a03793c6 | 110 | control_led(); |
mrbhatter | 3:1722a03793c6 | 111 | } |
SIT2016 | 1:8ab009672555 | 112 | else |
SIT2016 | 1:8ab009672555 | 113 | pc.printf("No response while sending URL \r\n"); |
mrbhatter | 3:1722a03793c6 | 114 | |
mrbhatter | 3:1722a03793c6 | 115 | //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server |
mrbhatter | 3:1722a03793c6 | 116 | //wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 117 | //pc.printf("%s", resp); |
eduvanceIoT | 0:34d3f68b920e | 118 | } |
pratit911 | 4:00c227a97ba9 | 119 | |
eduvanceIoT | 0:34d3f68b920e | 120 | |
mrbhatter | 3:1722a03793c6 | 121 | void control_led(void) |
mrbhatter | 2:97dbf542ebee | 122 | { |
mrbhatter | 3:1722a03793c6 | 123 | int length=strlen(resp); |
pratit911 | 4:00c227a97ba9 | 124 | |
mrbhatter | 3:1722a03793c6 | 125 | pc.printf("Length of response: %d\r\n",strlen(resp)); |
mrbhatter | 3:1722a03793c6 | 126 | ledstatus=int(resp[length-13]); |
mrbhatter | 3:1722a03793c6 | 127 | pc.printf("LED Status is %c\r\n",resp[length-13]); |
mrbhatter | 3:1722a03793c6 | 128 | pc.printf("LED status is %d\r\n",ledstatus); |
mrbhatter | 3:1722a03793c6 | 129 | if(resp[length-13]==49){ |
pratit911 | 4:00c227a97ba9 | 130 | pc.printf("RED LED ON"); |
mrbhatter | 3:1722a03793c6 | 131 | rled=0; |
pratit911 | 4:00c227a97ba9 | 132 | bled=1; |
pratit911 | 4:00c227a97ba9 | 133 | gled=1; |
pratit911 | 4:00c227a97ba9 | 134 | } |
pratit911 | 4:00c227a97ba9 | 135 | else if(resp[length-13]==50) |
pratit911 | 4:00c227a97ba9 | 136 | |
pratit911 | 4:00c227a97ba9 | 137 | { pc.printf("GREEN LED ON"); |
pratit911 | 4:00c227a97ba9 | 138 | rled=1; |
pratit911 | 4:00c227a97ba9 | 139 | bled=0; |
pratit911 | 4:00c227a97ba9 | 140 | gled=1; |
pratit911 | 4:00c227a97ba9 | 141 | |
mrbhatter | 3:1722a03793c6 | 142 | } |
pratit911 | 4:00c227a97ba9 | 143 | else if(resp[length-13]==51) |
pratit911 | 4:00c227a97ba9 | 144 | |
pratit911 | 4:00c227a97ba9 | 145 | { |
pratit911 | 4:00c227a97ba9 | 146 | pc.printf("BLUE LED ON"); |
mrbhatter | 3:1722a03793c6 | 147 | rled=1; |
pratit911 | 4:00c227a97ba9 | 148 | bled=1; |
pratit911 | 4:00c227a97ba9 | 149 | gled=0; |
pratit911 | 4:00c227a97ba9 | 150 | |
pratit911 | 4:00c227a97ba9 | 151 | } |
pratit911 | 4:00c227a97ba9 | 152 | else { |
pratit911 | 4:00c227a97ba9 | 153 | pc.printf("waiting"); |
pratit911 | 4:00c227a97ba9 | 154 | rled=1; |
pratit911 | 4:00c227a97ba9 | 155 | bled=1; |
pratit911 | 4:00c227a97ba9 | 156 | gled=1; |
pratit911 | 4:00c227a97ba9 | 157 | } |
mrbhatter | 3:1722a03793c6 | 158 | } |
pratit911 | 4:00c227a97ba9 | 159 | |
mrbhatter | 3:1722a03793c6 | 160 | |
mrbhatter | 3:1722a03793c6 | 161 | int main () { |
mrbhatter | 3:1722a03793c6 | 162 | |
mrbhatter | 3:1722a03793c6 | 163 | |
mrbhatter | 3:1722a03793c6 | 164 | wifi_initialize(); // Calls function defined above to initialize the wifi module |
mrbhatter | 3:1722a03793c6 | 165 | |
mrbhatter | 3:1722a03793c6 | 166 | while (1) { |
mrbhatter | 3:1722a03793c6 | 167 | wifi_send(); // Calls function defined above to send data to thingspeak |
mrbhatter | 3:1722a03793c6 | 168 | //wait(5); |
eduvanceIoT | 0:34d3f68b920e | 169 | } |
eduvanceIoT | 0:34d3f68b920e | 170 | } |