我的nodemcu实践
程序员文章站
2022-06-19 12:54:20
nodemcu连接bilnker实现湿温度上传,实时时钟天气显示。采用u8g2库显示/* ***************************************************************** Download latest Blinker library here: https://github.com/blinker-iot/blinker-library/archive/master.zip Blinker is a cross-hardware,...
nodemcu连接bilnker实现湿温度上传,实时时钟天气显示。采用u8g2库显示
/* *****************************************************************
Download latest Blinker library here:
https://github.com/blinker-iot/blinker-library/archive/master.zip
Blinker is a cross-hardware, cross-platform solution for the IoT.
It provides APP, device and server support,
and uses public cloud services for data transmission and storage.
It can be used in smart home, data monitoring and other fields
to help users build Internet of Things projects better and faster.
Make sure installed 2.5.0 or later ESP8266/Arduino package,
if use ESP8266 with Blinker.
https://github.com/esp8266/Arduino/releases
Make sure installed 1.0.2 or later ESP32/Arduino package,
if use ESP32 with Blinker.
https://github.com/espressif/arduino-esp32/releases
Docs: https://diandeng.tech/doc
https://github.com/blinker-iot/blinker-doc/wiki
* *****************************************************************
Blinker 库下载地址:
https://github.com/blinker-iot/blinker-library/archive/master.zip
Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
如果使用 ESP8266 接入 Blinker,
请确保安装了 2.5.0 或更新的 ESP8266/Arduino 支持包。
https://github.com/esp8266/Arduino/releases
如果使用 ESP32 接入 Blinker,
请确保安装了 1.0.2 或更新的 ESP32/Arduino 支持包。
https://github.com/espressif/arduino-esp32/releases
文档: https://diandeng.tech/doc
https://github.com/blinker-iot/blinker-doc/wiki
* *****************************************************************/
#define BLINKER_WIFI
#include <U8g2lib.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <Blinker.h>
// Download Adafruit DHT-sensor-library library here:
// https://github.com/adafruit/DHT-sensor-library
#include <DHT.h>
int count = 0;
bool WIFI_Status = true;
char auth[] = "你的设备";
#define DHTPIN D5//nodemcu D5对应esp8266GPIO14
#define BUTTON_1 "ButtonKey"
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerNumber HOUR("hour");
BlinkerNumber MIN("min");
//BlinkerNumber SEC("sec");
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, /* clock=*/ 5, /* data=*/ 4, /* reset=*/ U8X8_PIN_NONE); // 对应的都是esp8266的GPIO
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);//使用wifi的STA模式
Serial.println("\r\nWait for Smartconfig...");//串口打印
WiFi.beginSmartConfig();//等待手机端发出的名称与密码
//死循环,等待获取到wifi名称和密码
while (1)
{
//等待过程中一秒打印一个.
Serial.print(".");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(1000);
if (WiFi.smartConfigDone())//获取到之后退出等待
{
Serial.println("SmartConfig Success");
//打印获取到的wifi名称和密码
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;
//break;
}
else
{
Serial.print("AutoConfig Waiting......");
Serial.println(wstatus);
delay(1000);
}
}
Serial.println("AutoConfig Faild!" );
return false;
//WiFi.printDiag(Serial);
}
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
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);
// SEC.print(sec);
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);
// strcpy(m_str, u8x8_u8toa(hour, 2));
// u8g2.drawStr(30, 15, m_str);
u8g2.drawStr(55, 15, ":");
// strcpy(m_str, u8x8_u8toa(min, 2));
// u8g2.drawStr(65, 15, m_str);
u8g2.setCursor(65, 15);
u8g2.print(min);
// itoa(year, m_str, 10); //itoa()
// u8g2.drawStr(20, 30, m_str);
u8g2.setCursor(20, 30);
u8g2.print(year);
u8g2.drawStr(55, 30, "-");
// strcpy(m_str, u8x8_u8toa(month, 2));
// u8g2.drawStr(65, 30, m_str);
u8g2.setCursor(65, 30);
u8g2.print(month);
u8g2.drawStr(90, 30, "-");
// strcpy(m_str, u8x8_u8toa(month, 2));
// u8g2.drawStr(100, 30, m_str);
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)) //每一小时获取一次天气信息,参考https://diandeng.tech/doc/weather
{
Blinker.weather("hebei-pingshanxian");//默认查询设备ip所属地区的当前时刻的天气情况
first_weadher = false ; //开机第一次更新默认查询设备ip所属地区的当前时刻的天气情况
}
}
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);
}
}
本文地址:https://blog.csdn.net/weixin_46606318/article/details/108164285
上一篇: PyCharm中设置字体大小
推荐阅读
-
iOS中UIScrollView嵌套UITableView的实践教程
-
谈谈我是如何学习SQLSERVER的
-
小弟我想实现又可以让用户输入,又可以选择,现在一般是如何做的
-
给小弟我的个人网站的风格提点意见
-
浏览google用什么代理好呢?小弟我用在线的发现浏览出来很多功能都不全
-
我要点爆”微信小程序云开发之项目建立与我的页面功能实现
-
是什么浪费了我的上网时间? 博客分类: 程序人生 新浪微博浏览器能源Chrome读书
-
记我的第一次spring aop项目实践 博客分类: Java aopspringaspectjspring aop日志记录
-
curl真的没有对采集内容进行字节限制的设置么?例如小弟我只想要网页的header部分的keyword,全部都采集过来的话太浪费了
-
是什么浪费了我的上网时间? 博客分类: 程序人生 新浪微博浏览器能源Chrome读书