ESP32的智能药箱-WEB定时-舵机和语音控制-OLED实时时间显示

最近做的一个小项目,感觉还不错,具体的可以参考我发布的完整资源。

这里主要实现的功能,是通过WEB端设定闹钟,然后可以定时对舵机、语音、和蜂鸣器进行一个控制,并且设定了按钮进行关闭。为了达到多线程,所以采用了FreeROST的系统,并且使用了双核。

需要的配件很简单:ESP-WROOM-32,一个SG90舵机,一个蜂鸣器,一堆按键,一个LM386的功放,一个喇叭,

一个0.91寸的OLED

具体如图:

接下来,我来讲下具体的各部分的实现方法:

首先是实时的一个时间显示,显示再OLED上,这里的具体函数如下:



void printLocalTime()
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo))
  {
    display.println("Failed to obtain time");
    return;
  }
  display.println(&timeinfo, "%F"); // 格式化输出
  display.println(&timeinfo, "%T"); // 格式化输出
  display.println(&timeinfo, "%A"); // 格式化输出
}

void OLED_Function(void *pvParameters){
  for(;;){
  vTaskDelay(1000);
  //清除屏幕
  display.clearDisplay();
  //设置光标位置
  display.setCursor(0, 0);
  printLocalTime();
  display.display();
  Serial.println("DISPLAY ONCE AGAIN!");
    }
  }

这里使用了这两个库


#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

接下来就是舵机的控制,这个特别简单,使用的就是舵机库

//舵机使用的不是直接的舵机库
#include <Arduino.h>
#include <ESP32Servo.h>

void DuojiKAI(){
    myservo.write(15); 
    delay(2000);
    myservo.write(120);
    delay(2000);
  }
void DuojiGuan(){
   myservo.write(120); 
   delay(2000);
   myservo.write(15);
   delay(2000);
  }

蜂鸣器就不说了.....就是I/O口控制;

接下来就是重中之重,非常难的语音部分。这里我使用了两个软件:

这个软件有中文版的,就是标题哪个名字,可以将MP3转成WAV版本。接下来还有个软件:

 这个软件也有中文版的,可以将WAV转C语言的格式,非常方便。

具体代码如下:

//人工语音合成库
#include "SoundData.h"
#include "XT_DAC_Audio.h"

XT_Wav_Class ForceWithYou(Force);     //引言的XT库中对FORCE的处理                                      
XT_DAC_Audio_Class DacAudio(Yuyin_IO,0);      //设置引脚

void YuyinBB(){
int forceCounter=0;
for(;;){
  DacAudio.FillBuffer();
  if(ForceWithYou.Playing==false)       // if not playing,
  DacAudio.Play(&ForceWithYou);
  forceCounter++;
  Serial.println(forceCounter);
  if (forceCounter>4500)
      break;
    }
}

 然后其实主要是那俩头文件,需要在Github上面下载:

 最后,就是WEB端,其实这个反而很简单。我其实觉得SPIFFS其实应该是最好的,因为比较好看。但是我一直连接不上,本来html+css+javaScript都做好了,最后还是选择了简陋版本的这种方式。具体代码如下:

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html><head>
  <h1 align="center">ESP32 智能药箱 界面</h1>
  <br>
  <br>
  <meta name="viewport" charset="utf-8" content="width=device-width, initial-scale=1">
  <style>
    h1{
      color: #333;
      opacity: 0.7;
    }
    h2{
      color: #988;
      opacity: 0.7;
    }
</style>
  </head><body>
  <div style="display: block; margin: 0 auto; width: 50%; background: #ccc;">  
    <form action="/get">
    <br>
      <h2>输入闹钟( XX:XX:XX ) : </h2>
      <input type="text" name="HTML_INT_INPUT2" id = "alarm_inset">
      <input type="submit" value="Submit">
    </form><br>
    <br> 
    <button onclick="showUname()" align="center">显示上一次吃药时间</button>
    <div id="uname-show"></div>
    <script>
       function showUname(){
            document.getElementById("uname-show").innerHTML = "<h1>" + document.getElementById("alarm_inset").value + "</h1>";
        }
    </script>
  <br>
</div>
</body></html>)rawliteral";



void notFound(AsyncWebServerRequest *request) {
  request->send(404, "text/plain", "Not found");
}



void web_server_task_function(void* paramters)
{
  Serial.println("Setting up the HTTP server");//串口打印设置HTTP服务器
 // Send web page with input fields to client
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html);
    Serial.println("HTTP START");
  });

  // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
  server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
    String inputMessage;
    String inputParam;
    // GET input2 value on <ESP_IP>/get?input2=<inputMessage>
    if (request->hasParam(TEXT_INPUT2)) {
      inputMessage = request->getParam(TEXT_INPUT2)->value();
      inputParam = TEXT_INPUT2;
      alarm_time = inputMessage;
      Serial.println("alarm clock : ");
    }
    else{
      inputMessage = "No Input Text sent on ESP32";
      inputParam = "none";
    }
    Serial.println(inputMessage);
    request->send(200, "text/html", "HTTP GET request sent to your ESP on input field (" 
                                     + inputParam + ") with value: " + inputMessage +
                                     "<br><a href=\"/\">Return to Home Page</a>");
  });
  server.begin();//starting the server
  Serial.println("HTTP server setup completed");
  vTaskDelete(NULL);
  
}

成品如图:

 OK,那么其他的就是一些逻辑上的问题了,我就不再阐释了。具体的代码,朋友们可以在我的下载里面下载看看。积分我定的都很少

(41条消息) ESP32的网络定时舵机控制,廉价语音定时开关箱,OLED实时显示时间-智能家居文档类资源-CSDN文库

也可以直接在github上下载这个项目:

chenyuhan1997/ESP32_SMART_BOX_OLED_WEB: The main function here is to set the alarm clock through the WEB terminal, and then you can control the servo, voice, and buzzer regularly, and set the button to close. In order to achieve multi-threading, the FreeROST system is used, and dual-core is used. (github.com)

  • 15
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReedswayYuH.C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值