|
楼主 |
发表于 2020-12-14 10:43
|
显示全部楼层
多谢各位帮助,终于解决。代码如下:
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#define BLINKER_WITHOUT_SSL
#include <Blinker.h>
char auth[] = "";
char ssid[] = "";
char pswd[] = "";
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerButton b1("b1");
BlinkerButton Button("btn");
#include <DHT.h>
#define DHTPIN 4
int OUT1 = 12;
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
float humi_read, temp_read;
void button_callback(const String & state) //手机控制LED的亮灭//
{
BLINKER_LOG("get button state: ", state);
digitalWrite(OUT1, !digitalRead(OUT1));
if (digitalRead(OUT1)==HIGH)
{
Button.color("#008000"); //green
Button.text("on");
Button.print("on");
}
else
{
Button.color("#FF0000"); //red
Button.text("off");
Button.print("off");
}
// delay(100);
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("间隔", BlinkerTime/1000);
}
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
if (digitalRead(OUT1)==HIGH)
{
Button.color("#008000"); //green
Button.text("已开x");
Button.print("on");
}
else
{
Button.color("#FF0000"); //red
Button.text("已关x");
Button.print("off");
}
}
void dataStorage() {
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// BLINKER_DEBUG.debugAll();
pinMode(OUT1, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Button.attach(button_callback);
Blinker.attachDataStorage(dataStorage);
dht.begin();
digitalWrite(OUT1, HIGH);
}
void loop()
{
Blinker.run();
digitalWrite(LED_BUILTIN, HIGH);
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
return;
}
humi_read = 33.33;
temp_read = 66.666;
digitalWrite(LED_BUILTIN, LOW);
}
} |
|