#include <ESP8266WiFi.h>
static WiFiClient espClient;
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <CustomWiFiManager.h>
#include <ArduinoJson.h>
#include <DHT.h>
#include <AliyunIoTSDK.h>
AliyunIoTSDK iot;
#define PRODUCT_KEY "xxx"
#define DEVICE_NAME "xxx"
#define DEVICE_SECRET "xxx"
#define REGION_ID "cn-shanghai" unsigned long lastMsMain = 0;
int powerPin = 2;
#define DHTPIN 3
#define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE);bool waterShortage;
float targetHumidity;
enum FOGLEVEL{yidang = 1,erdang,sandang,sidang,wudang};
enum FOGLEVEL fogLevel;
enum MODE{zidong = 2,chushi = 13,shuimian = 14,zhienng = 32,putong = 39,biaozhun = 48};
enum MODE mode;
int fog;
bool powerstate;
float temperature;
const char Icon[] PROGMEM = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGkAAACACAMAAAA/F7dCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAzUExURQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKMFRskAAAAQdFJOUwAQIDBAUGBwf4+fr7/P3+8FUYRsAAAACXBIWXMAABcRAAAXEQHKJvM/AAAFMUlEQVRoQ+2aabedKgyGDw7bWff//7VVEyWShEFou9a9fb60oAeUN2TA/fN/xuC/T+TeDEy/fL9Tg62bZtm+64CNItT7PAdjhR0nZoDepcaOfAxOtPPBrp1mw77vVmoNzYQjHlwv0MzYcTBDXzYdjodMXV134/1CJx3emgl9eoUNb82jxtG8FDGKHgfzMuLNWVjD81Bi+QyOFeCx097R4lABCljfiEMFmPD2DFYcKkC+UFE2fpAt1AcHCpItFPV5XrKFipQpX6homZIcUtWNvaur48d9uEI18zKRcEbozkDgxOpomVyhMKhJ6l2+4Plo0TK5Ql1+ucc24XakdL0TZPp+aUJzh37BUO4gupKpEmR6PH5t14LtaOKyN6tjgkxEE/Mh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Z9KW5ekj9SN/5szVmZR9skM3pX6YzJ059GODoNevVGz9ebjjg35sEJQdeUDcsV4LcscH/diw6FJTodRNJzkJ6MeGxTMG8Wm6gQpbF/qxYfF97rNC6TIJWxf6sWHxPC0RyiMm31DQjQ2L52mtXfmWmJs5dGPD4nvaW23fEnMzh25s3PhMz2qg+5EdZnzQjY2bwFEGCuV9cWYS0I2NG69BXCvjf3FmEtCLjRuvQVwro7vXEzdbhV5sXIROnGBlAt/q3KQFerFxEXhaFCpwiOgKBb2VA/TqmJibzpEs2Pvf4ufnFxnTN6gaOi7lAAAAAElFTkSuQmCC";void setup() {Serial.begin(115200);pinMode(LED_BUILTIN, OUTPUT);WiFiManager wifiManager;wifiManager.setHeadImgBase64(FPSTR(Icon)); wifiManager.setPageTitle("ESP8266物联网设备WiFi配置页"); wifiManager.autoConnect("ESP8266-IoT");Serial.println("connected...恭喜!一切就绪。");AliyunIoTSDK::begin(espClient, PRODUCT_KEY, DEVICE_NAME, DEVICE_SECRET, REGION_ID);dht.begin();AliyunIoTSDK::bindData("powerstate", powerstate1);AliyunIoTSDK::bindData("targetHumidity", targetHumidity1);AliyunIoTSDK::bindData("waterShortage", waterShortage1);AliyunIoTSDK::bindData("fog", fog1);AliyunIoTSDK::bindData("fogLevel", fogLevel1);AliyunIoTSDK::bindData("mode", mode1);}
int test=0;
void loop() {powerstate = !digitalRead(LED_BUILTIN);AliyunIoTSDK::loop(); if (millis() - lastMsMain >= 2000){lastMsMain = millis();AliyunIoTSDK::send("powerstate", powerstate);test = test + 2;}delay(1000);if (test > 180){AliyunIoTSDK::sendEvent("filterExpired"); Serial.println("滤芯寿命到期");}if (test == 360){test = 0;Serial.println("已更换滤芯");}}
void powerstate1(JsonVariant L)
{bool i = (L["powerstate"]);powerstate = not i;Serial.printf("powerstate:"); Serial.println(powerstate);digitalWrite(LED_BUILTIN, powerstate);AliyunIoTSDK::send("powerstate", !digitalRead(LED_BUILTIN));
}
void targetHumidity1(JsonVariant L)
{targetHumidity = L["targetHumidity"];Serial.printf("targetHumidity:"); Serial.println(targetHumidity);AliyunIoTSDK::send("targetHumidity", targetHumidity);
}
void waterShortage1(JsonVariant L)
{waterShortage = L["waterShortage"];Serial.printf("waterShortage:"); Serial.println(waterShortage);AliyunIoTSDK::send("waterShortage", waterShortage);
}
void fog1(JsonVariant L)
{fog = L["fog"];Serial.printf("fog:"); Serial.println(fog);AliyunIoTSDK::send("fog", fog);
}
void fogLevel1(JsonVariant L)
{fogLevel = L["fogLevel"];Serial.printf("fogLevel:"); Serial.println(fogLevel);AliyunIoTSDK::send("fogLevel", fogLevel);
}
void mode1(JsonVariant L)
{mode = L["mode"];Serial.printf("mode:"); Serial.println(mode);AliyunIoTSDK::send("mode", mode);
}