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

Dependencies:   mbed LM75B

Committer:
SIT2016
Date:
Sat Jun 17 09:53:27 2017 +0000
Revision:
1:8ab009672555
Parent:
0:34d3f68b920e
Child:
2:e259a0cfc9f1
Cleaned Code - Changed variable, Removed unnecessary variables, bugs fixes

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
SIT2016 1:8ab009672555 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
SIT2016 1:8ab009672555 13 char 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
SIT2016 1:8ab009672555 19 #define SSID "Eduvance"
SIT2016 1:8ab009672555 20 #define PASS "eduvance123"
eduvanceIoT 0:34d3f68b920e 21
eduvanceIoT 0:34d3f68b920e 22 //Remote IP
eduvanceIoT 0:34d3f68b920e 23 #define IP "184.106.153.149"
eduvanceIoT 0:34d3f68b920e 24
SIT2016 1:8ab009672555 25 //global variable
SIT2016 1:8ab009672555 26 float potvalue = 0;
SIT2016 1:8ab009672555 27
SIT2016 1:8ab009672555 28 //Update key for thingspeak
SIT2016 1:8ab009672555 29 char* Update_Key = "O1VOWGC3WXI981H6";
eduvanceIoT 0:34d3f68b920e 30
eduvanceIoT 0:34d3f68b920e 31 //Wifi init function
eduvanceIoT 0:34d3f68b920e 32 void wifi_initialize(void){
eduvanceIoT 0:34d3f68b920e 33
eduvanceIoT 0:34d3f68b920e 34 pc.printf("******** Resetting wifi module ********\r\n");
eduvanceIoT 0:34d3f68b920e 35 wifi.Reset();
eduvanceIoT 0:34d3f68b920e 36
eduvanceIoT 0:34d3f68b920e 37 //wait for 5 seconds for response, else display no response receiveed
eduvanceIoT 0:34d3f68b920e 38 if (wifi.RcvReply(resp, 5000))
eduvanceIoT 0:34d3f68b920e 39 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 40 else
eduvanceIoT 0:34d3f68b920e 41 pc.printf("No response");
eduvanceIoT 0:34d3f68b920e 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");
eduvanceIoT 0:34d3f68b920e 49
eduvanceIoT 0:34d3f68b920e 50 pc.printf("******** Joining network with SSID and PASS ********\r\n");
eduvanceIoT 0:34d3f68b920e 51 wifi.Join(SSID, PASS);
eduvanceIoT 0:34d3f68b920e 52 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 53 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 54 else
eduvanceIoT 0:34d3f68b920e 55 pc.printf("No response while connecting to network \r\n");
eduvanceIoT 0:34d3f68b920e 56
eduvanceIoT 0:34d3f68b920e 57 pc.printf("******** Getting IP and MAC of module ********\r\n");
eduvanceIoT 0:34d3f68b920e 58 wifi.GetIP(resp);
eduvanceIoT 0:34d3f68b920e 59 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 60 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 61 else
eduvanceIoT 0:34d3f68b920e 62 pc.printf("No response while getting IP \r\n");
eduvanceIoT 0:34d3f68b920e 63
eduvanceIoT 0:34d3f68b920e 64 pc.printf("******** Setting WIFI UART passthrough ********\r\n");
eduvanceIoT 0:34d3f68b920e 65 wifi.setTransparent();
eduvanceIoT 0:34d3f68b920e 66 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 67 pc.printf("%s",resp);
eduvanceIoT 0:34d3f68b920e 68 else
eduvanceIoT 0:34d3f68b920e 69 pc.printf("No response while setting wifi passthrough. \r\n");
eduvanceIoT 0:34d3f68b920e 70 wait(1);
eduvanceIoT 0:34d3f68b920e 71
eduvanceIoT 0:34d3f68b920e 72 pc.printf("******** Setting single connection mode ********\r\n");
eduvanceIoT 0:34d3f68b920e 73 wifi.SetSingle();
eduvanceIoT 0:34d3f68b920e 74 wifi.RcvReply(resp, timeout);
eduvanceIoT 0:34d3f68b920e 75 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 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
eduvanceIoT 0:34d3f68b920e 82 void wifi_send(void){
eduvanceIoT 0:34d3f68b920e 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);
eduvanceIoT 0:34d3f68b920e 87 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 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);
eduvanceIoT 0:34d3f68b920e 92
eduvanceIoT 0:34d3f68b920e 93 //create link
SIT2016 1:8ab009672555 94 sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,potvalue);
eduvanceIoT 0:34d3f68b920e 95 pc.printf(http_cmd);
eduvanceIoT 0:34d3f68b920e 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
eduvanceIoT 0:34d3f68b920e 99 if (wifi.RcvReply(resp, timeout))
eduvanceIoT 0:34d3f68b920e 100 pc.printf("%s",resp);
SIT2016 1:8ab009672555 101 else
SIT2016 1:8ab009672555 102 pc.printf("No response while sending URL \r\n");
eduvanceIoT 0:34d3f68b920e 103 }
eduvanceIoT 0:34d3f68b920e 104
eduvanceIoT 0:34d3f68b920e 105 int main () {
eduvanceIoT 0:34d3f68b920e 106
eduvanceIoT 0:34d3f68b920e 107
eduvanceIoT 0:34d3f68b920e 108 wifi_initialize();
eduvanceIoT 0:34d3f68b920e 109
eduvanceIoT 0:34d3f68b920e 110 while (1) {
SIT2016 1:8ab009672555 111 potvalue = 255*pot.read();
SIT2016 1:8ab009672555 112 pc.printf("Current potvalue is = %.3f \r\n", potvalue);
eduvanceIoT 0:34d3f68b920e 113
eduvanceIoT 0:34d3f68b920e 114 wifi_send();
SIT2016 1:8ab009672555 115 wait(30);
eduvanceIoT 0:34d3f68b920e 116 }
eduvanceIoT 0:34d3f68b920e 117 }