我只是刚刚入门的萌新,所以有不足请指出
先展示下
功能:开关机,开关机检测,运行时间显示
但是不知道怎么配置电源检测,我这使用INPUT,可状态一直闪烁
材料
esp-01s,继电器,杜邦线继电器接线:电脑开机线接com与no(不分顺序) 5v供电接继电器vcc和gnd(分+-)如图但是这个5v供电不要用充电器,直接接电脑主板上,不然检测无法用
开发板部分全部用杜邦线延长到继电器即可 但是GPIO2的线从开发板连接到电脑主板5v+ 继电器上的该孔就空着
附上源码
- #define BLINKER_WIFI
- #include <Blinker.h>
- char auth[] = "key";
- // 编写:1812z
- char ssid[] = "wifi";
- char pswd[] = "wifi密码";
- #define BUTTON_1 "ButtonKey"
- BlinkerButton Button1("BUTTON_1");
- BlinkerText TEXT1("tex-s");
- BlinkerText TEXT2("power");
- BlinkerText TEXT3("powerj");
- void button1_callback(const String & state)
- {
- digitalWrite(0, !digitalRead(0));
- BLINKER_LOG("get button state: ", state);
- if (state == BLINKER_CMD_BUTTON_TAP) {
- BLINKER_LOG("Button tap!");
- Button1.icon("icon_1");
- Button1.color("#00FF00");
- Button1.text("短按");
- Button1.print();
- Blinker.delay(500);
- digitalWrite(0, !digitalRead(0));
- }
- else if (state == BLINKER_CMD_BUTTON_PRESSED) {
- BLINKER_LOG("Button pressed!");
- Button1.icon("icon_1");
- Button1.color("#FF0000");
- Button1.text("长按中");
- Button1.print();
- }
- else if (state == BLINKER_CMD_BUTTON_RELEASED) {
- BLINKER_LOG("Button released!");
- Button1.icon("icon_1");
- Button1.color("#FF0000");
- Button1.text("成功长按");
- // Button1.text("Your button name", "describe");
- Button1.print();
- }
- else if (state == BLINKER_CMD_ON) {
- BLINKER_LOG("Toggle on!");
- Button1.icon("icon_1");
- Button1.color("#FF0000");
- Button1.text("继电器闭合");
- // Button1.text("Your button name", "describe");
- Button1.print("on");
- }
- else if (state == BLINKER_CMD_OFF) {
- BLINKER_LOG("Toggle off!");
- Button1.icon("icon_1");
- Button1.color("#FF0000");
- Button1.text("继电器断开");
- // Button1.text("Your button name", "describe");
- Button1.print("off");
- }
- else {
- BLINKER_LOG("Get user setting: ", state);
- Button1.icon("icon_1");
- Button1.color("#FFFFFF");
- Button1.text("?");
- // Button1.text("Your button name", "describe");
- Button1.print();
- }
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
- uint32_t BlinkerTime = millis();
- Blinker.print("millis", BlinkerTime);
- }
- String rts()//计算运行时间
- {
- int rt =Blinker.runTime();
- int r,e,f,s;
- String fh;
- Blinker.delay(100);
- if(rt >= 86400)//天数
- {r = rt / 86400;
- e = rt / 3600 - r*24;
- f = rt / 60 - r*1440 - e*60;
- s = rt - r*86400 - e*3600 - f*60;}
- else if(rt >= 3600)
- {r = 0;
- e = rt / 3600;
- f = rt / 60 - e*60;
- s = rt - e*3600 - f*60;}
- else
- {r = 0;
- e = 0;
- f = rt / 60;
- s = rt - f*60;}
- //BLINKER_LOG(r," ",e," ",f," ",s);//输出数据测试
- if(f==0 & e==0 & r==0)
- {fh = String("")+ s +"秒";}
- else if(r == 0 & e == 0 )
- {fh = String("")+ f + "分" + s +"秒";}
- else if(r == 0)
- {fh = String("")+ e + "时" + f + "分" + s +"秒"; }
- else
- {fh = String("")+ r + "天" + e + "时" + f + "分" + s +"秒";}
- return(fh);
- }
- void heartbeat()//心跳
- {
- TEXT1.print(rts());
- }
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- pinMode(0, OUTPUT);
- digitalWrite(0, HIGH);
- pinMode(2, INPUT_PULLUP);
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachData(dataRead);
- Button1.attach(button1_callback);
- Blinker.attachHeartbeat(heartbeat);//附加心跳
- }
- void loop()
- {
- int sensorVal = digitalRead(2);
- Serial.println(sensorVal);
- if (sensorVal == LOW) {
- TEXT2.print("电源接通");
- } else {
- TEXT2.print("电源断开");
- }
- int sensorVal2 = digitalRead(0);
- Serial.println(sensorVal2);
- Blinker.delay(1000);
- if (sensorVal2 == HIGH) {
- TEXT3.print("断开");
- } else {
- TEXT3.print("闭合");
- }
- Blinker.run();
- }
复制代码
附上app界面配置:
{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/bg/f5.jpg¨¨isFull¨»}}¨dashboard¨|{¨type¨¨btn¨¨ico¨¨fad fa-arrow-alt-up¨¨mode¨É¨t0¨¨单击¨¨t1¨¨文本2¨¨bg¨É¨cols¨Ë¨rows¨Ë¨key¨¨BUTTON_1¨´x´É´y´Ë¨speech¨|{¨cmd¨¨开关电脑¨¨act¨¨tap¨}÷¨lstyle¨Ê¨clr¨¨#076EEF¨}{ßA¨tex¨ßF¨继电器控制¨ßH¨已接入电脑¨ßJÉßC¨fad fa-digital-tachograph¨ßKÍßLËßM´´´x´Ë´y´ËßO|÷ßTÌßUßV}{ßAßWßF¨文本1¨ßH¨开发板运行时间¨ßJÉßC¨fad fa-database¨ßKÍßLËßM¨tex-s¨´x´Í´y´ÍßO|÷ßTÌßUßV}{ßAßWßF¨状态中心¨ßH´´ßJÉßC¨fad fa-power-off¨ßKËßLËßM´´´x´Ï´y´ËßO|÷ßTËßUßV}{ßAßWßF¨调试---->¨ßH¨手动控制¨ßJÉßC¨fad fa-lock¨ßKËßLËßM´´´x´É´y´ÏßO|÷ßUßVßTË}{ßAßBßC¨fal fa-power-off¨ßEÊßFßhßHßIßJÉßKËßLËßMßN´x´Ë´y´ÏßO|÷¨cus¨¨off¨ßTÊ}{ßAßWßFßaßH¨继电器操作¨ßJÉßC¨fal fa-font¨ßKÍßLËßMßN´x´É´y´ÍßO|÷ßTÌßUßV}{ßAßWßFßaßH¨电脑电源状态¨ßJÉßC¨fad fa-server¨ßKËßLËßM¨power¨´x´Í´y´ÏßO|÷ßTËßUßV}{ßAßWßFßaßH¨继电器状态¨ßJÉßCßZßKËßLËßM¨powerj¨´x´Ï´y´ÏßO|÷ßTËßUßV}{ßAßWßF¨闪烁¨ßH¨为开机¨ßJÉßCßDßKËßLËßM´´´x´Í´y´ÑßO|÷ßTË}{ßAßWßF¨操作和真的开机¨ßH¨长按和短按¨ßJÉßCßnßKÍßLËßM¨tex-l0w¨´x´É´y´ÉßO|÷ßTÌ}{ßAßWßF¨键一样¨ßHßIßJÉßCßnßKËßLËßM¨tex-az0¨´x´Í´y´ÉßO|÷ßTË}÷¨actions¨|¦ßP¦¨switch¨‡¨text¨‡´on´¨打开?name¨ßl¨关闭?name¨—÷¨triggers¨|{¨source¨ß11¨source_zh¨¨开关状态¨¨state¨|´on´ßl÷¨state_zh¨|´打开´´关闭´÷}÷}
|