I used some example code to create a working sketch of a HttpClient sending JSON from a Uno rv2 to a C# api. All good.
Next was planning to add this to a sketch to send same JSON using the Sparkfun weather shield. I thought to keep the shield code cleaner I'd create a custom library inheriting the HttpClient and just pass the WifiClient, serverName, port and wifi credentials in at the constructor.
I haven't done a lot of C, C++ for 20 odd years. Is what I'm trying to do possible? Or am I just very rusty and haven't figured it out yet. (Probably should have picked something simpler for my first try at a custom library)
If it is possible I'm guessing I've buggered up the constructors and have tried many variations on a theme with no result.
Appreciate any insights.
UPDATE: So I have made some changes buit I'm still getting compile errors I was told to add virtual methods of inherited class (HttpClient) which I've done but I'm still getting compile issues. Errors attached.
Here's my header.
#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
And the .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;
}
Finally my sketch .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);
}
Here's the error output
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
I managed to get this working with the following...
.h
` .cpp
` // inlcludes #include "arduino.h" #include "SparkfunWifi.h" #include "WeatherData.h"
} `