Blinker 如何判断连接状态
我想通过获取连接状态来指示led,这样写:while(! Blinker.connect()) {
digitalWrite(WiFiLedPin, HIGH);
delay(100);
digitalWrite(WiFiLedPin, LOW);
delay(100);
}
digitalWrite(WiFiLedPin, HIGH);未连接成功时闪烁,连接成功后常亮。应该是不行的吧?
我测试使用上面这段代码,运行过几次没有问题,可是后来就连接不上服务器了,提示:
11:56:38.347 -> WiFi Connected.
11:56:38.347 -> IP Address:
11:56:38.347 -> 192.168.0.110
11:56:42.685 -> ERROR: Maybe you have put in the wrong AuthKey!
11:56:42.685 -> ERROR: Or maybe your request is too frequently!
11:56:42.685 -> ERROR: Or maybe your network is disconnected!
应该是那个while导致频繁连接,被服务器拒绝了吧?
我看了源码,这里应该是源码吧?arduino-1.8.13/libraries/blinker-library-dev_3.0/src/Blinker/BlinkerProtocol.h
<div> <span style="color: #0000ff;">int</span> <span style="color: #795e26;">connect</span>() { <span style="color: #af00db;">return</span> <span style="color: #001080;">isInit</span> ? <span style="color: #001080;">conn</span>-><span style="color: #795e26;">connect</span>() : <span style="color: #0000ff;">false</span>; }</div><div> <span style="color: #0000ff;">int</span> <span style="color: #795e26;">connected</span>()</div><div> {</div><div> <span style="color: #af00db;">if</span> (<span style="color: #001080;">isInit</span>)</div><div> {</div><div><span style="color: #008000;"> // if (conn->connected())</span></div><div><span style="color: #008000;"> // {</span></div><div><span style="color: #008000;"> // state == CONNECTED;</span></div><div><span style="color: #008000;"> // return true;</span></div><div><span style="color: #008000;"> // }</span></div><div><span style="color: #008000;"> // else</span></div><div><span style="color: #008000;"> // {</span></div><div><span style="color: #008000;"> // state = DISCONNECTED;</span></div><div><span style="color: #008000;"> // return false;</span></div><div><span style="color: #008000;"> // }</span></div><div> <span style="color: #af00db;">return</span> <span style="color: #001080;">conn</span>-><span style="color: #795e26;">connected</span>();</div><div> }</div><div> <span style="color: #af00db;">else</span></div><div> {</div><div> <span style="color: #001080;">state</span> = <span style="color: #0070c1;">DISCONNECTED</span>;</div><div> <span style="color: #af00db;">return</span> <span style="color: #0000ff;">false</span>;</div><div> }</div>这个 conn->connect() 到底是获取连接状态还是执行连接呢?我猜测大概率是执行连接。
那么,还有什么方法能获取到 WiFi 连接成功以及服务器连接成功的方法呢?这个方法应该是从服务器获取状态而非执行连接后返回的结果。
begin 方法没有返回值,无法作为判断。
//未配网成功,板载灯闪
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define led D4 //D4
#define Relay_5 D5 //D5
#define Relay_6 D6 //D6
#define Relay_7 D7 //D7
#define Relay_8 D8 //D8
#include <DHT.h>
#include <Blinker.h>
#include <ESP8266WiFi.h>
#define DHTPIN D1 //D0
#define DHTTYPE DHT11 // DHT 11
char auth[] = "*******";
char ssid[] = "*******";
char pswd[] = "*******";
// 新建组件对象
BlinkerButton Button5("btn-d5");
BlinkerButton Button6("btn-d6");
BlinkerButton Button7("btn-d7");
BlinkerButton Button8("btn-d8");
BlinkerButton Button9("btn-re");
BlinkerButton Button("btn-w");
BlinkerNumber HUMI("num-shidu");//caiyq52-此组件未点灯app中自己设置的湿度数据键名,必须与自己APP中设定的名称一致
BlinkerNumber TEMP("num-wendu");//caiyq52-此组件未点灯app中自己设置的温度数据键名,必须与自己APP中设定的名称一致
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
float humi_read=0, temp_read=0;
//心跳包
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
}
void button5_callback(const String & state) {
BLINKER_LOG("Relay_5 get button state: ", state);
if (state=="on") {
digitalWrite(Relay_5, HIGH); // 反馈开关状态
Button5.print("on");
} else if(state=="off"){
digitalWrite(Relay_5, LOW); // 反馈开关状态
Button5.print("off");
}
Blinker.vibrate();
}
void button6_callback(const String & state) {
BLINKER_LOG("Relay_6 get button state: ", state);
if (state=="on") {
digitalWrite(Relay_6, HIGH); // 反馈开关状态
Button6.print("on");
} else if(state=="off"){
digitalWrite(Relay_6, LOW); // 反馈开关状态
Button6.print("off");
}
Blinker.vibrate();
}
void button7_callback(const String & state) {
BLINKER_LOG("Relay_7 get button state: ", state);
if (state=="on") {
digitalWrite(Relay_7, HIGH); // 反馈开关状态
Button7.print("on");
} else if(state=="off"){
digitalWrite(Relay_7, LOW); // 反馈开关状态
Button7.print("off");
}
Blinker.vibrate();
}
void button8_callback(const String & state) {
BLINKER_LOG("Relay_8 get button state: ", state);
if (state=="on") {
digitalWrite(Relay_8, HIGH); // 反馈开关状态
Button8.print("on");
} else if(state=="off"){
digitalWrite(Relay_8, LOW); // 反馈开关状态
Button8.print("off");
}
Blinker.vibrate();
}
//刷新显示所有开关状态
void button9_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
//5678继电器刷新
if(digitalRead (Relay_5)==HIGH){
Button5.print("on"); }
else if(digitalRead (Relay_5)==LOW){
Button5.print("off");
}
if(digitalRead (Relay_6)==HIGH){
Button6.print("on");
}
else if(digitalRead (Relay_6)==LOW){
Button6.print("off");
}
if(digitalRead (Relay_7)==HIGH){
Button7.print("on");
}
else if(digitalRead (Relay_7)==LOW){
Button7.print("off");
}
if(digitalRead (Relay_8)==HIGH){
Button8.print("on");
}
else if(digitalRead (Relay_8)==LOW){
Button8.print("off");
}
//温湿度刷新
HUMI.print(humi_read);
TEMP.print(temp_read);
}
void setup() {
// 初始化串口,并开启调试信息
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
pinMode(Relay_5, OUTPUT);
digitalWrite(Relay_5,LOW);
pinMode(Relay_6, OUTPUT);
digitalWrite(Relay_6, LOW);
pinMode(Relay_7, OUTPUT);
digitalWrite(Relay_7, LOW);
pinMode(Relay_8, OUTPUT);
digitalWrite(Relay_8, LOW);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
dht.begin();
// Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
Button7.attach(button7_callback);
Button8.attach(button8_callback);
Button9.attach(button9_callback);
//Blinker.attachDataStorage(dataStorage);
gotoWifi();
}
void loop() {
if(WiFi.status()!=WL_CONNECTED){
WiFi.disconnect();
Blinker.begin(auth, ssid, pswd);
}
Blinker.run();
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
}
else
{
BLINKER_LOG("Humidity: ", h, " %");
BLINKER_LOG("Temperature: ", t, " *C");
humi_read = h;
temp_read = t;
if (h>60)
{ Button.print("on");}
if (h<60)
{Button.print("off");}
}
Blinker.delay(2000);
}
void gotoWifi(){
while(WiFi.status()!=WL_CONNECTED){
bool is=digitalRead(led);
digitalWrite(led,!is);
delay(500);
}
digitalWrite(led,0);
}
参考的一位大佬程序,连接wifi时,LED闪烁,连接后,常亮
songyx005 发表于 2021-3-6 11:19
谢谢,已解决。
页:
[1]