-2

Below code was working fine without using classes, after using class an error pops up: esp8266 is not defined in this scope

#include<SoftwareSerial.h>
#include<ArduinoJson.h>
#include<Wire.h>
#include<LCD.h>
#include<LiquidCrystal_I2C.h>
#include<SPI.h>

#define DEBUG true
// Housekeeping stuff for LCD display
#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
LiquidCrystal_I2C
lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

class baseFunctions {
  public:
    baseFunctions() {
      Serial.begin(115200); // Housekeeping stuff
      esp8266.begin(115200);  // Housekeeping stuff
      sendData("AT+RST\r\n", 2000, DEBUG); // reset module

      // Code for display
      lcd.begin (16, 2);
      lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
      lcd.setBacklight(HIGH); // Switch on the backlight
      lcd.home (); // go home
      lcd.print("In development");

      pinMode(8, OUTPUT);
    }

    String sendData(String command, const int timeout, boolean debug) {
      String response = "";
      esp8266.print(command); // send the read characte to esp8266
      long int time = millis();
      while ( (time + timeout) > millis() ) {
        while (esp8266.available()) {
          // The esp has data so display its output to serial window
          char c = esp8266.read(); // read the next character
          response += c;
        }
      }
      if (debug) {
        Serial.print(response);
      }
      return response;
    }

    void connectToWifi() {
      sendData("AT+CWMODE=1\r\n", 3000, DEBUG); // Configure as clint
      sendData("AT+CWJAP=\"moto g\", \"hvats555\"\r\n", 5000, DEBUG);  // Connects to wifi
    }

    String fetchJson() {
      StaticJsonBuffer<200> jsonBuffer;
      char json[] = "{\"power\":\"high\"}";
      JsonObject& root = jsonBuffer.parseObject(json);

      const char* power = root["power"];
      return power;
    }
};

void setup() {
  baseFunctions go;
}

void loop() {
}

Here is the error

exit status 1
'esp8266' was not declared in this scope
  • 2
    It's right, you know. esp8266 has not been declared. Maybe you should declare it? – Majenko Nov 14 '17 at 15:49
  • code is working fine without using class – Himalya vats Nov 14 '17 at 15:58
  • 1
    I'm voting to close this question as off-topic because resolving compiling errors is not this part of the Arduino SE charter. – user31481 Nov 14 '17 at 16:11
  • Oh I apologise. – Himalya vats Nov 14 '17 at 16:16
  • 4
    @LookAlterno I disagree. Helping people to learn how to code their Arduinos is within our remit - and if that means solving the odd compilation problem then so be it – Majenko Nov 14 '17 at 16:34
2

When you added the class you obviously deleted/moved code into it from elsewhere.

One bit of code that you missed in that moving around will be the code that declares what esp8266 is:

SoftwareSerial esp8266(2,3); // for example

Find that in your original code and copy it across.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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