|
爱科技、爱创意、爱折腾、爱极致,我们都是技术控
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 胡奚曷 于 2021-11-12 18:20 编辑
1.前言
鉴于空气果AirNutFun没有热度了,这里特地开源一下软件和硬件希望能帮助有需要的人,主要是害怕商家涨价,抱歉没有及时开源
接上个帖子:10块包邮的墨迹空气果fun换主板,ESP8266接入blinker APP,联网对时显示天气
https://www.mydigit.cn/thread-280524-1-1.html
(出处: 数码之家)
2.功能
1.实现时间显示,基于DS3231,7天对时一次,单击按键对时,上电对时,时区为东八区 2.实现天气显示,每2小时更新一次 3.实现自带触摸功能,单击显示日期3s 4.双击按键启用、关闭wifi 5.可以显示WiFi强度 6.背光亮度0~100调整 7.定时采集PM2.5,0~100分钟可调 8.采集PM2.5时ogo闪烁 9.app上可以显示温度、湿度、PM2.5和曲线,可以设定亮度和PM2.5定时 10.长按3s可以重新配网,配网使用web 11.支持web OTA
3.代码文件结构 4.代码 AirNutFun.ino - #include "Blwifi.h"
- #include "Task.h"
- #include "Display.h"
- #include "SHT20.h"
- void setup()
- {
- Serial.begin(9600);
- Display_Init();
- Blwifi_Init();
- Task_Creat(500, Display_DateTime_Task500ms);
- Task_Creat(100, Blwifi_Ds3231_Task100ms);
- Task_Creat(100, SHT20_Task);
- }
- void loop()
- {
- Blwifi_Loop();
- Task_Loop();
- Display_Loop();
- }
复制代码ANF_Lcd.c - /**
- * @file ANF_Lcd.C
- * [url=home.php?mod=space&uid=650075]@brief[/url] Airnut fun LCD Display User Interface
- * [url=home.php?mod=space&uid=2115719]@author[/url] Hu Xihe
- * [url=home.php?mod=space&uid=1360894]@copyright[/url] All rights reserved.
- */
- #include "ANF_Lcd.h"
- /**
- * @brief LCD display RAM
- */
- unsigned char LCD_Ram[16] =
- {
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
- /**
- * @brief LCD Temperature 8-Seg.
- * @param[in] addr: slave address
- * data: as byte
- * @param[out] none.
- * @return none.
- */
- static void IIC_WriteByte(unsigned char addr, unsigned char data)
- {
- unsigned char buffer[1];
- buffer[0] = data;
- IIC_WriteData(addr, buffer, 1);
- }
- /**
- * @brief LCD Temperature 8-Seg.
- * @param[in] ind1: index of ram1
- * ind2: index of ram2
- * num: value 0~9, 10 = ' ', 11 = '-'
- * @param[out] none.
- * @return none.
- */
- static void Temp_Seg8(unsigned char ind1, unsigned char ind2, unsigned char num)
- {
- const unsigned char seg1[]=
- {
- 0x0F, 0x00, 0x0D, 0x09, 0x02, 0x0B, 0x0F, 0x01, 0x0F,0x0B
- };
- const unsigned char seg2[]=
- {
- 0x50, 0x50, 0x30, 0x70, 0x70, 0x60, 0x60, 0x50, 0x70, 0x70
- };
-
- if(ind1 < 16 && ind2 < 16)
- {
- LCD_Ram[ind1] &= ~(seg1[8]);
- LCD_Ram[ind2] &= ~(seg2[8]);
-
- if(num < 10)
- {
- LCD_Ram[ind1] |= seg1[num];
- LCD_Ram[ind2] |= seg2[num];
- }
- else if(num == 11) // display '-'
- {
- LCD_Ram[ind2] = 0x20;
- }
- }
- }
- /**
- * @brief Pm25 and Humidity 8-Seg.
- * @param[in] ind1: index of ram1
- * ind2: index of ram2
- * num: value 0~9, 10 = ' '
- * @param[out] none.
- * @return none.
- */
- static void Pm25_Seg8(unsigned char ind1, unsigned char ind2, unsigned char num)
- {
- const unsigned char seg1[]=
- {
- 0xF0, 0x00, 0xD0, 0x90, 0x20, 0xB0, 0xF0, 0x10, 0xF0, 0xB0
- };
- const unsigned char seg2[]=
- {
- 0x05, 0x05, 0x03, 0x07, 0x07, 0x06, 0x06, 0x05, 0x07, 0x07
- };
-
- if(ind1 < 16 && ind2 < 16)
- {
- LCD_Ram[ind1] &= ~(seg1[8]);
- LCD_Ram[ind2] &= ~(seg2[8]);
-
- if(num < 10)
- {
- LCD_Ram[ind1] |= seg1[num];
- LCD_Ram[ind2] |= seg2[num];
- }
- }
- }
- /**
- * @brief time hour 8-Seg.
- * @param[in] ind1: index of ram1
- * ind2: index of ram2
- * num: value 0~9, 10 = ' '
- * @param[out] none.
- * @return none.
- */
- static void TimeHour_Seg8H(unsigned char ind1, unsigned char ind2, unsigned char num)
- {
- const unsigned char seg1[]=
- {
- 0xF0, 0x00, 0xB0, 0x90, 0x40, 0xD0, 0xF0, 0x80, 0xF0, 0xD0
- };
- const unsigned char seg2[]=
- {
- 0x05, 0x05, 0x06, 0x07, 0x07, 0x03, 0x03, 0x05, 0x07, 0x07
- };
-
- if(ind1 < 16 && ind2 < 16)
- {
- LCD_Ram[ind1] &= ~(seg1[8]);
- LCD_Ram[ind2] &= ~(seg2[8]);
-
- if(num < 10)
- {
- LCD_Ram[ind1] |= seg1[num];
- LCD_Ram[ind2] |= seg2[num];
- }
- }
- }
- /**
- * @brief time hour 8-Seg.
- * @param[in] ind1: index of ram1
- * ind2: index of ram2
- * num: value 0~9, 10 = ' '
- * @param[out] none.
- * @return none.
- */
- static void TimeHour_Seg8L(unsigned char ind1, unsigned char ind2, unsigned char num)
- {
- const unsigned char seg1[]=
- {
- 0xF0, 0x00, 0xB0, 0x90, 0x40, 0xD0, 0xF0, 0x80, 0xF0, 0xD0
- };
- const unsigned char seg2[]=
- {
- 0x50, 0x50, 0x60, 0x70, 0x70, 0x30, 0x30, 0x50, 0x70, 0x70
- };
-
- if(ind1 < 16 && ind2 < 16)
- {
- LCD_Ram[ind1] &= ~(seg1[8]);
- LCD_Ram[ind2] &= ~(seg2[8]);
-
- if(num < 10)
- {
- LCD_Ram[ind1] |= seg1[num];
- LCD_Ram[ind2] |= seg2[num];
- }
- }
- }
- /**
- * @brief time minute 8-Seg.
- * @param[in] ind1: index of ram1
- * ind2: index of ram2
- * num: value 0~9, 10 = ' '
- * @param[out] none.
- * @return none.
- */
- static void TimeMinute_Seg8(unsigned char ind1, unsigned char ind2, unsigned char num)
- {
- const unsigned char seg1[]=
- {
- 0x0F, 0x00, 0x0B, 0x09, 0x04, 0x0D, 0x0F, 0x08, 0x0F, 0x0D
- };
- const unsigned char seg2[]=
- {
- 0x50, 0x50, 0x60, 0x70, 0x70, 0x30, 0x30, 0x50, 0x70, 0x70
- };
-
- if(ind1 < 16 && ind2 < 16)
- {
- LCD_Ram[ind1] &= ~(seg1[8]);
- LCD_Ram[ind2] &= ~(seg2[8]);
-
- if(num < 10)
- {
- LCD_Ram[ind1] |= seg1[num];
- LCD_Ram[ind2] |= seg2[num];
- }
- }
- }
- /**
- * @brief LCD airnut logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdAirNutLogo(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[3] &= 0xF7;
- else
- LCD_Ram[3] |= 0x08;
- }
- /**
- * @brief LCD wifi logo set.
- * @param[in] num as WIFI_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdWifi(WIFI_DEF num)
- {
- unsigned char data[WIFI_COUNT] =
- {
- 0x00,0x80,0xC0,0xE0,0xF0
- };
-
- if(num < WIFI_COUNT)
- {
- LCD_Ram[1] &= 0x0F;
- LCD_Ram[1] |= data[num];
- }
- }
- /**
- * @brief LCD separate line set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdSeparateLine(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[4] &= 0xF7;
- else
- LCD_Ram[4] |= 0x08;
- }
- /**
- * @brief LCD battery set.
- * @param[in] num as BATTERY_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdBattery(BATTERY_DEF num)
- {
- unsigned char data[BAT_COUNT] =
- {
- 0x00,0x08,0x09,0x0B,0x0F
- };
-
- if(num < BAT_COUNT)
- {
- LCD_Ram[1] &= 0xF0;
- LCD_Ram[1] |= data[num];
- }
- }
- /**
- * @brief LCD Time point set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTimePoint(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[15] &= 0x7F;
- else
- LCD_Ram[15] |= 0x80;
- }
- /**
- * @brief LCD Time set.
- * @param[in] hour, minute.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTimeNumber(unsigned char hour, unsigned char minute)
- {
- if(hour > 99)
- hour = 99;
- #if 0
- if(hour < 10)
- {
- TimeHour_Seg8H(2, 3, 10);
- TimeHour_Seg8L(3, 15, (unsigned char)hour);
- }
- else
- #endif
- {
- TimeHour_Seg8H(2, 3, (unsigned char)(hour / 10));
- TimeHour_Seg8L(3, 15, (unsigned char)(hour % 10));
- }
- if(minute > 99)
- minute = 99;
- #if 0
- if(minute < 10)
- {
- TimeMinute_Seg8(15, 14, 10);
- TimeMinute_Seg8(14, 13, (unsigned char)minute);
- }
- else
- #endif
- {
- TimeMinute_Seg8(15, 14, (unsigned char)(minute / 10));
- TimeMinute_Seg8(14, 13, (unsigned char)(minute % 10));
- }
- }
- /**
- * @brief LCD PM2.5 Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Logo(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[10] &= 0xF7;
- else
- LCD_Ram[10] |= 0x08;
- }
- /**
- * @brief LCD PM2.5 number set.
- * @param[in] num:value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Number(short num)
- {
- short tmp;
- if(0 <= num)
- {
- if(num > 999)
- num = 999;
- if(num < 10)
- {
- Pm25_Seg8(8, 9, 10);
- Pm25_Seg8(9, 10, 10);
- Pm25_Seg8(10, 11, (unsigned char)num);
- }
- else if(num < 100)
- {
- Pm25_Seg8(8, 9, 10);
- Pm25_Seg8(9, 10, (unsigned char)(num / 10));
- Pm25_Seg8(10, 11, (unsigned char)(num % 10));
- }
- else
- {
- tmp = num;
- Pm25_Seg8(8, 9, (unsigned char)(tmp / 100));
- tmp %= 100;
- Pm25_Seg8(9, 10, (unsigned char)(tmp / 10));
- Pm25_Seg8(10, 11, (unsigned char)(tmp % 10));
- }
- }
- }
- /**
- * @brief LCD PM2.5 unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Unit(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[11] &= 0xF7;
- else
- LCD_Ram[11] |= 0x08;
- }
- /**
- * @brief LCD Temperature Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempLogo(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[9] &= 0xF7;
- else
- LCD_Ram[9] |= 0x08;
- }
- /**
- * @brief LCD Temperature number set.
- * @param[in] num:value 10 factor of real value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempNumber(short num)
- {
- short tmp;
- if(num < 0)
- {
- Temp_Seg8(8, 7, 11);
-
- num = 0 - num;
- if(num < 100)
- {
- Temp_Seg8(7, 6, (unsigned char)(num / 10));
- Temp_Seg8(6, 5, (unsigned char)(num % 10));
- LCD_Ram[6] |= 0x80;
- }
- else
- {
- tmp = num / 10;
- Temp_Seg8(7, 6, (unsigned char)(tmp / 10));
- Temp_Seg8(6, 5, (unsigned char)(tmp % 10));
- LCD_Ram[6] &= 0x7F;
- }
- }
- else
- {
- if(num < 100)
- {
- Temp_Seg8(8, 7, 10);
- Temp_Seg8(7, 6, (unsigned char)num / 10);
- Temp_Seg8(6, 5, (unsigned char)num % 10);
- LCD_Ram[6] |= 0x80;
- }
- else if(num < 1000)
- {
- tmp = num;
- Temp_Seg8(8, 7, (unsigned char)(tmp / 100));
- tmp %= 100;
- Temp_Seg8(7, 6, (unsigned char)(tmp / 10));
- Temp_Seg8(6, 5, (unsigned char)(tmp % 10));
- LCD_Ram[6] |= 0x80;
- }
- else
- {
- tmp = num / 10;
- Temp_Seg8(8, 7, (unsigned char)(tmp / 100));
- tmp %= 100;
- Temp_Seg8(7, 6, (unsigned char)(tmp / 10));
- Temp_Seg8(6, 5, (unsigned char)(tmp % 10));
- LCD_Ram[6] &= 0x7F;
- }
- }
- }
- /**
- * @brief LCD Temperature unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempUnit(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[5] &= 0x7F;
- else
- LCD_Ram[5] |= 0x80;
- }
- /**
- * @brief LCD Humidity Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityLogo(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[12] &= 0xF7;
- else
- LCD_Ram[12] |= 0x08;
- }
- /**
- * @brief LCD Humidity number set.
- * @param[in] num:value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityNumber(short num)
- {
- if(0 <= num)
- {
- if(num > 99)
- num = 99;
- if(num < 10)
- {
- Pm25_Seg8(11, 12, 10);
- Pm25_Seg8(12, 13, (unsigned char)num);
- }
- else
- {
- Pm25_Seg8(11, 12, (unsigned char)(num / 10));
- Pm25_Seg8(12, 13, (unsigned char)(num % 10));
- }
- }
- }
- /**
- * @brief LCD Humidity unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityUnit(unsigned char st)
- {
- if(st == 0)
- LCD_Ram[13] &= 0xF7;
- else
- LCD_Ram[13] |= 0x08;
- }
- /**
- * @brief LCD Weather set.
- * @param[in] WEATHER_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdWeather(WEATHER_DEF st)
- {
- if(st & WTHR_Sunny)
- LCD_Ram[2] |= 0x08;
- else
- LCD_Ram[2] &= 0xF7;
-
- if(st & WTHR_Cloudy)
- LCD_Ram[2] |= 0x04;
- else
- LCD_Ram[2] &= 0xFB;
-
- if(st & WTHR_Overcast)
- LCD_Ram[2] |= 0x02;
- else
- LCD_Ram[2] &= 0xFD;
-
- if(st & WTHR_LightRain)
- LCD_Ram[2] |= 0x01;
- else
- LCD_Ram[2] &= 0xFE;
-
- if(st & WTHR_ModerateRain)
- LCD_Ram[4] |= 0x02;
- else
- LCD_Ram[4] &= 0xFD;
-
- if(st & WTHR_HeavyRain)
- LCD_Ram[13] |= 0x80;
- else
- LCD_Ram[13] &= 0x7F;
-
- if(st & WTHR_Haze)
- LCD_Ram[14] |= 0x80;
- else
- LCD_Ram[14] &= 0x7F;
-
- if(st & WTHR_Foggy)
- LCD_Ram[4] |= 0x01;
- else
- LCD_Ram[4] &= 0xFE;
- }
- /**
- * @brief LCD Init.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdInit(void)
- {
- IIC_WriteByte(0x7E,0x70);
- IIC_WriteByte(0x7E,0xC0);
- ANF_LcdClear();
- ANF_LcdUpdate();
- IIC_WriteByte(0x7E,0x8C);
- }
- /**
- * @brief LCD update, IIC send data.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdUpdate(void)
- {
- IIC_WriteData(0x7E, LCD_Ram, 16);
- }
- /**
- * @brief LCD clear.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdClear(void)
- {
- int i;
- for(i=0; i<16; i++)
- LCD_Ram[i] = 0;
- }
- /**
- * @brief LCD all display.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdAll(void)
- {
- int i;
- for(i=1; i<16; i++)
- LCD_Ram[i] = 0xFF;
- }
复制代码ANF_Lcd.h - /**
- * @file ANF_Lcd.h
- * @brief Airnut fun LCD Display User Interface
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
-
- #ifndef __ANF_LCD_H__
- #define __ANF_LCD_H__
- #ifdef __cplusplus
- extern "C"{
- #endif
- /**
- * @brief status type for weather
- */
- typedef enum
- {
- WTHR_None = 0x00,
- WTHR_Sunny = 0x01,
- WTHR_Cloudy = 0x02,
- WTHR_Overcast = 0x04,
- WTHR_LightRain = 0x08,
- WTHR_ModerateRain = 0x10,
- WTHR_HeavyRain = 0x20,
- WTHR_Haze = 0x40,
- WTHR_Foggy = 0x80,
- WTHR_All = 0xFF
- }WEATHER_DEF;
- /**
- * @brief status type for battery
- */
- typedef enum
- {
- BAT_None = 0x00,
- BAT_Zero = 0x01,
- BAT_Low = 0x02,
- BAT_Middle = 0x03,
- BAT_High = 0x04,
- BAT_COUNT
- }BATTERY_DEF;
- /**
- * @brief status type for WIFI
- */
- typedef enum
- {
- WIFI_None = 0x00,
- WIFI_Low = 0x01,
- WIFI_Middle = 0x02,
- WIFI_High = 0x03,
- WIFI_Full = 0x04,
- WIFI_COUNT
- }WIFI_DEF;
- /**
- * @brief IIC api
- * @param[in] addr as slave address.
- * data[] as set value
- * length as data length
- * @param[out] none.
- * @return none.
- */
- extern void IIC_WriteData(unsigned char addr, unsigned char data[], unsigned char length);
- /**
- * @brief LCD airnut logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdAirNutLogo(unsigned char st);
- /**
- * @brief LCD wifi logo set.
- * @param[in] num as WIFI_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdWifi(WIFI_DEF num);
- /**
- * @brief LCD separate line set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdSeparateLine(unsigned char st);
- /**
- * @brief LCD battery set.
- * @param[in] BATTERY_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdBattery(BATTERY_DEF num);
- /**
- * @brief LCD Time point set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTimePoint(unsigned char st);
- /**
- * @brief LCD Time set.
- * @param[in] hour, minute.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTimeNumber(unsigned char hour, unsigned char minute);
- /**
- * @brief LCD PM2.5 Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Logo(unsigned char st);
- /**
- * @brief LCD PM2.5 number set.
- * @param[in] num:value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Number(short num);
- /**
- * @brief LCD PM2.5 unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdPm25Unit(unsigned char st);
- /**
- * @brief LCD Temperature Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempLogo(unsigned char st);
- /**
- * @brief LCD Temperature number set.
- * @param[in] num:value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempNumber(short num);
- /**
- * @brief LCD Temperature unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdTempUnit(unsigned char st);
- /**
- * @brief LCD Humidity Logo set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityLogo(unsigned char st);
- /**
- * @brief LCD Humidity number set.
- * @param[in] num:value
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityNumber(short num);
- /**
- * @brief LCD Humidity unit set.
- * @param[in] st:0-NotDisplay 1-Display.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdHumidityUnit(unsigned char st);
- /**
- * @brief LCD Weather set.
- * @param[in] WEATHER_DEF.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdWeather(WEATHER_DEF st);
- /**
- * @brief LCD Init.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdInit(void);
- /**
- * @brief LCD update, IIC send data.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdUpdate(void);
- /**
- * @brief LCD clear.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdClear(void);
- /**
- * @brief LCD all display.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void ANF_LcdAll(void);
- #ifdef __cplusplus
- }
-
- #endif
- #endif /*end of __ANF_LCD_H__*/
复制代码Blwifi.cpp - /**
- * @file Blwifi.cpp
- * @brief wifi config
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
- #define BLINKER_WIFI
- #define BLINKER_MIOT_OUTLET
- #include <WiFiManager.h>
- #include <Blinker.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <EEPROM.h>
- #include "GpioButton.h"
- #include "Blwifi.h"
- #include "Display.h"
- #include "DS3231SN.h"
- #include "ANF_Lcd.h"
- WiFiManager wifiManager;
- bool shouldSaveConfig=false;
- Settings wifiSettings;
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- const char* host = "AirNutFunUpdate";
- // 定义按钮对象,指定按钮的GPIO口
- GpioButton myButton(RESET_WIFI_PIN);
- uint8_t setDs3231 = 0;
- /**
- * @brief set ds3231 by net time
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static uint8_t Set_Ds3231(void)
- {
- uint8_t ret;
- if(0<= Blinker.hour() && Blinker.hour() < 24 && wifiSettings.UseWifi == WIFI_CONNECT)
- {
- Ds3231_SetDate(Blinker.year()%100, Blinker.month(), Blinker.mday());
- Ds3231_SetTime(Blinker.hour(), Blinker.minute(), Blinker.second());
- ret = 1;
- }
- else
- ret = 0;
- return ret;
- }
- /**
- * @brief to reset wifi information.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void Wifi_Reset(void)
- {
- Display_WifiFlash(3);
- EEPROM.begin(4096);
- EEPROM.get<Settings>(2448, wifiSettings);
- wifiSettings.auth_key[0]='\0';
- EEPROM.put<Settings>(2448, wifiSettings);
- if (EEPROM.commit())
- {
- Serial.println(F("EEPROM successfully committed"));
- }
- else
- {
- Serial.println(F("ERROR! EEPROM commit failed"));
- }
- EEPROM.end();
- wifiManager.resetSettings();
- ESP.restart();
- }
- /**
- * @brief to set wifi saved infor.
- * @param[in] st as WIFI_SET_ST.
- * @param[out] none.
- * @return none.
- */
- static void Wifi_OnOffSet(WIFI_SET_ST st)
- {
- EEPROM.begin(4096);
- EEPROM.get<Settings>(2448, wifiSettings);
- wifiSettings.UseWifi = st;
- EEPROM.put<Settings>(2448, wifiSettings);
- if (EEPROM.commit())
- {
- Serial.println(F("EEPROM successfully committed"));
- }
- else
- {
- Serial.println(F("ERROR! EEPROM commit failed"));
- }
- EEPROM.end();
- }
- /**
- * @brief my button init.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void MyButton_Init()
- {
- // 绑定按钮事件处理
- myButton.BindBtnPress([](){
- Serial.println(F("Time sync."));
- Set_Ds3231();
- });
- // 绑定按钮事件处理
- myButton.BindBtnDblPress([](){
- if(wifiSettings.UseWifi != WIFI_ST_OFF)
- {
- Serial.println(F("WiFi Off."));
- Wifi_OnOffSet(WIFI_ST_OFF);
- ESP.restart();
- }
- else
- {
- Serial.println(F("WiFi On."));
- Wifi_OnOffSet(WIFI_WAIT_CONFIG);
- ESP.restart();
- }
- });
- // 绑定长按事件处理(长按判定为3000ms)
- myButton.BindBtnLongPress([]()
- {
- Serial.println(F("WiFi resetSettings."));
- Wifi_Reset();
- }, 3000);
- }
- /**
- * @brief wifi save call back.
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void saveConfigCallback(void)
- {
- Serial.println("Should save config");
- shouldSaveConfig = true;
- }
- /**
- * @brief to check the author key.
- * @param[in] key as char*
- * len as int.
- * @param[out] none.
- * @return none.
- */
- static bool chkAuthkey(char* key, int len)
- {
- if (len != 12) return false;
- for (int i=0;key[i]!=0;i++){
- if (!isxdigit(key[i])) return false;
- }
- return true;
- }
- /**
- * @brief wifi config
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void Blwifi_Config(void)
- {
- EEPROM.begin(4096);
- if( wifiSettings.auth_key[0]=='\0'||wifiSettings.auth_key[0]==0xFF)
- {
- Display_WaitWifi();
- WiFi.mode(WIFI_STA);
- //wifiManager.setDebugOutput(true);
-
- wifiManager.resetSettings();
- wifiManager.setAPStaticIPConfig(IPAddress(192,168,10,1), IPAddress(192,168,10,1), IPAddress(255, 255, 255, 0));
- // 3分钟配网时间,如没有完成则退出配网.
- // 例如原正常连接的WIFI路由掉线死机或不通电等情况, 通过配网超时后, 会重新进行连接原WIFI信号。 避免停在配网模式下等待
- wifiManager.setConfigPortalTimeout(180);
- //wifiManager.setConnectTimeout(240);
- // 设置点击保存的回调
- wifiManager.setSaveConfigCallback(saveConfigCallback);
- WiFiManagerParameter custom_authkey("auth_key", "Authkey", wifiSettings.auth_key, 12);
- wifiManager.addParameter(&custom_authkey);
- //AP名称:ESP_AP 密码:12345678
- if(!wifiManager.autoConnect("ESP_AP","12345678"))
- {
- Serial.println(F("Failed to connect. Reset and try again. . ."));
- ESP.restart();
- delay(5000);
- }
- Serial.println(F("Connected to Wifi."));
- Serial.print(F("My IP:"));
- Serial.println(WiFi.localIP());
- // 保存自定义信息
- if (shouldSaveConfig)
- {
- Serial.println(F("saving config..."));
- //Serial.println(custom_authkey.getValue());
- strncpy(wifiSettings.auth_key, custom_authkey.getValue(), 12);
- wifiSettings.auth_key[12] = '\0';
- strcpy(wifiSettings.ssid, wifiManager.getWiFiSSID().c_str());
- wifiSettings.ssid[wifiManager.getWiFiSSID().length()]='\0';
-
- strcpy(wifiSettings.pswd, wifiManager.getWiFiPass().c_str());
- wifiSettings.pswd[wifiManager.getWiFiPass().length()]='\0';
-
- if (!chkAuthkey(wifiSettings.auth_key, strlen(wifiSettings.auth_key)))
- {
- Serial.println(F("Authkey is wrong."));
- Wifi_Reset();
- delay(5000);
- }
- EEPROM.put<Settings>(2448, wifiSettings);
- if (EEPROM.commit())
- {
- Serial.println(F("EEPROM successfully committed"));
- EEPROM.end();
- ESP.restart();
- }
- else
- {
- Serial.println(F("ERROR! EEPROM commit failed"));
- }
- }
- }
- EEPROM.end();
- wifiSettings.auth_key[12] = '\0';
- }
- BlinkerNumber Temperature("num-tmp");
- BlinkerNumber Humidity("num-hum");
- BlinkerNumber PM25Number("num-pm25");
- BlinkerText IpText("txt-ip");
- BlinkerSlider BackLight("ran-light");
- BlinkerSlider Pm25UpdatePreiod("ran-pm25");
- #define WEATHER_UPDATE 2*60*60*10 /* unit 100ms */
- uint32_t weatherTime = 10*10; /* unit 100ms */
- void BackLight_Callback(int32_t value)
- {
- wifiSettings.backlight = value;
- EEPROM.begin(4096);
-
- EEPROM.put<Settings>(2448, wifiSettings);
- if (EEPROM.commit())
- {
- }
- EEPROM.end();
- Display_SetBacklight(wifiSettings.backlight);
- BLINKER_LOG("back light set: ", wifiSettings.backlight);
- }
- void Pm25UpdatePreiod_Callback(int32_t value)
- {
- wifiSettings.pm25period = value;
- EEPROM.begin(4096);
-
- EEPROM.put<Settings>(2448, wifiSettings);
- if (EEPROM.commit())
- {
- }
- EEPROM.end();
- BLINKER_LOG("pm2.5 update period set: ", wifiSettings.pm25period);
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
-
- uint32_t BlinkerTime = millis();
-
- Blinker.print("millis", BlinkerTime);
- }
- void heartbeat()
- {
- Temperature.print(Dis_Temperature);
- Humidity.print(Dis_Humidity);
- PM25Number.print(Dis_Pm25);
- IpText.print("http://" + WiFi.localIP().toString() + "/update");
- BLINKER_LOG("heart beat temperature: ", Dis_Temperature);
- BLINKER_LOG("heart beat humidity: ", Dis_Humidity);
- BackLight.print(wifiSettings.backlight);
- Pm25UpdatePreiod.print(wifiSettings.pm25period);
- }
- void dataStorage()
- {
- Blinker.dataStorage("temp", Dis_Temperature);
- Blinker.dataStorage("humi", Dis_Humidity);
- Blinker.dataStorage("pm25", Dis_Pm25);
- BLINKER_LOG("data Storage temperature: ", Dis_Humidity);
- BLINKER_LOG("data Storage humidity: ", Dis_Humidity);
- BLINKER_LOG("data Storage pm25: ", Dis_Pm25);
- if(weatherTime == 0)
- {
- weatherTime = WEATHER_UPDATE;
- Blinker.weather();
- }
- }
- void weatherData(const String & data)
- {
- BLINKER_LOG("weather: ", data);
- DynamicJsonDocument jsonBuffer(1024);
- DeserializationError error = deserializeJson(jsonBuffer, data);
- JsonObject wth = jsonBuffer.as<JsonObject>();
- Display_Weather(wth["weather"]);
- }
- void miotQuery(int32_t queryCode)
- {
- BLINKER_LOG("MIOT Query codes: ", queryCode);
- switch (queryCode)
- {
- case BLINKER_CMD_QUERY_ALL_NUMBER :
- BLINKER_LOG("MIOT Query All");
- BlinkerMIOT.temp(Dis_Temperature);
- BlinkerMIOT.humi(Dis_Humidity);
- BlinkerMIOT.pm25(Dis_Pm25);
- BlinkerMIOT.print();
- break;
- default :
- BlinkerMIOT.temp(Dis_Temperature);
- BlinkerMIOT.humi(Dis_Humidity);
- BlinkerMIOT.pm25(Dis_Pm25);
- BlinkerMIOT.print();
- break;
- }
- }
- /**
- * @brief wifi init, read eeprom
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Init(void)
- {
- MyButton_Init();
-
- EEPROM.begin(4096);
- EEPROM.get<Settings>(2448, wifiSettings);
- if(wifiSettings.UseWifi != WIFI_ST_OFF)
- wifiSettings.UseWifi = WIFI_WAIT_CONFIG;
- else
- ANF_LcdWifi(WIFI_None);
- EEPROM.end();
- Display_SetBacklight(wifiSettings.backlight);
- }
- /**
- * @brief task every 100ms
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Ds3231_Task100ms(void)
- {
- static uint32_t syncTime = 7*24*3600*10;
- if(setDs3231 == 0)
- {
- setDs3231 = Set_Ds3231();
- }
- if(weatherTime > 0)
- {
- weatherTime--;
- }
- if(syncTime > 0)
- {
- syncTime--;
- }
- else
- {
- Set_Ds3231();
- syncTime = 7*24*3600*10;
- }
- }
- /**
- * @brief wifi loop
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Loop(void)
- {
- switch(wifiSettings.UseWifi)
- {
- case WIFI_ST_OFF:
- break;
- case WIFI_WAIT_CONFIG:
- Blwifi_Config();
- wifiSettings.UseWifi = WIFI_WAIT_CONNECT;
- break;
- case WIFI_WAIT_CONNECT:
- BLINKER_DEBUG.stream(Serial);
- Blinker.setTimezone(8.0);
- Blinker.begin(wifiSettings.auth_key, wifiSettings.ssid, wifiSettings.pswd);
- Blinker.attachData(dataRead);
- Blinker.attachHeartbeat(heartbeat);
- Blinker.attachWeather(weatherData);
- Blinker.attachDataStorage(dataStorage);
- BackLight.attach(BackLight_Callback);
- Pm25UpdatePreiod.attach(Pm25UpdatePreiod_Callback);
- BlinkerMIOT.attachQuery(miotQuery);
-
- MDNS.begin(host);
- httpUpdater.setup(&httpServer);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- wifiSettings.UseWifi = WIFI_CONNECT;
- break;
- case WIFI_CONNECT:
- Blinker.run();
- httpServer.handleClient();
- break;
- }
- myButton.loop();
- }
复制代码Blwifi.h - /**
- * @file Blwifi.h
- * @brief wifi config
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
- #ifndef __BLWIFI_H__
- #define __BLWIFI_H__
- #define RESET_WIFI_PIN 14
- typedef enum
- {
- WIFI_ST_OFF,
- WIFI_WAIT_CONFIG,
- WIFI_WAIT_CONNECT,
- WIFI_CONNECT
- }WIFI_SET_ST;
- struct Settings
- {
- char auth_key[13];
- char ssid[128];
- char pswd[32];
- WIFI_SET_ST UseWifi;
- int backlight;
- int pm25period;
- };
- extern Settings wifiSettings;
- /**
- * @brief wifi init, read eeprom
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Init(void);
- /**
- * @brief task every 100ms
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Ds3231_Task100ms(void);
- /**
- * @brief wifi loop
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Blwifi_Loop(void);
- #endif /*end of __BLWIFI_H__*/
复制代码DS3231SN.c - /**
- * @file DS3231SN.c
- * @brief DS3231 RTC
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
-
- #include "DS3231SN.h"
- #include "IIC.h"
- /**
- * @brief ds3231 write command
- * @param[in] addr as ds3231 register address
- * dat as ds3231 command.
- * @param[out] none.
- * @return none.
- */
- static void DS3231_WriteByte(uint8_t addr,uint8_t dat)
- {
- IIC_Start();
- IIC_Send_Byte(DS3231_WriteAddress);
- (void)IIC_Wait_Ack();
- IIC_Send_Byte(addr);
- (void)IIC_Wait_Ack();
- IIC_Send_Byte(dat);
- (void)IIC_Wait_Ack();
- IIC_Stop();
- }
- /**
- * @brief ds3231 read current data
- * @param[in] none.
- * @param[out] none.
- * @return as uint8_t .
- */
- static uint8_t DS3231_ReadCurrent(void)
- {
- uint8_t read_dat;
- IIC_Start();
- IIC_Send_Byte(DS3231_ReadAddress);
- (void)IIC_Wait_Ack();
- read_dat=IIC_Read_Byte(1);
- IIC_Stop();
- return read_dat;
- }
- /**
- * @brief ds3231 read Random data
- * @param[in] random_addr as uint8_t.
- * @param[out] none.
- * @return as uint8_t .
- */
- static uint8_t DS3231_ReadRandom(uint8_t random_addr)
- {
- IIC_Start();
- IIC_Send_Byte(DS3231_WriteAddress);
- (void)IIC_Wait_Ack();
- IIC_Send_Byte(random_addr);
- (void)IIC_Wait_Ack();
- return (DS3231_ReadCurrent());
- }
- /**
- * @brief BCD to byte
- * @param[in] bcd as uint8_t.
- * @param[out] none.
- * @return as uint8_t .
- */
- static uint8_t BCD2Byte(uint8_t bcd)
- {
- uint8_t ret = (bcd >> 4) * 10;
- ret += (bcd & 0x0F);
- return ret;
- }
- /**
- * @brief BCD to byte
- * @param[in] bcd as uint8_t.
- * @param[out] none.
- * @return as uint8_t .
- */
- static uint8_t Byte2BCD(uint8_t data)
- {
- uint8_t ret = (data / 10) * 16;
- ret += (data % 10);
- return ret;
- }
- /**
- * @brief ds3231 set time
- * @param[in] hour as uint8_t
- * minute as uint8_t
- * second as uint8_t.
- * @param[out] none.
- * @return none .
- */
- void Ds3231_SetTime(uint8_t hour, uint8_t minute, uint8_t second)
- {
- DS3231_WriteByte(DS3231_HOUR, Byte2BCD(hour));
- DS3231_WriteByte(DS3231_MINUTE, Byte2BCD(minute));
- DS3231_WriteByte(DS3231_SECOND, Byte2BCD(second));
- }
- /**
- * @brief ds3231 set date
- * @param[in] year as uint8_t
- * month as uint8_t
- * day as uint8_t.
- * @param[out] none.
- * @return none .
- */
- void Ds3231_SetDate(uint8_t year, uint8_t month, uint8_t day)
- {
- DS3231_WriteByte(DS3231_YEAR,Byte2BCD(year));
- DS3231_WriteByte(DS3231_MONTH, Byte2BCD(month));
- DS3231_WriteByte(DS3231_DAY, Byte2BCD(day));
- }
- /**
- * @brief ds3231 get time
- * @param[in] none.
- * @param[out] hour as uint8_t
- * minute as uint8_t
- * second as uint8_t.
- * @return none .
- */
- void Ds3231_GetTime(uint8_t *hour, uint8_t *minute, uint8_t *second)
- {
- *hour = BCD2Byte(DS3231_ReadRandom(DS3231_HOUR));
- *minute = BCD2Byte(DS3231_ReadRandom(DS3231_MINUTE));
- *second = BCD2Byte(DS3231_ReadRandom(DS3231_SECOND));
- }
- /**
- * @brief ds3231 get date
- * @param[in] none.
- * @param[out] year as uint8_t
- * month as uint8_t
- * day as uint8_t.
- * @return none .
- */
- void Ds3231_GetDate(uint8_t *year, uint8_t *month, uint8_t *day)
- {
- *year = BCD2Byte(DS3231_ReadRandom(DS3231_YEAR));
- *month = BCD2Byte(DS3231_ReadRandom(DS3231_MONTH));
- *day = BCD2Byte(DS3231_ReadRandom(DS3231_DAY));
- }
复制代码DS3231SN.h - /**
- * @file DS3231SN.h
- * @brief DS3231 RTC
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
- #ifndef __DS3231SN_H__
- #define __DS3231SN_H__
- #ifdef __cplusplus
- extern "C"{
- #endif
- #include <Arduino.h>
- #define DS3231_WriteAddress 0xD0 //器件写地址
- #define DS3231_ReadAddress 0xD1 //器件读地址
- #define DS3231_SECOND 0x00 //秒
- #define DS3231_MINUTE 0x01 //分
- #define DS3231_HOUR 0x02 //时
- #define DS3231_WEEK 0x03 //星期
- #define DS3231_DAY 0x04 //日
- #define DS3231_MONTH 0x05 //月
- #define DS3231_YEAR 0x06 //年
- #define DS3231_TEMPERATURE 0x0E //温度
- #define DS3231_TEMP_CONVERT 0x20
- #define DS3231_TEMP_HIGH_uint8_t 0x11
- #define DS3231_TEMP_LOW_uint8_t 0x12
- /**
- * @brief ds3231 set time
- * @param[in] hour as uint8_t
- * minute as uint8_t
- * second as uint8_t.
- * @param[out] none.
- * @return none .
- */
- void Ds3231_SetTime(uint8_t hour, uint8_t minute, uint8_t second);
- /**
- * @brief ds3231 set date
- * @param[in] year as uint8_t
- * month as uint8_t
- * day as uint8_t.
- * @param[out] none.
- * @return none .
- */
- void Ds3231_SetDate(uint8_t year, uint8_t month, uint8_t day);
- /**
- * @brief ds3231 get time
- * @param[in] none.
- * @param[out] hour as uint8_t
- * minute as uint8_t
- * second as uint8_t.
- * @return none .
- */
- void Ds3231_GetTime(uint8_t *hour, uint8_t *minute, uint8_t *second);
- /**
- * @brief ds3231 get date
- * @param[in] none.
- * @param[out] year as uint8_t
- * month as uint8_t
- * day as uint8_t.
- * @return none .
- */
- void Ds3231_GetDate(uint8_t *year, uint8_t *month, uint8_t *day);
- #ifdef __cplusplus
- }
-
- #endif
- #endif /*end of __DS3231SN_H__*/
复制代码Display.cpp - /**
- * @file Display.cpp
- * @brief display task
- * @author Hu Xihe
- * @copyright All rights reserved.
- */
- #include "Display.h"
- #include "IIC.h"
- #include "ANF_Lcd.h"
- #include "DS3231SN.h"
- #include "GpioButton.h"
- #include "SHT20.h"
- #include "Blwifi.h"
- #include <ESP8266WiFi.h>
- uint8_t pm25Buffer[32];
- uint8_t pm25Index = 0;
- float Dis_Temperature = 0;
- float Dis_Humidity = 0;
- float Dis_Pm25 = 0;
- /**
- * @brief Display init
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_Init(void)
- {
- IIC_Init(12, 13);
- ANF_LcdInit();
- TouchButton_Init();
- ANF_LcdAll();
- ANF_LcdUpdate();
- delay(100);
- pinMode(BACK_LED_PIN, OUTPUT);
- pinMode(PM25_POWER_PIN, OUTPUT);
- digitalWrite(PM25_POWER_PIN, LOW);
- analogWriteRange(100);
- Display_SetBacklight(100);
- ANF_LcdBattery(BAT_None);
- ANF_LcdWeather(WTHR_None);
- ANF_LcdUpdate();
- }
- /**
- * @brief set display after wifi init
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_AfterWifiInit(void)
- {
- ANF_LcdAll();
- ANF_LcdBattery(BAT_None);
- ANF_LcdWeather(WTHR_None);
- Display_Time();
- Display_TempHumidity();
- }
- /**
- * @brief only display wifi logo
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_WaitWifi(void)
- {
- ANF_LcdClear();
- ANF_LcdWifi(WIFI_Full);
- ANF_LcdUpdate();
- }
- /**
- * @brief Display time, value from ds3231
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_Time(void)
- {
- static uint8_t point = 0;
- uint8_t gethour, getminute, getsencond;
- Ds3231_GetTime(&gethour, &getminute, &getsencond);
- ANF_LcdTimeNumber(gethour, getminute);
- ANF_LcdTimePoint(point);
- point = !point;
- }
- /**
- * @brief Display date, value from ds3231
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_Date(void)
- {
- uint8_t getyear, getmonth, getday;
- Ds3231_GetDate(&getyear, &getmonth, &getday);
- ANF_LcdTimeNumber(getmonth, getday);
- ANF_LcdTimePoint(0);
- }
- /**
- * @brief Display temperature humidity, value from sht20
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_TempHumidity(void)
- {
- short tmp;
-
- tmp = SHT20_GetTemperature();
- Dis_Temperature = (float)(tmp/10.0);
- ANF_LcdTempNumber(tmp);
-
- tmp = SHT20_GetHumidity();
- Dis_Humidity = (float)tmp;
- ANF_LcdHumidityNumber(tmp);
- }
- /**
- * @brief set pwm value.
- * @param[in] value as int.
- * @param[out] none.
- * @return none.
- */
- void Display_SetBacklight(int value)
- {
- analogWrite(BACK_LED_PIN, value);
- }
- /**
- * @brief display weather info.
- * @param[in] str as string.
- * @param[out] none.
- * @return none.
- */
- void Display_Weather(const String & str)
- {
- WEATHER_DEF wthr = WTHR_None;
- if(str == "晴") wthr = WTHR_Sunny;
- else if(str == "多云") wthr = WTHR_Cloudy;
- else if(str == "阴") wthr = WTHR_Overcast;
- else if(str == "小雨") wthr = WTHR_LightRain;
- else if(str == "中雨") wthr = WTHR_ModerateRain;
- else if(str == "大雨") wthr = WTHR_HeavyRain;
- else if(str == "霾") wthr = WTHR_Haze;
- else if(str == "雾") wthr = WTHR_Foggy;
- ANF_LcdWeather(wthr);
- }
- /**
- * @brief Wifi logo flash.
- * @param[in] times as uint8_t.
- * @param[out] none.
- * @return none.
- */
- void Display_WifiFlash(uint8_t times)
- {
- uint8_t i;
- for(i=0; i<times; i++)
- {
- ANF_LcdWifi(WIFI_None);
- ANF_LcdUpdate();
- delay(200);
- ANF_LcdWifi(WIFI_Full);
- ANF_LcdUpdate();
- delay(200);
- }
-
- }
- uint16_t ShowDate = 0;
- GpioButton TouchButton(5);
- /**
- * @brief touch key init, press show date
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void TouchButton_Init()
- {
- TouchButton.BindBtnPress([](){
- if(ShowDate > 0)
- {
- ShowDate = 0;
- Display_Time();
- }
- else
- {
- ShowDate = 6;
- Display_Date();
- }
- });
- }
- /**
- * @brief show pm2.5 call by 500ms
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void Display_Pm25(void)
- {
- static uint32_t period = wifiSettings.pm25period*60*2;
- static uint32_t sampTime = 0;
- uint32_t setPeriod = wifiSettings.pm25period*60*2;
- if(period < setPeriod)
- {
- period++;
- digitalWrite(PM25_POWER_PIN, LOW);
- }
- else if(period == setPeriod)
- {
- digitalWrite(PM25_POWER_PIN, HIGH);
- period++;
- sampTime = PM25_SAMP_TIME;
- }
- else
- {
- if(sampTime > 0)
- {
- if(setPeriod > 0)
- {
- if(sampTime & 0x01)
- ANF_LcdPm25Logo(1);
- else
- ANF_LcdPm25Logo(0);
- }
- else
- ANF_LcdPm25Logo(1);
- ANF_LcdPm25Number(Dis_Pm25);
- sampTime--;
- }
- else
- {
- period = 0;
- }
- }
- }
- /**
- * @brief show date and time call by 500ms
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_DateTime_Task500ms(void)
- {
- if(ShowDate == 0)
- Display_Time();
- else
- {
- Display_Date();
- ShowDate--;
- }
- Display_TempHumidity();
- Display_Pm25();
- int rssi = WiFi.RSSI();
- if(rssi <= 0 && rssi >= -50)
- ANF_LcdWifi(WIFI_Full);
- else if(rssi < -50 && rssi >= -70)
- ANF_LcdWifi(WIFI_High);
- else if(rssi < -70 && rssi >= -80)
- ANF_LcdWifi(WIFI_Middle);
- else if(rssi <-80 && rssi >= -100)
- ANF_LcdWifi(WIFI_Low);
- else
- ANF_LcdWifi(WIFI_None);
-
- ANF_LcdUpdate();
- ST20_StartSample();
- }
- typedef enum
- {
- PM25_Head1,
- PM25_Head2,
- PM25_Length,
- PM25_Data
- }PM25_ST;
- PM25_ST pm25St = PM25_Head1;
- /**
- * @brief serial of pm2.5 loop
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- static void Display_PM25_Loop(void)
- {
- static uint16_t dataLength = 0;
- uint16_t sum, i, chksum;
- if(digitalRead(PM25_POWER_PIN) == HIGH)
- {
- if(Serial.available() > 0)
- {
- switch(pm25St)
- {
- case PM25_Head1:
- pm25Index = 0;
- pm25Buffer[pm25Index] = Serial.read();
- if(pm25Buffer[pm25Index] == 0x32)
- pm25St = PM25_Head2;
- break;
- case PM25_Head2:
- pm25Index = 1;
- pm25Buffer[pm25Index] = Serial.read();
- if(pm25Buffer[pm25Index] == 0x3D)
- pm25St = PM25_Length;
- else
- pm25St = PM25_Head1;
- break;
- case PM25_Length:
- pm25Index++;
- pm25Buffer[pm25Index] = Serial.read();
- if(pm25Index >= 3)
- {
- pm25Index = 4;
- pm25St = PM25_Data;
- dataLength = pm25Buffer[2] * 256 + pm25Buffer[3] + 4;
- }
- break;
- case PM25_Data:
- pm25Buffer[pm25Index] = Serial.read();
- pm25Index++;
- if(pm25Index >= dataLength)
- {
- sum = 0;
- for(i=0; i<dataLength-2; i++)
- {
- sum += pm25Buffer[i];
- }
- chksum = pm25Buffer[dataLength-2]*256 + pm25Buffer[dataLength-1];
- if(chksum == sum)
- Dis_Pm25 = pm25Buffer[6]*256+pm25Buffer[7];
- pm25St = PM25_Head1;
- }
- break;
- }
- }
- }
- else if(Serial.available() > 0)
- {
- Serial.flush();
- }
- }
- /**
- * @brief key loop for touch key
- * @param[in] none.
- * @param[out] none.
- * @return none.
- */
- void Display_Loop(void)
- {
- TouchButton.loop();
- Display_PM25_Loop();
- }
复制代码Display.h GpioButton.h - #ifndef _GPIO_BUTTON_H_
- #define _GPIO_BUTTON_H_
- #include <Arduino.h>
- #define DEF_ELIMINATING_JITTER_MS 20 // default eliminating jitter ms
- #define DEF_LONG_PRESS_WAIT_MS 1000 // default long press wait ms
- #define DEF_DB_PRESS_MS 300
- class GpioButton {
- public:
- GpioButton(uint8_t gpio_pin, void(*btn_press_event)()=nullptr) :
- GpioPin(gpio_pin),
- ButtonPressEvent(btn_press_event),
- LongPressWaitMS(DEF_LONG_PRESS_WAIT_MS),
- ButtonLongPressEvent(nullptr),
- first_key_down_millis(0),
- first_key_up_millis(0),
- action_done(false),
- last_gpio_state(HIGH) {
- pinMode(GpioPin, INPUT_PULLUP);
- digitalWrite(GpioPin, HIGH);
- };
- // bind click event CB function
- void BindBtnPress(void(*btn_press_event)()) {
- ButtonPressEvent = btn_press_event;
- };
- // bind long key press CB function
- bool BindBtnLongPress(void(*btn_long_press_event)(), uint16_t wait_ms=DEF_LONG_PRESS_WAIT_MS) {
- if(wait_ms < DEF_LONG_PRESS_WAIT_MS) return false;
- ButtonLongPressEvent = btn_long_press_event;
- LongPressWaitMS = wait_ms;
- return true;
- };
- // bind double click CB function
- void BindBtnDblPress(void(*btn_dbl_press_event)()) {
- ButtonDblPressEvent = btn_dbl_press_event;
- };
- // loop function
- void loop(){
-
- uint8_t current_gpio_state = digitalRead(GpioPin);
- uint32_t current_millis = millis();
-
- // gpio status no change
- if(current_gpio_state == last_gpio_state) {
- if(current_gpio_state == LOW) {
- if(first_key_down_millis && !first_key_up_millis && (current_millis - first_key_down_millis > LongPressWaitMS)) {
- if(!action_done && ButtonLongPressEvent != nullptr) {
- ButtonLongPressEvent();
- action_done = true;
- }
- }
- }
- else {
- if(first_key_up_millis && (current_millis - first_key_up_millis > DEF_DB_PRESS_MS)) {
- if(!action_done && ButtonPressEvent != nullptr) {
- // Serial.println("Debug:Press Event.");
- ButtonPressEvent();
- action_done = true;
- }
- }
- }
- }
- // gpio status changed
- else {
- if(current_millis - last_jitter_millis > DEF_ELIMINATING_JITTER_MS) {
- // key down
- if(current_gpio_state == LOW) {
- // is first keydown in cycle
- if(0 == first_key_down_millis) {
- first_key_down_millis = current_millis;
- first_key_up_millis = 0;
- action_done = false;
- }
- // is not first key down in cycle
- else {
- // has define double click CB function
- if(nullptr != ButtonDblPressEvent){
- // key down mill - last key up mill > elimination jitter interval
- if( 0 != first_key_up_millis // is release key in event cycle
- && (current_millis - first_key_up_millis) > DEF_ELIMINATING_JITTER_MS) { // skip eliminating jitter
- // is double click?
- if( false == action_done // did in event cycle?
- && current_millis - first_key_up_millis < DEF_DB_PRESS_MS) { // and 2nd click is in interval
- // call double click event function
- // Serial.println("Debug:Double Press Event.");
- ButtonDblPressEvent();
- action_done = true;
- }
-
- }
- }
- }
- }
- // key up
- else {
- if(!action_done && first_key_down_millis && first_key_up_millis == 0) {
- first_key_up_millis = current_millis;
- }
- }
- // Keep gpio status
- last_gpio_state = current_gpio_state;
- last_jitter_millis = current_millis;
- }
- }
-
- if(action_done && current_gpio_state == HIGH) {
- // Serial.println("Event Reset.");
- first_key_down_millis = 0;
- first_key_up_millis = 0;
- action_done = false;
- }
- };
- protected:
- uint8_t GpioPin; // gpio pin of key
- void (*ButtonPressEvent)(); // Click Event CB function
- uint16_t LongPressWaitMS; // Long press ms
- void (*ButtonLongPressEvent)(); // Long press Event CB function
- void (*ButtonDblPressEvent)(); // Double click Event CB function
- uint32_t first_key_down_millis;
- uint32_t first_key_up_millis;
- bool action_done;
- uint8_t last_gpio_state;
- uint32_t last_jitter_millis;
- };
- #endif
复制代码5.Blinker界面配置 - {¨version¨¨2.0.0¨¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨cha¨¨bg¨É¨sty¨¨line¨¨clr¨¨#FBA613¨¨sty1¨ßG¨clr1¨¨#076EEF¨¨sty2¨ßG¨clr2¨¨#595959¨¨cols¨Ñ¨rows¨Í¨key¨¨cha-oh6¨´x´É´y´Ï¨speech¨|÷¨t0¨¨温度¨¨key0¨¨temp¨¨lstyle¨Ê¨key1¨¨humi¨¨t1¨¨湿度¨¨key2¨¨pm25¨¨t2¨¨Pm25¨}{ßC¨num¨ßU¨室内温度¨¨ico¨¨fad fa-thermometer-three-quarters¨ßHßI¨min¨É¨max¨¢1c¨uni¨¨℃ ¨ßEÉßPÍßQËßR¨num-tmp¨´x´Í´y´ËßT|÷ßYÊ}{ßCßhßU¨室内湿度¨ßj¨fad fa-humidity¨ßH¨#389BEE¨ßlÉßmº0ßn´%´ßEÉßPÍßQËßR¨num-hum¨´x´Í´y´ÍßT|÷ßYÊ}{ßCßhßU¨室内PM25¨ßj¨fal fa-question¨ßHßsßlÉßm¢G7ßn¨ug/m3¨ßEÉßPÍßQÍßR¨num-pm25¨´x´É´y´ËßT|÷ßYÍ}{ßC¨ran¨ßU¨屏幕背光亮度¨ßHßsßmº0ßlÉßEÉßPÑßQËßR¨ran-light¨´x´É´y´¤CßT|÷}{ßCßyßU¨PM2.5采集周期¨ßHßsßmº0ßlÉßEÉßPÑßQËßR¨ran-pm25¨´x´É´y´¤EßT|÷}{ßC¨tex¨ßU¨IP:¨ßb¨固件升级地址¨¨size¨¤EßEÉßj¨fal fa-font¨ßPÑßQÊßR¨txt-ip¨´x´É´y´¤BßT|÷ßYÎ}÷¨actions¨|÷¨triggers¨|÷}
复制代码
5.软件和硬件 6.注意事项 (a)PCB为1mm厚 (b)烧录的时候要短接GPIO 0到地,板上没有预留,建议用镊子捅 (c)运行到现在,遇到过一次温度显示56度,重启就好了 (d)帖子字数限制,删了一部分代码,附件里是全的
|
打赏
-
查看全部打赏
|