A system to control fan speed according to temperature values.

Dependencies:   mbed-rtos mbed

Fork of Home_Monitoring_System by Saad Habach

Committer:
shabach
Date:
Wed Dec 13 15:44:24 2017 +0000
Revision:
3:399fd9e24cea
Parent:
2:3f45a93a19e5
Child:
4:bfc60dcb627a
This is my program for a wireless connected embedded system using wifi.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wmerghan 0:4c4db81e5752 1 #include "mbed.h"
shabach 3:399fd9e24cea 2 #include "ESP8266.h"
shabach 3:399fd9e24cea 3 #include <string>
shabach 3:399fd9e24cea 4
shabach 3:399fd9e24cea 5 #define APIKEY V5N1NKKKH5T51D48 //Put "Write key" of your channel in thingspeak.com
shabach 3:399fd9e24cea 6 #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\"
shabach 3:399fd9e24cea 7 #define WIFI_SSID "Jeremy"
shabach 3:399fd9e24cea 8 #define WIFI_PASS "mbedproject"
shabach 3:399fd9e24cea 9
shabach 3:399fd9e24cea 10 Serial pc(USBTX, USBRX); // tx, rx
shabach 3:399fd9e24cea 11 ESP8266 WiFi (D1, D0, 115200);
wmerghan 0:4c4db81e5752 12
wmerghan 0:4c4db81e5752 13 AnalogIn temp(A0);
wmerghan 0:4c4db81e5752 14 DigitalOut test(D3, 0);
wmerghan 0:4c4db81e5752 15 InterruptIn motion(D2);
shabach 3:399fd9e24cea 16
shabach 3:399fd9e24cea 17 void WiFi_Init (void);
shabach 3:399fd9e24cea 18 void WiFi_Send (void);
wmerghan 0:4c4db81e5752 19
shabach 3:399fd9e24cea 20 char snd[255];
shabach 3:399fd9e24cea 21 char rcv[1000];
shabach 3:399fd9e24cea 22
shabach 3:399fd9e24cea 23 int motion_cnt = 0;
wmerghan 0:4c4db81e5752 24 int motion_detected = 0;
shabach 3:399fd9e24cea 25 double tempC;
shabach 3:399fd9e24cea 26 double tempF;
shabach 3:399fd9e24cea 27
wmerghan 0:4c4db81e5752 28 void irq_handler(void)
wmerghan 0:4c4db81e5752 29 {
wmerghan 0:4c4db81e5752 30 motion_detected = 1;
wmerghan 0:4c4db81e5752 31 }
wmerghan 0:4c4db81e5752 32
wmerghan 0:4c4db81e5752 33 int main(void)
wmerghan 0:4c4db81e5752 34 {
shabach 3:399fd9e24cea 35 pc.baud(115200);
shabach 3:399fd9e24cea 36 pc.printf("Program Started \r\n");
shabach 3:399fd9e24cea 37
shabach 3:399fd9e24cea 38 WiFi_Init();
shabach 3:399fd9e24cea 39
shabach 3:399fd9e24cea 40 pc.printf("WiFi Connected! \r\n");
wmerghan 0:4c4db81e5752 41
wmerghan 0:4c4db81e5752 42 //time_t is used to store the calender time format
wmerghan 0:4c4db81e5752 43 time_t rawtime;
wmerghan 0:4c4db81e5752 44 struct tm *timeinfo;
wmerghan 0:4c4db81e5752 45
wmerghan 0:4c4db81e5752 46 motion.rise(&irq_handler);
wmerghan 0:4c4db81e5752 47
wmerghan 0:4c4db81e5752 48 while(1)
wmerghan 0:4c4db81e5752 49 {
shabach 3:399fd9e24cea 50 tempC = (temp*300);
wmerghan 0:4c4db81e5752 51 tempF = (9.0*tempC)/5.0 + 32.0;
wmerghan 1:5245173228f2 52
shabach 3:399fd9e24cea 53 pc.printf("Temperature is %.2f C %.2f F\n\r", tempC, tempF);
shabach 3:399fd9e24cea 54
wmerghan 0:4c4db81e5752 55 wait(1);
wmerghan 0:4c4db81e5752 56
wmerghan 0:4c4db81e5752 57 if(motion_detected)
wmerghan 0:4c4db81e5752 58 {
wmerghan 0:4c4db81e5752 59 //get the current time
wmerghan 0:4c4db81e5752 60 time(&rawtime);
wmerghan 0:4c4db81e5752 61 timeinfo = localtime(&rawtime);
wmerghan 0:4c4db81e5752 62
wmerghan 0:4c4db81e5752 63 motion_cnt++;
wmerghan 0:4c4db81e5752 64 motion_detected = 0;
wmerghan 0:4c4db81e5752 65 pc.printf("Motion %d Detected at %d:%d:%d \n\r", motion_cnt, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wmerghan 0:4c4db81e5752 66 }
wmerghan 0:4c4db81e5752 67
shabach 3:399fd9e24cea 68 if(tempF > 90)
wmerghan 0:4c4db81e5752 69 {
wmerghan 0:4c4db81e5752 70 DigitalOut test(D3,1);
wmerghan 0:4c4db81e5752 71
wmerghan 0:4c4db81e5752 72 }
wmerghan 0:4c4db81e5752 73 else
wmerghan 0:4c4db81e5752 74 {
wmerghan 0:4c4db81e5752 75 DigitalOut test(D3,0);
wmerghan 0:4c4db81e5752 76 }
shabach 3:399fd9e24cea 77 WiFi_Send();
wmerghan 0:4c4db81e5752 78 }
wmerghan 0:4c4db81e5752 79 }
shabach 3:399fd9e24cea 80
shabach 3:399fd9e24cea 81 void WiFi_Init (void)
shabach 3:399fd9e24cea 82 {
shabach 3:399fd9e24cea 83 pc.printf("Initializing ESP \r\n");
shabach 3:399fd9e24cea 84
shabach 3:399fd9e24cea 85 pc.printf("Reset WiFi \r\n");
shabach 3:399fd9e24cea 86 WiFi.Reset(); //RESET ESP
shabach 3:399fd9e24cea 87 WiFi.RcvReply(rcv, 400); //receive a response from ESP
shabach 3:399fd9e24cea 88 pc.printf("%s \r\n", rcv);
shabach 3:399fd9e24cea 89 wait(2);
shabach 3:399fd9e24cea 90
shabach 3:399fd9e24cea 91 strcpy(snd, "AT");
shabach 3:399fd9e24cea 92 WiFi.SendCMD(snd);
shabach 3:399fd9e24cea 93 pc.printf("%s \r\n", snd);
shabach 3:399fd9e24cea 94 WiFi.RcvReply(rcv, 400);
shabach 3:399fd9e24cea 95 pc.printf("%s \r\n", rcv);
shabach 3:399fd9e24cea 96 wait(2);
shabach 3:399fd9e24cea 97
shabach 3:399fd9e24cea 98 strcpy(snd, "AT+CWMODE=1");
shabach 3:399fd9e24cea 99 WiFi.SendCMD(snd);
shabach 3:399fd9e24cea 100 pc.printf("%s \r\n", snd);
shabach 3:399fd9e24cea 101 WiFi.RcvReply(rcv, 400);
shabach 3:399fd9e24cea 102 pc.printf("%s \r\n", rcv);
shabach 3:399fd9e24cea 103 wait(2);
shabach 3:399fd9e24cea 104
shabach 3:399fd9e24cea 105 strcpy(snd, "AT+CWJAP=\"");
shabach 3:399fd9e24cea 106 strcat(snd, WIFI_SSID);
shabach 3:399fd9e24cea 107 strcat(snd, "\",\"");
shabach 3:399fd9e24cea 108 strcat(snd, WIFI_PASS);
shabach 3:399fd9e24cea 109 strcat(snd, "\"");
shabach 3:399fd9e24cea 110
shabach 3:399fd9e24cea 111 WiFi.SendCMD(snd);
shabach 3:399fd9e24cea 112 pc.printf("%s \r\n", snd);
shabach 3:399fd9e24cea 113 wait(2);
shabach 3:399fd9e24cea 114 WiFi.RcvReply(rcv, 400);
shabach 3:399fd9e24cea 115 pc.printf("%s \r\n", rcv);
shabach 3:399fd9e24cea 116 wait(2);
shabach 3:399fd9e24cea 117
shabach 3:399fd9e24cea 118 strcpy(snd, "AT+CIPMUX=1");
shabach 3:399fd9e24cea 119 WiFi.SendCMD(snd);
shabach 3:399fd9e24cea 120 pc.printf("%s \r\n", snd);
shabach 3:399fd9e24cea 121 WiFi.RcvReply(rcv, 400);
shabach 3:399fd9e24cea 122 pc.printf("%s \r\n", rcv);
shabach 3:399fd9e24cea 123 wait(2);
shabach 3:399fd9e24cea 124 }
shabach 3:399fd9e24cea 125
shabach 3:399fd9e24cea 126 void WiFi_Send (void)
shabach 3:399fd9e24cea 127 {
shabach 3:399fd9e24cea 128 //ESP updates the Status of Thingspeak channel//
shabach 3:399fd9e24cea 129
shabach 3:399fd9e24cea 130 strcpy(snd,"AT+CIPSTART=");
shabach 3:399fd9e24cea 131 strcat(snd,"\"TCP\",\"");
shabach 3:399fd9e24cea 132 strcat(snd,IP);
shabach 3:399fd9e24cea 133 strcat(snd,"\",80");
shabach 3:399fd9e24cea 134
shabach 3:399fd9e24cea 135 WiFi.SendCMD(snd);
shabach 3:399fd9e24cea 136 pc.printf("Send\r\n%s",snd);
shabach 3:399fd9e24cea 137 WiFi.RcvReply(rcv, 1000);
shabach 3:399fd9e24cea 138 pc.printf("Receive\r\n%s",rcv);
shabach 3:399fd9e24cea 139 wait(1);
shabach 3:399fd9e24cea 140
shabach 3:399fd9e24cea 141 sprintf(snd,"GET https://api.thingspeak.com/update?api_key=V5N1NKKKH5T51D48&field1=%f&field2=%d\r\n", tempF, motion_cnt);
shabach 3:399fd9e24cea 142
shabach 3:399fd9e24cea 143 int i=0;
shabach 3:399fd9e24cea 144 for(i=0; snd[i]!='\0'; i++);
shabach 3:399fd9e24cea 145 i++;
shabach 3:399fd9e24cea 146 char cmd[255];
shabach 3:399fd9e24cea 147
shabach 3:399fd9e24cea 148 sprintf(cmd,"AT+CIPSEND=%d",i); //Send Number of open connection and Characters to send
shabach 3:399fd9e24cea 149 WiFi.SendCMD(cmd);
shabach 3:399fd9e24cea 150 pc.printf("Send\r\n%s",cmd);
shabach 3:399fd9e24cea 151 while(i<=20 || rcv == ">") {
shabach 3:399fd9e24cea 152 WiFi.RcvReply(rcv, 1000);
shabach 3:399fd9e24cea 153 wait(100);
shabach 3:399fd9e24cea 154 i++;
shabach 3:399fd9e24cea 155 }
shabach 3:399fd9e24cea 156 pc.printf("Receive\r\n%s",rcv);
shabach 3:399fd9e24cea 157
shabach 3:399fd9e24cea 158 WiFi.SendCMD(snd); //Post value to thingspeak channel
shabach 3:399fd9e24cea 159 pc.printf("Send\r\n%s",snd);
shabach 3:399fd9e24cea 160
shabach 3:399fd9e24cea 161 while(i<=20 || rcv == "OK") {
shabach 3:399fd9e24cea 162 WiFi.RcvReply(rcv, 1000);
shabach 3:399fd9e24cea 163 wait(100);
shabach 3:399fd9e24cea 164 i++;
shabach 3:399fd9e24cea 165 }
shabach 3:399fd9e24cea 166 pc.printf("Receive\r\n%s",rcv);
shabach 3:399fd9e24cea 167 }