Skip to content

Instantly share code, notes, and snippets.

@Staler2019
Last active June 29, 2022 13:51
Show Gist options
  • Save Staler2019/dcb9b7502b1808e65e0d41a820d24940 to your computer and use it in GitHub Desktop.
Save Staler2019/dcb9b7502b1808e65e0d41a820d24940 to your computer and use it in GitHub Desktop.
#include "DHT.h"
#include "mbed.h"
using namespace std;
#define NEED_LIGHT_HOURS 12
#define MINUTE_CYCLE 30
// sensor port
DHT dht(PC_13, DHT22); // PC13 = D2
AnalogIn in(PA_5);
int lightCounter = 0; // from the night 0:00
int timeCounter = 0;
void checkDayLight(int light) {
if (light >= 5000) { // 天亮
lightCounter++;
if (lightCounter >= NEED_LIGHT_HOURS * (60 / MINUTE_CYCLE)) printf("close the ceiling\r\n");
// cout << "close the ceiling\r\n";
} else {
if (lightCounter < NEED_LIGHT_HOURS * (60 / MINUTE_CYCLE)) {
printf("turn light on\r\n");
// cout << "turn light on\r\n";
lightCounter++;
}
}
}
void checkDegree(float temp) {
if (temp < (float)23)
printf("add up temperature\r\n");
else if (temp > (float)30)
printf("lower the temperature\r\n");
}
void checkHumidity(float hum) {
if (hum < (float)60)
printf("add some water fog\r\n");
else if (hum > (float)85)
printf("make the air dryer\r\n");
}
void resetCounter() {
timeCounter = 0;
lightCounter = 0;
}
int main() {
while (1) {
// get data
time_t now = time(0);
char* dt = ctime(&now);
printf("---The local date and time is: %s\r\n---", dt);
float temp, hum;
temp = dht.ReadTemperature(CELCIUS);
hum = dht.ReadHumidity();
dht.readData();
printf(" temperature = %.2f\r\n humidity = %.2f\r\n", temp, hum);
int light;
light = in.read_u16();
printf(" light sensitive = %d\r\n", light);
printf("\r\n");
// deal with data
checkDayLight(light);
checkDegree(temp);
checkHumidity(hum);
// timer
timeCounter++;
if (timeCounter == (24 * 60 / MINUTE_CYCLE)) { // 重置時間為 0:00
resetCounter();
}
wait(MINUTE_CYCLE * 60); // sec
// wait(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment