blinke数据上传手机端无法实现实时刷新,求大佬指点
#define BLINKER_WIFI#include <Blinker.h>
char auth[] = "60a493b141d1"; // 点灯科技秘钥
char ssid[] = "2.4G"; // WiFi名称
char pswd[] = "112233445566"; // WiFi密码
#define bt1 50
#define bt2 51
#define bt3 52
#define bt4 53
#define bt5 54
#define bt6 55
#define bt7 56
int jishu=0;
float humi_read = 45, temp_read = 45,tvoc_read = 45,co22_read = 45,pmm25_read = 45;
int commaPosition;//存储还没有分离出来的字符串
int getstrval;
int shuzujishu;
BlinkerNumber HUMI("num-abc2");
BlinkerNumber TEMP("num-abc1");
BlinkerNumber TVOC("num-abc3");
BlinkerNumber CO2("num-abc4");
BlinkerNumber PM25("num-abc5");
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
TVOC.print(tvoc_read);
CO2.print(co22_read);
PM25.print(pmm25_read);
}
// 新建组件对象
BlinkerButton Button1("btn1-abc");
BlinkerButton Button2("btn2-abc");
BlinkerButton Button3("btn3-abc");
BlinkerButton Button4("btn4-abc");
BlinkerButton Button5("btn5-abc");
BlinkerButton Button6("btn6-abc");
BlinkerButton Button7("btn7-abc");
int aaab = 0;
// 自动手动函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt1, LOW);
Serial.println("a");
// 反馈开关状态
Button1.print("on");
} else if (state == "off") {
digitalWrite(bt1, HIGH);
Serial.println("b");
// 反馈开关状态
Button1.print("off");
}
}
// 加湿wifi函数
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt2, LOW);
Serial.println("c");
// 反馈开关状态
Button2.print("on");
} else if (state == "off") {
digitalWrite(bt2, HIGH);
Serial.println("d");
// 反馈开关状态
Button2.print("off");
}
}
// 消毒wifi函数
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt3, LOW);
Serial.println("e");
// 反馈开关状态
Button3.print("on");
} else if (state == "off") {
digitalWrite(bt3, HIGH);
Serial.println("f");
// 反馈开关状态
Button3.print("off");
}
}
// 换风wifi函数
void button4_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt4, LOW);
Serial.println("g");
// 反馈开关状态
Button4.print("on");
} else if (state == "off") {
digitalWrite(bt4, HIGH);
Serial.println("h");
// 反馈开关状态
Button4.print("off");
}
}
//风机wifi函数
void button5_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt5, LOW);
Serial.println("i");
// 反馈开关状态
Button5.print("on");
} else if (state == "off") {
digitalWrite(bt5, HIGH);
Serial.println("j");
// 反馈开关状态
Button5.print("off");
}
}
//加热wifi函数
void button6_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt6, LOW);
Serial.println("k");
// 反馈开关状态
Button6.print("on");
} else if (state == "off") {
digitalWrite(bt6, HIGH);
Serial.println("l");
// 反馈开关状态
Button6.print("off");
}
}
//摆风wifi函数
void button7_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt7, LOW);
Serial.println("m");
// 反馈开关状态
Button7.print("on");
} else if (state == "off") {
digitalWrite(bt7, HIGH);
Serial.println("n");
// 反馈开关状态
Button7.print("off");
}
}
String comdata = "";
void setup()
{// 初始化串口
Serial.begin(9600);
// 初始化有LED的IO
pinMode(bt1, OUTPUT);
digitalWrite(bt1, HIGH);
pinMode(bt2, OUTPUT);
digitalWrite(bt2, HIGH);
pinMode(bt3, OUTPUT);
digitalWrite(bt3, HIGH);
pinMode(bt4, OUTPUT);
digitalWrite(bt4, HIGH);
pinMode(bt5, OUTPUT);
digitalWrite(bt5, HIGH);
pinMode(bt6, OUTPUT);
digitalWrite(bt6, HIGH);
pinMode(bt7, OUTPUT);
digitalWrite(bt7, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
Button7.attach(button7_callback);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
while (Serial.available() > 0){
comdata += char(Serial.read());//每次读一个char字符,并相加
Blinker.delay(2);
}
if (comdata.length() > 0){
Serial.println(comdata); //打印接收到的字符
fenjie(comdata);
comdata="";
}
jishu=jishu+1;
if(jishu>=200){
jishu=0;
temp_read = getstrval;
humi_read = getstrval;
tvoc_read = getstrval;
co22_read = getstrval;
pmm25_read = getstrval;
}
Blinker.run();
Blinker.delay(10);
}
void fenjie(String comdata){
shuzujishu=0;
do
{
commaPosition = comdata.indexOf(',');//检测字符串中的逗号
if(commaPosition != -1)//如果有逗号存在就向下执行
{
Serial.println("fwnjie");
Serial.println( comdata.substring(0,commaPosition));//打印出第一个逗号位置的字符串
getstrval=comdata.substring(0,commaPosition).toInt();
// getstrval=comdata.substring(commaPosition+1, comdata.length()).toInt();
shuzujishu=shuzujishu+1;
comdata = comdata.substring(commaPosition+1, comdata.length());//打印字符串,从当前位置+1开始
}
else
{
//找到最后一个逗号,如果后面还有文字,就打印出来
if(comdata.length() > 0){
//shuzujishu=shuzujishu+1;
// Serial.println(comdata);
getstrval=comdata.toInt();
}
}
}
while(commaPosition >=0);
}
实时数据使用方法:https://www.arduino.cn/thread-105898-1-1.html
页:
[1]