esp-12f测试12864oled
本帖最后由 wenjun262 于 2022-6-7 06:01 编辑#define BLINKER_WIFI //定义wifi模块
#define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈
#include <Blinker.h>
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <ESP8266WiFi.h> //包含ESP8266头文件,8266必须要使用的头文件
#include <EEPROM.h> //包含EEPROM头文件,用于EEPROM存储
#include <DHT.h> //DHT库
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
#define INFO_SCREEN_DELAY 1000
//设置DHT引脚
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
char ssid[] = "wenjun262";
char pswd[] = "************";
char auth[] = "*************";
#define Slider_1 "ran-shuaxin"
BlinkerNumber Number1("num-xinhao"); //定义信号强度键名
#define TEXTE_1"tex-ip"
BlinkerNumber Number3("num3");
BlinkerNumber Number4("num4");
BlinkerNumber Number5("num5");
BlinkerText Text1(TEXTE_1);
BlinkerButton ButtonOn1("btn2");//按键的名称 led
BlinkerButton ButtonOn2("btn0");//按键的名称 指示
BlinkerButton ButtonOn3("btn1");//按键的名称 灯
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
bool w1=true;
bool wh=true;
bool wm=true;
bool ws=true;
BlinkerSlider Slider1(Slider_1);
int led=2;
int k1=12;
int h1=0,m=0,s=0,v1=0;
int a=3;
uint32_tbtime=0;
bool w=false;
bool w2=false;
floath;
float t;
float humi_read = 0, temp_read = 0;
int hou1=0,min1=0,sec1=0;
//#define DHTTYPEDHT11 // DHT 11
#define DHTTYPEHT DHT22 // DHT 22(AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
void button1_callback(const String & state)
{
//digitalWrite(led, !digitalRead(led));
BLINKER_LOG("get button state: ", state);
if (state == "on") {
BLINKER_LOG("Button ON!");w2=true;
// digitalWrite(k1,HIGH);
ButtonOn1.color("red"); //设置app按键是红色
ButtonOn1.print("on");
}else if(state=="off"){
//digitalWrite(k1,LOW);
w2=false;
ButtonOn1.color("gray"); //设置app按键是灰色
ButtonOn1.print("off");
}
EEPROM.begin(4000);
EEPROM.write(2553,w2);
EEPROM.end();
Blinker.vibrate();
}
void button2_callback(const String & state) {
if (state == "on") {
BLINKER_LOG("Button ON!");
w=true;
ButtonOn2.color("green"); //设置app按键是绿色
ButtonOn2.print("on");
}else if(state=="off"){w=false;
digitalWrite(led,HIGH);
// Button1.color("");
ButtonOn2.print("OFF");
}
EEPROM.begin(4000);
EEPROM.write(2552,w);
EEPROM.end();
Blinker.vibrate();
}
void button3_callback(const String & state)
{
//digitalWrite(led, !digitalRead(led));
BLINKER_LOG("get button state: ", state);
if (state == "on") {
BLINKER_LOG("Button ON!");
digitalWrite(k1,LOW);
ButtonOn3.color("red"); //设置app按键是红色
ButtonOn3.print("on");
}else if(state=="off"){
digitalWrite(k1,HIGH);
ButtonOn3.color("gray"); //设置app按键是灰色
ButtonOn3.print("off");
}Blinker.vibrate();
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
uint32_tbtime=millis(); //获取当前的系统运行时间长度
}
// 云存储回调函数
//*********************************************************************************************************/
void dataStorage() {
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void heartbeat(){
HUMI.print(humi_read);
TEMP.print(temp_read);
Number3.print(h1);
Number4.print(m);
Number5.print(s);
Number1.icon("fal fa-wifi");
Number1.print((WiFi.RSSI()+90));
Text1.print( WiFi.localIP().toString().c_str(),ssid);
EEPROM.begin(4000);
v1=EEPROM.read(2449);
int32_tvalue=v1;
Slider1.print(value);
EEPROM.end();
if( w==false){ ButtonOn2.color("gray"); //设置app按键是灰色
ButtonOn2.print("off");}else { ButtonOn2.color("green"); //设置app按键是红色
ButtonOn2.print("on");}
if(w2==true)
// digitalWrite(k1,HIGH);
{ButtonOn1.color("red"); //设置app按键是红色
ButtonOn1.print("on");}else{ButtonOn1.color("gray"); //设置app按键是灰色
ButtonOn1.print("off");}
}
void slider1_callback(int32_t value)
{
BLINKER_LOG("get slider value: ", value);
EEPROM.begin(4000);
EEPROM.write(2449, value);
Slider1.print(value);
EEPROM.end();
}
void time1 (){
if(h1==6&m==0&s==0){wh=true;wm=true;ws=true;}//6点更新时间
if(h1==12&m==0&s==0){wh=true;wm=true;ws=true;}
if(h1==18&m==0&s==0){wh=true;wm=true;ws=true;}
if(h1==23&m==0&s==0){wh=true;wm=true;ws=true;}
if(wh==true){//开机更新时间
int8_thou= Blinker.hour(); //时
// Blinker.delay(1000);
if(hou>-1){ wh=false; h1=hou;}
}
if(wm==true){
int8_t mi= Blinker.minute(); //分
//Blinker.delay(1000);
if(mi>-1){ wm=false;m=mi;}
}
if(ws==true){
int8_t sec= Blinker.second(); //秒
// Blinker.delay(1000);
if(sec>-1){ws=false;s=sec; }
}
}
void setup()
{
pinMode(k1, OUTPUT);
digitalWrite(k1,HIGH);
pinMode(led, OUTPUT);
digitalWrite(led,HIGH);
Blinker.begin(auth, ssid, pswd);
ButtonOn1.attach(button1_callback); //APP回调函数
ButtonOn2.attach(button2_callback); //APP回调函数
ButtonOn3.attach(button3_callback); //APP回调函数
Blinker.attachHeartbeat(heartbeat);
Blinker.attachData(dataRead);
Blinker.attachDataStorage(dataStorage); //注册回调函数,开启历史数据存储功能
Slider1.attach(slider1_callback);
dht.begin();
u8g2.begin();
u8g2.enableUTF8Print();
EEPROM.begin(4000);
v1=EEPROM.read(2449);
int32_tvalue=v1;
w= EEPROM.read(2552);
w2= EEPROM.read(2553);
EEPROM.end();
}
void OLED (){
String wendu;
wendu = String(" ")+"T" + temp_read + "C";
String shidu;
shidu = String(" ")+ "H" + humi_read + "%";
u8g2.setFont(u8g2_font_unifont_t_chinese2);//
u8g2.setFontDirection(0);
u8g2.firstPage();
do {if(w2==false){
u8g2.setCursor(55,10);
u8g2.setPowerSave(0);
if(w1==false){ u8g2.print(String("@_@"));}else{ u8g2.print(String(" "));}
u8g2.setCursor(30,27);
u8g2.print(String("wifi ")+String((WiFi.RSSI()+90)));
u8g2.setCursor(30,40);
if(h1<10){ u8g2.print(String("0")+String(h1)+String(":"));}
else {u8g2.print(String(h1)+String(":"));}
if(m<10){u8g2.print(String("0")+String(m)+String(":"));}
else {u8g2.print(String(m)+String(":"));}
if(s<10){u8g2.print(String("0")+String(s));}
else {u8g2.print(String(s));}
u8g2.setCursor(0,60);
u8g2.print(wendu);
// u8g2.setCursor(0, 31);
u8g2.print(shidu); // Chinese "Hello World"
}else {u8g2.setPowerSave(1);
}
}while ( u8g2.nextPage() );
}
void loop()
{
Blinker.run();
time1();
digitalWrite(led,HIGH);w1=true;
if(millis()- btime>865){btime= millis();w1=false;//产生秒定时,驱动指示灯
// if(a>0){ a--;}if(a==0){a=0;}
s++;if(s==60){s=0;m++;}
if(m==60){m=0;h1++;}
if(h1==24){h1=0;}
if(w==true){digitalWrite(led,LOW ); }
}
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
}
else{
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
humi_read = h;
temp_read = t;
}
OLED();
}
优秀的在哪里都是闪闪发光 66666666666
页:
[1]