esp32全自动洗衣机程序,OLED显示,带连接WiFi天气预报,触摸按键,手搓谐振硬件测水位原理,外部中断计频率,硬件时间中断计时,多线程运行,全汉语拼音编程

#include <Arduino.h>
#include <U8g2lib.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <esp_task_wdt.h>

// 自定义CLK和SDA引脚
#define OLED_CLK  22  
#define OLED_SDA  21
//#define WDT_TIMEOUT 10000  //定义看门狗时间 单位毫秒
// 构造对象
U8G2_SSD1306_128X64_NONAME_F_SW_I2C OLED(U8G2_R0, OLED_CLK, OLED_SDA, U8X8_PIN_NONE);
const char *ssid="zxcvbb";                      //输入自己的wifi账号
const char *password="asdf123456";         //输入自己的wifi密码
//定义   此处是聚合数据官网给的,可以自己创建账号获得key参数
String url="http://apis.juhe.cn/simpleWeather/query";    //请求网址响应
String city="东丰";                                      //输入想要获得的城市
String key="df39b169ab000f5874c44a5a8027f5f4";

int shui_wei_xuan_ze_an_niu;                 //输入IO
int cheng_xu_xuan_ze_an_niu;
int qi_ting_an_niu;
int dian_yuan_an_niu;

const int yun_xing_deng = 2;         //输出IO
const int li_he_qi = 4;
const int feng_ming = 23;
const int jin_shui = 25;
const int pai_shui = 26;
const int shui_wei_ce_liang = 27;
const int zheng_zhuan = 32;
const int fan_zhuan = 33;

unsigned long miao_lei_ji;
unsigned long bao_jing_ji_shi;
unsigned long chang_yan_shi;
unsigned long zong_shi_jian;
int piao_xi_ci_shu=3;

boolean yun_xing;
boolean qi_ting_biao_zhi;
boolean pai_jin_shui_zan_ting;
int lian_wang_huo_qu;
int huo_qu_tian_qi;
int xian_shi_biao_zhi;
int kong_qi_zhi_shu;
String dang_qian_ri_qi;
String wen_du;
String jin_ri_tian_qi;
String feng_li_ji_shu;
String feng_xiang;
String cheng_xu_xian_shi;
String shui_wei_xian_shi;
int xi_biao_zhi;
int piao_xi_biao_zhi;
int tuo_shui_biao_zhi;
int jin_shui_wan_biao_zhi;
int pai_shui_wan_biao_zhi;
int cheng_xu_xuan_ze_ji_shu;
int dan_xi;
int dan_piao_xi;
int dan_tuo_shui;
int xi_piao_xi;
int xi_tuo_shui;
int piao_xi_tuo_shui;
int pin_lv;
int shui_wei_pin_lv;
int shui_wei_xuan_ze_ji_shu;
int dao_ji_shi;
int dao_ji_shi_fen;
int dao_ji_shi_miao;
hw_timer_t *timer = NULL;//定时中断
volatile double fre = 0;//频率
unsigned long xi_tong_hao_miao;
unsigned long hao_miao_0 = 0; // 毫秒时间记录
const long hao_miao_100 = 100; // 时间间隔
unsigned long fang_dou = 0; // 毫秒时间记录
const long fang_dou_yan_shi = 500; // 时间间隔
volatile unsigned long raiseTime = 0;//前一次上升沿时间
volatile unsigned long fallTime = 0;//前一次下降沿时间
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;//自旋锁

void TaskBlink1(void *pvParameters);
void TaskBlink2(void *pvParameters);
void TaskBlink3(void *pvParameters);

void tian_qi()
{
  OLED.setCursor(0,10);
  OLED.print("属地 东丰");
  OLED.setCursor(62,10);
  OLED.print(dang_qian_ri_qi);
  OLED.setCursor(0,27);
  OLED.print("温度");
  OLED.setCursor(25,27);
  OLED.print(wen_du);
  OLED.setCursor(70,27);
  OLED.print(feng_li_ji_shu);
  OLED.setCursor(90,27);
  OLED.print(feng_xiang);
  OLED.setCursor(0,43);
  OLED.print("空指");
  OLED.setCursor(28,43);
  OLED.print(kong_qi_zhi_shu);
  OLED.setCursor(44,43);
  OLED.print("今天");
  OLED.setCursor(68,43);
  OLED.print(jin_ri_tian_qi);
}

void changeISR()//外部中断操作函数一秒钟频率
{
  if(digitalRead(shui_wei_ce_liang))//现在是高电平
  {
    fre++;
  }
}

void timer_init(void)           //硬件定时中断初始化
{
  timer = timerBegin(0, 80, true);      //0定时器变化可选0-3,80为主频80MHz分频,true为向上计数,false向下计数
  timerAttachInterrupt(timer, timerIAR, true);         //timer指向已初始化的定时器,timerIAR定时器函数,true边沿触发 false电平触发
  timerAlarmWrite(timer,1000*1000,true);            //timer指向已初始化的定时器,1000*1000为1秒,true自动重载false只触发一次
  timerAlarmEnable(timer);               //启动定时器,Disable关闭定时器
}

void timerIAR()        //硬件定时中断函数
{
  pin_lv = fre;
  fre = 0;
}

void cheng_xu_fu_wei()
{
  dan_xi=0;
  dan_piao_xi=0;
  dan_tuo_shui=0;
  xi_piao_xi=0;
  xi_tuo_shui=0;
  piao_xi_tuo_shui=0;
}

void cheng_xu_xuan_ze_an_niu_cheng_xu()
{
  if(cheng_xu_xuan_ze_an_niu<70)//程序选择
  {
    if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
    {
      fang_dou=xi_tong_hao_miao;
      if(cheng_xu_xuan_ze_an_niu<70)
      {
        while(1)
        {
          esp_task_wdt_reset();                         //硬件看门狗复位
          dian_yuan_an_niu=touchRead(15);
          qi_ting_an_niu=touchRead(14);
          cheng_xu_xuan_ze_an_niu=touchRead(13);
          xian_shi_biao_zhi=1;
          Serial.print(cheng_xu_xuan_ze_ji_shu);Serial.println(":程序标,");
          if(cheng_xu_xuan_ze_an_niu<70)//程序选择
          {
            cheng_xu_xuan_ze_ji_shu++;
            if(cheng_xu_xuan_ze_ji_shu > 9)
            {
              cheng_xu_xuan_ze_ji_shu=1;
            }
            while(1)
            {
              esp_task_wdt_reset();
              cheng_xu_xuan_ze_an_niu=touchRead(13);
              if(cheng_xu_xuan_ze_an_niu>70)
              {
                break;
              }
            }
          }
          if(qi_ting_an_niu<70)//启动
          {
            if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
            {
              fang_dou=xi_tong_hao_miao;
              qi_ting_biao_zhi =! qi_ting_biao_zhi;
              if(qi_ting_an_niu<70)
              {
                while(1)
                {
                  esp_task_wdt_reset();
                  qi_ting_an_niu=touchRead(14);
                  if(qi_ting_an_niu>70)
                  {
                    break;
                  }
                }
              }
            }
            break; 
          }
          if(dian_yuan_an_niu<70)//关闭电源
          {
            break;
          }
        }
      }
    }  
  }
}

void shui_wei_xuan_ze_an_niu_cheng_xu()
{
  if(shui_wei_xuan_ze_an_niu<70)//水位选择
  {
    if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
    {
      fang_dou=xi_tong_hao_miao;
      if(shui_wei_xuan_ze_an_niu<70)
      {
        while(1)
        {
          esp_task_wdt_reset();
          dian_yuan_an_niu=touchRead(15);
          qi_ting_an_niu=touchRead(14);
          shui_wei_xuan_ze_an_niu=touchRead(12);
          xian_shi_biao_zhi=2;
          Serial.print(shui_wei_xuan_ze_ji_shu);Serial.println(":水位标,");
          if(shui_wei_xuan_ze_an_niu<70)//水位选择
          {
            shui_wei_xuan_ze_ji_shu++;
            if(shui_wei_xuan_ze_ji_shu > 6)
            {
              shui_wei_xuan_ze_ji_shu=1;
            }
            while(1)
            {
              esp_task_wdt_reset();
              shui_wei_xuan_ze_an_niu=touchRead(12);
              if(shui_wei_xuan_ze_an_niu>70)
              {
                break;
              }
            }
          }
          if(qi_ting_an_niu<70)//启动
          {
            if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
            {
              fang_dou=xi_tong_hao_miao;
              qi_ting_biao_zhi =! qi_ting_biao_zhi;
              if(qi_ting_an_niu<70)
              {
                while(1)
                {
                  esp_task_wdt_reset();
                  qi_ting_an_niu=touchRead(14);
                  if(qi_ting_an_niu>70)
                  {
                    break;
                  }
                }
              }
            }
            break; 
          }
          if(dian_yuan_an_niu<70)//关闭电源
          {
            break;
          }
        }
      }
    }  
  }
}
      
void xi_cheng_xu_shui_wei_que_ding_cheng_xu()
{
  if(cheng_xu_xuan_ze_ji_shu==1)   //确定自动程序
  {
    cheng_xu_fu_wei();
    zong_shi_jian=0;
    dao_ji_shi=22610;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==2)   //确定洗程序
  {
    cheng_xu_fu_wei();
    dan_xi=1;
    zong_shi_jian=0;
    dao_ji_shi=9000;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==3)
  {
    cheng_xu_fu_wei();
    dan_piao_xi=1;
    zong_shi_jian=9000;
    dao_ji_shi=11110;
    pai_jin_shui_zan_ting=1;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==4)
  {
    cheng_xu_fu_wei();
    dan_tuo_shui=1;
    zong_shi_jian=20110;
    dao_ji_shi=2500;
    pai_jin_shui_zan_ting=1;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==5)
  {
    cheng_xu_fu_wei();
    xi_piao_xi=1;
    zong_shi_jian=0;
    dao_ji_shi=20110;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==6)
  {
    cheng_xu_fu_wei();
    xi_tuo_shui=1;
    zong_shi_jian=0;
    dao_ji_shi=11500;
    cheng_xu_xuan_ze_ji_shu=0;
  }
  if(cheng_xu_xuan_ze_ji_shu==7)
  {
    cheng_xu_fu_wei();
    pai_jin_shui_zan_ting=1;
    piao_xi_tuo_shui=1;
    zong_shi_jian=9000;
    dao_ji_shi=13610;
    cheng_xu_xuan_ze_ji_shu=0;
  }
      
  if(shui_wei_xuan_ze_ji_shu==1)     //确定水位
  {
    shui_wei_pin_lv=3000;
    shui_wei_xuan_ze_ji_shu=0;
  }
  if(shui_wei_xuan_ze_ji_shu==2)
  {
    shui_wei_pin_lv=4000;
    shui_wei_xuan_ze_ji_shu=0;
  }
  if(shui_wei_xuan_ze_ji_shu==3)
  {
    shui_wei_pin_lv=5000;
    shui_wei_xuan_ze_ji_shu=0;
  }
  if(shui_wei_xuan_ze_ji_shu==4)
  {
    shui_wei_pin_lv=6000;
    shui_wei_xuan_ze_ji_shu=0;
  }
  if(shui_wei_xuan_ze_ji_shu==5)
  {
    shui_wei_pin_lv=7000;
    shui_wei_xuan_ze_ji_shu=0;
  }
  if(shui_wei_xuan_ze_ji_shu==6)
  {
    shui_wei_pin_lv=8000;
    shui_wei_xuan_ze_ji_shu=0;
  }
}

void fu_wei_guan_jiao()
{
  digitalWrite(li_he_qi,LOW);
  digitalWrite(jin_shui,LOW);
  digitalWrite(pai_shui,LOW);
  digitalWrite(zheng_zhuan,LOW);
  digitalWrite(fan_zhuan,LOW);
}

void fu_wei_ji_shi()
{
  bao_jing_ji_shi=0;
  miao_lei_ji=0;
}
void feng_ming_qi()
{
  if(bao_jing_ji_shi>11)
  {
    bao_jing_ji_shi=0;
  }
  if(bao_jing_ji_shi<5)
  {
    digitalWrite(feng_ming,HIGH);
  }
  if(5<=bao_jing_ji_shi<11)
  {
    digitalWrite(feng_ming,LOW);
  }
}

void xi_cheng_xu()
{
  if(jin_shui_wan_biao_zhi==0)
  {
    if(miao_lei_ji>3000)
    {
      feng_ming_qi();
    }
    if(miao_lei_ji>3050)
    {
      yun_xing=0;
    }
    digitalWrite(jin_shui,HIGH);
    pai_jin_shui_zan_ting=0;
  }
  if(jin_shui_wan_biao_zhi==1)
  {
    digitalWrite(jin_shui,LOW);
    pai_jin_shui_zan_ting=1;
  }
  if(pai_jin_shui_zan_ting==1 & jin_shui_wan_biao_zhi==1 & digitalRead(jin_shui)==0 & digitalRead(pai_shui)==0)
  {
    if(miao_lei_ji>300)
    {
      miao_lei_ji=0;
    }
    if(miao_lei_ji<100)
    {
      digitalWrite(zheng_zhuan,HIGH);
    }
    if(100<=miao_lei_ji&miao_lei_ji<150)
    {
      digitalWrite(zheng_zhuan,LOW);
    }
    if(150<=miao_lei_ji&miao_lei_ji<250)
    {
      digitalWrite(fan_zhuan,HIGH);
    }
    if(250<=miao_lei_ji&miao_lei_ji<300)
    {
      digitalWrite(fan_zhuan,LOW);
    }
  }
}

void tuo_shui_ping_heng()
{
  if(pai_shui_wan_biao_zhi==0)
  {
    if(miao_lei_ji>3000)
    {
      feng_ming_qi();
    }
    if(miao_lei_ji>3050)
    {
      yun_xing=0;
    }
    pai_jin_shui_zan_ting=0;
  }
  if(pai_shui_wan_biao_zhi==1)
  {
    pai_jin_shui_zan_ting=1;
  }
  if(pai_jin_shui_zan_ting==1 &pai_shui_wan_biao_zhi==1 & digitalRead(jin_shui)==0 & digitalRead(pai_shui)==1& digitalRead(li_he_qi)==1)
  {
    if(miao_lei_ji>150)
    {
      miao_lei_ji=0;
    }
    if(miao_lei_ji<50)
    {
      digitalWrite(zheng_zhuan,HIGH);
    }
    if(50<=miao_lei_ji&miao_lei_ji<150)
    {
      digitalWrite(zheng_zhuan,LOW);
    }
  }
}

void piao_xi()
{
  if(chang_yan_shi<1300)
  {
    digitalWrite(pai_shui,HIGH);
    digitalWrite(li_he_qi,HIGH);
    if(98<=chang_yan_shi & chang_yan_shi<700)
    {
      tuo_shui_ping_heng();
    }
    if(700<=chang_yan_shi)
    {
      digitalWrite(zheng_zhuan,HIGH);
    }
  }
  if(1300<=chang_yan_shi & chang_yan_shi<1302)
  {
    fu_wei_guan_jiao();
  }
  if(1302<=chang_yan_shi)
  {
    xi_cheng_xu();
    if(3700<=chang_yan_shi)
    {
      piao_xi_ci_shu-=1;
      if(0 < piao_xi_ci_shu);
      {
        chang_yan_shi=0;
      }
    }
  }
}

void tuo_shui()
{ 
  digitalWrite(pai_shui,HIGH);
  digitalWrite(li_he_qi,HIGH);
  if(100<=chang_yan_shi & chang_yan_shi<1300)
  {
    tuo_shui_ping_heng();
  }
  if(1300<=chang_yan_shi)
  {
    digitalWrite(zheng_zhuan,HIGH);
  }
}


void setup()
{
  
  Serial.begin(115200);
  timer_init();    //初始化硬件定时器
  //esp_task_wdt_init(WDT_TIMEOUT, true);//初始化看门狗
  OLED.begin(); //初始化屏幕
  OLED.enableUTF8Print();//设置字体
  OLED.setFont(u8g2_font_wqy12_t_gb2312b);//设置字体方向gb2312字符数7539字节208228 ,chinese1字符数411字节9491
  OLED.setFontDirection(0);
  pinMode(yun_xing_deng,OUTPUT);
  pinMode(feng_ming,OUTPUT);
  pinMode(jin_shui,OUTPUT);
  pinMode(li_he_qi,OUTPUT);
  pinMode(pai_shui,OUTPUT);
  pinMode(zheng_zhuan,OUTPUT);
  pinMode(fan_zhuan,OUTPUT);
  pinMode(shui_wei_ce_liang,INPUT_PULLUP);//设定引脚上拉_PULLUP  设定引脚下拉_PULLDOWN
  attachInterrupt(digitalPinToInterrupt(shui_wei_ce_liang), changeISR, RISING);//中断指定引脚函数LOW低电平触发CHANGE电平变化RISING上升沿触发FALLING下降沿触发HIGH高电平触发
  
  xTaskCreate(
  TaskBlink1
  ,  "TaskBlink1"   // 任务名
  ,  2048  // This stack size can be checked & adjusted by reading the Stack Highwater
  ,  NULL
  ,  0  // 任务优先级, with 3 (configMAX_PRIORITIES - 1) 是最高的,0是最低的.
  ,  NULL 
  ); 

  xTaskCreate(
  TaskBlink2
  ,  "TaskBlink2" //任务名
  ,  2048  // 栈大小
  ,  NULL
  ,  0  // 任务优先级
  ,  NULL 
  ); 

  xTaskCreate(
  TaskBlink3
  ,  "TaskBlink3" //任务名
  ,  10000  // 栈大小
  ,  NULL
  ,  0  // 任务优先级
  ,  NULL 
  ); 
  //delay(1000);
}

void loop(){}

void TaskBlink1(void *pvParameters)   // 多线程1 主程序
{  
  while (1)// for死循环// 多线程,每个任务必须是死循环
  { 
    fu_wei_ji_shi();
    digitalWrite(yun_xing_deng,0);
    fu_wei_guan_jiao();
    cheng_xu_fu_wei();
    dian_yuan_an_niu=touchRead(15);//读取触摸值
    qi_ting_an_niu=touchRead(14);
    cheng_xu_xuan_ze_an_niu=touchRead(13);
    shui_wei_xuan_ze_an_niu=touchRead(12);
    Serial.print(dian_yuan_an_niu);Serial.print(":电源--");Serial.print(qi_ting_an_niu);Serial.print(":启停--");Serial.print(cheng_xu_xuan_ze_an_niu);Serial.print(":程序--");Serial.print(shui_wei_xuan_ze_an_niu);Serial.print(":水位--");Serial.print(pin_lv);Serial.println(":频率计");
    zong_shi_jian=0;
    chang_yan_shi=0;
    dao_ji_shi = 22610;
    qi_ting_biao_zhi=0;
    pai_jin_shui_zan_ting=0;
    cheng_xu_xuan_ze_ji_shu=1;
    shui_wei_xuan_ze_ji_shu=1;
    cheng_xu_xian_shi="自动";
    shui_wei_xian_shi="10升水";
    unsigned long xi_tong_hao_miao = millis();
  
    if(dian_yuan_an_niu<70)
    {
      if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
      {
        fang_dou=xi_tong_hao_miao;
        if(dian_yuan_an_niu<70)
        {
          digitalWrite(yun_xing_deng,1);
          while(1)
          {
            esp_task_wdt_reset();
            dian_yuan_an_niu=touchRead(15);
            if(dian_yuan_an_niu>70)
            {
              yun_xing =! yun_xing;
              xian_shi_biao_zhi=0;
              break;
            }
          }
        }
      }  
    }
    if(yun_xing==1)
    {
      while(1)
      {
        Serial.print(digitalRead(jin_shui));Serial.print(":进水阀,");Serial.print(digitalRead(li_he_qi));Serial.print(":离合器,");      
        Serial.print(digitalRead(pai_shui));Serial.print(":排水阀,");Serial.print(digitalRead(feng_ming));Serial.print(":蜂鸣器,");      
        Serial.print(digitalRead(zheng_zhuan));Serial.print(":正转,");Serial.print(digitalRead(fan_zhuan));Serial.print(":反转,");Serial.print(pin_lv);Serial.print(":频率计,");      
        Serial.print(pai_shui_wan_biao_zhi);Serial.print(jin_shui_wan_biao_zhi);Serial.print(pai_jin_shui_zan_ting);Serial.print(xi_biao_zhi);Serial.print(piao_xi_biao_zhi);Serial.print(tuo_shui_biao_zhi);Serial.print(":标志,");Serial.print(cheng_xu_xuan_ze_ji_shu);Serial.print(":程序标,");      
        Serial.print(miao_lei_ji);Serial.print(":秒累计,");Serial.print(bao_jing_ji_shi);Serial.print(":报警时,");Serial.print(chang_yan_shi);Serial.print(":长延时,");Serial.print(dao_ji_shi);Serial.print(":倒计时");Serial.print(zong_shi_jian);Serial.print(":总时间,");
        Serial.println();
        unsigned long xi_tong_hao_miao = millis();
        dian_yuan_an_niu=touchRead(15);
        qi_ting_an_niu=touchRead(14);
        cheng_xu_xuan_ze_an_niu=touchRead(13);
        shui_wei_xuan_ze_an_niu=touchRead(12);
        
        if(dian_yuan_an_niu<70)//关闭电源
        {
          if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
          {
            fang_dou=xi_tong_hao_miao;
            if(dian_yuan_an_niu<70)
            {
              while(1)
              {
                esp_task_wdt_reset();
                dian_yuan_an_niu=touchRead(15);
                if(dian_yuan_an_niu>70)
                {
                  yun_xing=0;
                  break;
                }
              }
              break;
            }
          }  
        }

        if(qi_ting_an_niu<70)//启动
        {
          if(xi_tong_hao_miao - fang_dou > fang_dou_yan_shi)
          {
            fang_dou=xi_tong_hao_miao;
            qi_ting_biao_zhi =! qi_ting_biao_zhi;
            if(qi_ting_an_niu<70)
            {
              while(1)
              {
                esp_task_wdt_reset();
                qi_ting_an_niu=touchRead(14);
                if(qi_ting_an_niu>70)
                {
                  break;
                }
              }
            }
          }  
        }
      
        if(pin_lv > 8000)
        {
          pai_shui_wan_biao_zhi=1;
        }
        else{pai_shui_wan_biao_zhi=0;}
        if(pin_lv < shui_wei_pin_lv)
        {
          jin_shui_wan_biao_zhi=1;
        }
        else{jin_shui_wan_biao_zhi=0;}

        if(qi_ting_biao_zhi==1 )
        {
          if(xi_tong_hao_miao - hao_miao_0 > hao_miao_100)
          {
            hao_miao_0=xi_tong_hao_miao;
            miao_lei_ji++;
            bao_jing_ji_shi++;
            if(pai_jin_shui_zan_ting==1)
            {
              dao_ji_shi = dao_ji_shi - 1;
              zong_shi_jian++;
              chang_yan_shi++;
            }
            else{digitalWrite(fan_zhuan,LOW);digitalWrite(zheng_zhuan,LOW);}
            xian_shi_biao_zhi=0;
          }
          if(0<=zong_shi_jian & zong_shi_jian<9000)//洗时间
          {
            xi_biao_zhi=1;
            piao_xi_biao_zhi=0;
            tuo_shui_biao_zhi=0;
          }
          if(9000<=zong_shi_jian & zong_shi_jian<20110)//漂洗时间
          {
            piao_xi_biao_zhi=1;
            xi_biao_zhi=0;
            tuo_shui_biao_zhi=0;
          }
          if(20110<=zong_shi_jian & zong_shi_jian<22611)//脱水时间
          {
            tuo_shui_biao_zhi=1;
            xi_biao_zhi=0;
            piao_xi_biao_zhi=0;
          }
        }
        if(qi_ting_biao_zhi==0)
        {
          fu_wei_guan_jiao();
          fu_wei_ji_shi();
          xi_biao_zhi=0;
          piao_xi_biao_zhi=0;
          tuo_shui_biao_zhi=0;
          cheng_xu_xuan_ze_an_niu_cheng_xu();
          shui_wei_xuan_ze_an_niu_cheng_xu();
          xi_cheng_xu_shui_wei_que_ding_cheng_xu();
        }
        if(9000<=zong_shi_jian & zong_shi_jian<9002)//洗完复位计时
        {
          fu_wei_guan_jiao();
          fu_wei_ji_shi();
          chang_yan_shi=0;
          if(dan_xi==1)
          {
            yun_xing=0;
          }
          if(xi_tuo_shui==1)
          {
            zong_shi_jian=20110;
          }
        }      
        if(20110<=zong_shi_jian & zong_shi_jian<20112)//漂洗完复位计时
        {
          fu_wei_guan_jiao();
          fu_wei_ji_shi();
          chang_yan_shi=0;
          if(dan_piao_xi==1|xi_piao_xi==1)
          {
            yun_xing=0;
          }
        }      
        if(22616<zong_shi_jian)//结束退出
        {
          yun_xing=0;
          break;
        }
        if(yun_xing==0)//结束退出
        {
          break;
        }
        if(xi_biao_zhi==1)
        {
          xi_cheng_xu();
        }
        if(piao_xi_biao_zhi==1)
        {
          piao_xi();
        }
        if(tuo_shui_biao_zhi==1)
        {
          tuo_shui();
        }
      

      }
    }
  }
}

void TaskBlink2(void *pvParameters)    // 多线程2 OLED显示
{  
  OLED.begin(); //初始化屏幕
  OLED.enableUTF8Print();//设置字体
  
  OLED.setFontDirection(0);
  while (1)   // A Task shall never return or exit.
  { 
    if(yun_xing==1)
    {
      if(xian_shi_biao_zhi==0)
      {
        dao_ji_shi_fen=dao_ji_shi/10/60;
        dao_ji_shi_miao=dao_ji_shi/10%60;
        //OLED.setFont(u8g2_font_wqy12_t_gb2312);//设置字体大小字型
        OLED.clearBuffer();
        tian_qi();
        OLED.setCursor(0,59);
        OLED.print(cheng_xu_xian_shi);
        OLED.setCursor(30,59);
        OLED.print("剩余时间");
        OLED.setCursor(100,59);
        OLED.print(":");
        OLED.setCursor(85,59);
        OLED.print(dao_ji_shi_fen);
        OLED.setCursor(105,59);
        OLED.print(dao_ji_shi_miao);
        OLED.sendBuffer();
      }
      if(xian_shi_biao_zhi==1)
      {
        if(cheng_xu_xuan_ze_ji_shu==1)
        {
          cheng_xu_xian_shi="自动";
        }
        if(cheng_xu_xuan_ze_ji_shu==2)
        {
          cheng_xu_xian_shi="单洗";
        }
        if(cheng_xu_xuan_ze_ji_shu==3)
        {
          cheng_xu_xian_shi="单漂";
        }
        if(cheng_xu_xuan_ze_ji_shu==4)
        {
          cheng_xu_xian_shi="单脱";
        }
        if(cheng_xu_xuan_ze_ji_shu==5)
        {
          cheng_xu_xian_shi="洗漂";
        }
        if(cheng_xu_xuan_ze_ji_shu==6)
        {
          cheng_xu_xian_shi="洗脱";
        }
        if(cheng_xu_xuan_ze_ji_shu==7)
        {
          cheng_xu_xian_shi="漂脱";
        }
        if(cheng_xu_xuan_ze_ji_shu==8)
        {
          cheng_xu_xian_shi="重新获取天气";
          huo_qu_tian_qi=0;
        }
        if(cheng_xu_xuan_ze_ji_shu==9)
        {
          cheng_xu_xian_shi="重新连接WiFi";
          lian_wang_huo_qu=0;
        }
        //OLED.setFont(u8g2_font_wqy16_t_gb2312);//设置字体大小字型
        OLED.clearBuffer();
        OLED.setCursor(42,27);
        OLED.print("程序选择");
        OLED.setCursor(30,43);
        OLED.print(cheng_xu_xian_shi);
        OLED.sendBuffer();
      }
      if(xian_shi_biao_zhi==2)
      {
        if(shui_wei_xuan_ze_ji_shu==1)
        {
          shui_wei_xian_shi="10升水";
        }
        if(shui_wei_xuan_ze_ji_shu==2)
        {
          shui_wei_xian_shi="9升水";
        }
        if(shui_wei_xuan_ze_ji_shu==3)
        {
          shui_wei_xian_shi="8升水";
        }
        if(shui_wei_xuan_ze_ji_shu==4)
        {
          shui_wei_xian_shi="7升水";
        }
        if(shui_wei_xuan_ze_ji_shu==5)
        {
          shui_wei_xian_shi="6升水";
        }
        if(shui_wei_xuan_ze_ji_shu==6)
        {
          shui_wei_xian_shi="5升水";
        }
        
        OLED.clearBuffer();
        OLED.setCursor(44,27);
        OLED.print("水量选择");
        OLED.setCursor(50,43);
        OLED.print(shui_wei_xian_shi);
        OLED.sendBuffer();
      }
      
    }
    else{OLED.clearDisplay();}
    if(xian_shi_biao_zhi==3)
    {
      OLED.clearBuffer();
      OLED.setCursor(10,32);
      OLED.print("正在连接网络请稍等");
      OLED.sendBuffer();
    }
    if(xian_shi_biao_zhi==4)
    {
      OLED.clearBuffer();
      OLED.setCursor(10,32);
      OLED.print("正在获取天气请稍等");
      OLED.sendBuffer();
    }
  }
}

void TaskBlink3(void *pvParameters)    // 多线程3联网获取天气数据
{  
  xian_shi_biao_zhi=3;
  while (1)   // A Task shall never return or exit.
  { 
    if(lian_wang_huo_qu == 0)
    {
      esp_task_wdt_reset();
      WiFi.begin(ssid,password);
      while(WiFi.status()!=WL_CONNECTED){
      delay(500);
      Serial.print(".");
      }
      xian_shi_biao_zhi=0;
      lian_wang_huo_qu=1;
    }
    
    if(lian_wang_huo_qu==1)
    {
      if(huo_qu_tian_qi==0)
      {
        xian_shi_biao_zhi=4;
        //创建HTTPCLient 对象
        HTTPClient http;
        //发送GET请求
        http.begin(url+"?city="+city+"&key="+key);
        int httpCode=http.GET();
        //获取响应状态码
        Serial.printf("HTTP 状态码:%d",httpCode);
        //获取响应正文
        String response=http.getString();
        Serial.println("响应数据");
        Serial.println(response);
        http.end();
        //创建DynamicJsonDocument对象
        DynamicJsonDocument doc(1024);
        //解析JSON数据
        deserializeJson(doc,response);
        //从解析后的JSON文档中获取值
        kong_qi_zhi_shu = doc["result"]["realtime"]["aqi"].as<int>();
        dang_qian_ri_qi = doc["result"]["future"][0]["date"].as<String>();
        wen_du = doc["result"]["future"][0]["temperature"].as<String>();
        jin_ri_tian_qi = doc["result"]["future"][0]["weather"].as<String>();
        feng_xiang = doc["result"]["realtime"]["direct"].as<String>();
        feng_li_ji_shu = doc["result"]["realtime"]["power"].as<String>();
        xian_shi_biao_zhi=0;
        huo_qu_tian_qi=1;

      }
    }
  }
}


 

 

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴听到您对ESP8266 OLED屏幕的应用感兴趣。下面是一个简单的程序示例,它可以实现天气预报、温度显示、NTP时间同步和中文显示。 首先,您需要准备以下材料: - ESP8266开发板(如NodeMCU) - OLED屏幕(如0.96英寸128x64 OLED) - DHT11温湿度传感器 - 一些杜邦线 - Arduino IDE(或其他适当的IDE) 接下来,让我们开始编写程序: 1. 引入所需库文件 ```c++ #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> ``` 2. 定义OLED屏幕对象和温湿度传感器对象 ```c++ #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); #define DHTPIN 5 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); ``` 3. 定义WiFi连接信息和NTP服务器信息 ```c++ const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* ntpServerName = "cn.pool.ntp.org"; const int timeZone = 8; WiFiUDP udp; IPAddress localIP; ``` 4. 连接WiFi和获取本地IP地址 ```c++ void connectWiFi() { Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); localIP = WiFi.localIP(); Serial.println(localIP); } ``` 5. 从NTP服务器获取当前时间 ```c++ void getNTPTime() { byte packetBuffer[NTP_PACKET_SIZE]; memset(packetBuffer, 0, NTP_PACKET_SIZE); packetBuffer[0] = 0b11100011; packetBuffer[1] = 0; packetBuffer[2] = 6; packetBuffer[3] = 0xEC; packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; udp.beginPacket(ntpServerName, 123); udp.write(packetBuffer, NTP_PACKET_SIZE); udp.endPacket(); delay(1000); int cb = udp.parsePacket(); if (cb) { udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears; epoch += timeZone * SECS_PER_HOUR; setTime(epoch); } } ``` 6. 定义获取天气信息的函数 ```c++ void getWeather() { WiFiClient client; const int httpPort = 80; if (!client.connect("api.openweathermap.org", httpPort)) { Serial.println("connection failed"); return; } String url = "/data/2.5/weather?q=Beijing,cn&lang=zh_cn&units=metric&appid=your_APPID"; Serial.print("Requesting URL: "); Serial.println(url); client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: api.openweathermap.org\r\n" + "Connection: close\r\n\r\n"); while (!client.available()) { delay(100); } String line; while (client.available()) { line = client.readStringUntil('\r'); if (line.indexOf("\"temp\":") >= 0) { String temp = line.substring(line.indexOf("\"temp\":") + 7, line.indexOf(",")); Serial.println("Temperature: " + temp + "°C"); display.setCursor(0, 10); display.print("Temperature: "); display.print(temp); display.print("C"); } if (line.indexOf("\"description\":") >= 0) { String desc = line.substring(line.indexOf("\"description\":") + 15, line.indexOf("\",")); Serial.println("Description: " + desc); display.setCursor(0, 20); display.print("Description: "); display.print(desc); } } } ``` 7. 在setup函数中初始化各组件 ```c++ void setup() { Serial.begin(115200); dht.begin(); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display(); delay(2000); display.clearDisplay(); connectWiFi(); udp.begin(2390); setSyncProvider(getNTPTime); display.setCursor(0, 0); display.print("IP address: "); display.println(localIP); display.display(); delay(2000); display.clearDisplay(); display.setCursor(0, 0); display.print("NTP time: "); display.println(getFormattedTime()); display.display(); delay(2000); display.clearDisplay(); getWeather(); } ``` 8. 在loop函数中更新温湿度和时间 ```c++ void loop() { delay(2000); float t = dht.readTemperature(); float h = dht.readHumidity(); display.setCursor(0, 30); display.print("Temp: "); display.print(t); display.print("C"); display.setCursor(0, 40); display.print("Humidity: "); display.print(h); display.print("%"); display.setCursor(0, 50); display.print("Time: "); display.println(getFormattedTime()); display.display(); } ``` 9. 定义一个辅助函数,将当前时间格式化为字符串 ```c++ String getFormattedTime() { char buffer[20]; sprintf(buffer, "%02d:%02d:%02d", hour(), minute(), second()); return String(buffer); } ``` 这只是一个简单的程序示例,您可以根据自己的需要进行修改和扩展。希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值