1
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,8,6,5,4,3);
#include <SoftwareSerial.h>
#define RX 9
#define TX 10
//ESP8266 CODE
String AP = "OPPO F11 PRO";       // AP NAME
String PASS = "bhargav123"; // AP PASSWORD
String API = "DWX3OYUU26GI5IJ7";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
  
SoftwareSerial esp8266(RX,TX); 

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("Cable");
 delay(1000);
 lcd.setCursor(0,1);
 lcd.print("Fault Detection");
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A3,INPUT);
  pinMode(A4,INPUT);
  pinMode(A5,INPUT);


//ESP8266 CODE

  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");  


  
}

void loop() {
//ESP8266 CODE
String getData = "GET /update?api_key="+ API +"&field1="+Insulation();
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");

String Insulation1(){
  if(value>=500 && value1<=700)
  int xoutput=0;
  else
  xoutput=1;
  return String(xoutput);
}

  int value1=analogRead(A0);
  delay(1000);
  lcd.clear();
  lcd.setCursor(0,0);
  Serial.println("value1");
  Serial.println(value1);
  if(value1>=500 && value1<=700)
  {
    Serial.println("Insulation at 1KM is good");
    lcd.print("Insul good 1KM");
    delay(3000);
    lcd.clear();
  }
else
   { 
     Serial.println("Insulation at 1KM got damaged");
   lcd.print("Insul damage 1KM");
   delay(3000);
   lcd.clear();
   }
  int value2=analogRead(A1);
  Serial.println("value2 ");
  Serial.print(value2);
  delay(1000);
  if(value2>=500 && value2<=700)
  {
    Serial.println("Insulation at 2KM is good");
    lcd.print("Insul good 2KM");
    delay(3000);
    lcd.clear();

  }
else{
   Serial.println("Insulation at 2KM got damaged");
  lcd.print("Insul damage 2KM");
   delay(3000);
   lcd.clear();
}
    int value3=analogRead(A2);
    Serial.println("value3 ");
    Serial.print(value3);
    if(value3>=1000)
    {
      Serial.println("short circuit at 1KM ");
      lcd.print("Short at 1KM");
      delay(3000);
      lcd.clear();
    }
    int value4=analogRead(A3);
    Serial.println("value4 ");
    Serial.print(value4);
    if(value4<=1000)
    {
      Serial.println("Open circuit at 1KM");
      lcd.print("open at 1KM");
      delay(3000);
      lcd.clear();
    
    }
    int value5=analogRead(A4);
    Serial.println("value5 ");
    Serial.print(value5);
    if(value5>=1000)
    {
      Serial.println("short circuit at 2KM");
      lcd.print("short at 2KM");
      delay(3000);
      lcd.clear();
    
    }
    int value6=analogRead(A5);
    Serial.println("value6 ");
    Serial.print(value6);    
    if(value6<=1000)
    {
      Serial.println("open circuit at 2KM");
      lcd.print("Open at 2KM");
      delay(3000);
      lcd.clear();
    }


//ESP8266 CODE
    void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

 }
1
  • 1
    I am sure there are more error messages. read them
    – Juraj
    Mar 24, 2023 at 8:55

1 Answer 1

2

First thing: format your code properly. Statements that are at the same nesting level should have the same amount of indenting. If you find this rule too hard to follow, you can try the auto-format feature of the Arduino IDE (Ctrl-T).

Once the code is properly indented, its whole structure should become clear. Here is what it looks like:

// Preprocessor directives and declarations of variables...

// Definition of the function "setup":
void setup() {
  // Some code...

  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

// Definition of the function "loop":
void loop() {
  // Some code...

  // Nested function definition:
  String Insulation1() {
    // ...
  }

  // More code...

  // Nested function definition:
  void sendCommand(String command, int maxTime, char readReplay[]) {
    // ...
  }
}

The C++ language does not support nested function definitions. Even if it did, using in setup() a function that is only defined within loop() would not work. The solution is simple: move the definition of sendCommand() out of loop(), into the global scope. Oh, and remove the definition of Insulation1(): it is never used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.