赞
踩
持续更新
应用:远程控制esp32的io。
先看官方给的示例点灯科技 (diandeng.tech)
#define BLINKER_WIFI #include <Blinker.h> char auth[] = "Your Device Secret Key"; char ssid[] = "Your WiFi network SSID or name"; char pswd[] = "Your WiFi network WPA password or WEP key"; // 新建组件对象 BlinkerButton Button1("btn-abc"); BlinkerNumber Number1("num-abc"); int counter = 0; // 按下按键即会执行该函数 void button1_callback(const String & state) { BLINKER_LOG("get button state: ", state); digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); } // 如果未绑定的组件被触发,则会执行其中内容 void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data); counter++; Number1.print(counter); } void setup() { // 初始化串口 Serial.begin(115200); BLINKER_DEBUG.stream(Serial); // 初始化有LED的IO pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); // 初始化blinker Blinker.begin(auth, ssid, pswd); Blinker.attachData(dataRead); Button1.attach(button1_callback); } void loop() { Blinker.run(); }
上面的代码中有很多的函数,我提取了一部分写下了能直接控制io的代码。
#define BLINKER_WIFI #include <Blinker.h> char auth[] = "安全码"; char ssid[] = "wifi名"; char pswd[] = "wifi密码"; BlinkerButton Button1("btn-test"); //在blinkerapp上创建一个按钮组件,名称需要相同 int ledPin = 4;//我的esp32的io4是板载led //当app上的按钮组件被按下时执行 void button1_callback(const String & state){ BLINKER_LOG("BLINKER_LOG: ", !digitalRead(ledPin));//日志,在编译器串口上显示。 digitalWrite(ledPin, !digitalRead(ledPin));//简易写法,把每次读取的led状态写反,实现简单开关功能 if(digitalRead(ledPin)==HIGH){ BLINKER_LOG("BLINKER_LOG: ", "开灯"); }else if(digitalRead(ledPin)==LOW){ BLINKER_LOG("BLINKER_LOG: ", "关灯"); } } void setup() { // put your setup code here, to run once: BLINKER_DEBUG.stream(Serial); Serial.begin(115200); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Blinker.begin(auth, ssid, pswd); Button1.attach(button1_callback); } void loop() { // put your main code here, to run repeatedly: Blinker.run(); }
下面的是实现滑块的数值显示在num上,正在完成中。。
#define BLINKER_WIFI #define LED 4 #include <Blinker.h> #define BUTTON1 "control" #define BUTTON2 "settime" #define NUMBER1 "clockdown" #define NUMBER2 "timecount" #define SLIDER1 "timemin" BlinkerButton Button1(BUTTON1); BlinkerNumber Number1(NUMBER1); BlinkerSlider Slider1(SLIDER1); void Number1_callback(const String & data) { } void Button1_callback(const String & state) { } void slider1_callback(int32_t value) { BLINKER_LOG("get slider value: ", value); } void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data); Blinker.vibrate(); uint32_t BlinkerTime = millis(); Blinker.print("millis", BlinkerTime); Number1.icon("icon_1"); Number1.color("#FFFFFF"); Number1.unit("ms"); Number1.print(Button1_callback.value); //Slider1.color("#FFFFFF"); //Slider1.print(random(0, 128)); } //af1d7be5879a //TSLMTH2.4G //lff870311 char auth[] = "af1d7be5879a"; char ssid[] = "TSLMTH2.4G"; char pswd[] = "lff870311"; void setup() { // put your setup code here, to run once: Serial.begin(115200); BLINKER_DEBUG.stream(Serial); Blinker.begin(auth, ssid, pswd); Slider1.attach(slider1_callback); Blinker.attachData(dataRead); } void loop() { // put your main code here, to run repeatedly: Blinker.run(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。