A prototype build for pipe freeze detection & prevention system using K64F
Dependencies: mbed DHT ESP8266
main.cpp@1:8bdc77b1fc60, 2018-12-13 (annotated)
- Committer:
- geekypirate
- Date:
- Thu Dec 13 00:50:21 2018 +0000
- Revision:
- 1:8bdc77b1fc60
- Parent:
- 0:9d72427a0730
This is the prototype build of pipe freeze detection using K64F, DHT11 & ESP8266 WiFi chip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregC | 0:9d72427a0730 | 1 | #include "mbed.h" |
geekypirate | 1:8bdc77b1fc60 | 2 | #include "ESP8266.h" // Include header file from Author: Antonio Quevedo |
geekypirate | 1:8bdc77b1fc60 | 3 | #include "math.h" |
geekypirate | 1:8bdc77b1fc60 | 4 | #include <string> |
GregC | 0:9d72427a0730 | 5 | #include "DHT.h" |
GregC | 0:9d72427a0730 | 6 | |
geekypirate | 1:8bdc77b1fc60 | 7 | #define APIKEY OF2O1FTK5UNXYWO8 //Put "Write key" of your channel in thingspeak.com |
geekypirate | 1:8bdc77b1fc60 | 8 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
geekypirate | 1:8bdc77b1fc60 | 9 | #define WIFI_SSID "WiFi" |
geekypirate | 1:8bdc77b1fc60 | 10 | #define WIFI_PASS "b5c7f638" |
geekypirate | 1:8bdc77b1fc60 | 11 | |
geekypirate | 1:8bdc77b1fc60 | 12 | //Board Pinouts****************** |
geekypirate | 1:8bdc77b1fc60 | 13 | DigitalOut heater(D2); //Defining heater pin |
geekypirate | 1:8bdc77b1fc60 | 14 | DHT sensor(D4, DHT11); //defining temperature sensor |
geekypirate | 1:8bdc77b1fc60 | 15 | //******************************* |
geekypirate | 1:8bdc77b1fc60 | 16 | |
geekypirate | 1:8bdc77b1fc60 | 17 | Serial pc(USBTX,USBRX); |
geekypirate | 1:8bdc77b1fc60 | 18 | |
geekypirate | 1:8bdc77b1fc60 | 19 | ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi |
geekypirate | 1:8bdc77b1fc60 | 20 | |
geekypirate | 1:8bdc77b1fc60 | 21 | 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 |
geekypirate | 1:8bdc77b1fc60 | 22 | |
geekypirate | 1:8bdc77b1fc60 | 23 | float temperature = 0.0f; |
geekypirate | 1:8bdc77b1fc60 | 24 | |
GregC | 0:9d72427a0730 | 25 | |
geekypirate | 1:8bdc77b1fc60 | 26 | void esp_initialize(void); // Function used to initialize ESP8266 wifi module |
geekypirate | 1:8bdc77b1fc60 | 27 | void esp_send(void); // Function used to connect with thingspeak.com and update channel using ESP8266 wifi module |
geekypirate | 1:8bdc77b1fc60 | 28 | |
geekypirate | 1:8bdc77b1fc60 | 29 | //test variable |
geekypirate | 1:8bdc77b1fc60 | 30 | float i=0; |
geekypirate | 1:8bdc77b1fc60 | 31 | float j=10; |
geekypirate | 1:8bdc77b1fc60 | 32 | |
geekypirate | 1:8bdc77b1fc60 | 33 | |
geekypirate | 1:8bdc77b1fc60 | 34 | int main() |
GregC | 0:9d72427a0730 | 35 | { |
geekypirate | 1:8bdc77b1fc60 | 36 | |
geekypirate | 1:8bdc77b1fc60 | 37 | pc.baud(115200); // Baud rate used for communicating with Tera-term on PC |
geekypirate | 1:8bdc77b1fc60 | 38 | |
geekypirate | 1:8bdc77b1fc60 | 39 | pc.printf("START\r\n"); // Starting point |
geekypirate | 1:8bdc77b1fc60 | 40 | |
GregC | 0:9d72427a0730 | 41 | int error = 0; |
geekypirate | 1:8bdc77b1fc60 | 42 | |
geekypirate | 1:8bdc77b1fc60 | 43 | esp_initialize(); |
GregC | 0:9d72427a0730 | 44 | |
GregC | 0:9d72427a0730 | 45 | while(1) { |
geekypirate | 1:8bdc77b1fc60 | 46 | |
geekypirate | 1:8bdc77b1fc60 | 47 | |
GregC | 0:9d72427a0730 | 48 | wait(2.0f); |
geekypirate | 1:8bdc77b1fc60 | 49 | error = sensor.readData(); //read sensor for error |
GregC | 0:9d72427a0730 | 50 | if (0 == error) { |
geekypirate | 1:8bdc77b1fc60 | 51 | temperature = sensor.ReadTemperature(CELCIUS); |
geekypirate | 1:8bdc77b1fc60 | 52 | printf("Temperature in Celcius: %4.2f\n", temperature); |
geekypirate | 1:8bdc77b1fc60 | 53 | } |
geekypirate | 1:8bdc77b1fc60 | 54 | else { |
GregC | 0:9d72427a0730 | 55 | printf("Error: %d\n", error); |
GregC | 0:9d72427a0730 | 56 | } |
geekypirate | 1:8bdc77b1fc60 | 57 | |
geekypirate | 1:8bdc77b1fc60 | 58 | if (temperature > 24.00) { |
geekypirate | 1:8bdc77b1fc60 | 59 | heater = 1; |
geekypirate | 1:8bdc77b1fc60 | 60 | printf("Heater is OFF\n"); |
geekypirate | 1:8bdc77b1fc60 | 61 | } |
geekypirate | 1:8bdc77b1fc60 | 62 | else { |
geekypirate | 1:8bdc77b1fc60 | 63 | heater = 0; |
geekypirate | 1:8bdc77b1fc60 | 64 | printf("Heater is ON\n"); |
geekypirate | 1:8bdc77b1fc60 | 65 | } |
geekypirate | 1:8bdc77b1fc60 | 66 | |
geekypirate | 1:8bdc77b1fc60 | 67 | wait(3); |
geekypirate | 1:8bdc77b1fc60 | 68 | esp_send(); |
geekypirate | 1:8bdc77b1fc60 | 69 | i++; |
geekypirate | 1:8bdc77b1fc60 | 70 | j--; |
geekypirate | 1:8bdc77b1fc60 | 71 | |
GregC | 0:9d72427a0730 | 72 | } |
GregC | 0:9d72427a0730 | 73 | } |
geekypirate | 1:8bdc77b1fc60 | 74 | |
geekypirate | 1:8bdc77b1fc60 | 75 | |
geekypirate | 1:8bdc77b1fc60 | 76 | void esp_initialize(void) |
geekypirate | 1:8bdc77b1fc60 | 77 | { |
geekypirate | 1:8bdc77b1fc60 | 78 | pc.printf("Initializing ESP\r\n"); |
geekypirate | 1:8bdc77b1fc60 | 79 | |
geekypirate | 1:8bdc77b1fc60 | 80 | pc.printf("Reset ESP\r\n"); |
geekypirate | 1:8bdc77b1fc60 | 81 | esp.Reset(); //RESET ESP |
geekypirate | 1:8bdc77b1fc60 | 82 | esp.RcvReply(rcv, 400); //receive a response from ESP |
geekypirate | 1:8bdc77b1fc60 | 83 | //pc.printf(rcv); //Print the response onscreen |
geekypirate | 1:8bdc77b1fc60 | 84 | wait(2); |
geekypirate | 1:8bdc77b1fc60 | 85 | |
geekypirate | 1:8bdc77b1fc60 | 86 | strcpy(snd,"AT"); |
geekypirate | 1:8bdc77b1fc60 | 87 | esp.SendCMD(snd); |
geekypirate | 1:8bdc77b1fc60 | 88 | pc.printf(snd); |
geekypirate | 1:8bdc77b1fc60 | 89 | //wait(2); |
geekypirate | 1:8bdc77b1fc60 | 90 | esp.RcvReply(rcv, 400); |
geekypirate | 1:8bdc77b1fc60 | 91 | pc.printf(rcv); |
geekypirate | 1:8bdc77b1fc60 | 92 | wait(0.1); |
geekypirate | 1:8bdc77b1fc60 | 93 | |
geekypirate | 1:8bdc77b1fc60 | 94 | strcpy(snd,"AT+CWMODE=1"); |
geekypirate | 1:8bdc77b1fc60 | 95 | esp.SendCMD(snd); |
geekypirate | 1:8bdc77b1fc60 | 96 | pc.printf(snd); |
geekypirate | 1:8bdc77b1fc60 | 97 | wait(2); |
geekypirate | 1:8bdc77b1fc60 | 98 | |
geekypirate | 1:8bdc77b1fc60 | 99 | strcpy(snd,"AT+CWJAP=\""); |
geekypirate | 1:8bdc77b1fc60 | 100 | strcat(snd,WIFI_SSID); |
geekypirate | 1:8bdc77b1fc60 | 101 | strcat(snd,"\",\""); |
geekypirate | 1:8bdc77b1fc60 | 102 | strcat(snd,WIFI_PASS); |
geekypirate | 1:8bdc77b1fc60 | 103 | strcat(snd,"\""); |
geekypirate | 1:8bdc77b1fc60 | 104 | |
geekypirate | 1:8bdc77b1fc60 | 105 | esp.SendCMD(snd); |
geekypirate | 1:8bdc77b1fc60 | 106 | pc.printf(snd); |
geekypirate | 1:8bdc77b1fc60 | 107 | wait(5); |
geekypirate | 1:8bdc77b1fc60 | 108 | esp.RcvReply(rcv, 400); |
geekypirate | 1:8bdc77b1fc60 | 109 | pc.printf("\n %s \n", rcv); |
geekypirate | 1:8bdc77b1fc60 | 110 | |
geekypirate | 1:8bdc77b1fc60 | 111 | strcpy(snd,"AT+CIPMUX=0"); |
geekypirate | 1:8bdc77b1fc60 | 112 | esp.SendCMD(snd); |
geekypirate | 1:8bdc77b1fc60 | 113 | pc.printf(snd); |
geekypirate | 1:8bdc77b1fc60 | 114 | //wait(2); |
geekypirate | 1:8bdc77b1fc60 | 115 | esp.RcvReply(rcv, 400); |
geekypirate | 1:8bdc77b1fc60 | 116 | pc.printf("\n %s \n", rcv); |
geekypirate | 1:8bdc77b1fc60 | 117 | |
geekypirate | 1:8bdc77b1fc60 | 118 | } |
geekypirate | 1:8bdc77b1fc60 | 119 | |
geekypirate | 1:8bdc77b1fc60 | 120 | |
geekypirate | 1:8bdc77b1fc60 | 121 | void esp_send(void) |
geekypirate | 1:8bdc77b1fc60 | 122 | { |
geekypirate | 1:8bdc77b1fc60 | 123 | |
geekypirate | 1:8bdc77b1fc60 | 124 | //ESP updates the Status of Thingspeak channel// |
geekypirate | 1:8bdc77b1fc60 | 125 | |
geekypirate | 1:8bdc77b1fc60 | 126 | strcpy(snd,"AT+CIPSTART="); |
geekypirate | 1:8bdc77b1fc60 | 127 | strcat(snd,"\"TCP\",\""); |
geekypirate | 1:8bdc77b1fc60 | 128 | strcat(snd,IP); |
geekypirate | 1:8bdc77b1fc60 | 129 | strcat(snd,"\",80"); |
geekypirate | 1:8bdc77b1fc60 | 130 | |
geekypirate | 1:8bdc77b1fc60 | 131 | esp.SendCMD(snd); |
geekypirate | 1:8bdc77b1fc60 | 132 | pc.printf("S\r\n%s",snd); |
geekypirate | 1:8bdc77b1fc60 | 133 | //wait(2); |
geekypirate | 1:8bdc77b1fc60 | 134 | esp.RcvReply(rcv, 1000); |
geekypirate | 1:8bdc77b1fc60 | 135 | pc.printf("R\r\n%s",rcv); |
geekypirate | 1:8bdc77b1fc60 | 136 | wait(1); |
geekypirate | 1:8bdc77b1fc60 | 137 | |
geekypirate | 1:8bdc77b1fc60 | 138 | sprintf(snd,"GET https://api.thingspeak.com/update?key=OF2O1FTK5UNXYWO8&field1=%f&field2=%f\r\n",temperature,heater); |
geekypirate | 1:8bdc77b1fc60 | 139 | |
geekypirate | 1:8bdc77b1fc60 | 140 | int i=0; |
geekypirate | 1:8bdc77b1fc60 | 141 | for(i=0;snd[i]!='\0';i++); |
geekypirate | 1:8bdc77b1fc60 | 142 | i++; |
geekypirate | 1:8bdc77b1fc60 | 143 | char cmd[255]; |
geekypirate | 1:8bdc77b1fc60 | 144 | |
geekypirate | 1:8bdc77b1fc60 | 145 | sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send |
geekypirate | 1:8bdc77b1fc60 | 146 | esp.SendCMD(cmd); |
geekypirate | 1:8bdc77b1fc60 | 147 | pc.printf("S\r\n%s",cmd); |
geekypirate | 1:8bdc77b1fc60 | 148 | while(i<=20 || rcv == ">") |
geekypirate | 1:8bdc77b1fc60 | 149 | { |
geekypirate | 1:8bdc77b1fc60 | 150 | esp.RcvReply(rcv, 1000); |
geekypirate | 1:8bdc77b1fc60 | 151 | wait(100); |
geekypirate | 1:8bdc77b1fc60 | 152 | i++; |
geekypirate | 1:8bdc77b1fc60 | 153 | } |
geekypirate | 1:8bdc77b1fc60 | 154 | pc.printf("R\r\n%s",rcv); |
geekypirate | 1:8bdc77b1fc60 | 155 | |
geekypirate | 1:8bdc77b1fc60 | 156 | esp.SendCMD(snd); //Post value to thingspeak channel |
geekypirate | 1:8bdc77b1fc60 | 157 | pc.printf("S\r\n%s",snd); |
geekypirate | 1:8bdc77b1fc60 | 158 | |
geekypirate | 1:8bdc77b1fc60 | 159 | while(i<=20 || rcv == "OK") |
geekypirate | 1:8bdc77b1fc60 | 160 | { |
geekypirate | 1:8bdc77b1fc60 | 161 | esp.RcvReply(rcv, 1000); |
geekypirate | 1:8bdc77b1fc60 | 162 | wait(100); |
geekypirate | 1:8bdc77b1fc60 | 163 | i++; |
geekypirate | 1:8bdc77b1fc60 | 164 | } |
geekypirate | 1:8bdc77b1fc60 | 165 | pc.printf("R\r\n%s",rcv); |
geekypirate | 1:8bdc77b1fc60 | 166 | |
geekypirate | 1:8bdc77b1fc60 | 167 | } |