thingspeak using ESP8266 wifi module

Dependencies:   mbed ESP8266

Committer:
SIT2016
Date:
Fri Jun 24 02:22:53 2016 +0000
Revision:
1:2012c31aee1b
Parent:
0:8ca71ccb52db
Child:
2:817794b2f733
Changes in library made. no at commands shown in code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SIT2016 0:8ca71ccb52db 1 #include "mbed.h"
SIT2016 0:8ca71ccb52db 2 #include "ESP8266.h"
SIT2016 0:8ca71ccb52db 3
SIT2016 0:8ca71ccb52db 4 Serial pc(USBTX,USBRX);
SIT2016 1:2012c31aee1b 5
SIT2016 1:2012c31aee1b 6 //POT sensor
SIT2016 1:2012c31aee1b 7 AnalogIn pot(PTB0);
SIT2016 1:2012c31aee1b 8
SIT2016 1:2012c31aee1b 9 //wifi UART port and baud rate
SIT2016 1:2012c31aee1b 10 ESP8266 wifi(PTE0, PTE1, 115200);
SIT2016 1:2012c31aee1b 11
SIT2016 1:2012c31aee1b 12 //buffers for wifi library
SIT2016 1:2012c31aee1b 13 char snd[255],resp[1000];
SIT2016 1:2012c31aee1b 14 char http_cmd[300], comm[300];
SIT2016 1:2012c31aee1b 15
SIT2016 1:2012c31aee1b 16 int timeout = 3000; //timeout for wifi commands
SIT2016 1:2012c31aee1b 17
SIT2016 1:2012c31aee1b 18 //SSID and password for connection
SIT2016 1:2012c31aee1b 19 #define SSID "Eduvance_WiFi"
SIT2016 1:2012c31aee1b 20 #define PASS "eduvance"
SIT2016 1:2012c31aee1b 21
SIT2016 1:2012c31aee1b 22 //Remote IP
SIT2016 1:2012c31aee1b 23 #define IP "192.168.0.13"
SIT2016 1:2012c31aee1b 24
SIT2016 1:2012c31aee1b 25 //ldrvalue global variable
SIT2016 1:2012c31aee1b 26 float ldrval = 0;
SIT2016 1:2012c31aee1b 27
SIT2016 1:2012c31aee1b 28 //Public and private keys for phant
SIT2016 1:2012c31aee1b 29 char* Public_Key = "wyJD1Pza1OCk1QEKk9K9C10zdyj3";
SIT2016 1:2012c31aee1b 30 char* Private_Key = "OrxMl9BLlOCvPZdBv4B4H3mDOAlx";
SIT2016 0:8ca71ccb52db 31
SIT2016 1:2012c31aee1b 32 //Wifi init function
SIT2016 1:2012c31aee1b 33 void wifi_initialize(void){
SIT2016 1:2012c31aee1b 34
SIT2016 1:2012c31aee1b 35 pc.printf("******** Resetting wifi module ********\r\n");
SIT2016 1:2012c31aee1b 36 wifi.Reset();
SIT2016 1:2012c31aee1b 37
SIT2016 1:2012c31aee1b 38 //wait for 5 seconds for response, else display no response receiveed
SIT2016 1:2012c31aee1b 39 if (wifi.RcvReply(resp, 5000))
SIT2016 1:2012c31aee1b 40 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 41 else
SIT2016 1:2012c31aee1b 42 pc.printf("No response");
SIT2016 1:2012c31aee1b 43
SIT2016 1:2012c31aee1b 44 pc.printf("******** Setting Station mode of wifi with AP ********\r\n");
SIT2016 1:2012c31aee1b 45 wifi.SetMode(1); // set transparent mode
SIT2016 1:2012c31aee1b 46 if (wifi.RcvReply(resp, timeout)) //receive a response from ESP
SIT2016 1:2012c31aee1b 47 pc.printf("%s",resp); //Print the response onscreen
SIT2016 1:2012c31aee1b 48 else
SIT2016 1:2012c31aee1b 49 pc.printf("No response while setting mode. \r\n");
SIT2016 1:2012c31aee1b 50
SIT2016 1:2012c31aee1b 51 pc.printf("******** Joining network with SSID and PASS ********\r\n");
SIT2016 1:2012c31aee1b 52 wifi.Join(SSID, PASS);
SIT2016 1:2012c31aee1b 53 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 54 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 55 else
SIT2016 1:2012c31aee1b 56 pc.printf("No response while connecting to network \r\n");
SIT2016 1:2012c31aee1b 57
SIT2016 1:2012c31aee1b 58 pc.printf("******** Getting IP and MAC of module ********\r\n");
SIT2016 1:2012c31aee1b 59 wifi.GetIP(resp);
SIT2016 1:2012c31aee1b 60 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 61 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 62 else
SIT2016 1:2012c31aee1b 63 pc.printf("No response while getting IP \r\n");
SIT2016 1:2012c31aee1b 64
SIT2016 1:2012c31aee1b 65 }
SIT2016 0:8ca71ccb52db 66
SIT2016 1:2012c31aee1b 67 void wifi_send(void){
SIT2016 1:2012c31aee1b 68
SIT2016 1:2012c31aee1b 69 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
SIT2016 1:2012c31aee1b 70 wifi.setTransparent();
SIT2016 1:2012c31aee1b 71 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 72 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 73 else
SIT2016 1:2012c31aee1b 74 pc.printf("No response while setting wifi passthrough. \r\n");
SIT2016 1:2012c31aee1b 75 wait(1);
SIT2016 1:2012c31aee1b 76
SIT2016 1:2012c31aee1b 77 pc.printf("******** Setting single connection mode ********\r\n");
SIT2016 1:2012c31aee1b 78 wifi.SetSingle();
SIT2016 1:2012c31aee1b 79 wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 80 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 81 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 82 else
SIT2016 1:2012c31aee1b 83 pc.printf("No response while setting single connection \r\n");
SIT2016 1:2012c31aee1b 84 wait(1);
SIT2016 1:2012c31aee1b 85
SIT2016 1:2012c31aee1b 86 pc.printf("******** Starting TCP connection on IP and port ********\r\n");
SIT2016 1:2012c31aee1b 87 wifi.startTCPConn(IP, 8080); //cipstart
SIT2016 1:2012c31aee1b 88 wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 89 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 90 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 91 else
SIT2016 1:2012c31aee1b 92 pc.printf("No response while starting TCP connection \r\n");
SIT2016 1:2012c31aee1b 93 wait(1);
SIT2016 1:2012c31aee1b 94
SIT2016 1:2012c31aee1b 95 //create link
SIT2016 1:2012c31aee1b 96 sprintf(http_cmd,"/input/%s?private_key=%s&val=%0.2f",Public_Key,Private_Key,ldrval);
SIT2016 1:2012c31aee1b 97
SIT2016 1:2012c31aee1b 98 pc.printf("******** Sending URL to wifi ********\r\n");
SIT2016 1:2012c31aee1b 99 wifi.sendURL(http_cmd, comm); //cipsend and get command
SIT2016 1:2012c31aee1b 100 if (wifi.RcvReply(resp, timeout))
SIT2016 1:2012c31aee1b 101 pc.printf("%s",resp);
SIT2016 1:2012c31aee1b 102 else
SIT2016 1:2012c31aee1b 103 pc.printf("No response while sending URL \r\n");
SIT2016 1:2012c31aee1b 104
SIT2016 1:2012c31aee1b 105 //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
SIT2016 1:2012c31aee1b 106 //wifi.RcvReply(resp, timeout);
SIT2016 1:2012c31aee1b 107 //pc.printf("%s", resp);
SIT2016 1:2012c31aee1b 108 }
SIT2016 1:2012c31aee1b 109
SIT2016 0:8ca71ccb52db 110 int main () {
SIT2016 0:8ca71ccb52db 111
SIT2016 1:2012c31aee1b 112
SIT2016 1:2012c31aee1b 113 wifi_initialize();
SIT2016 1:2012c31aee1b 114
SIT2016 0:8ca71ccb52db 115 while (1) {
SIT2016 1:2012c31aee1b 116 ldrval = pot.read();
SIT2016 1:2012c31aee1b 117 pc.printf("Potentiometer ldrvalue=%.3f \r\n", ldrval);
SIT2016 1:2012c31aee1b 118
SIT2016 1:2012c31aee1b 119 wifi_send();
SIT2016 1:2012c31aee1b 120 wait(3);
SIT2016 0:8ca71ccb52db 121 }
SIT2016 0:8ca71ccb52db 122 }
SIT2016 0:8ca71ccb52db 123
SIT2016 0:8ca71ccb52db 124