Arduino 使用Ardrino Sketch动态发布到多个MQTT主题

Arduino 使用Ardrino Sketch动态发布到多个MQTT主题,arduino,ide,mqtt,Arduino,Ide,Mqtt,我已经有了所有读取温度和湿度值的代码,但是我很难让它正确编译和发布。我有两个数组声明了读数的存储位置工作我已成功连接到我的MQTT代理-working。我可以打印到控制台-工作。但就我个人而言,我似乎无法获得使用#include和#include发布数据的正确代码。这是发布循环,我将数据打印到控制台,并希望使用它发布到我的8个不同MQTT主题 // MQTT Setup MQTT: ID, server IP, port, username and password #include <S

我已经有了所有读取温度和湿度值的代码,但是我很难让它正确编译和发布。我有两个数组声明了读数的存储位置工作我已成功连接到我的MQTT代理-working。我可以打印到控制台-工作。但就我个人而言,我似乎无法获得使用#include和#include发布数据的正确代码。这是发布循环,我将数据打印到控制台,并希望使用它发布到我的8个不同MQTT主题

// MQTT Setup MQTT: ID, server IP, port, username and password
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1 

String sPayload;
char* cPayload;
String sTopic;
char* cTopic;
float pubtemp;

//This is the array for the topics
String station[8] ={
{"home/Front_Porch"},
{"home/Living_Room"},
{"home/Plant_Room"},
{"home/Office"},
{"appliances/Basement_Fridge"}, 
{"appliances/Basement_Freezr"},
{"appliances/Beer_Fridge"},
{"appliances/Pellet_Stove"}};

//These are the arrays for the variable readings
//Initialize the Temperature and Humidity Values
int Ch[8][2] = {{2120,00},{2120,00},{2120,00},{2120,00},{2120,00},{2120,00},
{2120,00},{2120,00}};
//This is the array for the low battery flag on the sensors
int Batt[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // added 2/2/18 for battery status
//MQTT设置MQTT:ID、服务器IP、端口、用户名和密码
#包括
#包括
#包括
#包括
#定义MQTT_版本MQTT_版本_3_1_1
线绳;
char*cPayload;
串叠层;
char*cTopic;
浮球温度;
//这是主题的数组
弦乐台[8]={
{“家/前廊”},
{“家/客厅”},
{“家庭/厂房”},
{“家庭/办公室”},
{“电器/地下室冰箱”},
{“电器/地下室”},
{“电器/啤酒冰箱”},
{“器具/小球炉”};
//这些是变量读数的数组
//初始化温度和湿度值
int Ch[8][2]={{2120,00},{2120,00},{2120,00},{2120,00},{2120,00},{2120,00},
{2120,00},{2120,00}};
//这是传感器上电池电量低标志的阵列
int Batt[8]={0,0,0,0,0,0,0,0};//增加了电池状态的2/2/18
下面是执行发布的代码

if (now - lastSampleTime >= fiveMinutes)
 {
lastSampleTime += fiveMinutes;
 // Serial.print (lastSampleTime);
 // Serial.println("***");
if (!client.connected()) {
 reconnect();
}
for (int m=0; m <=7; m++){
  Serial.print ("Channel ");
  Serial.print (m);
  Serial.print("\t");
  for (int n=0; n<=1; n++){
    if (n == 0) {
      Serial.print ("Temp ");
    //          Serial.print (Ch[m][n]);
      Serial.print ("\t");
      float mytemp= (Ch[m][n]);
      pubtemp=mytemp/10;
      Serial.print (pubtemp,1);
      Serial.print("\t");
      }
    else if (n == 1){
      Serial.print ("RH ");
      Serial.print("\t");
      Serial.print (Ch[m][n]);
      Serial.print("\t");
      }
    }

   Serial.print (Batt[m]);
   Serial.print ("\t");
   Serial.print (station[m]);   
   Serial.println (" ");
 //Insert code here to publish the MQTT data before the next brace
 //This will publish a topic and associated data for each sensor
   StaticJsonBuffer<200> jsonBuffer;
   JsonObject& payload = jsonBuffer.createObject();
   payload["temperature"] = pubtemp;
   payload["humidity"] = Ch[m][1];
   payload["batt"] = Batt[m];  

   /This formats the json for the data and is working
   sPayload = "";
   payload.printTo(sPayload);
   cPayload = &sPayload[0u];
   Serial.println (cPayload);    

   This is where I'm having difficulties with the variable topics
   sTopic="";




 client.publish(station[m],cPayload);  This does not work.

 }
 //Publish a message when the full scan completes and this works
 client.publish("outTopic","Scan Complete");
 Serial.println ("Scan Complete");  
}
if(现在-lastSampleTime>=5分钟)
{
lastSampleTime+=五分钟;
//Serial.print(lastSampleTime);
//Serial.println(“***”);
如果(!client.connected()){
重新连接();
}

对于(int m=0;m),我想我找到了自己的答案,将字符串station[8]更改为char*station[8],它可以工作