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.
- coding temperature to thingspeak
- coding gas to thingspeak
- coding flame to thingspeak
so, here is by coding
- 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;
}
- 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;
}
- 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