|
点灯app打开后 开关状态和真实状态一致,
但是第一次点击按钮时,状态不改变,第二次点击才正常。刷新监视器时,状态也不同步。代码如下,请大侠指点
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#include <Blinker.h>
char auth[] = "*****";
char ssid[] = "m*****";
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("AAA button state: ", state);
if (state == BLINKER_CMD_ON)
{
BLINKER_LOG("Toggle on!");
digitalWrite(OUT1, HIGH);//开灯
Button.print("on");
Button.color("#FF0000"); //red
Button.text("已关b");
}
else if(state==BLINKER_CMD_OFF)
{
BLINKER_LOG("Toggle off!");
digitalWrite(OUT1, LOW);
Button.print("off");
Button.color("#008000"); //green
Button.text("已开b");
}
}
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.print("off");
Button.color("#008000"); //green
Button.text("已开x");
}
else
{
Button.print("on");
Button.color("#FF0000"); //red
Button.text("已关x");
}
}
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, LOW);
// Blinker.delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Button.attach(button_callback);
Blinker.attachDataStorage(dataStorage);
dht.begin();
}
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();
humi_read = 33.33;
temp_read = 66.666;
digitalWrite(LED_BUILTIN, LOW);
}
}
|
|