代码: 全选
/************************************************************************************************************/
#include "Button2.h"; //按钮控制库https://github.com/LennartHennigs/Button2 按钮控制库
#include "Rotary.h"; //编码器库 https://github.com/skathir38/Rotary
#include "TickTwo.h" //定时库 https://github.com/sstaub/TickTwo
#include <Arduino_GFX_Library.h> //液晶屏驱动库
Arduino_DataBus *bus = new Arduino_ESP8266SPI(4 /* DC */, 15 /* CS */); //液晶屏控制芯片的选择与引脚的设置
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 2 /* RST */, 0 /* rotation */, true /* IPS */);//屏幕型号的选择
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80); //建立WEB服务器端口80
#include <WiFiManager.h>
/***********************************************************************************************************/
#define ROTARY_PIN1 D1 //定义ESP8266接旋转编码器的输入引脚与名称
#define ROTARY_PIN2 D6 //定义ESP8266接旋转编码器的输入引脚与名称
#define BUTTON_PIN D3 //定义ESP8266接旋转编码器按钮的输入引脚与名称
#define Relay_PIN D0 //定义ESP8266接继电器的输出引脚与名称
#define CLICKS_PER_STEP 4 //定义旋转编码器时钟数及名称(这个数字取决于您的旋转编码器)
#define MIN_POS -90 //定义旋转编码器最小值及名称
#define MAX_POS 270 //定义旋转编码器最大值及名称
#define START_POS -90 //定义旋转编码器起始值及名称
#define INCREMENT 6 //定义旋转编码器每一步的增量
/************************************************************************************************************/
Rotary r; //声明Rotary对象
Button2 b; // 声明Button2对象
WiFiManager wifiManager;
void blink();
TickTwo timer1(blink, 60000); //声明TickTwo对象
/************************************************************************************************************/
byte cx=120, cy=120, outerR=110, innerR=100; //定义实心圆弧起始坐标X、Y、外圆半径、内圆半径变量名称
uint16_t color_1, color_2=BLACK,color_3; //定义颜色变量名称
int originate=-90, ultimate=-90, originate_1; //定义实心圆弧开始角度、结束角度、反向擦除圆弧起始角度变量名称
int Temperature=0,set_time_valu=0, Numerical_value, Clear_value=0;//定义温度、定时、显示数值、清除数值变量名称
byte x, y, x_1, y_1, x_2, y_2, Size, Size_1; //定义显示坐标、文字大小变量名称
byte x_temp, x_var, x_time=114; //定义坐标转换值变量名称
int Tem_switch, get_value, value; //定义整数型变量名称
String word_1; //定义字符串变量名称
/************************************************************************************************************/
void setup()
{
Serial.begin(9600);
gfx->begin(); //运行液晶屏库
setup_wifi(); //WIFI初始化
Start_Display(); //初始化液晶屏显示
//开启按钮程序并设定按钮为输入
b.begin(BUTTON_PIN);
b.setClickHandler(handler); //设置点击一次处理程序
b.setLongClickHandler(handler); //设置长按处理程序
b.setDoubleClickHandler(handler); //设置点击两次处理程序
b.setTripleClickHandler(handler); //设置点击三次处理程序
//开启旋转编码器程序并设定变量名称
r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
r.setChangedHandler(temperature); //温度设置的处理程序
r.setLeftRotationHandler(set_time); //设置左旋转处理程序
r.setRightRotationHandler(set_time); //设置右旋转处理程序
timer1.start(); //开启定时程序
pinMode(Relay_PIN, OUTPUT); //设置D0脚为输出型
digitalWrite(Relay_PIN, HIGH); //写入D0脚高电平
timing_run(); //定时运行
Temperature_adjust(); //远程温度调整
Time_adjust(); //远程时间调整
Wifi_rest(); //wifi账号与密码删除
Stop(); //停止运行
}
//主循环程序
void loop()
{
b.loop();
r.loop();
if(set_time_valu>0){
timer1.update();
}
reset_Position();
server.handleClient();
}
/************************************************************************************************************/
// 温度设定调整
void temperature(Rotary& r)
{
if(Tem_switch==0)
{
if(r.getDirection()==1) //判断r.getDirection()的取值等于1时执行Temperature自加否则自减
{
Temperature++;
}
else{
Temperature--;
}
Temperature_Display(); //数值显示程序
}
}
// 设置定时时间并画圆弧
void set_time(Rotary& r) {
if(Tem_switch==1){
if(r.getDirection()==1){ //判断编码器增加侧执行下列程序
Time_plus();
}else{ //判断编码器如果没有增加侧执行下列程序
Time_sub();
}
Time_Display();
}
}
/**********************************************************************************************************
功能选择,按钮一次Tem_switch=2定时开始运行,按钮两次Tem_switch=1时间调节且停止,长按按钮Tem_switch=0调节温度且停止
连续按钮三次WIFI设置,wifi设置时其他功能均不可使用,需等待WIFI联网后方能恢复
**********************************************************************************************************/
void handler(Button2& btn) {
switch (btn.getType()) {
case single_click:
if(set_time_valu>0){
SET_RUN();
}
break;
case double_click:
SET_time();
break;
case triple_click:
//setup_wifi();
break;
case long_click:
SET_TEMP_STOP();
break;
}
}
//定时运行程序
void blink()
{
if(Tem_switch==2)
{
if(set_time_valu>0){
set_time_valu--;
ultimate = constrain(ultimate, -90, 270); //将ultimate数值限制-90与270之间
originate_1 = ultimate-INCREMENT;
gfx->fillArc(cx, cy, outerR, innerR, originate_1, ultimate, BLUE); //反画实心圆弧
ultimate = ultimate-INCREMENT;
get_value = set_time_valu;
coordinate();
x=x_var, x_time=x_var, y=140, Size=3, color_1=WHITE, Numerical_value=set_time_valu;
Display(); //显示后退数值
}
if(set_time_valu==0){
SET_TEMP_STOP();
}
}
}
//旋转编码器自动归零
void reset_Position()
{
if(r.getPosition()==MAX_POS || r.getPosition()==MIN_POS )
{
r.resetPosition();
}
}
//纯数值显示,无擦除原显示功能
void Value_Display()
{
gfx->setCursor(x, y);//设置光标坐标
gfx->setTextColor(color_1);//设置文字颜色
gfx->setTextSize(Size);//设置文字大小
gfx->println(value);//输出值
}
//温度与时间调整时数值显示
void Display()
{
gfx->setCursor(x_1, y_1);//设置光标坐标
gfx->setTextColor(color_2);//设置文字颜色
gfx->setTextSize(Size);//设置文字大小
gfx->println(Clear_value);//输出文字E
gfx->setCursor(x, y);//设置光标坐标
gfx->setTextColor(color_1);//设置文字颜色
gfx->setTextSize(Size);//设置文字大小
gfx->println(Numerical_value);//输出文字
Clear_value = Numerical_value;
x_1=x;
y_1=y;
}
//字符串文字显示
void Word_Display()
{
gfx->setCursor(x_2, y_2);//设置光标坐标
gfx->setTextColor(color_3);//设置文字颜色
gfx->setTextSize(Size_1);//设置文字大小
gfx->println(word_1);//输出文字
}
//根据Temperature的数值调整tx坐标值始温度值显示在屏幕中间
void coordinate()
{
if(get_value<10)
{
x_var = 114;
}
else{
if(get_value<100 &&get_value>9)
{
x_var = 106;
}
else{
x_var = 96;
}
}
}
//按钮温度选择
void SET_TEMP_STOP()
{
x_1 = x_temp, y_1=70, Clear_value = Temperature;
Tem_switch=0;
x_2=94, y_2=176, Size_1=3, color_3=BLACK, word_1="RUN";
Word_Display();
x_2=50, y_2=142, Size_1=2, color_3=BLACK, word_1="SET";
Word_Display();
x_2=50, y_2=72, Size_1=2, color_3=BLUE, word_1="SET";
Word_Display();
x_2=90, y_2=176, Size_1=3, color_3=RED, word_1="STOP";
Word_Display();
digitalWrite(Relay_PIN, HIGH);
}
//按钮时间选择
void SET_time()
{
x_1 = x_time, y_1=140,Clear_value = set_time_valu;
Tem_switch=1;
x_2=94, y_2=176, Size_1=3, color_3=BLACK, word_1="RUN";
Word_Display();
x_2=50, y_2=72, Size_1=2, color_3=BLACK, word_1="SET";
Word_Display();
x_2=50, y_2=142, Size_1=2, color_3=RED, word_1="SET";
Word_Display();
x_2=90, y_2=176, Size_1=3, color_3=RED, word_1="STOP";
Word_Display();
digitalWrite(Relay_PIN, HIGH);
}
//按钮控制运行
void SET_RUN()
{
x_1 = x_time, y_1=140, Clear_value = set_time_valu;
Tem_switch=2;
x_2=50, y_2=142, Size_1=2, color_3=BLACK, word_1="SET";
Word_Display();
x_2=50, y_2=72, Size_1=2, color_3=BLACK, word_1="SET";
Word_Display();
x_2=90, y_2=176, Size_1=3, color_3=BLACK, word_1="STOP";
Word_Display();
x_2=94, y_2=176, Size_1=3, color_3=GREEN, word_1="RUN";
Word_Display();
digitalWrite(Relay_PIN, LOW);
}
//WIFI启动与设置
void setup_wifi() {
gfx->fillScreen(color_2); //给屏幕充满黑色
wifiManager.setConfigPortalTimeout(120); //WIFI没有连接自动跳出
x_2=45, y_2=70, Size_1=2, color_3=WHITE, word_1="Please connect";
Word_Display();
x_2=15, y_2=100, Size_1=2, color_3=WHITE, word_1="SSID:AutoConnectAP";
Word_Display();
x_2=40, y_2=130, Size_1=2, color_3=RED, word_1="IP:192.168.4.1";
Word_Display();
x_2=50, y_2=160, Size_1=2, color_3=WHITE, word_1="Wait two Min";
Word_Display();
wifiManager.autoConnect("AutoConnectAP"); //自动连接WIFI
server.onNotFound(handleNotFound);
server.begin(); //开启web服务器
gfx->fillScreen(color_2);
x_2=50, y_2=70, Size_1=2, color_3=WHITE, word_1="Connected to:";
Word_Display();
x_2=80, y_2=100, Size_1=2, color_3=WHITE, word_1=WiFi.SSID();
Word_Display();
x_2=30, y_2=130, Size_1=2, color_3=WHITE, word_1="IP ";
Word_Display();
gfx->setCursor(60, 130);//设置光标坐标
gfx->setTextColor(WHITE);//设置文字颜色
gfx->setTextSize(2);//设置文字大小
gfx->println(WiFi.localIP());//输出值
delay(10000);
//Start_Display();
//SET_TEMP_STOP();
Serial.print("ESP8266 Connected to ");
Serial.println(WiFi.SSID()); // WiFi名称
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // IP
}
//温度显示
void Temperature_Display()
{
Temperature = constrain(Temperature, 0, 220); //将数值限制在0与220之间
get_value = Temperature;
coordinate(); //坐标调整函数
x=x_var, x_temp=x_var, y=70, Size=3, color_1=WHITE, Numerical_value=Temperature; //给显示程序变量赋值
Display(); //数值显示函数
}
//时间显示
void Time_Display()
{
set_time_valu = constrain(set_time_valu, 0, 60); //将数值限制在0与60之间
get_value = set_time_valu;
coordinate(); //坐标调整程序
x=x_var, x_time=x_var, y=140, Size=3, color_1=WHITE, Numerical_value=set_time_valu; //给显示程序变量赋值
Display(); //函数
}
//时间加
void Time_plus()
{
ultimate = constrain(ultimate, -90, 270); //将ultimate数值限制-90与270之间
ultimate = ultimate+INCREMENT; //每次加INCREMENT值给ultimate
set_time_valu++; //set_time_valu自加
gfx->fillArc(cx, cy, outerR, innerR, originate, ultimate, WHITE); //画实心圆弧白色
}
//时间减
void Time_sub()
{
ultimate = constrain(ultimate, -90, 270); //将ultimate数值限制-90与270之间
set_time_valu--;
originate_1 = ultimate-INCREMENT;
gfx->fillArc(cx, cy, outerR, innerR, originate_1, ultimate, BLUE); //画实心圆弧
ultimate = ultimate-INCREMENT;
}
//远程温度调节
void Temperature_adjust()
{
server.on("/Temp_plus", []() {
server.send(200, "text/plain", "Temp plus");
if(Tem_switch==0){
Temperature++;
Temperature_Display();
}else{
SET_TEMP_STOP();
Temperature++;
Temperature_Display();
}
});
server.on("/Temp_sub", []() {
server.send(200, "text/plain", "Temp sub");
if(Tem_switch==0){
Temperature--;
Temperature_Display();
}else{
SET_TEMP_STOP();
Temperature--;
Temperature_Display();
}
});
}
//远程时间调节
void Time_adjust()
{
server.on("/Time_plus", []() {
server.send(200, "text/plain", "Time plus");
if(Tem_switch==1){
Time_plus();
Time_Display();
}else{
SET_time();
Time_plus();
Time_Display();
}
});
server.on("/Time_sub", []() {
server.send(200, "text/plain", "Time sub");
if(Tem_switch==1){
Time_sub();
Time_Display();
}else{
SET_time();
Time_sub();
Time_Display();
}
});
}
//WIFI账户与密码删除
void Wifi_rest()
{
server.on("/REST", []() {
server.send(200, "text/plain", "wifi rest");
ESP.eraseConfig();
delay(5000);
ESP.reset();
});
}
//远程定时控制
void timing_run()
{
server.on("/timing_control", []() {
server.send(200, "text/plain", "timing Start");
if(set_time_valu>0){
SET_RUN();
}
});
}
//远程停止控制
void Stop()
{
server.on("/STOP", []() {
server.send(200, "text/plain", "STOP");
SET_TEMP_STOP();
});
}
//开始显示界面
void Start_Display()
{
gfx->fillScreen(color_2); //给屏幕充满蓝色
gfx->fillArc(cx, cy, outerR, innerR, originate, 270, BLUE); //画蓝色实心圆弧
get_value = set_time_valu;
coordinate(); //坐标调整函数
x=x_var, x_time=x_var, y=140, Size=3, color_1=WHITE, value=set_time_valu; //给显示程序变量赋值
Value_Display(); //数值显示函数
gfx->fillArc(cx, cy, outerR, innerR, originate, ultimate, WHITE);
get_value = Temperature;
coordinate(); //坐标调整函数
x=x_var, x_temp=x_var, y=70, Size=3, color_1=WHITE, value=Temperature; //给显示程序变量赋值
Value_Display();
x_2=154, y_2=142, Size_1=2, color_3=DARKGREY, word_1="Min"; //屏幕上显示Min
Word_Display();
x_2=100, y_2=40, Size_1=2, color_3=DARKCYAN, word_1="TEMP"; //屏幕上显示TEMP
Word_Display();
x_2=100, y_2=110, Size_1=2, color_3=DARKCYAN, word_1="TIME"; //屏幕上显示TIME
Word_Display();
x_2=50, y_2=72, Size_1=2, color_3=BLUE, word_1="SET"; //屏幕上显示SET
Word_Display();
x_2=90, y_2=176, Size_1=3, color_3=RED, word_1="STOP"; //屏幕上显示STOP
Word_Display();
x_2=166, y_2=70, Size_1=3, color_3=WHITE, word_1="C"; //屏幕上显示C
Word_Display();
x_2=158, y_2=64, Size_1=1, color_3=WHITE, word_1="o"; //屏幕上显示O
Word_Display(); //字符串显示函数
}
//网页未发现反馈
void handleNotFound() {
// run when no route is found
String message = "File Not Found\n\n";
message += "URI: ";
// get request uri
message += server.uri();
message += "\nMethod: ";
// get request method
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
// get request args
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
// send with 404 code
server.send(404, "text/plain", message);
}