#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include <Blinker.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
// 定义温湿度传感器引脚
#define DHTPIN D3//温湿度传感器接至引脚D3
#define DHTTYPE DHT11// 认证信息
#define BLINKER_WIFI
#include <Blinker.h>
#include <Arduino.h>
#include <NTPClient.h>
#include <U8g2lib.h>
#include <DHT.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
char auth[] = "7faffa178d3c";
char ssid[] = "nova 5";
char pswd[] = "12131245678";
// 创建组件
BlinkerNumber HUMI("num-R");
BlinkerNumber TEMP("num-T");
// DHT11初始化
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
float humi_read, temp_read;// 数据读取
// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerNumber Number1("num-abc");
int counter = 0;
// 按下按键即会执行该函数
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/
D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);
void dataRead(const String & data){
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
counter++;
Number1.print(counter);}
// 温湿度数据发送
void heartbeat(){
HUMI.print(humi_read);
TEMP.print(temp_read);
}
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
// 如果未绑定的组件被触发,则会执行其中内容
void setup(){
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
u8g2.begin();
u8g2.enableUTF8Print();
dht.begin();
}
void loop(){
Blinker.run();
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
BLINKER_LOG("Failed to read from DHT sensor!");
return;
}
float hic = dht.computeHeatIndex(t, h, false);
humi_read = h;
temp_read = t;
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, "*C");
BLINKER_LOG("Heat index: ", hic, "*C");
}
float h = dht.readHumidity();
float t = dht.readTemperature();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_unifont_t_chinese2);
u8g2.setFontDirection(0);
u8g2.setCursor(0, 15);
u8g2.print("Humidity:");
u8g2.setCursor(80, 15);
u8g2.print(h);
u8g2.setCursor(120, 15);
u8g2.print("%");
u8g2.setCursor(0, 40);
u8g2.print("Temperature:");
u8g2.setCursor(0, 55);
u8g2.print(t);
u8g2.setCursor(40, 55);
u8g2.print("C");
u8g2.sendBuffer();
delay(1000);
Blinker.run();
} |