#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <PubSubClient.h>

const char* ssid = "webserver";            //WIFI名字
const char* password = "www.nobiggie.cn";  //WIFI密码

const char* mqtt_server = "tcp://bemfa.com:9501";      //服务器与 端口9501
String clientId = "07b120062a7972cf9831d7d9bc8012bc";  //mqttID
const char* inTopic = "esp8266/inTopic";                    //订阅主题
const char* outTopic = "esp8266/outTopic";                  //发送主题

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

#define DHTPIN 13          // 数字引脚连接DHT传感器
#define PHOTOSENSITIVE 14  //光敏传感器 默认高电平
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
#define DHTTYPE DHT11  // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
float humidity;
float temperature;
void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(PHOTOSENSITIVE, INPUT);  // 将引脚设置为输入模式
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.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);
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  } else {
    humidity = event.relative_humidity;
  }
  String th = String(temperature) + "℃" + String(humidity) + "%";
  Serial.println(th);

  int v = digitalRead(PHOTOSENSITIVE);  // 读取引脚的电平值
  if (v == 0) {
    Serial.println("night");
  } else {
    Serial.println("day");
  }
  delay(3000);
}
void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    clientId += String(random(0xffff), HEX);
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      client.publish(outTopic, "hello world");  //向主题outTopic 发送hello world
      client.subscribe(inTopic);                //订阅主题inTopic
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String str2;
  for (int i = 0; i < length; i++) {
    str2 += (char)payload[i];
  }
  str2.trim();
  Serial.print(str2);
  if (str2 == "buzzerOn") {  //蜂鸣器响

  } else if (str2 == "buzzerOff") {  //蜂鸣器关

  } else if (str2 == "relayOn") {   //继电器通

  } else if (str2 == "relayOff") {  //继电器关

  }
}