无法从arduino库调用函数

can't call functions from arduino library

当我尝试调用我的库中的任何函数时,出现以下错误:

In function 'void setup()':
error: expected unqualified-id before '.' token
netti.SetupWifi();
^

昨天它运行良好,我很确定我没有改变任何东西,所以我不知道它有什么问题。该库用于使 wifi 连接和 telnet 监控更容易。

Adruino 代码:

#include <netti.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  netti.SetupWifi(); //error here
}

void loop() {
  // put your main code here, to run repeatedly:
  netti.ReconnectWifi(); //same error here
  Serial.println("runs"); // test so it's running atleast some code
  delay(1000);
  

}

netti.h:

#ifndef netti_h
#define netti_h

#if (ARDUINO >= 100)
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif

#include <ESP8266WiFi.h>

class netti{
    public:
    //constructor
    netti(bool displayMsg=false);
    //methods
    void SetupWifi();
    void ReconnectWifi();
    void SetupMobile(); // for different wifi
    void ReconnectMobile(); //for different wifi
    WiFiClient Telnet;
    private:
    

};

#endif

和netti.cpp:

#include <ESP8266WiFi.h>
#include "netti.h"
#include "Arduino.h"
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);

IPAddress ip(192, 168, 1, 100);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

WiFiServer TelnetServer(23);
WiFiClient Telnet;


netti::netti(bool displayMsg){
  Serial.println("netti kirjasto käytössä");
}

void(* resetFunc) (void) = 0;

void handleTelnet(){
  Telnet.println("test");
  Serial.println("telnet handler called");
  if (TelnetServer.hasClient()){
    if (!Telnet || !Telnet.connected()){
      if(Telnet) Telnet.stop();
      Serial.println("telnet available");
      Telnet =TelnetServer.available();
      
    }else {
      TelnetServer.available().stop();
      Serial.println("telnet in use");
    }
  }
}

void netti::SetupWifi() {
  delay(100);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println("wifi");
  WiFi.hostname("ESP_host");
  WiFi.config(ip, gateway, subnet);
  WiFi.begin("wifi name","pass place");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  };

  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  TelnetServer.begin();
  TelnetServer.setNoDelay(true);
};


void netti::ReconnectWifi() {
  int i=0;
  handleTelnet();
  Telnet.println("test2"); // does nothing, why??
  if(WiFi.status() != WL_CONNECTED){
    delay(5000);
      SetupWifi();
    };
};

void netti::SetupMobile() {
  delay(100);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println("mobile");
  WiFi.hostname("ESP_host");
  WiFi.begin("mobile_place","pass place");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  };

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  TelnetServer.begin();
  TelnetServer.setNoDelay(true);
};


void netti::ReconnectMobile() {
  int i=0;
  handleTelnet();
  if(WiFi.status() != WL_CONNECTED){
    delay(5000);
    SetupMobile();
  }
};


这是我第一次尝试创建自己的库,我不知道为什么它不起作用。 预先感谢所有帮助

PS。我在使用 Telnet 时遇到问题,试图找到解决方案,但当库没有 运行

时,这很难

错误如下。您已经定义了一个名为 netti 的 class,但您还没有创建 class 的实例。因此,您不能在此 class 上调用实例方法。例如,如果您有

class netti{
    public:
    //constructor
    netti(bool displayMsg=false);
    //methods
    void SetupWifi();
    void ReconnectWifi();
    void SetupMobile(); // for different wifi
    void ReconnectMobile(); //for different wifi
    WiFiClient Telnet;
    private:
    

};

你需要写

netti  netti_instance(true);
netti_instance.SetupWifi();

如果您希望能够在没有实例的情况下调用 class 上的方法,您需要将它们声明为 static methods

我建议您查看一些有关如何使用 C++ 的基础教程classes。