本帖最后由 宇之枫海云 于 2020-4-16 10:58 编辑
先说写贴原因,最近空了下来,看到一堆买来没有用的8266,心血来潮到论坛看看,看到不少好帖子,又想到家里小爱同学吃灰状态,就想起利用起来!
构思:利用8266控制家里的电脑开关机,家里有孩子喜欢玩游戏,为了方便管理再加个DHT11放到机箱测温,就知道电脑的开关状态了。后来看到帖子说可以学习遥控器,就想加上此功能,就不需要到处找遥控器了,家里孩子总是乱扔,我的手机是小米的有遥控功能,但是我老婆的没有,现在顺便做一下这个功能,在这感谢@zhangjunyan同学,我之前也是搞维修的,看你的帖子很亲切。
实施:只需要将6266的控制脚默认高电平,控制开关机时只需要delay一段时间的低电平就可以了,(一秒以内为好我的是600毫秒左右)。接PC的power脚正极就可以了(别忘了pc的负极也要接8266的负极)因为我的8266取电是从USB取得的,所以我这里没接。dht供电3.3这里注意一下,红外接收头我正好学习套件有,发射头拆的报废遥控器,
代码:
/*
开机-----D2 DH11---D1 发射----D5 接收---D6
*/
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <EEPROM.h>
#include <DHT.h>
#include <Blinker.h>
//#define BLINKER_MIOT_OUTLET
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float humi_read = 0, temp_read = 0;
const uint16_t kIrLed= 14 ; //esp01的gpio03接口(RX接口)
uint16_t RECV_PIN= 12 ; //esp01的gpio2接口
IRsend irsend(kIrLed); // 定义红外发射端接口 接红外发射器的正极
IRrecv irrecv(RECV_PIN); //定义红外接收端口
#define LED_BUILTIN 4
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
}
decode_results results;
char auth[] = "b20109793276";
//char auth[] = "ef1ca26d5f6c";
char ssid[] = "yangqu3333";
char pswd[] = "ting198603";
BlinkerButton Button0("btn-xuexi"); //学习开关数据键名
BlinkerButton Button1("btn-f"); //发射键数据键名
BlinkerButton Button2("btn-abc"); //电脑开机建
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(600);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
bool 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.temp(20);
BlinkerMIOT.humi(20);
BlinkerMIOT.pm25(20);
BlinkerMIOT.co2(20);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.temp(20);
BlinkerMIOT.humi(20);
BlinkerMIOT.pm25(20);
BlinkerMIOT.co2(20);
BlinkerMIOT.print();
break;
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
delay(700);
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, LOW);
delay(700);
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = false;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
bool xuexi = false;
decode_type_t protocol;
uint16_t size;
const uint16_t kFrequency = 38000;
bool success = true;
String modedate;
String bitdate;
String date;
void button0_callback(const String & state)
{
if(state == "on"){
Serial.println("开");
xuexi = true;
Button0.print("on");
Blinker.delay(100);
Blinker.println("开始接收");
}else if (state == "off"){
Serial.println("关");
xuexi = false;
Button0.print("off");
Blinker.delay(100);
Blinker.println("停止接收");
}
}
void button1_callback(const String & state)
{
Serial.println(state);
Serial.println(state.substring(0,state.indexOf(',')));
Serial.println(state.substring(state.indexOf(',')+1,state.lastIndexOf(',')));
Serial.println(state.substring(state.lastIndexOf(',')+1));
String modes = state.substring(0,state.indexOf(','));
char zhi[5] ;
strcpy(zhi,state.substring(state.indexOf(',')+1,state.lastIndexOf(',')).c_str());
int data;
sscanf(zhi, "%x", &data);
int b = state.substring(state.lastIndexOf(',')+1).toInt();
Serial.println(data);
Serial.println(b);
//irsend.sendRC5(data,b);
if (modes == "NEC") {
irsend.sendNEC(data,b);
} else if (modes == "SONY") {
irsend.sendSony(data,b);
} else if (modes == "RC5") {
irsend.sendRC5(data,b);
} else if (modes == "RC6") {
irsend.sendRC6(data,b);
} else if (modes == "RCMM") {
irsend.sendRCMM(data,b);
} else if (modes == "PANASONIC") {
irsend.sendPanasonic(data,b);
} else if (modes == "LG") {
irsend.sendLG(data,b);
} else if (modes == "JVC") {
irsend.sendJVC(data,b);
} else if (modes == "AIWA_RC_T501") {
irsend.sendAiwaRCT501(data,b);
} else if (modes == "WHYNTER") {
irsend.sendWhynter(data,b);
} else if (modes == "NIKAI") {
irsend.sendNikai(data,b);
}
}
void dump(decode_results *results) {
uint16_t count = results->rawlen;
if (results->decode_type == UNKNOWN) {
modedate = "Unknown encoding: ";
} else if (results->decode_type == NEC) {
modedate ="NEC";
} else if (results->decode_type == SONY) {
modedate ="SONY";
} else if (results->decode_type == RC5) {
modedate ="RC5";
} else if (results->decode_type == RC5X) {
modedate ="RC5X";
} else if (results->decode_type == RC6) {
modedate ="RC6";
} else if (results->decode_type == RCMM) {
modedate ="RCMM";
} else if (results->decode_type == PANASONIC) {
modedate ="PANASONIC";
Serial.print(results->address, HEX);
Serial.print(" Value");
} else if (results->decode_type == LG) {
modedate ="LG";
} else if (results->decode_type == JVC) {
modedate ="JVC";
} else if (results->decode_type == AIWA_RC_T501) {
modedate ="AIWA RC T501";
} else if (results->decode_type == WHYNTER) {
modedate ="Whynter";
} else if (results->decode_type == NIKAI) {
modedate ="Nikai";
}
date = uint64ToString(results->value, 16);
bitdate = results->bits;
modedate.concat(",");
modedate.concat(date);
modedate.concat(",");
modedate.concat(bitdate);
Blinker.println(modedate);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1);
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
Button2.attach(button2_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
dht.begin();
Blinker.attachData(dataRead);
Blinker.delay(10);
irsend.begin();
irrecv.enableIRIn();
}
void loop()
{
Blinker.run();
Button0.attach(button0_callback);
Button1.attach(button1_callback);
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);
if(xuexi==true){
if (irrecv.decode(&results)) {
dump(&results);
irrecv.resume();
}
}
}
后记:写意此贴也是怕以后我也忘记了,毕竟这里面没有一行代码是我写的,全是拿来主义。还有很多代码没有理解,只是编译通过而已,功能经测试全部正常,
|