Updating two ThingSpeak channels

Hi,

I’m struggling updating two ThingSpeak channels.
I have 15 fields of data that I need to upload.

Please help!

This is my current code:

//update ThingSpeak channel 1

updated1 = updateThingSpeak1(String(value_water_area_a), String(value_water_area_b), String(value_light), String(value_uv), String(indice_uv), String(value_watertank_level), String(value_water_temp), String(value_rtc_temp));

//if update succeeded light up green LED, else light up red LED
if(updated1){
LightGreen();
}else{
LightRed();
}

//wait for delay time before attempting to post again
delay(DELAY_TIME);
}

bool updateThingSpeak1(String Uvalue_water_area_a, String Uvalue_water_area_b, String Uvalue_light, String Uvalue_uv, String Uindice_uv, String Uvalue_watertank_level, String Uvalue_water_temp, String Uvalue_rtc_temp) {
//initialize your AT command string
String cmd = “AT+CIPSTART=“TCP”,”";

//add IP address and port
cmd += IP;
cmd += “”,80";

//connect
Serial.println(cmd);
delay(2000);
if(Serial.find(“Error”)){
return false;
}

//build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
cmd = GET_a;
cmd += Uvalue_water_area_a;
cmd += FIELD2_a;
cmd += Uvalue_water_area_b;
cmd += FIELD3_a;
cmd += Uvalue_light;
cmd += FIELD4_a;
cmd += Uvalue_uv;
cmd += FIELD5_a;
cmd += Uindice_uv;
cmd += FIELD6_a;
cmd += Uvalue_watertank_level;
cmd += FIELD7_a;
cmd += Uvalue_water_temp;
cmd += FIELD8_a;
cmd += Uvalue_rtc_temp;

cmd += “\r\n”;

//Use AT commands to send data
Serial.print(“AT+CIPSEND=”);
Serial.println(cmd.length());
if(Serial.find(">")){
//send through command to update values
Serial.print(cmd);
}else{
Serial.println(“AT+CIPCLOSE”);
}

if(Serial.find(“OK”)){
//success! Your most recent values should be online.
return true;
}else{
return false;
}
}

boolean connectWiFi(){
//set ESP8266 mode with AT commands
Serial.println(“AT+CWMODE=1”);
delay(2000);

//build connection command
String cmd=“AT+CWJAP=”";
cmd+=SSID;
cmd+="","";
cmd+=PASS;
cmd+=""";

//connect to WiFi network and wait 5 seconds
Serial.println(cmd);
delay(5000);

//if connected return true, else false
if(Serial.find(“OK”)){
return true;
}else{
return false;
}
}

void LightGreen(){
// digitalWrite(red, LOW);
// digitalWrite(green, HIGH);
// delay(500);
for (int j=1; j<=10; j=j+1) { // Start our for loop
digitalWrite(green, HIGH); //Turn green LED on
delay(250);
digitalWrite(green, LOW); //Turn green LED off
delay(250);
}
Serial.print("…ThingSpeak update channel 1 was successful… \n");

digitalWrite (blue, LOW);
digitalWrite (red, LOW);
digitalWrite (green, LOW);
}

void LightRed(){
// digitalWrite(green, LOW);
// digitalWrite(red, HIGH);
for (int k=1; k<=10; k=k+1) { // Start our for loop
digitalWrite(red, HIGH); //Turn green LED on
delay(250);
digitalWrite(red, LOW); //Turn green LED off
delay(250);
}
Serial.print(".!.ThingSpeak update channel 1 was unsuccessful.!. \n");
digitalWrite (blue, LOW);
digitalWrite (red, LOW);
digitalWrite (green, LOW);
}

//if an error has occurred alternate green and red leds
//is dit nodig?
void Error(){
while(true){
LightRed();
delay(2000);
LightGreen();
delay(2000);

//end ThinkSpeak channel 1 update

// } ??

// update ThingSpeak channel 2 with new values

Serial.print("\n");
digitalWrite (blue, LOW); //switch all LED’s off to start with
digitalWrite (red, LOW);
digitalWrite (green, LOW);

updated2 = updateThingSpeak2(String(value_t1), String(value_h1), String(value_hic1), String(value_t2), String(value_h2), String(value_hic2), String(power_reset));

//if update succeeded light up green LED, else light up red LED
if(updated2){
LightGreen();
}else{
LightRed();
}

//wait for delay time before attempting to post again
delay(DELAY_TIME);
}

bool updateThingSpeak2(String Uvalue_t1, String Uvalue_h1, String Uvalue_hic1, String Uvalue_t2, String Uvalue_h2, String Uvalue_hic2, String Upower_reset) {
//initialize your AT command string
String cmd = “AT+CIPSTART=“TCP”,”";

//add IP address and port
cmd += IP;
cmd += “”,80";

//connect
Serial.println(cmd);
delay(2000);
if(Serial.find(“Error”)){
return false;
}

//build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
cmd = GET_b;
cmd += Uvalue_t1;
cmd += FIELD2_b;
cmd += Uvalue_h1;
cmd += FIELD3_b;
cmd += Uvalue_hic1;
cmd += FIELD4_b;
cmd += Uvalue_t2;
cmd += FIELD5_b;
cmd += Uvalue_h2;
cmd += FIELD6_b;
cmd += Uvalue_hic2;
cmd += FIELD7_b;
cmd += Upower_reset;

//continue to add data here if you have more fields such as a light sensor
//cmd += FIELD3;
//cmd += <field 3 value>

cmd += “\r\n”;

//Use AT commands to send data
Serial.print(“AT+CIPSEND=”);
Serial.println(cmd.length());
if(Serial.find(">")){
//send through command to update values
Serial.print(cmd);
}else{
Serial.println(“AT+CIPCLOSE”);
}

if(Serial.find(“OK”)){
//success! Your most recent values should be online.
return true;
}else{
return false;
}
}

boolean connectWiFi2(){
//set ESP8266 mode with AT commands
Serial.println(“AT+CWMODE=1”);
delay(2000);

//build connection command
String cmd=“AT+CWJAP=”";
cmd+=SSID;
cmd+="","";
cmd+=PASS;
cmd+=""";

//connect to WiFi network and wait 5 seconds
Serial.println(cmd);
delay(5000);

//if connected return true, else false
if(Serial.find(“OK”)){
return true;
}else{
return false;
}
}

void LightGreen2(){
// digitalWrite(red, LOW);
// digitalWrite(green, HIGH);
// delay(500);
for (int j=1; j<=10; j=j+1) { // Start our for loop
digitalWrite(green, HIGH); //Turn green LED on
delay(250);
digitalWrite(green, LOW); //Turn green LED off
delay(250);
}
Serial.print("…ThingSpeak update channel 2 was successful… \n");

digitalWrite (blue, LOW);
digitalWrite (red, LOW);
digitalWrite (green, LOW);
}

void LightRed2(){
// digitalWrite(green, LOW);
// digitalWrite(red, HIGH);
for (int k=1; k<=10; k=k+1) { // Start our for loop
digitalWrite(red, HIGH); //Turn green LED on
delay(250);
digitalWrite(red, LOW); //Turn green LED off
delay(250);
}
Serial.print(".!.ThingSpeak update was unsuccessful.!. \n");

digitalWrite (blue, LOW);
digitalWrite (red, LOW);
digitalWrite (green, LOW);
}

//if an error has occurred alternate green and red leds
//is dit nodig?
void Error2(){
while(true){
LightRed();
delay(2000);
LightGreen();
delay(2000);

//end ThinkSpeak channel 2

This is my current code:

Some of it, maybe. The code you posted incorrectly does not even compile.

What your code actually does is a mystery.

How that differs from what you want is a mystery, too.

Why you are pissing away so much memory on the String class is a mystery, too.

Unless it is a murder mystery, I'm not really interested in mysteries.

PaulS:
Some of it, maybe. The code you posted incorrectly does not even compile.

What your code actually does is a mystery.

How that differs from what you want is a mystery, too.

Why you are pissing away so much memory on the String class is a mystery, too.

Unless it is a murder mystery, I'm not really interested in mysteries.

Clearly I'm no programmer :slight_smile: I just need it to work, it's for our greenhouse on the farm. Everything works except for the ThingSpeak part...
But I do have to find a way to update two channels in ThingSpeak with 15 fields, somehow.
Hoping someone has done something similar and can help me with a example or sketch!

PeterTree:
Clearly I'm no programmer :slight_smile: I just need it to work, it's for our greenhouse on the farm. Everything works except for the ThingSpeak part...

So you want to create an Internet enabled software, and "everything works", except the Internet part?

In that case I'd say: Hardly anything works.
:grinning:

PeterTree:
But I do have to find a way to update two channels in ThingSpeak with 15 fields, somehow.
Hoping someone has done something similar and can help me with a example or sketch!

And you are bound to using a wireless ESP8266 controller with AT-firmware?

The Arduino way of connecting an Arduino board to the Internet is by using the "Ethernet shield". So in this forum it will be more likely that you can find experts which are able to help sending GET requests to an webserver on the Internet using the Arduino Ethernet Shield than experts for doing the same with an ESP8266.

BTW: I don't think that this is a proper way of error handling:

//Use AT commands to send data
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    //send through command to update values
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
  
  if(Serial.find("OK")){
    //success! Your most recent values should be online.
    return true;
  }else{
    return false;
  }

This code is only handling communication and command errors between the Arduino Serial and the ESP82766 controller. That piece of code is NOT dealing with Internet errors sending the GET request and getting back a response from the Thingspeak webserver.

What I meant was, the system works without the ThingSpeak part, thus not dependent on the internet to do what it's supposed to do, automate the solenoids, pumps etc. in our greenhouse. The ThingSpeak part is just to keep me updated of the environmental reading like soil moisture, temperature, humidity, lux, UV, water temp, water tank level etc, and also to send me warnings when anything needs urgent attention. But when there is no internet, the system keeps on doing it's job, caring for the plants...

I really just need help uploading various fields to more than one ThingSpeak channel. There are plenty of examples available uploading a maximum of 8 fields to 1 channel, but not more than 8 fields to 2 channels.
I was hoping someone has done this before using an Arduino Uno or Mega with an ESP8266 just so that I can learn from an example or adopt a sketch.

Kind Regards,
Peter

+1 for using code tags! Makes it so much easier to read.

I don't see where you include your api_key in either get request. Also you don't appear to include the URL path and the trailing "HTTP/1.0\r\n\r\n" to properly construct a GET request. You can add some debugging print statements to check on the command string before you actually send it to Thingspeak.

See Instructions | ESP8266 Retro Browser | Hackaday.io for how to construct a http request and Update channel data with HTTP GET or POST - MATLAB - MathWorks Australia for how to include the parameters.

I suggest you comment out updatethingspeak2() until you get the first one working.