|
经过将近一个月的稳定试验决定发出来(不要轻易模仿有一定的危险性)可通过点灯app远程启动汽车,锁车门,开车门,以及检测车内温度湿度,升窗,降窗,锁车门自动升窗。所有代码都是自己改的!
共使用了7个端口,还有两个端口预留其中D6用来控制车后LED屏广告
D0上锁
D1开锁
D2温度湿度回传
D3升车窗
D4降车窗
D5acc on
D6广告开关 预留
D7点火启动
代码如下
#define BLINKER_WIFI
#define BLINKER_MIOT_MULTI_OUTLET
#include <Blinker.h>
#include <DHT.h>
BlinkerButton Button1("sm");
BlinkerButton Button2("km");
BlinkerButton Button3("sc");
BlinkerButton Button4("jc");
BlinkerButton Button5("gd");
BlinkerButton Button6("gg");
BlinkerButton Button7("qd");
BlinkerText Text1("text_1");
char i;
char auth[] = "13fd71ec75f3";
char ssid[] = "wlzg";
char pswd[] = "86058605";
BlinkerNumber HUMI("sd"); //定义湿度数据键名
BlinkerNumber TEMP("wd"); //定义温度数据键名
#define DHTPIN D2 //定义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[5] = { false }; //设置5路开关控制
void miotPowerState(const String & state, uint8_t num) //小爱同学控制指令
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (num == 1) {
i = D0; //指定每一路开关对应在开发板上的通道接口
} if (num == 2) {
i = D1;
} if (num == 3) {
i = D3;
} if (num == 4) {
i = D4;
} else if (num == 5){
i = D6;
} else if (num == 5){
i = D5;
} else if(num == 6) {
i = D7;
}
if (state == BLINKER_CMD_ON) {
digitalWrite(i,HIGH);
BlinkerMIOT.powerState("on");
BLINKER_LOG("");
BlinkerMIOT.print();
oState[num] = false;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(i, LOW);
BlinkerMIOT.powerState("off");
BLINKER_LOG("");
BlinkerMIOT.print();
oState[num] = false;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
}
}
}
}
void miotQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("AliGenie Query outlet: ", num, ", codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
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 button state: ", state);
digitalWrite(D0, HIGH);
Blinker.delay(200);
digitalWrite(D0, LOW);
BLINKER_LOG("get button state: ", state);
digitalWrite(D3, HIGH);
Blinker.delay(4500);
digitalWrite(D3, LOW);
}
void button2_callback(const String & state) //开锁
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, HIGH);
Blinker.delay(200);
digitalWrite(D1, LOW);
}
void button3_callback(const String & state) //升窗
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D3, HIGH);
Blinker.delay(1000);
digitalWrite(D3, LOW);
}
void button4_callback(const String & state) //降窗
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D4, HIGH);
Blinker.delay(1000);
digitalWrite(D4, LOW);
}
void button5_callback(const String & state) //熄火//广告关//通电
{
if (digitalRead(D5) == HIGH) {
BLINKER_LOG("关闭!");
digitalWrite(D5, LOW);
Button5.color("#999999");
Button5.text("熄火/通电");
Button5.print("off");
digitalWrite(D6, LOW);
BLINKER_LOG("get button state: ", state);
}
else if (digitalRead(D5) == LOW) {
BLINKER_LOG("打开!");
digitalWrite(D5, HIGH);
Button5.color("#0066FF");
Button5.text("断电");
Button5.print("on");
}
}
void button6_callback(const String & state) //车窗广告
{
if (digitalRead(D6) == HIGH){
BLINKER_LOG("关闭!");
digitalWrite(D6, LOW);
Button6.color("#999999");
Button6.text("广告关");
Button6.print("off");
}
else if (digitalRead(D6) == LOW){
BLINKER_LOG("打开!");
digitalWrite(D6, HIGH);
Button6.color("#0066FF");
Button6.text("广告开");
Button6.print("on");
}
}
void button7_callback(const String & state) //开锁//通电//启动//广告开
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, HIGH);
Blinker.delay(200);
digitalWrite(D1, LOW);
digitalWrite(D5,HIGH);
BLINKER_LOG("get button state:on", state);
BLINKER_LOG("get button state: ", state);
digitalWrite(D7, HIGH);
Blinker.delay(700);
Blinker.print("on");
digitalWrite(D7, LOW);
digitalWrite(D6, HIGH);
BLINKER_LOG("get button state:on", state);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
BlinkerMIOT.attachQuery(miotQuery);
pinMode(D0, OUTPUT);
digitalWrite(D0, LOW);
pinMode(D1, OUTPUT);
digitalWrite(D1,LOW);
pinMode(D3, OUTPUT);
digitalWrite(D3,LOW);
pinMode(D4, OUTPUT);
digitalWrite(D4,LOW);
pinMode(D5, OUTPUT);
digitalWrite(D5,LOW);
pinMode(D6, OUTPUT);
digitalWrite(D6,LOW);
pinMode(D7, OUTPUT);
digitalWrite(D7,LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
Button7.attach(button7_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);
}
硬件如下
车窗升降
WiFi
手机控制
|
|