0
votes

I'm learning how to use 'class' in C++, and when I'm trying to call a function from my class I'm getting this error:

Error C:\Users\Mateus\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.0\libraries\ESP8266WebServer\src/ESP8266WebServer.h:90:8: note: void ESP8266WebServer::on(const String&, HTTPMethod, ESP8266WebServer::THandlerFunction, ESP8266WebServer::THandlerFunction)

void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);

    ^

C:\Users\Mateus\Documents\ArduinoData\packages\esp8266\hardware\esp8266\2.5.0\libraries\ESP8266WebServer\src/ESP8266WebServer.h:90:8: note: candidate expects 4 arguments, 2 provided

exit status 1 no matching function for call to 'ESP8266WebServer::on(const char [6], )'

My code:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server;
class Light {
  public:
      int InPin;
      int OutPin;
      Light(int aInPin, int aOutPin){
          InPin = aInPin;
          OutPin = aOutPin;
      }
      void setPin() {
          pinMode(InPin, INPUT_PULLUP);
          pinMode(OutPin, OUTPUT);
      }
      void toggleLight()
      {
        digitalWrite(InPin,!digitalRead(InPin));
        String lstate = digitalRead(InPin) ? "Status: ON" : "Status: OFF";
        server.send(200,"text/html", lstate);
      }
};


char* ssid = "MaIs2";
char* password = "3134767570";

const char index_html[] PROGMEM ={"HUGE html string"};

const char style_css[] PROGMEM = {"HUGE css string"};

int Door = 4;
int BDoor = 14;
Light l1(0, 16);
Light l2(2, 5);

void setup()
{
    WiFi.begin(ssid,password);
    Serial.begin(9600);
    while(WiFi.status()!=WL_CONNECTED)
    {
      Serial.print(".");
      delay(500);
    }
    Serial.println("");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
    l1.setPin();
    l2.setPin();
    server.on("/",[](){server.send_P(200,"text/html",index_html);});
    server.on("/style.css", handleCss);
    server.on("/Lg1", l1.toggleLight); //ERROR HERE
    server.on("/Lg2", l2.toggleLight); //ERROR HERE
    server.on("/Door",TDoor);
    server.on("/Doorpulse", PDoor);
    server.begin();


}

void handleCss(){
  server.send_P(200, "text/css", style_css);
}

void loop()
{
  server.handleClient();
  if(digitalRead(l1.InPin) == LOW){
    l1.toggleLight();
    delay(1000);
  }
  if(digitalRead(l2.InPin) == LOW){
    l2.toggleLight();
    delay(1000);
  }
    if(digitalRead(Bdoor) == LOW){
    Tdoor();
    delay(1000);
  }
}

void PDoor()
{
  digitalWrite(Door, HIGH);
  delay(5000);
  digitalWrite(Door, LOW);
}
void TDoor(){
  digitalWrite(Door,!digitalRead(Door));
  String doorstate = digitalRead(Door) ? "Status: Unlocked" : "Status: Locked";
  server.send(200,"text/html", doorstate);
}
1

1 Answers

0
votes

Looks like you need a lambda function, similar to your previous example

server.on("/Lg1", [](){ l1.toggleLight(); });