blinke数据上传手机端无法实现实时刷新,求大佬指点-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1228|回复: 1

[已解答] blinke数据上传手机端无法实现实时刷新,求大佬指点

[复制链接]
发表于 2021-10-23 16:15 | 显示全部楼层 |阅读模式
#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[5];
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[0];
  humi_read = getstrval[1];
  tvoc_read = getstrval[2];
  co22_read = getstrval[3];
  pmm25_read = getstrval[4];  

}
  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[shuzujishu]=comdata.substring(0,commaPosition).toInt();
// getstrval[shuzujishu+1]=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[shuzujishu]=comdata.toInt();                                    
      }
   }
  }

   while(commaPosition >=0);
  }

发表于 2021-10-24 16:22 | 显示全部楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 11:54 , Processed in 0.095922 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表