Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 您能否继承HttpClient并创建自己的自定义Arduino库?_Inheritance_Arduino_Httpclient_Libraries_Arduino Uno Wifi - Fatal编程技术网

Inheritance 您能否继承HttpClient并创建自己的自定义Arduino库?

Inheritance 您能否继承HttpClient并创建自己的自定义Arduino库?,inheritance,arduino,httpclient,libraries,arduino-uno-wifi,Inheritance,Arduino,Httpclient,Libraries,Arduino Uno Wifi,我使用一些示例代码创建了一个HttpClient的工作草图,该客户端将JSON从Uno rv2发送到C#api。一切都好 下一步计划将其添加到草图中,以使用Sparkfun天气盾发送相同的JSON。我想,为了保持屏蔽代码更干净,我应该创建一个自定义库,继承HttpClient,并传递 WifiClient、serverName、端口和wifi凭据位于构造函数中 20多年来我没有做很多C,C++。 我想做的事可能吗?或者我只是太生疏了,还没弄明白。(我第一次在自定义库中尝试时,可能应该选择一些更简

我使用一些示例代码创建了一个HttpClient的工作草图,该客户端将JSON从Uno rv2发送到C#api。一切都好

下一步计划将其添加到草图中,以使用Sparkfun天气盾发送相同的JSON。我想,为了保持屏蔽代码更干净,我应该创建一个自定义库,继承HttpClient,并传递 WifiClient、serverName、端口和wifi凭据位于构造函数中

20多年来我没有做很多C,C++。 我想做的事可能吗?或者我只是太生疏了,还没弄明白。(我第一次在自定义库中尝试时,可能应该选择一些更简单的)

如果可能的话,我猜我已经搞错了构造器,并且尝试了很多关于主题的变体,但是没有结果

欣赏任何见解

更新:所以我做了一些更改,但仍然出现编译错误 我被告知要添加继承类(HttpClient)的虚拟方法,我已经这样做了,但仍然遇到编译问题。附加错误

这是我的头

#ifndef SPARKFUN_WIFI
#define SPARKFUN_WIFI

#include <ArduinoJson.h>
#include <b64.h>
#include <HttpClient.h>
#include <URLEncoder.h>
#include <WebSocketClient.h>
#include <WiFiNINA.h>
#include "arduino.h"
#include <ArduinoHttpClient.h>

class SparkfunWifi : public HttpClient { 

    public:
    // Contructor
    
    /** Connect to the server and start to send a GET request.
        @param serverName The server name to conect to.
        @param port The port number.
        @param ssid The network ssid.
        @param pass The network password.
    */
    SparkfunWifi(Client& client, char* serverName, char* port, char* ssid, char* pass);
    
    // Methods

    /** Connect to the server and start to send a GET request.
        @return true if connection successful
    */
    bool connectWifi();

    /** Sends HTTP weather POST to weather.api.
        @return 200 if successful response from api.
    */
    int sendWeather();

    virtual bool endOfStream() { return endOfBodyReached(); };
    virtual bool completed() { return endOfBodyReached(); };

    // Inherited from HttpClient which inherited them from Print
    // Note: 1st call to these indicates the user is sending the body, so if need
    // Note: be we should finish the header first
    virtual size_t write(uint8_t aByte) { if (iState < eRequestSent) { finishHeaders(); }; return iClient-> write(aByte); };
    virtual size_t write(const uint8_t *aBuffer, size_t aSize) { if (iState < eRequestSent) { finishHeaders(); }; return iClient->write(aBuffer, aSize); };
    // Inherited from Stream
    virtual int available();
    /** Read the next byte from the server.
      @return Byte read or -1 if there are no bytes available.
    */
    virtual int read();
    virtual int read(uint8_t *buf, size_t size);
    virtual int peek() { return iClient->peek(); };
    virtual void flush() { iClient->flush(); };

    // Inherited from Client
    virtual int connect(IPAddress ip, uint16_t port) { return iClient->connect(ip, port); };
    virtual int connect(const char *host, uint16_t port) { return iClient->connect(host, port); };
    virtual void stop();
    virtual uint8_t connected() { return iClient->connected(); };
    virtual operator bool() { return bool(iClient); };
    virtual uint32_t httpResponseTimeout() { return iHttpResponseTimeout; };
    virtual void setHttpResponseTimeout(uint32_t timeout) { iHttpResponseTimeout = timeout; };
    
    private:

    int _serverName;
    int _status;
    char* _ssid;
    char* _pass;

};
#endif
这是错误输出

SparkfunWifi.cpp:6:1: error: prototype for 'SparkfunWifi::SparkfunWifi(arduino::Client&, char*, int, char*, char*)' does not match any in class 'SparkfunWifi'

 SparkfunWifi::SparkfunWifi(Client& client, char* serverName, int port, char* ssid, char* pass)

 ^~~~~~~~~~~~

In file included from sketch\SparkfunWifi.cpp:3:0:

SparkfunWifi.h:13:7: error: candidates are: SparkfunWifi::SparkfunWifi(SparkfunWifi&&)

 class SparkfunWifi : public HttpClient {

       ^~~~~~~~~~~~

SparkfunWifi.h:13:7: error: SparkfunWifi::SparkfunWifi(const SparkfunWifi&)

SparkfunWifi.h:24:5: error: SparkfunWifi::SparkfunWifi(arduino::Client&, char*, char*, char*, char*)

     SparkfunWifi(Client& client, char* serverName, char* port, char* ssid, char* pass);

     ^~~~~~~~~~~~

sketch\SparkfunWifi.cpp: In member function 'int SparkfunWifi::sendWeather()':

SparkfunWifi.cpp:48:5: error: 'client' was not declared in this scope

     client.beginRequest();

     ^~~~~~

sketch\SparkfunWifi.cpp:48:5: note: suggested alternative: 'Client'

     client.beginRequest();

     ^~~~~~

     Client

exit status 1
[Error] Exit with code=1

我设法让这项工作与以下

h

` .cpp

` //包括 #包括“arduino.h” #包括“sparkfunfi.h” #包括“WeatherData.h”

//构造函数
SparkfunWifi::SparkfunWifi(客户端和wifi、字符串和服务器名、uint16_t端口、字符*
ssid,char*pass)
:(wifi、服务器名、端口)
{
_ssid=ssid;
_通过=通过;
_状态!=WL_已连接;
_serverName=serverName;
}
bool sparkfunfi::connectWifi(){
Serial.begin(9600);
整数倍=1;
while(_status!=WL_CONNECTED){
时代++;

如果(非常锈蚀).h,在构造函数参数中有端口的conchch*。C++是区分大小写的,所以ICLITENT不是ICLITENT。在构造函数中的CPP中,代码> ISErVrNeNeX(AsServNeNT)< /Case>什么是AsServNeX?在构造函数参数中,端口中仍然有H代码?
// inlcludes
#include "arduino_secrets.h"
#include "SparkfunWifi.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char* ssid = SECRET_SSID;
char* pass = SECRET_PASS;

char serverName[] = "dionysus";  // server address
int port = 5000; // port number

WiFiClient wifi;
SparkfunWifi client(wifi, serverName, port, ssid, pass);

void setup() {
    client.connectWifi();
}

void loop() {
  Serial.println("Send weather...");
  client.sendWeather();
  Serial.println("Wait five seconds");
  delay(5000);
}
SparkfunWifi.cpp:6:1: error: prototype for 'SparkfunWifi::SparkfunWifi(arduino::Client&, char*, int, char*, char*)' does not match any in class 'SparkfunWifi'

 SparkfunWifi::SparkfunWifi(Client& client, char* serverName, int port, char* ssid, char* pass)

 ^~~~~~~~~~~~

In file included from sketch\SparkfunWifi.cpp:3:0:

SparkfunWifi.h:13:7: error: candidates are: SparkfunWifi::SparkfunWifi(SparkfunWifi&&)

 class SparkfunWifi : public HttpClient {

       ^~~~~~~~~~~~

SparkfunWifi.h:13:7: error: SparkfunWifi::SparkfunWifi(const SparkfunWifi&)

SparkfunWifi.h:24:5: error: SparkfunWifi::SparkfunWifi(arduino::Client&, char*, char*, char*, char*)

     SparkfunWifi(Client& client, char* serverName, char* port, char* ssid, char* pass);

     ^~~~~~~~~~~~

sketch\SparkfunWifi.cpp: In member function 'int SparkfunWifi::sendWeather()':

SparkfunWifi.cpp:48:5: error: 'client' was not declared in this scope

     client.beginRequest();

     ^~~~~~

sketch\SparkfunWifi.cpp:48:5: note: suggested alternative: 'Client'

     client.beginRequest();

     ^~~~~~

     Client

exit status 1
[Error] Exit with code=1
#ifndef SPARKFUN_WIFI
#define SPARKFUN_WIFI

#include "arduino.h"
#include "ArduinoJson.h"
#include "HttpClient.h"
#include "ArduinoHttpClient.h"
#include "WiFiNINA.h"
#include "WeatherData.h"

class SparkfunWifi : public HttpClient { 

public:
// Contructor

/** Create a new Sparkwifi class inheriting HttpClient.
    @param wifi The wifi client.
    @param port The port number.
    @param ssid The network ssid.
    @param pass The network password.
*/
SparkfunWifi(Client& wifi, String& serverName, uint16_t port, char* ssid, char* pass);

// Methods

/** Connect to the server and start to send a GET request.
    @return true if connection successful
*/
bool connectWifi();

/** Sends HTTP weather POST to weather.api.
    @return 200 if successful response from api.
*/
int sendWeather(WeatherData weatherData);

private:
int _status;
char* _ssid;
char* _pass;
String _serverName; 


};
#endif
// constructor
 SparkfunWifi::SparkfunWifi(Client& wifi, String& serverName, uint16_t port, char* 
    ssid, char* pass)
 : (wifi, serverName, port)
 {
 _ssid = ssid;
 _pass = pass;
 _status != WL_CONNECTED;
 _serverName = serverName;
 }

bool SparkfunWifi::connectWifi(){
Serial.begin(9600);
int times = 1;
while (_status != WL_CONNECTED) {
    times++;
    if (times <= 5){
        // attempt to connect 5 times
        Serial.println("Attempting to connect to Network named: ");
        Serial.println(_ssid);   // print the network name (SSID);
        Serial.println("Attempt:");   // print the network name (SSID);
        Serial.println(times);   // print the network name (SSID);
    
        // Connect to WPA/WPA2 network:
        _status = WiFi.begin(_ssid, _pass);
        Serial.print("SSID: ");
        Serial.println(WiFi.SSID());
        // print your WiFi shield's IP address:
        IPAddress ip = WiFi.localIP();
        Serial.print("IP Address: ");
        Serial.println(ip);
        return true;
    }
    else 
    {
        Serial.print("Could not connect on 5th attempt to: ");
        Serial.println(_ssid);   // print the network name (SSID);
        return false;
    }
 }
}

int SparkfunWifi::sendWeather(WeatherData weatherData) {
beginRequest();
DynamicJsonDocument doc(500); 
doc["temp"] = weatherData.getTemp();  
doc["humidity"] = weatherData.getHumidity(); 
doc["windDir"] = weatherData.getWindDir();  
doc["windSpeed"] = weatherData.getWindSpeed();
doc["windGust"] = weatherData.getWindGust(); 
doc["windGustDir"] = weatherData.getWindGustDir();  
doc["windSpeedAvg2m"] = weatherData.getWindSpeedAvg2m();  
doc["windDirAvg2m"] = weatherData.getWindDirAvg2m(); 
doc["windGustAvg10m"] = weatherData.getWindGustAvg10m();  
doc["windGustDirAvg10m"] = weatherData.getWindGustDirAvg10m(); 
doc["dailyRain"] = weatherData.getDailyRain();
doc["pressure"] = weatherData.getPressure();  
doc["batteryLevel"] = weatherData.getBatteryLevel();
doc["lightLevel"] = weatherData.getLightLevel();  
post("/addWeather"); 
sendHeader("Content-Type", "application/json");
sendHeader("Content-Length", measureJsonPretty(doc));
beginBody(); // send the JSON 
serializeJsonPretty(doc, Serial);
serializeJsonPretty(doc, (*iClient));
endRequest(); // end http request

// read the status code
int statusCode = responseStatusCode();
Serial.println(statusCode);

return statusCode;