Update two channels (different API’s) in Thingspeak

Uploading to two different channels in Thingspeak is fairly easy and can be done with the code below.
The code does close connection in between the uploads, perhaps that isnt necessary but I had some trouble getting it to work without closing the connection,not saying it cant be done

void  sendDataToThingspeak(void)
{

// make TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(thingspeakServer, httpPort)) {
    return;
  }

  String url = "/update?key=";
  url += writeAPIKey;
  url += "&field1=";
  url += String(humidity);//
  url += "&field2=";
  url += String(humiditySetPoint );//
  url += "&field3=";
  url += String(humidifier);
  url += "&field4=";
  url += String(autoMode); //
  url += "&field5=";
  url += String(t);
  url += "\r\n";

  // Send request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
               
  // Second channel            

//-------- Make new connection
 if (!client.connect(thingspeakServer, httpPort)) {
    return;
  }
  url = "/update?key=";
  url += writeAPIKey2;
  url += "&field1=";
  url += String(value1);// 
  url += "&field2=";
  url += String(value2 );// 
  url += "\r\n";
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
 Serial.println("Thingspeak");
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: