Hi! I need help because for some reason when i upload my code to my Arduino nano and NodeMCU, the code won't show up in my Thingspeak channel. Can you help analyze my code to find my errors. Thank you and have a nice day. For this project, i used https://create.arduino.cc/projecthub/256867/atmospheric-air-analyser-c467b1?ref=tag&ref_id=environment&offset=1 as reference.
Code for measuring gas PPM:
#include <ArduinoJson.h>
//Gas Sensor Pins
#define MQ4 A1
#define MQ135 A2
#define MQ7 A3
//Gas Sensor Load Resistance (RL)
#define RL_MQ4 10
#define RL_MQ135 10
#define RL_MQ7 10
/MQ4 GAS/
float CH4_A = 1036.746;
float CH4_B = -2.766449;
float LPG_A = 3658.81;
float LPG_B = -3.349998;
/MQ7 GAS/
float CO_A = 89.03861;
float CO_B = -1.583214;
/MQ-135 GASSES/
float CO2_A = 112.89808;
float CO2_B = -2.868463517;
float NOx_A = 34.69756084;
float NOx_B = -3.422829698;
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate 9600 bps
}
void loop()
{
//Refer to the documentation mentioned on this page to understand this better
float VRL_MQ4;
float Rs_MQ4;
float Ro_MQ4 = 56;
float ratio_MQ4;
VRL_MQ4 = analogRead(MQ4)(5.0/1023.0);
Rs_MQ4 = ((5.0/VRL_MQ4)-1)(RL_MQ4);
ratio_MQ4 = Rs_MQ4/Ro_MQ4;
float ppm_LPG = LPG_A * pow(ratio_MQ4, LPG_B);
float ppm_CH4 = CH4_A * pow(ratio_MQ4, CH4_B);
float VRL_MQ135;
float Rs_MQ135;
float Ro_MQ135 = 34;
float ratio_MQ135;
VRL_MQ135 = analogRead(MQ135)(5.0/1023.0);
Rs_MQ135 = ((5.0/VRL_MQ135)-1)(RL_MQ135);
ratio_MQ135 = Rs_MQ135/Ro_MQ135;
float ppm_CO2 = CO2_A * pow(ratio_MQ135, CO2_B);
float ppm_NOx = NOx_A * pow(ratio_MQ135, NOx_B);
float VRL_MQ7;
float Rs_MQ7;
float Ro_MQ7 = 72;
float ratio_MQ7;
VRL_MQ7 = analogRead(MQ7)(5.0/1023.0);
Rs_MQ7 = ((5.0/VRL_MQ7)-1)(RL_MQ7);
ratio_MQ7 = Rs_MQ7/Ro_MQ7;
float ppm_CO = CO_A * pow(ratio_MQ7, CO_B);
DynamicJsonBuffer jBuffer;
JsonObject& root = jBuffer.createObject();
root["LPG"] = ppm_LPG;
root["CH4"] = ppm_CH4;
root["CO"] = ppm_CO;
root["CO2"] = ppm_CO2;
root["NOx"] = ppm_NOx;
root.prettyPrintTo(Serial);
Serial.println("");
//Minimum delay required for ThingSpeak to update is 16 seconds
delay(16000);
}
Code for Thingspeak channel:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include "ThingSpeak.h"
SoftwareSerial mySerial(5, 6);
WiFiClient client; // Creating WiFiClient Object
//ThingSpeak Channel's API Keys
unsigned long myChannelNumber = CHANNEL NUMBER;
const char * myWriteAPIKey = "API KEY";
//Add your WiFi credentials here
const char * WIFI_SSID = "SSID";
const char * WIFI_PASSWORD = "PASSWORD";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
// Check WiFi Status
while (mySerial.available())
{
//To understand this section better, refer to the author: https://arduinojson.org/
const size_t capacity = JSON_OBJECT_SIZE(7) + 100;
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject& root = jsonBuffer.parseObject(mySerial);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
float CO2 = root["CO2"];
float CO = root["CO"];
float CH4 = root["CH4"];
float NOx = root["NOx"];
float LPG = root["LPG"];
Serial.print(CO2, 5); Serial.print(",");
Serial.print(CO, 5); Serial.print(",");
Serial.print(CH4, 5); Serial.print(",");
Serial.print(NOx, 5); Serial.print(",");
Serial.print(LPG, 5);
//Sending Gas Data to ThingSpeak
ThingSpeak.setField(1, CO2);
ThingSpeak.setField(2, CO);
ThingSpeak.setField(3, CH4);
ThingSpeak.setField(4, NOx);
ThingSpeak.setField(5, LPG);
ThingSpeak.writeFields(myChannelNumber,myWriteAPIKey);
}
}