#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 7       // 数字引脚连接DHT传感器
#define DHTTYPE DHT11  // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
float humidity;
float temperature;

#define cs 10
#define dc 9
#define rst 8

// TFT屏SDA  接11
// TFT屏SCL  接13
// TFT屏VDD  接5V
// TFT屏GND  接GND

// 创建实例
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char sensorPrintout[4];
int D2 = 2;               //蜂鸣器
int D4 = 4;               // 遥控LED灯
int flameSensorPin = 12;  //火焰传感器
bool firealarm = false;   //为false 代表火警关闭, 否知 代表火警响了
String oldT = "";
String oldH = "";
void setup() {
  Serial.begin(115200);
  pinMode(D2, OUTPUT);
  pinMode(D4, OUTPUT);
  digitalWrite(D2, LOW);
  digitalWrite(D4, LOW);
  pinMode(flameSensorPin, INPUT);
  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();

  //清除黑色背景的屏幕
  TFTscreen.background(0, 0, 0);

  // 将静态文本写入屏幕
  // 设置字体颜色为白色
  TFTscreen.stroke(255, 255, 255);
  // 设置字体大小
  TFTscreen.setTextSize(1.5);
  TFTscreen.text("temperature:\n ", 5, 25);
  TFTscreen.text("humidity:\n ", 20, 50);
  TFTscreen.setTextSize(1.9);
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
}

void loop() {
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  } else {
    temperature = event.temperature;
  }
  delay(500);
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  } else {
    humidity = event.relative_humidity;
  }
  // 读取火焰传感器的值，其返回值范围是0-1023（模拟输入值）


  // 设置字体颜色
  TFTscreen.stroke(255, 255, 255);
  // print the sensor value
  char tchar[20];
  char hchar[20];
  dtostrf(temperature, 4, 2, tchar);
  dtostrf(humidity, 4, 0, hchar);
  strcat(tchar, " C");
  strcat(hchar, " %");
  String tstr=String(tchar);
  tstr.trim();
  String hstr=String(hchar);
  hstr.trim();
  if (oldT != tstr || oldH != hstr) {  //如果 旧的 温度 湿度 有变化 就屏幕更新
    // 把你刚写的文字擦掉
    TFTscreen.stroke(0, 0, 0);
    TFTscreen.text(oldT.c_str(), 90, 25);
    TFTscreen.text(oldH.c_str(), 80, 50);
    oldT = tstr;
    oldH = hstr;
    String tempT = oldT.substring(0, oldT.length() - 2);
    String tempH = oldH.substring(0, oldH.length() - 2);
    String result=tempT+"&"+tempH;
    Serial.println(result);
    Serial.write(result.c_str(), 8);  //串口输出
    TFTscreen.stroke(255, 255, 255);
    TFTscreen.text(tstr.c_str(), 90, 25);
    TFTscreen.text(hstr.c_str(), 80, 50);
  }

  if (digitalRead(flameSensorPin) == LOW) {  // 如果检测到火焰，点亮LED灯
    if (!firealarm) {       //如果上次是火警关闭状态
      firealarm = true;
      Serial.write("Fire alarm", 10);  //串口输出
      TFTscreen.text("Fire alarm", 50, 80);
      digitalWrite(D2, HIGH);  //蜂鸣器响
    }
  } else {
    if (firealarm) {  //如果上次是火警响了,就把屏幕提示关闭
      firealarm = false;
      Serial.write("Fire stop", 9);  //串口输出
      TFTscreen.stroke(0, 0, 0);
      TFTscreen.text("Fire alarm", 50, 80);
      // 如果没检测到火焰，熄灭LED灯
      digitalWrite(D2, LOW);  //蜂鸣器关
    }
  }

  if (Serial.available() > 0) {  //获取串口消息
    String receivedString = "";
    while (Serial.available()) {
      receivedString = Serial.readStringUntil('\n');  // 读取直到遇到换行符
    }

    Serial.println(receivedString);
    // 在这里可以对读取到的数据进行处理
    if (receivedString == "on") {          //串口接收到on
      digitalWrite(D4, HIGH);              //LED 点亮
    } else if (receivedString == "off") {  //串口接收到off
      digitalWrite(D4, LOW);               //LED 熄灭
    }
  }
}
