• ESP32 入门笔记06: WIFI时钟 + FreeRTOS+《两只老虎》 (ESP32 for Arduino IDE)


    ESP32+FreeRTOS

    Esp32 模块中已经提供了 FreeRTOS(实时操作系统)固件。

    FreeRTOS有助于提高系统性能管理模块的资源。FreeRTOS允许用户处理多项任务,如测量传感器读数,发出网络请求,控制电机速度等,所有这些任务都可以同时独立运行。

    FreeRTOS为不同的应用程序提供了多个API。这些 API 可用于创建任务并使其在不同内核上运行。您需要创建任务以将代码的特定部分分配给特定内核。您还可以确定代码将在哪个内核上运行的优先级。

    硬件

    在这里插入图片描述

    软件

    在这里插入图片描述

    tone() 函数

    Arduino 内置函数:Reference > Language > Functions > Advanced io > Tone

    在引脚上生成指定频率(和 50% 占空比)的方波。可以指定持续时间,否则波形会一直持续到调用 noTone()。该引脚可以连接到压电蜂鸣器或其他扬声器以播放音调。 一次只能产生一种音调。如果一个音调已经在另一个引脚上播放,则对tone() 的调用将无效。如果音调在同一个引脚上播放,则呼叫将设置其频率。 使用tone() 函数会干扰引脚3 和11 上的PWM 输出(在Mega以外的板上)。 不可能产生低于 31Hz 的音调

    语法

    tone(pin, frequency) // 语法一
    
    • 1
    tone(pin, frequency, duration) // 语法二
    
    • 1
    tone(17, 1760, 1000, 0);  // 17:管脚  1760:频率 1000:延时 0:通道
    
    • 1

    在这里插入图片描述

    入口参数:

    • pin: 生成音调的 Arduino 引脚。
    • frequency: 以赫兹为单位的音调频率。允许的数据类型: unsigned int 无符号整数。
    • duration: 以毫秒为单位的音调持续时间(可选)。允许的数据类型:unsigned long

    noTone()函数

    停止生成由tone() 触发的方波。如果没有生成音调,则无效。

    语法

    noTone(pin)
    
    • 1

    注意: 如果您想在多个引脚上播放不同的音高,您需要在一个引脚上调用 noTone(),然后再在下一个引脚上调用tone()。

    示例程序P1

    开发环境是VSCode + PlatformIO IDE

    #include 
    #include 
    #include 
    #include 
    
    // buzzer pin
    #define BUZZER 19
    #define ADC_PIN 34
    uint16_t adc = 0;
    U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // U8G2_R0:屏幕旋转0°度
    
    
    /**
      * @brief  屏幕显示界面1
      * @param  无
      * @param  无
      * @retval adc:检测电池电量ADC34
      */
    void page1(int adc) {
      u8g2.setFont(u8g2_font_helvR08_tf); //设置字体 字号 字形
      u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
      u8g2.setCursor(0,0);  // 设置屏幕坐标
      u8g2.print(String("ADC:") + String(adc));
      u8g2.nextPage();//刷新屏幕
    }
    
    /**
      * @brief  《两只老虎》简谱
      * @param  无
      * @param  无
      * @retval count:计数
      */
    void two_tigers_music_play()
    {
    byte count = 0;                 // byte 占1个字节 1*8bit=8位 0~256 节省内存
    // low 1-7 低音
    unsigned int  LOW_1 = 262;      // unsigned int 占4个字节 4*8bit=32位  0~4294967295
    unsigned short LOW_1_P_5 = 277; // unsigned short 等于 uint16_t
    uint16_t LOW_2 = 294;           // uint16_t 占2个字节 2*8bit=16位  0~65535
    uint16_t LOW_2_P_5 = 311;
    uint16_t LOW_3 = 330;
    uint16_t LOW_4 = 349;
    uint16_t LOW_4_P_5 = 370;
    uint16_t LOW_5 = 392;
    uint16_t LOW_5_P_5 = 415;
    uint16_t LOW_6 = 440;
    uint16_t LOW_6_P_5 = 466;
    uint16_t LOW_7 = 494;
    
    for (count = 0; count < 2; count++) // 每句歌词唱两遍
    {
      tone(BUZZER, LOW_1);
      delay(500);
      tone(BUZZER, LOW_2);
      delay(500);
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_1);
      delay(500);
      
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_4);
      delay(500);
      tone(BUZZER, LOW_5);
      delay(1000);
    
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_5);
      delay(350);
      tone(BUZZER, LOW_6);
      delay(150); 
      tone(BUZZER, LOW_5);
      delay(350);
      tone(BUZZER, LOW_4);
      delay(150); 
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_1);
      delay(500);
    
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_1_P_5);
      delay(500);
      tone(BUZZER, LOW_5_P_5);
      delay(500);
      tone(BUZZER, LOW_1_P_5);
      delay(1000);
    
      noTone(BUZZER);
      delay(500);
    }
    
    } 
    
    /**
      * @brief  任务2函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_2( void * pvParameters ){
    for(;;){
      adc = analogRead(ADC_PIN);
      page1(adc); // 显示电量
      vTaskDelay(1);
    }
    }
    /**
      * @brief  任务3函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_3( void * pvParameters ){
    for(;;){
      two_tigers_music_play();// 播放营业
      vTaskDelay(1);
    }
    }
    
    /**
      * @brief  初始化setup()
      * @param  无
      * @param  无
      * @retval 
      */
    void setup() {
      pinMode(BUZZER, OUTPUT);
      pinMode(ADC_PIN, INPUT);
      u8g2.enableUTF8Print();
      u8g2.setI2CAddress(0x3C*2);
      u8g2.begin();
      u8g2.firstPage();
      do
      {
        page1(adc);
      }while(u8g2.nextPage());
    
    
      xTaskCreatePinnedToCore(task_2, "task_2", 4096, NULL, 0, NULL, 1);
      xTaskCreatePinnedToCore(task_3, "task_3", 4096, NULL, 8, NULL, 1);
    //xTaskCreatePinnedToCore 函数参数说明:
    // task_2:实现任务的函数名称(task1)
    // "task_2":任务的任何名称(“ task1”等)
    // 4096:分配给任务的堆栈大小,以字为单位
    // NULL:任务输入参数(可以为NULL)
    // 2:任务的优先级(0是最低优先级)
    // NULL:任务句柄(可以为NULL)
    // 1:任务将运行的ESP32D的内核ID(0或1)
    }
    
    void loop() {
      vTaskDelay(1);
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173

    示例程序P2

    开发环境是VSCode + PlatformIO IDE

    /*
    1.ESP32 NTP Client-Server: Get Date and Time (Arduino IDE) : https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
    2.ESP32 Weather Station Interface PCB Shield (Temperature, Humidity, Pressure, Date and Time):https://randomnerdtutorials.com/esp32-weather-station-pcb/
    3.esp32获取网络天气时钟--桌面旋转太空人天气预报站: https://blog.csdn.net/DWX_top/article/details/120650278
    */
    
    #include 
    // #include  // tone库 ESP32 for Arduino IDE  
    #include // 屏幕显示依赖库
    #include 
    #include 
    #include 
    // buzzer pin
    #define BUZZER 19
    #define ADC_PIN 34
    #define BUTTON 18
    uint16_t adc = 0;
    U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // U8G2_R0:屏幕旋转0°度
    // Insert your network credentials
    const char* ssid     = "CMCC-e47q";// wifi名称
    const char* password = "23vc6uxj"; // wifi 密码
    
    // NTP Server Details
    const char* ntpServer = "pool.ntp.org"; //"ntp.ntsc.ac.cn";
    const long  gmtOffset_sec = 28800;  // 北京时区
    const int   daylightOffset_sec = 3600;
    
    uint16_t alarm_clock_hour = 0;
    uint16_t alarm_clock_minute = 0;
    /**
      * @brief  屏幕界面1
      * @param  adc :读取电池的ADC
      * @retval alarm_clock_minute: 时钟的分钟数值
      */
    uint16_t page1(uint16_t adc) {
      //DATE AND TIME
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        //Serial.println("Failed to obtain time");
      }
      //Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
      
      //GET DATE
      //Get full weekday name
      char weekDay[10];
      strftime(weekDay, sizeof(weekDay), "%a", &timeinfo);
      //Get day of month
      char dayMonth[4];
      strftime(dayMonth, sizeof(dayMonth), "%d", &timeinfo);
      //Get abbreviated month name
      char monthName[5];
      strftime(monthName, sizeof(monthName), "%b", &timeinfo);
      //Get year
      char year[6];
      strftime(year, sizeof(year), "%Y", &timeinfo);
    
      //GET TIME
      //Get hour (12 hour format)
      /*char hour[4];
      strftime(hour, sizeof(hour), "%I", &timeinfo);*/
      
      //Get hour (24 hour format)
      char hour[4];
      strftime(hour, sizeof(hour), "%H", &timeinfo);
      alarm_clock_hour = atoi(hour);  // c标准库函数-- atoi() 将字符串转换为整型
      
      //Get minute
      char minute[4];
      strftime(minute, sizeof(minute), "%M", &timeinfo);
      alarm_clock_minute = atoi(minute); // c标准库函数-- atoi() 将字符串转换为整型
      
      u8g2.firstPage();
      do{
    	  u8g2.setFont(u8g2_font_timR08_tf); //设置字体 字号 字形
          u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(0,0);  // 设置屏幕坐标
    	  u8g2.print(String("bat:") + String(adc));
      
    	  u8g2.setFont(u8g2_font_timR18_tf); //设置字体 字号 字形
    	  u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(30,10);  // 设置屏幕坐标
    	  u8g2.print(String(hour) + String(":") + String(minute)); // 时 分
    
    	  u8g2.setFont(u8g2_font_timR10_tf); //设置字体 字号 字形
    	  u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(16,40);  // 设置屏幕坐标
    	  u8g2.print(String(weekDay) + String(",") + String(dayMonth) + String(" ") + String(monthName) + String(" ")+String(year)); // 星期 , 日 月 年
      
      }
      while(u8g2.nextPage());//刷新屏幕
      vTaskDelay(100);
      return alarm_clock_minute;
    }
    
    
    /**
      * @brief  《两只老虎》简谱
      * @param  无
      * @param  无
      * @retval 无
      */
    void two_tigers_music_play()
    {
    byte count = 0;                 // byte 占1个字节 1*8bit=8位 0~256 节省内存
    // low 1-7 低音
    unsigned int  LOW_1 = 262;      // unsigned int 占4个字节 4*8bit=32位  0~4294967295
    unsigned short LOW_1_P_5 = 277; // unsigned short 等于 uint16_t
    uint16_t LOW_2 = 294;           // uint16_t 占2个字节 2*8bit=16位  0~65535
    uint16_t LOW_2_P_5 = 311;
    uint16_t LOW_3 = 330;
    uint16_t LOW_4 = 349;
    uint16_t LOW_4_P_5 = 370;
    uint16_t LOW_5 = 392;
    uint16_t LOW_5_P_5 = 415;
    uint16_t LOW_6 = 440;
    uint16_t LOW_6_P_5 = 466;
    uint16_t LOW_7 = 494;
    
    for (count = 0; count < 2; count++) // 每句歌词唱两遍
    {
      tone(BUZZER, LOW_1);
      delay(500);
      tone(BUZZER, LOW_2);
      delay(500);
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_1);
      delay(500);
      
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_4);
      delay(500);
      tone(BUZZER, LOW_5);
      delay(1000);
    
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_5);
      delay(350);
      tone(BUZZER, LOW_6);
      delay(150); 
      tone(BUZZER, LOW_5);
      delay(350);
      tone(BUZZER, LOW_4);
      delay(150); 
      tone(BUZZER, LOW_3);
      delay(500);
      tone(BUZZER, LOW_1);       
      delay(500);
    
      noTone(BUZZER);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER, LOW_1_P_5);
      delay(500);
      tone(BUZZER, LOW_5_P_5);
      delay(500);
      tone(BUZZER, LOW_1_P_5);
      delay(1000);
    
      noTone(BUZZER);
      delay(500);
    }
    
    } 
    
    /**
      * @brief  任务2函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_2( void * pvParameters ){
    for(;;){
      adc = analogRead(ADC_PIN);
      page1(adc); // 显示电量
      vTaskDelay(100);
    }
    }
    /**
      * @brief  任务3函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_3( void * pvParameters ){
    for(;;){
    	if((alarm_clock_minute % 60) == 0 || digitalRead(BUTTON)== 0){
    		two_tigers_music_play();// 整点播放 or 按键触发播放
    	}
      vTaskDelay(1);
    }
    }
    
    /**
      * @brief  初始化setup()
      * @param  无
      * @param  无
      * @retval 
      */
    void setup() {
      Serial.begin(115200);
      pinMode(BUZZER, OUTPUT);
      pinMode(ADC_PIN, INPUT);
      pinMode(BUTTON,INPUT_PULLUP);
      
      u8g2.enableUTF8Print();
      u8g2.setI2CAddress(0x3C*2);
      u8g2.begin();
      u8g2.firstPage();
      do
      {
        page1(adc);
      }while(u8g2.nextPage());
    
      // Connect to Wi-Fi
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected.");
      
      // Init and get the time
      configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    
      xTaskCreatePinnedToCore(task_2, "task_2", 4096, NULL, 2, NULL, 1);
      xTaskCreatePinnedToCore(task_3, "task_3", 4096, NULL, 0, NULL, 1);
    //xTaskCreatePinnedToCore 函数参数说明:
    // task_2:实现任务的函数名称(task1)
    // "task_2":任务的任何名称(“ task1”等)
    // 4096:分配给任务的堆栈大小,以字为单位
    // NULL:任务输入参数(可以为NULL)
    // 2:任务的优先级(0是最低优先级)
    // NULL:任务句柄(可以为NULL)
    // 1:任务将运行的ESP32D的内核ID(0或1)
    }
    
    void loop() {
      vTaskDelay(1);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259

    在这里插入图片描述


    示例程序P3

    ESP32的ADC 和DAC 使用要点总结:

    • 1.要用 dacWrite() 而非 analogWrite() 函数,前者 输出模拟信号,后者输出 PWM 信号;
    • 2.要使用analogReadMilliVolts() 直接读取电压,这个函数会结合衰减系数和特征曲线进行计算。
    • 3.要把esp32 库升级到2.0.5版本以上,否则 analogReadMilliVolts() 读数是错误的。
    • 4.使用电阻分压,使待测电压降低到2500mV以内。
    • 5.根据 ADC原始结果计算电压,可以使用公式:Vout = Dout * Vmax / Dmax
      - Vout :电压
      - Dout :ADC 原始数字读数。
      - Vmax :最大可测量输入模拟电压,请参阅 ADC 衰减
      - Dmax:输出 ADC 原始数字读取结果的最大值,在 Single Read 模式下为 4095,在 Continuous Read 模式下为 4095。
    /*
    1.ESP32 NTP Client-Server: Get Date and Time (Arduino IDE) : https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
    2.ESP32 Weather Station Interface PCB Shield (Temperature, Humidity, Pressure, Date and Time):https://randomnerdtutorials.com/esp32-weather-station-pcb/
    3.esp32获取网络天气时钟--桌面旋转太空人天气预报站: https://blog.csdn.net/DWX_top/article/details/120650278
    */
    
    #include 
    // #include  // tone库 ESP32 for Arduino IDE  
    #include // 屏幕显示依赖库
    #include 
    #include 
    #include 
    // BUZZER_PIN pin
    #define BUZZER_PIN 19
    #define ADC_PIN 34
    #define BUTTON_PIN 18
    // uint16_t adc = 0;
    float volt = 0;
    
    U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); // U8G2_R0:屏幕旋转0°度
    // Insert your network credentials
    const char* ssid     = "CMCC-e47q";// wifi名称
    const char* password = "23vc6uxj"; // wifi 密码
    
    // NTP Server Details
    const char* ntpServer = "pool.ntp.org"; //"ntp.ntsc.ac.cn";
    const long  gmtOffset_sec = 28800;  // 北京时区
    const int   daylightOffset_sec = 3600;
    
    uint16_t alarm_clock_hour = 0;
    uint16_t alarm_clock_minute = 0;
    /**
      * @brief  屏幕界面1
      * @param  volt :读取电池的电压
      * @retval alarm_clock_minute: 时钟的分钟数值
      */
    uint16_t page1(float volt) {
      //DATE AND TIME
      struct tm timeinfo;
      if(!getLocalTime(&timeinfo)){
        //Serial.println("Failed to obtain time");
      }
      //Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
      
      //GET DATE
      //Get full weekday name
      char weekDay[10];
      strftime(weekDay, sizeof(weekDay), "%a", &timeinfo);
      //Get day of month
      char dayMonth[4];
      strftime(dayMonth, sizeof(dayMonth), "%d", &timeinfo);
      //Get abbreviated month name
      char monthName[5];
      strftime(monthName, sizeof(monthName), "%b", &timeinfo);
      //Get year
      char year[6];
      strftime(year, sizeof(year), "%Y", &timeinfo);
    
      //GET TIME
      //Get hour (12 hour format)
      /*char hour[4];
      strftime(hour, sizeof(hour), "%I", &timeinfo);*/
      
      //Get hour (24 hour format)
      char hour[4];
      strftime(hour, sizeof(hour), "%H", &timeinfo);
      alarm_clock_hour = atoi(hour);  // c标准库函数-- atoi() 将字符串转换为整型
      
      //Get minute
      char minute[4];
      strftime(minute, sizeof(minute), "%M", &timeinfo);
      alarm_clock_minute = atoi(minute); // c标准库函数-- atoi() 将字符串转换为整型
      
      u8g2.firstPage();
      do{
    	  u8g2.setFont(u8g2_font_timR08_tf); //设置字体 字号 字形
          u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(0,0);  // 设置屏幕坐标
    	  u8g2.print(String("bat:") + String(volt));
      
    	  u8g2.setFont(u8g2_font_timR18_tf); //设置字体 字号 字形
    	  u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(30,10);  // 设置屏幕坐标
    	  u8g2.print(String(hour) + String(":") + String(minute)); // 时 分
    
    	  u8g2.setFont(u8g2_font_timR10_tf); //设置字体 字号 字形
    	  u8g2.setFontPosTop();  // 设置定位的位置以字符顶开始
    	  u8g2.setCursor(16,40);  // 设置屏幕坐标
    	  u8g2.print(String(weekDay) + String(",") + String(dayMonth) + String(" ") + String(monthName) + String(" ")+String(year)); // 星期 , 日 月 年
      
      }
      while(u8g2.nextPage());//刷新屏幕
      vTaskDelay(100);
      return alarm_clock_minute;
    }
    
    
    /**
      * @brief  《两只老虎》简谱
      * @param  无
      * @param  无
      * @retval 无
      */
    void two_tigers_music_play()
    {
    byte count = 0;                 // byte 占1个字节 1*8bit=8位 0~256 节省内存
    // low 1-7 低音
    unsigned int  LOW_1 = 262;      // unsigned int 占4个字节 4*8bit=32位  0~4294967295
    unsigned short LOW_1_P_5 = 277; // unsigned short 等于 uint16_t
    uint16_t LOW_2 = 294;           // uint16_t 占2个字节 2*8bit=16位  0~65535
    uint16_t LOW_2_P_5 = 311;
    uint16_t LOW_3 = 330;
    uint16_t LOW_4 = 349;
    uint16_t LOW_4_P_5 = 370;
    uint16_t LOW_5 = 392;
    uint16_t LOW_5_P_5 = 415;
    uint16_t LOW_6 = 440;
    uint16_t LOW_6_P_5 = 466;
    uint16_t LOW_7 = 494;
    
    for (count = 0; count < 2; count++) // 每句歌词唱两遍
    {
      tone(BUZZER_PIN, LOW_1);
      delay(500);
      tone(BUZZER_PIN, LOW_2);
      delay(500);
      tone(BUZZER_PIN, LOW_3);
      delay(500);
      tone(BUZZER_PIN, LOW_1);
      delay(500);
      
      noTone(BUZZER_PIN);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER_PIN, LOW_3);
      delay(500);
      tone(BUZZER_PIN, LOW_4);
      delay(500);
      tone(BUZZER_PIN, LOW_5);
      delay(1000);
    
      noTone(BUZZER_PIN);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER_PIN, LOW_5);
      delay(350);
      tone(BUZZER_PIN, LOW_6);
      delay(150); 
      tone(BUZZER_PIN, LOW_5);
      delay(350);
      tone(BUZZER_PIN, LOW_4);
      delay(150); 
      tone(BUZZER_PIN, LOW_3);
      delay(500);
      tone(BUZZER_PIN, LOW_1);       
      delay(500);
    
      noTone(BUZZER_PIN);
      delay(500);
    }
    
    for (count = 0; count < 2; count++)
    {
      tone(BUZZER_PIN, LOW_1_P_5);
      delay(500);
      tone(BUZZER_PIN, LOW_5_P_5);
      delay(500);
      tone(BUZZER_PIN, LOW_1_P_5);
      delay(1000);
    
      noTone(BUZZER_PIN);
      delay(500);
    }
    
    } 
    
    /**
      * @brief  任务2函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_2( void * pvParameters ){
    
    for(;;){
      // volt = analogRead(ADC_PIN) *4.2 / 4095;           //  ADC 原始结果计算电压公式:Vout = Dout * Vmax / Dmax
      volt = analogReadMilliVolts(ADC_PIN) * 11 / 1000.0;  // 10k + 1k 电阻串联分4.2V电压  1V = 1000mV
      page1(volt); // 显示电量
      vTaskDelay(100);
    }
    }
    /**
      * @brief  任务3函数
      * @param  无
      * @param  无
      * @retval pvParameters:
      */
    void task_3( void * pvParameters ){
    for(;;){
    	if((alarm_clock_minute % 60) == 0 || digitalRead(BUTTON_PIN)== 0){
    		two_tigers_music_play();// 整点播放 or 按键触发播放
    	}
      vTaskDelay(1);
    }
    }
    
    /**
      * @brief  初始化setup()
      * @param  无
      * @param  无
      * @retval 
      */
    void setup() {
      Serial.begin(115200);
      pinMode(BUZZER_PIN, OUTPUT);
      pinMode(ADC_PIN, INPUT);
      pinMode(BUTTON_PIN,INPUT_PULLUP);
      
      u8g2.enableUTF8Print();
      u8g2.setI2CAddress(0x3C*2);
      u8g2.begin();
      u8g2.firstPage();
      do
      {
        page1(volt);
      }while(u8g2.nextPage());
    
      // Connect to Wi-Fi
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected.");
      
      // Init and get the time
      configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    
      xTaskCreatePinnedToCore(task_2, "task_2", 4096, NULL, 2, NULL, 1);
      xTaskCreatePinnedToCore(task_3, "task_3", 4096, NULL, 0, NULL, 1);
    //xTaskCreatePinnedToCore 函数参数说明:
    // task_2:实现任务的函数名称(task1)
    // "task_2":任务的任何名称(“ task1”等)
    // 4096:分配给任务的堆栈大小,以字为单位
    // NULL:任务输入参数(可以为NULL)
    // 2:任务的优先级(0是最低优先级)
    // NULL:任务句柄(可以为NULL)
    // 1:任务将运行的ESP32D的内核ID(0或1)
    }
    
    void loop() {
      vTaskDelay(1);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    float adc;
    adc = analogRead(ADC_PIN) * 4.2 / 4095;           //  ADC 原始结果计算电压公式:Vout = Dout * Vmax / Dmax
    adc = analogReadMilliVolts(ADC_PIN) * 11 / 1000.0;  // 10k + 1k 电阻串联分4.2V电压  1V = 1000mV
    
    • 1
    • 2
    • 3

    参考资料


    20240411 补充

    #include  // tone库 ESP32 for Arduino IDE  
    
    • 1

    若下载安装库文件后,蜂鸣器仍然不响,修改ESP32Tone.h中的通道15 改为 0试一试。

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    自己动手实现一个 RPC 框架
    电气工程中matlab程序拉格朗日松弛算法
    Less的强大变量用法
    Windows 下使用 nmap ncat 命令测试 UDP 端口连接
    Dubbo泛化调用入门到精通
    测试人生 | 转行测试开发,4年4“跳”年薪涨3倍,我的目标是星辰大海(附大厂面经)!
    8.1 Spring知识点——从Spring配置文件讲起到Spring和SpringBoot的yaml和properities配置文件的区别
    Ubuntu——配置安装服务
    SIPp使用经验
    Linux性能优化--性能工具-系统CPU
  • 原文地址:https://blog.csdn.net/Naiva/article/details/127767525