我使用一些示例代码创建了一个 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
和.cpp
// inlcludes
#include "arduino.h"
#include "SparkfunWifi.h"
// constructor
SparkfunWifi::SparkfunWifi(Client& client, char* serverName, int port, char* ssid, char* pass)
: client(client), serverName(serverName), serverAddress(), serverPort(port),
iConnectionClose(true), iSendDefaultRequestHeaders(true)
{
_ssid = ssid;
_pass = pass;
_status != WL_CONNECTED;
}
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() {
Serial.println("making POST request to " + _serverName);
client.beginRequest();
StaticJsonDocument<500> doc; // create a JSON document
doc["temp"] = "33.2"; // add the temperature elememnt and value
doc["humidity"] = "95.2"; // add the humidity elememnt and value
client.post("/addWeather"); // call the weather api post operation
client.sendHeader("Content-Type", "application/json"); // send content type and legnth headers
client.sendHeader("Content-Length", measureJsonPretty(doc));
client.beginBody(); // send the JSON
serializeJsonPretty(doc, Serial);
serializeJsonPretty(doc, client);
client.endRequest(); // end http request
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
return statusCode;
}
最后是我的草图.ino
// 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