请问红外遥控和DHT11代码怎么写到一起?还有加自动配网-Arduino中文社区 - Powered by Discuz! Archiver

千里独行 发表于 2020-4-18 22:31

请问红外遥控和DHT11代码怎么写到一起?还有加自动配网

#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR   //小爱同学定义为传感器设备

#include <Blinker.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";         //wifi ssid
char pswd[] = "#define BLINKER_WIFI
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <EEPROM.h>

#include <Blinker.h>

const uint16_t kIrLed = 3;//esp01的gpio03接口(RX接口)
uint16_t RECV_PIN = 2;      //esp01的gpio2接口
IRsend irsend(kIrLed);// 定义红外发射端接口 接红外发射器的正极
IRrecv irrecv(RECV_PIN);//定义红外接收端口

decode_results results;

char auth[] = "fa17b7546b89";   //key
char ssid[] = "ChinaNet-EMuM";   //ssid
char pswd[] = "bfbwvwdj";   //password

BlinkerButton Button0("btn-xuexi"); //学习开关数据键名
BlinkerButton Button1("btn-f");   //发射键数据键名


bool xuexi = false;


void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
   
    uint32_t BlinkerTime = millis();
   
    Blinker.print("millis", BlinkerTime);
}

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 ;
   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);
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Blinker.delay(10);
    irsend.begin();
    irrecv.enableIRIn();
}

void loop()
{
    Blinker.run();
    Button0.attach(button0_callback);
    Button1.attach(button1_callback);

    if(xuexi==true){
      if (irrecv.decode(&results)) {
      dump(&results);
      irrecv.resume();
      }
    }
}";   //wifi 密码

BlinkerNumber HUMI("humi");    //定义湿度数据键名
BlinkerNumber TEMP("temp");    //定义温度数据键名

#define DHTTYPE DHT11      // 使用DHT 11温度湿度模块
#define DHTPIN 2             //定义DHT11模块连接管脚io2
DHT dht(DHTPIN, DHTTYPE, 11);//定义DHT

float humi_read = 0, temp_read = 0;

void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();

Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
BlinkerMIOT.attachQuery(miotQuery);
}


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(100);
}
void heartbeat()                  //心跳包
{
if (humi_read< 45)
{
    HUMI.color("#DC143C");//红
    HUMI.unit("%RH(干燥)");
}
else if (humi_read> 75)
{
    HUMI.color("#1E90FF");//蓝
    HUMI.unit("%RH(湿润)");
}
else
{
    HUMI.color("#00DE00");//绿
    HUMI.unit("%RH(舒适)");
}

if (temp_read< 15)
{
    TEMP.color("#1E90FF");//蓝
    TEMP.unit("℃(寒冷)");
}
else if (temp_read> 25)
{
    TEMP.color("#DC143C");//红
    TEMP.unit("℃(炎热)");
}
else
{
    TEMP.color("#00DE00");//绿
    TEMP.unit("℃(舒适)");
}

HUMI.print(humi_read );      //给blinker app回传湿度数据
TEMP.print(temp_read );      //给blinker app回传温度数据
}
void miotQuery(int32_t queryCode)      //小爱同学语音命令反馈
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

            int humi_read_int=humi_read;   //去掉湿度浮点
            BlinkerMIOT.humi(humi_read_int);   //小爱接收湿度
            BlinkerMIOT.temp(temp_read);      //小爱接收温度
            BlinkerMIOT.print();
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}

千里独行 发表于 2020-4-18 22:37

#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR   //小爱同学定义为传感器设备

#include <Blinker.h>
#include <DHT.h>

char auth[] = "";
char ssid[] = "";         //wifi ssid
char pswd[] = "";   //wifi 密码

BlinkerNumber HUMI("humi");    //定义湿度数据键名
BlinkerNumber TEMP("temp");    //定义温度数据键名

#define DHTTYPE DHT11      // 使用DHT 11温度湿度模块
#define DHTPIN 2             //定义DHT11模块连接管脚io2
DHT dht(DHTPIN, DHTTYPE, 11);//定义DHT

float humi_read = 0, temp_read = 0;

void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();

Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
BlinkerMIOT.attachQuery(miotQuery);
}


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(100);
}
void heartbeat()                  //心跳包
{
if (humi_read< 45)
{
    HUMI.color("#DC143C");//红
    HUMI.unit("%RH(干燥)");
}
else if (humi_read> 75)
{
    HUMI.color("#1E90FF");//蓝
    HUMI.unit("%RH(湿润)");
}
else
{
    HUMI.color("#00DE00");//绿
    HUMI.unit("%RH(舒适)");
}

if (temp_read< 15)
{
    TEMP.color("#1E90FF");//蓝
    TEMP.unit("℃(寒冷)");
}
else if (temp_read> 25)
{
    TEMP.color("#DC143C");//红
    TEMP.unit("℃(炎热)");
}
else
{
    TEMP.color("#00DE00");//绿
    TEMP.unit("℃(舒适)");
}

HUMI.print(humi_read );      //给blinker app回传湿度数据
TEMP.print(temp_read );      //给blinker app回传温度数据
}
void miotQuery(int32_t queryCode)      //小爱同学语音命令反馈
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

            int humi_read_int=humi_read;   //去掉湿度浮点
            BlinkerMIOT.humi(humi_read_int);   //小爱接收湿度
            BlinkerMIOT.temp(temp_read);      //小爱接收温度
            BlinkerMIOT.print();
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
页: [1]
查看完整版本: 请问红外遥控和DHT11代码怎么写到一起?还有加自动配网