#define BLINKER_PRINT Serial #define BLINKER_BLE #include <Blinker.h> #include <LiquidCrystal.h> byte smiley[8] = { B00111, B00101, B00111, B00000, B00000, B00000, B00000, }; LiquidCrystal lcd(12, 10, 5, 4, 3, 2); int Temp;//温度 int Humi;//湿度 int tol;//校对码 int j; unsigned int loopCnt; int chr[40] = {0};//创建数字数组,用来存放40个bit unsigned long Time; #define pin 11 //DHT11 连接arduino11引脚 /**************WiFi模块配置*******************/ Blinker.begin(); // 默认设置: 数字IO 2(RX) 3(TX), 波特率 9600 bps /***************************************************/ BlinkerNumber HUMI("humi"); BlinkerNumber TEMP("temp"); BlinkerButton Button("btn"); uint32_t read_time = 0; float humi_read, temp_read; void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data); Blinker.vibrate(); uint32_t BlinkerTime = millis(); Blinker.print("millis", BlinkerTime); } /*心跳回调函数,向App传输数据*/ void heartbeat() { HUMI.print(humi_read); TEMP.print(temp_read); } /*按钮回调函数*/ void button_callback(const String & state) { if (state == BLINKER_CMD_ON) { digitalWrite(6, HIGH); // 驱动直流电机 Button.color("#FF0000"); } else if (state == BLINKER_CMD_OFF) { digitalWrite(6, LOW); // 不驱动直流电机 Button.color("#778899"); } } void setup() { Serial.begin(115200); BLINKER_DEBUG.stream(Serial); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); Blinker.begin(auth, ssid, pswd); /*绑定回调函数*/ Blinker.attachData(dataRead); Blinker.attachHeartbeat(heartbeat); Button.attach(button_callback); /*************************************/ lcd.begin(16, 2); // set up the LCD's number of columns and rows: lcd.print("Temp Humi"); // Print a message to the LCD. lcd.createChar(0, smiley); } void loop() { Blinker.run(); //解析接收到的数据,负责联网 pinMode(6, OUTPUT); bgn: delay(2000); pinMode(pin,OUTPUT);//设置11号接口模式为:输出 digitalWrite(pin,LOW);//输出低电平20ms(>18ms) delay(20); digitalWrite(pin,HIGH);//输出高电平40μs delayMicroseconds(40); digitalWrite(pin,LOW); pinMode(pin,INPUT);//设置11号接口模式:输入 //高电平响应信号 loopCnt=10000; while(digitalRead(pin) != HIGH) { if(loopCnt-- == 0) { Serial.println("HIGH");//如果长时间不返回高电平,输出个提示,重头开始。 goto bgn; } } //低电平响应信号 loopCnt=30000; while(digitalRead(pin) != LOW) { if(loopCnt-- == 0) { Serial.println("LOW");//如果长时间不返回低电平,输出个提示,重头开始。 goto bgn; } } //开始读取bit1-40的数值 for(int i=0;i<40;i++) { while(digitalRead(pin) == LOW) { } //当出现高电平时,记下时间“time” Time = micros(); while(digitalRead(pin) == HIGH) { } //当出现低电平,记下时间,再减去刚才储存的time //得出的值若大于50μs,则为‘1’,否则为‘0’ //并储存到数组里去 if (micros() - Time >50) { chr[i]=1; } else { chr[i]=0; } } //湿度,8位的bit,转换为数值 Humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7]; //温度,8位的bit,转换为数值 Temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23]; //校对码,8位的bit,转换为数值 tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39]; //输出:温度、湿度、校对码 lcd.setCursor(0, 1); // set the cursor to column 0, line 1 lcd.print(Temp); lcd.setCursor(2, 1); lcd.write(byte(0)); lcd.setCursor(3, 1); lcd.print("C"); lcd.setCursor(10, 1); // set the cursor to column 10, line 1 lcd.print(Humi); lcd.setCursor(12, 1); lcd.print("%"); if (Temp>27) { digitalWrite(6, HIGH); // 驱动直流电机 Button.color("#FF0000"); // Button.print("on"); } else { digitalWrite(6, LOW); // 不驱动直流电机 Button.color("#778899"); // Button.print("off"); } if (read_time == 0 || (millis() - read_time) >= 2000) { read_time = millis(); humi_read = Humi; temp_read = Temp; BLINKER_LOG("Humidity: ", humi_read, " %"); BLINKER_LOG("Temperature: ", temp_read, " *C"); } } |