|
回帖奖励 +1 金币
[pre]#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
#include <Servo.h>
Servo myservo;//定义舵机
Servo myservo1;//定义舵机
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
// 新建组件对象
BlinkerButton Button1("test");
BlinkerSlider Slider_servo("ran-servo");//滑块
BlinkerSlider Slider_servo1("ran-servo1");//滑块
int counter = 0;
int32_t servo_val = 90;
int32_t servo_val1 = 90;
void miotPowerState(const String & state)//电源类操作
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
myservo.write(servo_val);//收到“on”的指令后舵机旋转140度
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();//反馈状态
delay(2000);//延时两秒
myservo.write(90);//舵机归零,回到垂直状态
}
else if (state == BLINKER_CMD_OFF) {
myservo.write(servo_val);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
delay(2000);
myservo.write(90);
}
}
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state=="on")
{
myservo.write(servo_val);//收到“on”的指令后舵机旋转130
Button1.color("#00FF00"); /*设置app按钮颜色*/
Button1.text("开"); /*设置app显示文本*/
Button1.print("on"); /*设置app状态为on*/
delay(2000);//延时
myservo.write(90);//舵机归零垂直
} else if(state=="press")
{
myservo.write(servo_val);//长按开关按键后舵机旋转130
Button1.color("#FF0000"); /*设置app按钮颜色*/
Button1.text("关"); /*设置app显示文本*/
Button1.print("off"); /*设置app状态为on*/
delay(2000);//延时
myservo.write(90);//舵机归零垂直
}
}
void Slider_servo_callback(int32_t value)
{
servo_val = value;
}
void Slider1_servo_callback(int32_t value)
{
myservo1.write(value);
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化舵机
myservo.attach(13);//舵机的IO口,nodemcu的D6口
myservo.write(90);//上电时舵机归零垂直
myservo1.attach(12);
BlinkerMIOT.attachPowerState(miotPowerState);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Slider_servo.attach(Slider_servo_callback);
Slider_servo1.attach(Slider1_servo_callback);
}
void loop() {
Blinker.run();
}[/pre] |
|