Pratit Nayak
/
node_red_DL
wsedfyuji
Fork of thingspeak_LEDCONTROL by
main.cpp@3:1722a03793c6, 2018-05-22 (annotated)
- Committer:
- mrbhatter
- Date:
- Tue May 22 05:02:54 2018 +0000
- Revision:
- 3:1722a03793c6
- Parent:
- 2:97dbf542ebee
- Child:
- 4:a8e100b532ee
FRDM Thingspeak data and LED Control;
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); |
eduvanceIoT | 0:34d3f68b920e | 6 | |
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 |
eduvanceIoT | 0:34d3f68b920e | 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 |
mrbhatter | 3:1722a03793c6 | 22 | #define IP "184.106.153.149" // 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 |
mrbhatter | 3:1722a03793c6 | 28 | char* Update_Key = "QRCE7DOPVY3I49YN"; // 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 | |
eduvanceIoT | 0:34d3f68b920e | 34 | pc.printf("******** Resetting wifi module ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 35 | wifi.Reset(); |
mrbhatter | 3:1722a03793c6 | 36 | |
eduvanceIoT | 0:34d3f68b920e | 37 | //wait for 5 seconds for response, else display no response receiveed |
mrbhatter | 3:1722a03793c6 | 38 | if (wifi.RcvReply(resp, 5000)) |
mrbhatter | 3:1722a03793c6 | 39 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 40 | else |
eduvanceIoT | 0:34d3f68b920e | 41 | pc.printf("No response"); |
mrbhatter | 3:1722a03793c6 | 42 | |
eduvanceIoT | 0:34d3f68b920e | 43 | pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 44 | wifi.SetMode(1); // set transparent mode |
eduvanceIoT | 0:34d3f68b920e | 45 | if (wifi.RcvReply(resp, timeout)) //receive a response from ESP |
eduvanceIoT | 0:34d3f68b920e | 46 | pc.printf("%s",resp); //Print the response onscreen |
eduvanceIoT | 0:34d3f68b920e | 47 | else |
eduvanceIoT | 0:34d3f68b920e | 48 | pc.printf("No response while setting mode. \r\n"); |
mrbhatter | 3:1722a03793c6 | 49 | |
eduvanceIoT | 0:34d3f68b920e | 50 | pc.printf("******** Joining network with SSID and PASS ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 51 | wifi.Join(SSID, PASS); |
mrbhatter | 3:1722a03793c6 | 52 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 53 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 54 | else |
eduvanceIoT | 0:34d3f68b920e | 55 | pc.printf("No response while connecting to network \r\n"); |
mrbhatter | 3:1722a03793c6 | 56 | |
eduvanceIoT | 0:34d3f68b920e | 57 | pc.printf("******** Getting IP and MAC of module ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 58 | wifi.GetIP(resp); |
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 getting IP \r\n"); |
mrbhatter | 3:1722a03793c6 | 63 | |
eduvanceIoT | 0:34d3f68b920e | 64 | pc.printf("******** Setting WIFI UART passthrough ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 65 | wifi.setTransparent(); |
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 setting wifi passthrough. \r\n"); |
mrbhatter | 3:1722a03793c6 | 70 | wait(1); |
mrbhatter | 3:1722a03793c6 | 71 | |
eduvanceIoT | 0:34d3f68b920e | 72 | pc.printf("******** Setting single connection mode ********\r\n"); |
mrbhatter | 3:1722a03793c6 | 73 | wifi.SetSingle(); |
eduvanceIoT | 0:34d3f68b920e | 74 | wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 75 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 76 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 77 | else |
eduvanceIoT | 0:34d3f68b920e | 78 | pc.printf("No response while setting single connection \r\n"); |
eduvanceIoT | 0:34d3f68b920e | 79 | wait(1); |
eduvanceIoT | 0:34d3f68b920e | 80 | } |
eduvanceIoT | 0:34d3f68b920e | 81 | |
mrbhatter | 3:1722a03793c6 | 82 | void wifi_send(void){ |
mrbhatter | 3:1722a03793c6 | 83 | |
eduvanceIoT | 0:34d3f68b920e | 84 | pc.printf("******** Starting TCP connection on IP and port ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 85 | wifi.startTCPConn(IP,80); //cipstart |
eduvanceIoT | 0:34d3f68b920e | 86 | wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 87 | if (wifi.RcvReply(resp, timeout)) |
mrbhatter | 3:1722a03793c6 | 88 | pc.printf("%s",resp); |
eduvanceIoT | 0:34d3f68b920e | 89 | else |
eduvanceIoT | 0:34d3f68b920e | 90 | pc.printf("No response while starting TCP connection \r\n"); |
eduvanceIoT | 0:34d3f68b920e | 91 | wait(1); |
mrbhatter | 3:1722a03793c6 | 92 | |
mrbhatter | 3:1722a03793c6 | 93 | //create link |
mrbhatter | 3:1722a03793c6 | 94 | sprintf(http_cmd,"/channels/501352/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 | 95 | pc.printf(http_cmd); |
mrbhatter | 3:1722a03793c6 | 96 | |
eduvanceIoT | 0:34d3f68b920e | 97 | pc.printf("******** Sending URL to wifi ********\r\n"); |
eduvanceIoT | 0:34d3f68b920e | 98 | wifi.sendURL(http_cmd, comm); //cipsend and get command |
mrbhatter | 3:1722a03793c6 | 99 | if (wifi.RcvReply(resp, timeout)){ |
mrbhatter | 3:1722a03793c6 | 100 | pc.printf("--------------------------Request Response -------------------------------------------\r\n"); |
mrbhatter | 2:97dbf542ebee | 101 | pc.printf("%s",resp); |
mrbhatter | 3:1722a03793c6 | 102 | pc.printf("--------------------------Request Response -------------------------------------------\r\n"); |
mrbhatter | 3:1722a03793c6 | 103 | control_led(); |
mrbhatter | 3:1722a03793c6 | 104 | } |
SIT2016 | 1:8ab009672555 | 105 | else |
SIT2016 | 1:8ab009672555 | 106 | pc.printf("No response while sending URL \r\n"); |
mrbhatter | 3:1722a03793c6 | 107 | |
mrbhatter | 3:1722a03793c6 | 108 | //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server |
mrbhatter | 3:1722a03793c6 | 109 | //wifi.RcvReply(resp, timeout); |
mrbhatter | 3:1722a03793c6 | 110 | //pc.printf("%s", resp); |
eduvanceIoT | 0:34d3f68b920e | 111 | } |
eduvanceIoT | 0:34d3f68b920e | 112 | |
mrbhatter | 3:1722a03793c6 | 113 | void control_led(void) |
mrbhatter | 2:97dbf542ebee | 114 | { |
mrbhatter | 3:1722a03793c6 | 115 | int length=strlen(resp); |
mrbhatter | 3:1722a03793c6 | 116 | pc.printf("Length of response: %d\r\n",strlen(resp)); |
mrbhatter | 3:1722a03793c6 | 117 | ledstatus=int(resp[length-13]); |
mrbhatter | 3:1722a03793c6 | 118 | pc.printf("LED Status is %c\r\n",resp[length-13]); |
mrbhatter | 3:1722a03793c6 | 119 | pc.printf("LED status is %d\r\n",ledstatus); |
mrbhatter | 3:1722a03793c6 | 120 | if(resp[length-13]==49){ |
mrbhatter | 3:1722a03793c6 | 121 | pc.printf("LED ON"); |
mrbhatter | 3:1722a03793c6 | 122 | rled=0; |
mrbhatter | 3:1722a03793c6 | 123 | } |
mrbhatter | 3:1722a03793c6 | 124 | else{ |
mrbhatter | 3:1722a03793c6 | 125 | pc.printf("LED OFF"); |
mrbhatter | 3:1722a03793c6 | 126 | rled=1; |
mrbhatter | 3:1722a03793c6 | 127 | } |
mrbhatter | 3:1722a03793c6 | 128 | } |
mrbhatter | 3:1722a03793c6 | 129 | |
mrbhatter | 3:1722a03793c6 | 130 | |
mrbhatter | 3:1722a03793c6 | 131 | int main () { |
mrbhatter | 3:1722a03793c6 | 132 | |
mrbhatter | 3:1722a03793c6 | 133 | |
mrbhatter | 3:1722a03793c6 | 134 | wifi_initialize(); // Calls function defined above to initialize the wifi module |
mrbhatter | 3:1722a03793c6 | 135 | |
mrbhatter | 3:1722a03793c6 | 136 | while (1) { |
mrbhatter | 3:1722a03793c6 | 137 | wifi_send(); // Calls function defined above to send data to thingspeak |
mrbhatter | 3:1722a03793c6 | 138 | //wait(5); |
eduvanceIoT | 0:34d3f68b920e | 139 | } |
eduvanceIoT | 0:34d3f68b920e | 140 | } |