小白试水文
这里主要提供简单的代码,原理可以去瞅瞅别的大佬。
代码如下
#define BLINKER_WIFI#define BLINKER_ALIGENIE_LIGHT //天猫精灵#include <Blinker.h>char auth[] = "d2398502b79d"; //换成APP获取到的密匙char ssid[] = "sun";//WiFi账号char pswd[] = "*******"; //WIFI密码int pinRelay =0; // 新建组件对象BlinkerButton Button1("btn-abc");BlinkerNumber Number1("num-abc"); // 按下按键即会执行该函数void button1_callback(const String & state) {BLINKER_LOG("get button state: ", state);if (state=="on") {digitalWrite(LED_BUILTIN, LOW);digitalWrite(pinRelay,LOW);// 反馈开关状态Button1.print("on");} else if(state=="off"){digitalWrite(LED_BUILTIN, HIGH);// 反馈开关状态digitalWrite(pinRelay, HIGH);Button1.print("off");}}void heartbeat() //心跳包{Button1.icon("fas fa-lightbulb");Button1.color("#fddb00");Button1.text("关灯","打开啦");Number1.print(WiFi.RSSI()); //信号强度}//天猫精灵void aligeniePowerState(const String & state){BLINKER_LOG("need set power state: ", state);if (state == BLINKER_CMD_ON) {digitalWrite(LED_BUILTIN, LOW);digitalWrite(pinRelay, LOW);BlinkerAliGenie.powerState("off");BlinkerAliGenie.print();}else if (state == BLINKER_CMD_OFF) {digitalWrite(LED_BUILTIN, HIGH);digitalWrite(pinRelay, HIGH);BlinkerAliGenie.powerState("on");BlinkerAliGenie.print();}}void setup() {// 初始化串口Serial.begin(115200);// 初始化有LED的IOpinMode(LED_BUILTIN, OUTPUT);pinMode(0, OUTPUT);digitalWrite(LED_BUILTIN, HIGH);digitalWrite(pinRelay, HIGH);// 初始化blinkerBlinker.begin(auth, ssid, pswd);Button1.attach(button1_callback);BlinkerAliGenie.attachPowerState(aligeniePowerState);Blinker.attachHeartbeat(heartbeat);}void loop() {Blinker.run();//Blinker.run()语句负责处理Blinker收到的数据,每次运行都会将设备收到的数据进行一次解析。//在使用WiFi接入时,该语句也负责保持网络连接}
如果成功,通电后,继电器和esp8266 01s 都会闪烁下,然后打开时灯亮,关闭时灯灭。
如果想要将天猫精灵改成小爱同学,可以去点灯科技官网,将天猫精灵相应部分改成小爱同学。
附连接:
点灯科技