|
楼主 |
发表于 2022-1-19 22:53
|
显示全部楼层
升级了个最新版本 2.6.0 的APP,现在控制没问题了,也不知道为啥。。。 贴一下代码吧。。- bool oState = false;
- // 新建组件对象
- BlinkerButton Button1("btn-abc");
- void duerPowerState(const String & state)
- {
- BLINKER_LOG("need set power state: ", state);
- if (state == BLINKER_CMD_ON) {
- digitalWrite(LED_BUILTIN, HIGH);
- digitalWrite(0, HIGH);
- Button1.color("#708090");
- Button1.text("已关闭");
- Button1.print("off");
- BlinkerDuerOS.powerState("off");
- BlinkerDuerOS.print();
- oState = true;
- }
- else if (state == BLINKER_CMD_OFF) {
- digitalWrite(LED_BUILTIN, LOW);
- digitalWrite(0, LOW);
- Button1.color("#6666ff");
- Button1.text("已开启");
- Button1.print("on");
- BlinkerDuerOS.powerState("on");
- BlinkerDuerOS.print();
- oState = false;
- }
- }
- void duerQuery(int32_t queryCode)
- {
- BLINKER_LOG("DuerOS Query codes: ", queryCode);
- switch (queryCode)
- {
- case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
- BLINKER_LOG("DuerOS Query power state");
- BlinkerDuerOS.powerState(oState ? "on" : "off");
- BlinkerDuerOS.print();
- break;
- case BLINKER_CMD_QUERY_TIME_NUMBER :
- BLINKER_LOG("DuerOS Query time");
- BlinkerDuerOS.time(millis());
- BlinkerDuerOS.print();
- break;
- default :
- BlinkerDuerOS.powerState(oState ? "on" : "off");
- BlinkerDuerOS.print();
- break;
- }
- }
- // 按下按键即会执行该函数
- void button1_callback(const String & state)
- {
- //BLINKER_LOG("get button state: ", state);
- Serial.print(state);
- if(state == BLINKER_CMD_ON)
- {
- Serial.print("state ON");
- digitalWrite(LED_BUILTIN, HIGH);
- digitalWrite(0, LOW);
- Button1.color("#6666ff");
- Button1.text("已开启");
- Button1.print("on");
-
- }
- else if(state == BLINKER_CMD_OFF)
- {
- Serial.print("state OFF");
- digitalWrite(LED_BUILTIN, LOW);
- digitalWrite(0, HIGH);
- Button1.color("#708090");
- Button1.text("已关闭");
- Button1.print("off");
- }
- else{}
- }
- void setup()
- {
- // 初始化串口
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
-
- // 初始化有LED的IO
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
-
- pinMode(0, OUTPUT);
- digitalWrite(0, HIGH);
-
- // 初始化blinker
- Blinker.begin(auth, ssid, pswd);
- Button1.attach(button1_callback);
-
- BlinkerDuerOS.attachPowerState(duerPowerState);
- BlinkerDuerOS.attachQuery(duerQuery);
-
- }
- void loop() {
- Blinker.run();
- }
复制代码 |
|