Combining 3 Coding Thingspeak to 1 full coding

i'm doing gas leakage detector project.
using temperature sensor, flame sensor n gas/smoke detector. and my coding is attached to ThingSpeak.
but, i dont know how to compile 3 coding to be function in thingspeak.

  1. coding temperature to thingspeak
  2. coding gas to thingspeak
  3. coding flame to thingspeak

so, here is by coding

  1. coding temperature to thingspeak

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

#define DEBUG true
#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=5R4GSRYXVSG49***"; //change it with your api key like "GET /update?key=Your Api Key"

int error;
const int sensor_pin = A1;
float temp;
float output;

void setup()
{
Serial.begin(9600);
esp.begin(9600);
pinMode(sensor_pin,INPUT);

send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP="Selipar Surau","Muhdzaim0000"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connected");}
}

void loop()
{
output=analogRead(sensor_pin);
temp =(output*500)/1023;
start: //label
error=0;
updatedata();
if (error==1){
goto start; //go to label "start"
}
delay(1000);
}

void updatedata(){
String command = "AT+CIPSTART="TCP","";
command += IP;
command += "",80";
Serial.println(command);
esp.println(command);
delay(2000);
if(esp.find("Error")){
return;
}
command = Api_key ;
command += "&field1=";
command += temp;
command += "\r\n";
Serial.print("AT+CIPSEND=");
esp.print("AT+CIPSEND=");
Serial.println(command.length());
esp.println(command.length());
if(esp.find(">")){
Serial.print(command);
esp.print(command);
}
else{

Serial.println("AT+CIPCLOSE");
esp.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}

String send_command(String command, const int timeout, boolean debug)
{
String response = "";
esp.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

  1. coding gas to thingspeak

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

#define DEBUG true
#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=5R4GSRYXVSG49***"; //change it with your api key like "GET /update?key=Your Api Key"

int error;
const int GasPin = A0;
int sensorValue = 0;
float output;

void setup()
{
Serial.begin(9600);
esp.begin(9600);
pinMode(GasPin,INPUT);

send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP="Selipar Surau","Muhdzaim0000"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connected");}
}

void loop()
{
output=analogRead(GasPin);
int outputvalue = map (sensorValue, 0, 1023, 0, 255);
int outputMap2=0;
Serial.print ("sensorValue : ");
Serial.print (sensorValue, DEC);
Serial.print ("\t mapValue : ");
Serial.print (outputvalue, DEC);
Serial.print ("\t outputMap2 : ");
Serial.println (outputMap2, DEC);
start: //label
error=0;
updatedata();
if (error==1){
goto start; //go to label "start"
}
delay(1000);
}

void updatedata(){
String command = "AT+CIPSTART="TCP","";
command += IP;
command += "",80";
Serial.println(command);
esp.println(command);
delay(2000);
if(esp.find("Error")){
return;
}
command = Api_key ;
command += "&field2=";
command += sensorValue;
command += "\r\n";
Serial.print("AT+CIPSEND=");
esp.print("AT+CIPSEND=");
Serial.println(command.length());
esp.println(command.length());
if(esp.find(">")){
Serial.print(command);
esp.print(command);
}
else{

Serial.println("AT+CIPCLOSE");
esp.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}

String send_command(String command, const int timeout, boolean debug)
{
String response = "";
esp.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

  1. coding flame to thingspeak

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

#define DEBUG true
#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=5R4GSRYXVSG49***"; //change it with your api key like "GET /update?key=Your Api Key"

int error;
const int FlamePin = A2;
int FlameValue = 0;
float output;

void setup()
{
Serial.begin(9600);
esp.begin(9600);
pinMode(FlamePin,INPUT);

send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP="Selipar Surau","Muhdzaim0000"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connected");}
}

void loop()
{
FlameValue = analogRead(FlamePin);
Serial.print ("Flame = ");
Serial.print (FlameValue);

start: //label
error=0;
updatedata();
if (error==1){
goto start; //go to label "start"
}
delay(1000);
}

void updatedata(){
String command = "AT+CIPSTART="TCP","";
command += IP;
command += "",80";
Serial.println(command);
esp.println(command);
delay(2000);
if(esp.find("Error")){
return;
}
command = Api_key ;
command += "&field3=";
command += FlameValue;
command += "\r\n";
Serial.print("AT+CIPSEND=");
esp.print("AT+CIPSEND=");
Serial.println(command.length());
esp.println(command.length());
if(esp.find(">")){
Serial.print(command);
esp.print(command);
}
else{

Serial.println("AT+CIPCLOSE");
esp.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}

String send_command(String command, const int timeout, boolean debug)
{
String response = "";
esp.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

so, can anyone help me to combine this 3 coding?

thank you

Never used this stuff but in general you combine the fields into the URL. Look how e.g. a google search shows in the URL bar of your browser; I've broken it down into the pieces by adding spaces.

The first part would be your API key, the other parts your fields and values.

https://www.google.com/search?q=arduino+forum &client=firefox-b-d &source=lnms &tbm=isch &sa=X &ved=0ahUKEwj7rN6mxZzhAhWUo3EKHYqmAHQQ_AUIDigB &biw=1366 &bih=654 &dpr=1.2

After reading your values, you can combine them like below.

  command = Api_key ;
  command += "&field1=";   
  command += temp;

  command += "&field2=";   
  command += sensorValue;

  command += "&field1=3=";   
  command += FlameValue;

  command += "\r\n";

Give it a try.

like this?

#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);

#define DEBUG true
#define IP "184.106.153.149"// thingspeak.com ip
String Api_key = "GET /update?key=5R4GSRYXVSG49***"; //change it with your api key like "GET /update?key=Your Api Key"

int error;
const int GasPin = A0;
int sensorValue = 0;

const int sensor_pin = A1;
float temp;

const int FlamePin = A2;
int FlameValue = 0;

float output;

void setup()
{
Serial.begin(9600);
esp.begin(9600);
pinMode(sensor_pin,INPUT);
pinMode(GasPin,INPUT);
pinMode(FlamePin,INPUT);

send_command("AT+RST\r\n", 2000, DEBUG); //reset module
send_command("AT+CWMODE=1\r\n", 1000, DEBUG); //set station mode
send_command("AT+CWJAP="Selipar Surau","Muhdzaim0000"\r\n", 2000, DEBUG); //connect wifi network
while(!esp.find("OK")) { //wait for connection
Serial.println("Connected");}
}

void loop()
{
{
output=analogRead(sensor_pin);
temp =(output*500)/1023;
}
{
output=analogRead(GasPin);
int outputvalue = map (sensorValue, 0, 1023, 0, 255);
int outputMap2=0;
Serial.print ("sensorValue : ");
Serial.print (sensorValue, DEC);
Serial.print ("\t mapValue : ");
Serial.print (outputvalue, DEC);
Serial.print ("\t outputMap2 : ");
Serial.println (outputMap2, DEC);
}
{
FlameValue = analogRead(FlamePin);
Serial.print ("Flame = ");
Serial.print (FlameValue);
}
start: //label
error=0;
updatedata();
if (error==1){
goto start; //go to label "start"
}
delay(1000);
}

void updatedata(){
String command = "AT+CIPSTART="TCP","";
command += IP;
command += "",80";
Serial.println(command);
esp.println(command);
delay(2000);
if(esp.find("Error")){
return;
}
command = Api_key ;
command += "&field1=";
command += temp;
command += "&field2=";
command += sensorValue;
command += "&field3=";
command += FlameValue;
command += "\r\n";
Serial.print("AT+CIPSEND=");
esp.print("AT+CIPSEND=");
Serial.println(command.length());
esp.println(command.length());
if(esp.find(">")){
Serial.print(command);
esp.print(command);
}
else{

Serial.println("AT+CIPCLOSE");
esp.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}

String send_command(String command, const int timeout, boolean debug)
{
String response = "";
esp.print(command);
long int time = millis();
while ( (time + timeout) > millis())
{
while (esp.available())
{
char c = esp.read();
response += c;
}
}
if (debug)
{
Serial.print(response);
}
return response;
}

Did you try it?

Please read How to use this forum - please read., specifically point #7 how to post code. Make it easier to read and copy; next edit your previous reply and add the code tags.

Further, I suggest that you get rid of redundant { and }. E.g. the below does not need them.

  {
    output = analogRead(sensor_pin);
    temp = (output * 500) / 1023;
  }

And get rid of goto's; you do not need them.

start: //label
  error = 0;
  updatedata();
  if (error == 1) {
    goto start; //go to label "start"
  }

could (at quick glance) be rewritten as

error = 0;
do
{
  error = 0;
  updatedata();
} while (error == 1);

Note that both your update 'loop' and the above do-while block forever if your updatedata() function will not manage to send.