Good evening guys, so recently i'm doing my final year project regarding IOT stuff. I'm using ESP8266 WiFi module connecting with Arduino Mega 2560 and ThingSpeak platform to record the data. I had done with my coding but my ESP8266 Wifi Module unable to connect with Wifi. I had attached my coding below. The RX pin of wifi module is connected to TX1 which will be pin 18 of arduino and TX pin of wifi module is connected to RX1 pin 19 of arduino. I had removed my wifi id and password for some reason. If anyone know how to fix this stuff please help me. Thanks a lot guys
#include <SoftwareSerial.h>
String apiKey = "write key";
Serial (18,19); // RX, TX
int LM393 = 2; // Sound sensor pin
int MQ9 = A0; //Sensor pin
float m = -0.481; //Slope
float b = 1.54; //Y-Intercept
float R0 = 3.01; //Sensor Resistance in fresh air from previous code
void setup(){
ser.begin(115200);
Serial.begin(115200);
pinMode(MQ9,INPUT);
pinMode(LM393,INPUT);
unsigned char check_connection=0;
unsigned char times_check=0;
Serial.println("Connecting to Wifi");
while(check_connection==0)
{
Serial.print("..");
ser.print("AT+CWJAP="ssid","pass"\r\n");
ser.setTimeout(5000);
if(ser.find("WIFI CONNECTED\r\n")==1 )
{
Serial.println("WIFI CONNECTED");
break;
}
times_check++;
if(times_check>3)
{
times_check=0;
Serial.println("Trying to Reconnect..");
}
}
delay(5000);
}
void loop()
{
float sensor_volt; //Define variable for sensor voltage
float RS_gas; //Define variable for sensor resistance
float ratio; //Define variable for ratio
int sensorValue = analogRead(MQ9),digitalRead(2); //Read analog values of sensor
sensor_volt = sensorValue*(5.0/1023.0); //Convert analog values to voltage
RS_gas = ((5.0*10.0)/sensor_volt)-10.0; //Get value of RS in a gas
ratio = RS_gas/R0; // Get ratio RS_gas/RS_air
double ppm_log = (log10(ratio)-b)/m; //Get ppm value in linear scale according to the the ratio value
double ppm = pow(10, ppm_log); //Convert ppm value to log scale
Serial.print("Our desired PPM = ");
Serial.println(ppm);
double db = 20.0 * log(10) * (sensorValue +1.);
Serial.print("Our desired db = ");
Serial.println(db);
// TCP connection
String cmd = "AT+CIPSTART="TCP","";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += ppm;
getStr +="&field2=";
getStr += db;
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
Serial.println(getStr);
}
else{
ser.println("AT+CIPCLOSE");
Serial.println("CIPCLOSE");
}
// thingspeak needs 15 sec delay between updates
delay(16000);
}