用多线程解决了。ESP32两个物理核心真是一大亮点。
[pre]#define BLINKER_WIFI
#define BLINKER_ESP_TASK
#include <Blinker.h>
char auth[] = "e62c990ef5c8";
char ssid[] = "Tenda_5EF160";
char pswd[] = "19148011495";
// 新建组件对象
BlinkerButton Button1("swt-led1");
BlinkerNumber LUX("lux");
const int PhotocellPIN = 32; //光敏电阻
int PhotocellValue;//光敏电阻值
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
//读取Photocell(光敏电阻)
void dataRead_Photocell()
{
int P = analogRead(PhotocellPIN);
PhotocellValue = (4095 - P) / 51;
// Serial.println(PhotocellValue);
}
void heartbeat()
{
LUX.print(PhotocellValue);//光敏 光强
}
void Find_serveo(void *xtask)
{
(void) xtask;
for (;;){
dataRead_Photocell();
}
vTaskDelete(NULL);
}
void blinker_serveo(void *xtask)
{
(void) xtask;
for (;;){
Blinker.run();
}
vTaskDelete(NULL);
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{ BLINKER_LOG("Blinker readString: ", data);}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
BLINKER_TAST_INIT();
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Button1.attach(button1_callback);
xTaskCreatePinnedToCore(Find_serveo,"Find_serveo",4096,NULL,3,NULL,1);
xTaskCreatePinnedToCore(blinker_serveo,"blinker_serveo",4096,NULL,2,NULL,0);
}
void loop() {
}[/pre] |