742|1

57

帖子

5

TA的资源

一粒金砂(高级)

楼主
 

Beetle ESP32-C6 休眠、网络功能测试及其代码实现 [复制链接]

 
本帖最后由 MioChan 于 2024-5-20 17:16 编辑

首先,测试了一下ESP32的Deep-sleep模式,官方也有很多例程,例如下面的代码就可以实现RTC定时器唤醒功能

 

 


#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;

void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}

void setup(){
  Serial.begin(115200);
  delay(1000); //Take some time to open up the Serial Monitor

  //Increment boot number and print it every reboot
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  //Print the wakeup reason for ESP32
  print_wakeup_reason();

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  " Seconds");

  Serial.println("Going to sleep now");
  Serial.flush(); 
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
}

void loop(){
  //This is not going to be called
}

 

 

后续还测量了一下整个开发版的电流,发现大概为30mA,大概只有十几mA的变化。应该是因为这种开发版的外围电路太多,锂电充放电管理、ldo这些都会影响功耗测量,真要测的准还是得自己画个板子移除这些影响来测。


 

下面再来实现一些网络相关的东西,因为最终是要实现驱动点阵屏做一个可以显示时间以及投送弹幕文字的时钟,所以我们先实现一个基本的API服务器,用于接受其他设备post的字符串,代码如下:

#include <WiFi.h>
#include <ArduinoJson.h>

const char* ssid = "XXXX";
const char* password = "XXXX";

int status = WL_IDLE_STATUS;
String receivedContent = "";  
WiFiServer server(80);
const int led = 15;


void setup() {
  Serial.begin(9600);
 pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to  ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();


}

unsigned long startTime;  
const unsigned long runDuration = 6000;  


void loop() {
  checkForNewClient();  // Check for new client connections
  delay(2);

}

void checkForNewClient() {
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client");
    digitalWrite(led, 1);

    // Read the first line of the request
    String firstLine = client.readStringUntil('\n');

    // Check if this is a POST request
    if (firstLine.startsWith("POST")) {
      // Read the headers and find the Content-Length
      int contentLength = -1;
      while (client.connected()) {
        String line = client.readStringUntil('\n');
        if (line.startsWith("Content-Length:")) {
          contentLength = line.substring(15).toInt();
        }

        // Check if the end of headers is reached (empty line)
        if (line == "\r") {
          break;
        }
      }

      // Read the request body
      if (contentLength > 0) {
        String requestBody = client.readStringUntil('\n');

        // Parse JSON from the request body
        DynamicJsonDocument doc(1024);
        deserializeJson(doc, requestBody);

        String content = doc["content"];
        if (content != "") {
          Serial.println("Received content: " + content);
          receivedContent = "    "+content+"    ";  // Update the received string
        } else {
          Serial.println("No content received");
        }
      }
    }
    digitalWrite(led,0);
    // Send response to the client
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/plain");
    client.println("Connection: close");
    client.println();
    client.println("Response sent");
    client.stop();
    Serial.println("Client disconnected");
  }
}

 

直接用IOS快捷指令就能实现Post字符串的功能

 

运行

 

ESP32已经读取到了手机发来的字符串,并把它输出在了串口,这个功能实现完毕

  

 

顺便测试了一下,这个程序ESP32连接wifi后电流为40ma,在通过wifi交换报文时能达到270mA左右,如果使用电池供电的话加入休眠还是很有必要的。

 


因为要做时钟,所以NTP获取时间的功能也需要实现

#include <WiFi.h>
#include <NTPClient.h>
#include <ArduinoJson.h>
#include <NetworkUdp.h>


const char* ssid = "XXXX";
const char* password = "XXXX";

int status = WL_IDLE_STATUS;
String receivedContent = "";  // Variable to store the received string


const int led = 15;


const int timeZone = 8; 
const char* ntpServer = "pool.ntp.org";

NetworkUDP udp;
NTPClient timeClient(udp,ntpServer, timeZone * 3600, 60000);

void setup() {
  Serial.begin(9600);
 pinMode(led, OUTPUT);
  digitalWrite(led, 0);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  timeClient.begin();
  timeClient.update();

}


void loop() {
  Serial.println(timeClient.getFormattedTime());
  delay(1000);
}

 

 

可以正常获取到北京时间,实现完毕

 


之后我们基于这两个代码实现的功能,使用MD_Parola 、MD_MAX72xx 这两个库就可以实现驱动点阵屏了,后续内容就放到下一帖啦~

最新回复

真要测的准还是得自己画个板子移除这些影响来测,这个想法可以有   详情 回复 发表于 2024-5-21 07:32
点赞 关注
 
 

回复
举报

6802

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

真要测的准还是得自己画个板子移除这些影响来测,这个想法可以有

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表