作品展示
材料清单
- ESP8266 淘宝15左右
MG995 180°舵机 淘宝13左右已玩废,建议买全金属齿轮舵机不然两下齿轮就磨平了 25左右- 充电模块 2元左右
- 18650电池 15元3节
- CP2102 6元左右 这个模块有5V和3.3V输出 这里用于给开发板和舵机供电
- 杜邦线若干 2元左右
- 外卖一次性筷子和随便捡的铁丝用于连接门把手 不要钱
接线图
源码
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Servo.h>
#include <Blinker.h>
#define PIN_SERVO 16 //舵机信号线
Servo myservo;
char auth[] = "";//替换成点灯科技设备Key
char ssid[] = ""; //你家的WiFi热点名称
char pswd[] = ""; //你的WiFi密码
bool oState = false;
BlinkerButton Button1("door");// 电灯可以新增按钮 命名叫door
//小爱同学电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
myservo.attach(PIN_SERVO);
if (state == BLINKER_CMD_ON) {
openDoor();
}
else if (state == BLINKER_CMD_OFF) {
openDoor();
}
}
//小爱同学语音命令反馈
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 dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void button1_callback(const String & state)
{
BLINKER_LOG("get button1111 state: ", state);
if (state == BLINKER_CMD_ON) {
openDoor();
}
Blinker.vibrate();
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
myservo.write(0);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void openDoor()
{
myservo.write(0);
for(int i=0;i<=180;i+=20) //控制舵机角0~180°
{
BLINKER_LOG("开始旋转:",i);
myservo.write(i);
delay(5);
}
BLINKER_LOG("暂停");
delay(2000);
for(int i=180;i>=0;i-=20) //控制舵机角180°~0
{
BLINKER_LOG("开始旋转:",i);
myservo.write(i);
delay(5);
}
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
void loop()
{
Blinker.run();
}
评论区