通过点灯科技用一个8266实现开关灯·温湿度检测和小爱同学
用一块儿esp8266。同时控制灯的开关和温湿度获取。节约成本。控制灯的继电器接IO0,温度用的dht11,接IO2口,
这个程序同时支持小爱同学,天猫精灵和小度语音控制,
视频:
http://www.bilibili.com/video/BV1ee411x7tb?share_medium=android&share_source=copy_link&bbid=XYDC2CBD4063E5925CEAE5DC2E528CD7E1E50&ts=1585300230743
arduion 要安装8266的库和DHT的库和点灯科技的库
上传到8266里,即可代码如下(本人QQ:1824419695能帮忙的我尽力)
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET //小爱同学
#define BLINKER_DUEROS_OUTLET
#define BLINKER_ALIGENIE_OUTLET
#include <Blinker.h>
#include <DHT.h>
char auth[] = "";
char ssid[] = "";
char pswd[] = "";
BlinkerButton Button1("btn-off"); //定义按钮数据
BlinkerButton Button2("btn-on");
BlinkerButton Button3("btn-aaa");
BlinkerNumber HUMI("shidu"); //定义湿度数据键名
BlinkerNumber TEMP("wendu"); //定义温度数据键名
#define DHTPIN 2 //定义DHT11模块连接管脚io2
#define DHTTYPE DHT11 // 使用DHT 11温度湿度模块
//#define DHTTYPE DHT22 // DHT 22(AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE); //定义dht
float humi_read = 0, temp_read = 0;
void heartbeat()
{
HUMI.print(humi_read); //给blinkerapp回传湿度数据
TEMP.print(temp_read); //给blinkerapp回传温度数据
}
bool oState = false;
int counter = 0;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) { //小爱同学控制开命令
digitalWrite(0, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) { //小爱同学控制关命令
digitalWrite(0,LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
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 duerPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(0, HIGH);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(0, LOW);
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
oState = false;
}
}
void duerQuery(int32_t queryCode)
{
BLINKER_LOG("DuerOS Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_TIME_NUMBER :
BLINKER_LOG("DuerOS Query time");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("DuerOS Query time");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
default :
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
}
}
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) { //小爱同学控制开命令
digitalWrite(0, HIGH);
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) { //小爱同学控制关命令
digitalWrite(0,LOW);
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
oState = false;
}
}
void aligenieQuery(int32_t queryCode) //小爱同学控制
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.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) //点灯app内控制按键触发
{
digitalWrite(0,LOW);
BLINKER_LOG("get button state:on", state);
}
void button2_callback(const String & state) //点灯app内控制按键触发
{
digitalWrite(0,HIGH);
BLINKER_LOG("get button state:off", state);
}
void button3_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state:on", state);
Blinker.print(state);
if(state=="on"){
digitalWrite(0,LOW);
// Button3.print("on");
}else if(state =="off"){
digitalWrite(0,HIGH);
// Button3.print("off");
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(0, OUTPUT); //定义io口为输出
digitalWrite(0, LOW); //定义io默认为高电平
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
BlinkerAliGenie.attachPowerState(aligeniePowerState);
BlinkerAliGenie.attachQuery(aligenieQuery);
BlinkerDuerOS.attachPowerState(duerPowerState);
BlinkerDuerOS.attachQuery(duerQuery);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
}
void loop()
{
Blinker.run();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
}
else
{
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
humi_read = h;
temp_read = t;
}
Blinker.delay(2000);
}
好想法:lol 能上传一下代码吗 谢谢老师分享,模仿一下:lol,自己搞了好久都没成功:'(。 谢谢分享! 搞定了开关灯,怎么跟天猫精灵 控制呢?
页:
[1]