Monitors temperature using lm75b temperature sensor and it sends the data to thingspeak using esp8266 WIFI module.

Dependencies:   mbed LM75B

Committer:
eduvanceIoT
Date:
Sat Mar 18 09:00:35 2017 +0000
Revision:
0:34d3f68b920e
Child:
1:8ab009672555
Eduvance ThingSpeak using ESP8266 and frdm kl25z

Who changed what in which revision?

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