title: Dynamically publishing to multiple MQTT topics with Ardrino Sketch
body: <p>I've got all the the code working reading the temperatures and humidity values but I' having great difficulties trying to get it to compile and publish correctly. i have two arrays declared where the readings are stored. - Working. I have successfully connected to my MQTT broker - working. I can print to the console - working. Bu for the life of me, I cannot seem to get the code correct for publishing the data using #include  and #include . This is the publishing loop where I print the data to the console and wish to use this to publish to my 8 different MQTT Topics.</p>
  
  <pre><code>// MQTT Setup MQTT: ID, server IP, port, username and password
  #include &lt;SPI.h&gt;
  #include &lt;Ethernet.h&gt;
  #include &lt;PubSubClient.h&gt;
  #include &lt;ArduinoJson.h&gt;
  #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
  </code></pre>
  
  <p>here is the code that does the publishing</p>
  
  <pre><code>if (now - lastSampleTime &gt;= fiveMinutes)
   {
  lastSampleTime += fiveMinutes;
   // Serial.print (lastSampleTime);
   // Serial.println("***");
  if (!client.connected()) {
   reconnect();
  }
  for (int m=0; m &lt;=7; m++){
    Serial.print ("Channel ");
    Serial.print (m);
    Serial.print("\t");
    for (int n=0; n&lt;=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&lt;200&gt; jsonBuffer;
     JsonObject&amp; 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 = &amp;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");  
  }
  </code></pre>
  
  <p><strong>Serial Output of Data</strong></p>
  
  <ol>
  <li>Channel 0   Temp    19.8    RH  35  0   home/Front_Porch </li>
  <li>{"temp":19.8,"humid":35,"batt":0}</li>
  <li>Channel 1   Temp    67.7    RH  12  0   home/Living_Room </li>
  <li>{"temp":67.7,"humid":12,"batt":0}</li>
  <li>Channel 2   Temp    62.2    RH  24  0   home/Plant_Room </li>
  <li>{"temp":62.2,"humid":24,"batt":0}</li>
  <li>Channel 3   Temp    66.9    RH  16  0   home/Office </li>
  <li>{"temp":66.9,"humid":16,"batt":0}</li>
  <li>Channel 4   Temp    45.1    RH  0   0   appliances/Basement_Fridge </li>
  <li>{"temp":45.1,"humid":0,"batt":0}</li>
  <li>Channel 5  Temp    1.6 RH  0   0   appliances/Basement_Freezr </li>
  <li>{"temp":1.6,"humid":0,"batt":0}pi</li>
  <li>Channel 6  Temp    41.1    RH  0   0   appliances/Beer_Fridge </li>
  <li>{"temp":41.1,"humid":0,"batt":0}</li>
  <li>Channel 7  Temp    100.2   RH  0   0   appliances/Pellet_Stove </li>
  <li>{"temp":100.2,"humid":0,"batt":0}</li>
  <li>Scan Complete</li>
  </ol>
  
  <p>The json output is working, and the topics are printing, but I can't seem to get the client.publish{"topic","payload") to work with the variable topic</p>