初涉arduino,初涉esp8266,初涉Blinker,写了2个程序。第一个,控制水泵用。能app控制,能小爱语音控制,能自动断开。
用的硬件是ESP01S、对应继电器以及杜邦线(选用公母的,需要两头有插头时拆开一头)。单控火线。
方便在烧录程序时就可以看到操作效果,esp上的led与引脚0控制的继电器led亮灭同步。
代码如下:
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
#include <Ticker.h>
char auth[] = "***";
char ssid[] = "***";
char pswd[] = "***";
int GPIO = 0;//用变量GPIO表示引脚0,这个引脚和下面的语句高低电平变化配套
bool oState = false;
Ticker flipper;
int c = 0;
BlinkerButton Button1("btn-on");
BlinkerButton Button2("btn-off");
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {//只响应短按
if (digitalRead(GPIO) == LOW) {
c = 0;//删除此句可以继续上一次的c值开始计数,但注意loop里面的c值
flipper.attach(1, flip);//短按Button1键后,c值+1每秒,参考flip函数(往下数25行)
BLINKER_LOG("Button tap!");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(GPIO, HIGH);
Button1.color("#FFC800");
Button1.text("运行中");
Button1.print();
}
}
}
void button2_callback(const String & state)
{
Button2.color("#CCCCCC");
Button2.text("停止");
Button2.print();
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {//只响应短按
if (digitalRead(GPIO) == HIGH) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(GPIO, LOW);
Button1.color("#CCCCCC");
Button1.text("已关闭");
Button1.print();
flipper.detach();//按下停止按键,退出计数;不退出会一直运行flip(虽然此时LED_BUILTIN是高电平它啥也不做)
}
}
}
void flip() {
if (digitalRead(LED_BUILTIN) == LOW) {
c++;
BLINKER_LOG(c);//串口打印c值
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(GPIO, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
c = 0;//删除此句可以继续上一次的c值开始计数,但注意loop里面的c值
flipper.attach(1, flip);//c值+1每秒,参考flip函数(往上数10行)
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(GPIO, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
flipper.detach();//语音叫停,退出计数
oState = false;
}
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(GPIO) == HIGH) {
Button1.color("#FFC800");
Button1.text("运行中");
Button1.print();
//count++;//手机不在monitor界面or不打开串口监视器heartbeat函数不运行,本来想用这个计时的,F..k
}
else {
Button1.color("#CCCCCC");
Button1.text("已关闭");
Button1.print();
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);//这块esp01s的LED默认高电平关闭状态
pinMode(GPIO, OUTPUT);
digitalWrite(GPIO, LOW); //引脚0低电平时,继电器LED不亮
Blinker.begin(auth, ssid, pswd);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
Blinker.run();
// if (count > 60) {
// digitalWrite(LED_BUILTIN, HIGH);
// digitalWrite(GPIO, LOW);
// Button1.color("#CCCCCC");
// Button1.text("已关闭");
// Button1.print();
// count = 0;
// }//此段代码注释掉了,本想用heartbeat计数的,行不通,改用Ticker
if (c == 3600) {//计数到最大值3600秒,关闭设备,退出计数(若测试用,建议设置10-15)
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(GPIO, LOW);
Button1.color("#CCCCCC");
Button1.text("已关闭");
Button1.print();
flipper.detach();
c=0;//不可缺少此句,c可以是3600以外的任意值
}
}
下面的图片是代码存在的问题。网页搜索过,看到个说用天猫精灵语音控制就出现此问题。我这用小爱语音控制并到达最大倒计时大概率出现这问题,但硬件的实际动作并无出错。把小爱模块里面的计时功能去掉,不会有这问题。
|