ESP8266downstreaming

Dependencies:   mbed ESP8266

Committer:
rdverma
Date:
Wed Feb 12 11:56:04 2020 +0000
Revision:
2:ceb216ffbbbd
Parent:
1:cf615fecaa49
ESP8266-FRDM-KL25Z-THINGSPEAK downstream

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rdverma 0:6817186f0ebb 1 #include "mbed.h"
rdverma 0:6817186f0ebb 2 #include "ESP8266.h"
rdverma 0:6817186f0ebb 3
rdverma 0:6817186f0ebb 4 Serial pc(USBTX,USBRX);
rdverma 0:6817186f0ebb 5 DigitalOut rled(LED1);
rdverma 0:6817186f0ebb 6
rdverma 0:6817186f0ebb 7 //wifi UART port and baud rate
rdverma 0:6817186f0ebb 8 ESP8266 wifi(PTE0, PTE1, 115200); // Class Variable Pin dexlaration for WIFI
rdverma 0:6817186f0ebb 9
rdverma 0:6817186f0ebb 10 //buffers for wifi library
rdverma 0:6817186f0ebb 11 char snd[255],resp[9000];
rdverma 0:6817186f0ebb 12 char http_cmd[300], comm[300];
rdverma 0:6817186f0ebb 13 char url_response[9000];
rdverma 0:6817186f0ebb 14
rdverma 0:6817186f0ebb 15 int timeout = 5000; //timeout for wifi commands
rdverma 0:6817186f0ebb 16
rdverma 0:6817186f0ebb 17 //SSID and password for connection
rdverma 2:ceb216ffbbbd 18 #define SSID "abc.xyz"
rdverma 2:ceb216ffbbbd 19 #define PASS "motomoto2019"
rdverma 0:6817186f0ebb 20 void control_led(void);
rdverma 0:6817186f0ebb 21 //Remote IP
rdverma 0:6817186f0ebb 22 #define IP "184.106.153.149" // IP for thingspeak server. Remains same for al codes using thingspeak
rdverma 0:6817186f0ebb 23 //#define IP "192.168.0.25"
rdverma 0:6817186f0ebb 24 //waterlevelue global variable
rdverma 0:6817186f0ebb 25 int ledstatus=0;
rdverma 0:6817186f0ebb 26
rdverma 0:6817186f0ebb 27 //Public and private keys for phant
rdverma 2:ceb216ffbbbd 28 char* Update_Key = "YBGSY60REB45ZKKP"; // Copy the read key for your channel and paste it here.
rdverma 0:6817186f0ebb 29 //char* Private_Key = "GPoWnRM60yidrB0e1pXD";
rdverma 0:6817186f0ebb 30
rdverma 0:6817186f0ebb 31 //Wifi init function. Sets the module to connect to wifi accesspoint with ssid and password mentioned above
rdverma 0:6817186f0ebb 32 void wifi_initialize(void){
rdverma 0:6817186f0ebb 33
rdverma 0:6817186f0ebb 34 pc.printf("******** Resetting wifi module ********\r\n");
rdverma 0:6817186f0ebb 35 wifi.Reset();
rdverma 0:6817186f0ebb 36
rdverma 0:6817186f0ebb 37 //wait for 5 seconds for response, else display no response receiveed
rdverma 0:6817186f0ebb 38 if (wifi.RcvReply(resp, 5000))
rdverma 0:6817186f0ebb 39 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 40 else
rdverma 0:6817186f0ebb 41 pc.printf("No response");
rdverma 0:6817186f0ebb 42
rdverma 0:6817186f0ebb 43 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
rdverma 0:6817186f0ebb 44 wifi.SetMode(1); // set transparent mode
rdverma 0:6817186f0ebb 45 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
rdverma 0:6817186f0ebb 46 pc.printf("%s",resp); //Print the response onscreen
rdverma 0:6817186f0ebb 47 else
rdverma 0:6817186f0ebb 48 pc.printf("No response while setting mode. \r\n");
rdverma 0:6817186f0ebb 49
rdverma 0:6817186f0ebb 50 pc.printf("******** Joining network with SSID and PASS ********\r\n");
rdverma 0:6817186f0ebb 51 wifi.Join(SSID, PASS);
rdverma 0:6817186f0ebb 52 if (wifi.RcvReply(resp, timeout))
rdverma 0:6817186f0ebb 53 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 54 else
rdverma 0:6817186f0ebb 55 pc.printf("No response while connecting to network \r\n");
rdverma 0:6817186f0ebb 56
rdverma 0:6817186f0ebb 57 pc.printf("******** Getting IP and MAC of module ********\r\n");
rdverma 0:6817186f0ebb 58 wifi.GetIP(resp);
rdverma 0:6817186f0ebb 59 if (wifi.RcvReply(resp, timeout))
rdverma 0:6817186f0ebb 60 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 61 else
rdverma 0:6817186f0ebb 62 pc.printf("No response while getting IP \r\n");
rdverma 0:6817186f0ebb 63
rdverma 0:6817186f0ebb 64 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
rdverma 0:6817186f0ebb 65 wifi.setTransparent();
rdverma 0:6817186f0ebb 66 if (wifi.RcvReply(resp, timeout))
rdverma 0:6817186f0ebb 67 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 68 else
rdverma 0:6817186f0ebb 69 pc.printf("No response while setting wifi passthrough. \r\n");
rdverma 0:6817186f0ebb 70 wait(1);
rdverma 0:6817186f0ebb 71
rdverma 0:6817186f0ebb 72 pc.printf("******** Setting single connection mode ********\r\n");
rdverma 0:6817186f0ebb 73 wifi.SetSingle();
rdverma 0:6817186f0ebb 74 wifi.RcvReply(resp, timeout);
rdverma 0:6817186f0ebb 75 if (wifi.RcvReply(resp, timeout))
rdverma 0:6817186f0ebb 76 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 77 else
rdverma 0:6817186f0ebb 78 pc.printf("No response while setting single connection \r\n");
rdverma 0:6817186f0ebb 79 wait(1);
rdverma 0:6817186f0ebb 80 }
rdverma 0:6817186f0ebb 81
rdverma 0:6817186f0ebb 82 void wifi_send(void){
rdverma 0:6817186f0ebb 83
rdverma 0:6817186f0ebb 84 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
rdverma 0:6817186f0ebb 85 wifi.startTCPConn(IP,80); //cipstart
rdverma 0:6817186f0ebb 86 wifi.RcvReply(resp, timeout);
rdverma 0:6817186f0ebb 87 if (wifi.RcvReply(resp, timeout))
rdverma 0:6817186f0ebb 88 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 89 else
rdverma 0:6817186f0ebb 90 pc.printf("No response while starting TCP connection \r\n");
rdverma 0:6817186f0ebb 91 wait(1);
rdverma 0:6817186f0ebb 92
rdverma 0:6817186f0ebb 93 //create link
rdverma 2:ceb216ffbbbd 94 sprintf(http_cmd,"/channels/722168/feeds.json?api_key=%s&results=1",Update_Key); // Forms the url for transmitting to thingspeak server. the format can be seen on thingspeak site as well
rdverma 0:6817186f0ebb 95 pc.printf(http_cmd);
rdverma 0:6817186f0ebb 96
rdverma 0:6817186f0ebb 97 pc.printf("******** Sending URL to wifi ********\r\n");
rdverma 0:6817186f0ebb 98 wifi.sendURL(http_cmd, comm); //cipsend and get command
rdverma 0:6817186f0ebb 99 if (wifi.RcvReply(resp, timeout)){
rdverma 0:6817186f0ebb 100 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
rdverma 0:6817186f0ebb 101 pc.printf("%s",resp);
rdverma 0:6817186f0ebb 102 pc.printf("--------------------------Request Response -------------------------------------------\r\n");
rdverma 0:6817186f0ebb 103 control_led();
rdverma 0:6817186f0ebb 104 }
rdverma 0:6817186f0ebb 105 else
rdverma 0:6817186f0ebb 106 pc.printf("No response while sending URL \r\n");
rdverma 0:6817186f0ebb 107
rdverma 0:6817186f0ebb 108 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
rdverma 0:6817186f0ebb 109 //wifi.RcvReply(resp, timeout);
rdverma 0:6817186f0ebb 110 //pc.printf("%s", resp);
rdverma 0:6817186f0ebb 111 }
rdverma 0:6817186f0ebb 112
rdverma 0:6817186f0ebb 113 void control_led(void)
rdverma 0:6817186f0ebb 114 {
rdverma 0:6817186f0ebb 115 int length=strlen(resp);
rdverma 0:6817186f0ebb 116 pc.printf("Length of response: %d\r\n",strlen(resp));
rdverma 0:6817186f0ebb 117 ledstatus=int(resp[length-13]);
rdverma 0:6817186f0ebb 118 pc.printf("LED Status is %c\r\n",resp[length-13]);
rdverma 0:6817186f0ebb 119 pc.printf("LED status is %d\r\n",ledstatus);
rdverma 0:6817186f0ebb 120 if(resp[length-13]==49){
rdverma 0:6817186f0ebb 121 pc.printf("LED ON");
rdverma 0:6817186f0ebb 122 rled=0;
rdverma 0:6817186f0ebb 123 }
rdverma 0:6817186f0ebb 124 else{
rdverma 0:6817186f0ebb 125 pc.printf("LED OFF");
rdverma 0:6817186f0ebb 126 rled=1;
rdverma 0:6817186f0ebb 127 }
rdverma 0:6817186f0ebb 128 }
rdverma 0:6817186f0ebb 129
rdverma 0:6817186f0ebb 130
rdverma 0:6817186f0ebb 131 int main () {
rdverma 0:6817186f0ebb 132
rdverma 0:6817186f0ebb 133
rdverma 0:6817186f0ebb 134 wifi_initialize(); // Calls function defined above to initialize the wifi module
rdverma 0:6817186f0ebb 135
rdverma 0:6817186f0ebb 136 while (1) {
rdverma 0:6817186f0ebb 137 wifi_send(); // Calls function defined above to send data to thingspeak
rdverma 0:6817186f0ebb 138 //wait(5);
rdverma 0:6817186f0ebb 139 }
rdverma 0:6817186f0ebb 140 }