Dewant Katare
/
ESP8266_test
ESP8266, FRDM K64F, Thingspeak
main.cpp@3:164296bb4985, 2017-05-06 (annotated)
- Committer:
- agaikwad
- Date:
- Sat May 06 21:17:22 2017 +0000
- Revision:
- 3:164296bb4985
- Parent:
- 2:817794b2f733
- Child:
- 4:c747d23ffd0e
ESP 8266 WiFi Module test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SIT2016 | 0:8ca71ccb52db | 1 | #include "mbed.h" |
agaikwad | 3:164296bb4985 | 2 | #include "ESP8266.h" // Include header file from Author: Antonio Quevedo |
agaikwad | 3:164296bb4985 | 3 | #include "math.h" |
agaikwad | 3:164296bb4985 | 4 | #include <string> |
agaikwad | 3:164296bb4985 | 5 | |
agaikwad | 3:164296bb4985 | 6 | #define APIKEY E3AASCJVE2KQDD3V //Put "Write key" of your channel in thingspeak.com |
agaikwad | 3:164296bb4985 | 7 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
agaikwad | 3:164296bb4985 | 8 | #define WIFI_SSID "Redmi" |
agaikwad | 3:164296bb4985 | 9 | #define WIFI_PASS "akash12345" |
agaikwad | 3:164296bb4985 | 10 | |
SIT2016 | 0:8ca71ccb52db | 11 | Serial pc(USBTX,USBRX); |
SIT2016 | 1:2012c31aee1b | 12 | |
agaikwad | 3:164296bb4985 | 13 | ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi |
SIT2016 | 1:2012c31aee1b | 14 | |
agaikwad | 3:164296bb4985 | 15 | char snd[255],rcv[1000],snd_Data[255]; //snd= string used to send command to ESP 8266 wii and rcv = string used to receive response from ESP8266 wifi module |
SIT2016 | 1:2012c31aee1b | 16 | |
agaikwad | 3:164296bb4985 | 17 | void esp_initialize(void); // Function used to initialize ESP8266 wifi module |
agaikwad | 3:164296bb4985 | 18 | void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module |
SIT2016 | 1:2012c31aee1b | 19 | |
agaikwad | 3:164296bb4985 | 20 | //test variable |
agaikwad | 3:164296bb4985 | 21 | float i=0; |
agaikwad | 3:164296bb4985 | 22 | float j=10; |
SIT2016 | 1:2012c31aee1b | 23 | |
agaikwad | 3:164296bb4985 | 24 | int main() |
agaikwad | 3:164296bb4985 | 25 | { |
SIT2016 | 1:2012c31aee1b | 26 | |
agaikwad | 3:164296bb4985 | 27 | pc.baud(115200); // Baud rate used for communicating with Tera-term on PC |
agaikwad | 3:164296bb4985 | 28 | |
agaikwad | 3:164296bb4985 | 29 | pc.printf("START\r\n"); // Starting point |
agaikwad | 3:164296bb4985 | 30 | |
agaikwad | 3:164296bb4985 | 31 | esp_initialize(); |
SIT2016 | 1:2012c31aee1b | 32 | |
agaikwad | 3:164296bb4985 | 33 | |
agaikwad | 3:164296bb4985 | 34 | |
agaikwad | 3:164296bb4985 | 35 | while (1) |
agaikwad | 3:164296bb4985 | 36 | { wait(15); |
agaikwad | 3:164296bb4985 | 37 | pc.printf("test value is %f%f\n",i,j); |
agaikwad | 3:164296bb4985 | 38 | esp_send(); |
agaikwad | 3:164296bb4985 | 39 | i++; |
agaikwad | 3:164296bb4985 | 40 | j--; |
SIT2016 | 1:2012c31aee1b | 41 | |
agaikwad | 3:164296bb4985 | 42 | } |
SIT2016 | 2:817794b2f733 | 43 | } |
SIT2016 | 2:817794b2f733 | 44 | |
agaikwad | 3:164296bb4985 | 45 | |
agaikwad | 3:164296bb4985 | 46 | void esp_initialize(void) |
agaikwad | 3:164296bb4985 | 47 | { |
agaikwad | 3:164296bb4985 | 48 | pc.printf("Initializing ESP\r\n"); |
agaikwad | 3:164296bb4985 | 49 | |
agaikwad | 3:164296bb4985 | 50 | pc.printf("Reset ESP\r\n"); |
agaikwad | 3:164296bb4985 | 51 | esp.Reset(); //RESET ESP |
agaikwad | 3:164296bb4985 | 52 | esp.RcvReply(rcv, 400); //receive a response from ESP |
agaikwad | 3:164296bb4985 | 53 | //pc.printf(rcv); //Print the response onscreen |
agaikwad | 3:164296bb4985 | 54 | wait(2); |
agaikwad | 3:164296bb4985 | 55 | |
agaikwad | 3:164296bb4985 | 56 | strcpy(snd,"AT"); |
agaikwad | 3:164296bb4985 | 57 | esp.SendCMD(snd); |
agaikwad | 3:164296bb4985 | 58 | pc.printf(snd); |
agaikwad | 3:164296bb4985 | 59 | //wait(2); |
agaikwad | 3:164296bb4985 | 60 | esp.RcvReply(rcv, 400); |
agaikwad | 3:164296bb4985 | 61 | pc.printf(rcv); |
agaikwad | 3:164296bb4985 | 62 | wait(0.1); |
agaikwad | 3:164296bb4985 | 63 | |
agaikwad | 3:164296bb4985 | 64 | strcpy(snd,"AT+CWMODE=1"); |
agaikwad | 3:164296bb4985 | 65 | esp.SendCMD(snd); |
agaikwad | 3:164296bb4985 | 66 | pc.printf(snd); |
agaikwad | 3:164296bb4985 | 67 | wait(2); |
agaikwad | 3:164296bb4985 | 68 | |
agaikwad | 3:164296bb4985 | 69 | strcpy(snd,"AT+CWJAP=\""); |
agaikwad | 3:164296bb4985 | 70 | strcat(snd,WIFI_SSID); |
agaikwad | 3:164296bb4985 | 71 | strcat(snd,"\",\""); |
agaikwad | 3:164296bb4985 | 72 | strcat(snd,WIFI_PASS); |
agaikwad | 3:164296bb4985 | 73 | strcat(snd,"\""); |
SIT2016 | 1:2012c31aee1b | 74 | |
agaikwad | 3:164296bb4985 | 75 | esp.SendCMD(snd); |
agaikwad | 3:164296bb4985 | 76 | pc.printf(snd); |
agaikwad | 3:164296bb4985 | 77 | wait(5); |
agaikwad | 3:164296bb4985 | 78 | esp.RcvReply(rcv, 400); |
agaikwad | 3:164296bb4985 | 79 | pc.printf("\n %s \n", rcv); |
agaikwad | 3:164296bb4985 | 80 | |
agaikwad | 3:164296bb4985 | 81 | strcpy(snd,"AT+CIPMUX=0"); |
agaikwad | 3:164296bb4985 | 82 | esp.SendCMD(snd); |
agaikwad | 3:164296bb4985 | 83 | pc.printf(snd); |
agaikwad | 3:164296bb4985 | 84 | //wait(2); |
agaikwad | 3:164296bb4985 | 85 | esp.RcvReply(rcv, 400); |
agaikwad | 3:164296bb4985 | 86 | pc.printf("\n %s \n", rcv); |
agaikwad | 3:164296bb4985 | 87 | |
agaikwad | 3:164296bb4985 | 88 | } |
agaikwad | 3:164296bb4985 | 89 | |
agaikwad | 3:164296bb4985 | 90 | |
agaikwad | 3:164296bb4985 | 91 | void esp_send(void) |
agaikwad | 3:164296bb4985 | 92 | { |
agaikwad | 3:164296bb4985 | 93 | |
agaikwad | 3:164296bb4985 | 94 | //ESP updates the Status of Thingspeak channel// |
agaikwad | 3:164296bb4985 | 95 | |
agaikwad | 3:164296bb4985 | 96 | strcpy(snd,"AT+CIPSTART="); |
agaikwad | 3:164296bb4985 | 97 | strcat(snd,"\"TCP\",\""); |
agaikwad | 3:164296bb4985 | 98 | strcat(snd,IP); |
agaikwad | 3:164296bb4985 | 99 | strcat(snd,"\",80"); |
agaikwad | 3:164296bb4985 | 100 | |
agaikwad | 3:164296bb4985 | 101 | esp.SendCMD(snd); |
agaikwad | 3:164296bb4985 | 102 | pc.printf("S\r\n%s",snd); |
agaikwad | 3:164296bb4985 | 103 | //wait(2); |
agaikwad | 3:164296bb4985 | 104 | esp.RcvReply(rcv, 1000); |
agaikwad | 3:164296bb4985 | 105 | pc.printf("R\r\n%s",rcv); |
SIT2016 | 1:2012c31aee1b | 106 | wait(1); |
SIT2016 | 1:2012c31aee1b | 107 | |
agaikwad | 3:164296bb4985 | 108 | sprintf(snd,"GET https://api.thingspeak.com/update?key=E3AASCJVE2KQDD3V&field1=%f&field2=%f\r\n",i,j); |
agaikwad | 3:164296bb4985 | 109 | |
agaikwad | 3:164296bb4985 | 110 | int i=0; |
agaikwad | 3:164296bb4985 | 111 | for(i=0;snd[i]!='\0';i++); |
agaikwad | 3:164296bb4985 | 112 | i++; |
agaikwad | 3:164296bb4985 | 113 | char cmd[255]; |
SIT2016 | 1:2012c31aee1b | 114 | |
agaikwad | 3:164296bb4985 | 115 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
agaikwad | 3:164296bb4985 | 116 | esp.SendCMD(cmd); |
agaikwad | 3:164296bb4985 | 117 | pc.printf("S\r\n%s",cmd); |
agaikwad | 3:164296bb4985 | 118 | while(i<=20 || rcv == ">") |
agaikwad | 3:164296bb4985 | 119 | { |
agaikwad | 3:164296bb4985 | 120 | esp.RcvReply(rcv, 1000); |
agaikwad | 3:164296bb4985 | 121 | wait(100); |
agaikwad | 3:164296bb4985 | 122 | i++; |
agaikwad | 3:164296bb4985 | 123 | } |
agaikwad | 3:164296bb4985 | 124 | pc.printf("R\r\n%s",rcv); |
SIT2016 | 1:2012c31aee1b | 125 | |
agaikwad | 3:164296bb4985 | 126 | esp.SendCMD(snd); //Post value to thingspeak channel |
agaikwad | 3:164296bb4985 | 127 | pc.printf("S\r\n%s",snd); |
agaikwad | 3:164296bb4985 | 128 | |
agaikwad | 3:164296bb4985 | 129 | while(i<=20 || rcv == "OK") |
agaikwad | 3:164296bb4985 | 130 | { |
agaikwad | 3:164296bb4985 | 131 | esp.RcvReply(rcv, 1000); |
agaikwad | 3:164296bb4985 | 132 | wait(100); |
agaikwad | 3:164296bb4985 | 133 | i++; |
agaikwad | 3:164296bb4985 | 134 | } |
agaikwad | 3:164296bb4985 | 135 | pc.printf("R\r\n%s",rcv); |
agaikwad | 3:164296bb4985 | 136 | |
SIT2016 | 1:2012c31aee1b | 137 | } |