nodemcu连接bilnker实现湿温度上传,实时时钟天气显示。采用u8g2库显示
#define BLINKER_WIFI
#include
#include
#include
#include
#include
int count = 0;
bool WIFI_Status = true;
char auth[] = "你的设备";
#define DHTPIN D5
#define BUTTON_1 "ButtonKey"
#define DHTTYPE DHT11
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerNumber HOUR("hour");
BlinkerNumber MIN("min");
BlinkerNumber YEAR("year");
BlinkerNumber MONTH("month");
BlinkerNumber MDAY("mday");
BlinkerNumber WDAY("wday");
BlinkerButton Button1(BUTTON_1);
DHT dht(DHTPIN, DHTTYPE);
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, 5, 4, U8X8_PIN_NONE);
uint32_t read_time = 0;
float humi_read, temp_read;
bool sta = 0;
int tem, hum;
char weath;
bool first_weadher = true;
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)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
if (sta == 1) {
BLINKER_LOG("Button tap!");
Button1.icon("fal fa-lightbulb-on");
Button1.color("#fddb00");
Button1.text("light on");
Button1.print();
}
else {
BLINKER_LOG("Button tap!");
Button1.icon("fal fa-lightbulb-on");
Button1.color("#F5F5F5");
Button1.text("light off");
Button1.print();
}
sta = !sta;
}
}
void dataStorage()
{
Blinker.dataStorage("humi", humi_read);
Blinker.dataStorage("temp", temp_read);
}
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
}
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig...");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(1000);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
bool autoConfig()
{
WiFi.begin();
for (int i = 0; i < 20; i++)
{
int wstatus = WiFi.status();
if (wstatus == WL_CONNECTED)
{
Serial.println("AutoConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
WiFi.printDiag(Serial);
return true;
}
else
{
Serial.print("AutoConfig Waiting......");
Serial.println(wstatus);
delay(1000);
}
}
Serial.println("AutoConfig Faild!" );
return false;
}
void weatherData(const String & data)
{
BLINKER_LOG("weather: ", data);
Serial.println(data);
StaticJsonDocument<400> doc;
DeserializationError error = deserializeJson(doc, data);
if (error)
{
first_weadher == true;
return;
}
weath = doc["weather"];
tem = doc["temp"];
hum = doc["humi"];
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.setTimezone(8.0);
if (!autoConfig())
{
Serial.println("Start module");
smartConfig();
}
Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Blinker.attachHeartbeat(heartbeat);
Blinker.attachDataStorage(dataStorage, 60 , 1);
Blinker.attachWeather(weatherData);
dht.begin();
u8g2.begin();
u8g2.enableUTF8Print();
}
void loop()
{
char m_str[4];
Blinker.run();
int8_t hour = Blinker.hour();
int8_t min = Blinker.minute();
int8_t sec = Blinker.second();
int16_t year = Blinker.year();
int8_t month = Blinker.month();
int8_t mday = Blinker.mday();
int8_t wday = Blinker.wday();
HOUR.print(hour);
MIN.print(min);
YEAR.print(year);
MONTH.print(month);
MDAY.print(mday);
WDAY.print(wday);
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_luIS08_tf);
u8g2.setCursor(30, 15);
u8g2.print(hour);
u8g2.drawStr(55, 15, ":");
u8g2.setCursor(65, 15);
u8g2.print(min);
u8g2.setCursor(20, 30);
u8g2.print(year);
u8g2.drawStr(55, 30, "-");
u8g2.setCursor(65, 30);
u8g2.print(month);
u8g2.drawStr(90, 30, "-");
u8g2.setCursor(100, 30);
u8g2.print(mday);
u8g2.setFont(u8g2_font_luIS08_tf);
u8g2.setFontDirection(0);
u8g2.setCursor(40, 50);
itoa(wday, m_str, 10);
switch (wday) {
case 0: u8g2.print("Sunday");
break;
case 1: u8g2.print("Monday");
break;
case 2: u8g2.print("Tuesday");
break;
case 3: u8g2.print("Wednesday");
break;
case 4: u8g2.print("Thursday");
break;
case 5: u8g2.print("Friday");
break;
case 6: u8g2.print("Saturday");
break;
default: u8g2.print("0");
}
} while ( u8g2.nextPage() );
Blinker.delay(3000);
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;
}
humi_read = h;
temp_read = t;
if (sec > 0) {
if ((min > 58) || (first_weadher == true))
{
Blinker.weather("hebei-pingshanxian");
first_weadher = false ;
}
}
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_luIS08_tf);
u8g2.drawStr(0, 15, "indoor-humi");
u8g2.drawStr(65, 15, ":");
strcpy(m_str, u8x8_u8toa(h, 2));
u8g2.drawStr(75, 15, m_str);
u8g2.drawStr(90, 15, "%");
u8g2.drawStr(0, 30, "indoor-temp");
u8g2.drawStr(65, 30, ":");
strcpy(m_str, u8x8_u8toa(t, 2));
u8g2.drawStr(75, 30, m_str);
u8g2.drawStr(90, 30, "C");
u8g2.setFont(u8g2_font_luIS08_tf);
u8g2.drawStr(33, 45, "humi");
u8g2.drawStr(65, 45, ":");
u8g2.setCursor(75, 45);
u8g2.print(hum);
u8g2.drawStr(90, 45, "%");
u8g2.drawStr(33, 60, "temp");
u8g2.drawStr(65, 60, ":");
u8g2.setCursor(75, 60);
u8g2.print(tem);
u8g2.drawStr(90, 60, "C");
u8g2.setFont(u8g2_font_wqy12_t_gb2312);
u8g2.setCursor(0, 53);
u8g2.print("阴");
} while ( u8g2.nextPage() );
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
Blinker.delay(3000);
}
}