I wrote an open source network communication library that uses the Ethernet library. I'd like to add support for using this library with the WiFi library, Yun, and ESP8266. I found the arduino_exosite_library that does this and decided to attempt to use it as an reference for the changes I need to make on my library. The trouble is arduino_exosite_library only uses the Client class so I'm stuck on how to use Server instead of EthernetServer in my library. I wrote a minimal library to demonstrate what I'm trying to do. All 3 versions of the library are attached. The first version follows the exosite library's system but still uses EthernetClient and EthernetServer to make sure everything is working:
NetTest1.ino:
#include <SPI.h>
#include <Ethernet.h>
#include "NetTest1.h"
EthernetServer ethernetServer(1024);
EthernetClient ethernetClient;
NetTest netTest(ðernetServer, ðernetClient);
unsigned long sendTimeStamp;
void setup() {
Serial.begin(9600);
byte MACaddress[] = {0, 1, 2, 3, 4, 5};
Ethernet.begin(MACaddress, IPAddress(192, 168, 169, 104));
ethernetServer.begin();
}
void loop() {
netTest.available();
if (millis() - sendTimeStamp > 4000) {
sendTimeStamp = millis();
netTest.send(IPAddress(192, 168, 169, 100), 1024);
}
}
NetTest1.h:
#ifndef NetTest1_h
#define NetTest1_h
#include <Arduino.h>
#include <Ethernet.h>
class NetTest {
public:
NetTest(EthernetServer *_server, EthernetClient *_client);
void available();
void send(const IPAddress &target, const unsigned int port);
private:
class EthernetServer* server;
class EthernetClient* client;
};
#endif
NetTest1.cpp:
#include "NetTest1.h"
NetTest::NetTest(EthernetServer *_server, EthernetClient *_client) {
server = _server;
client = _client;
}
void NetTest::available() {
EthernetClient connectedClient = server->available();
if (connectedClient) {
Serial.println("available: client connected");
connectedClient.println("hello client");
}
if (const byte availableBytes = connectedClient.available()) {
// read the bytes incoming from the client:
Serial.print("available: message from client: ");
for (byte byteCounter = 0; byteCounter < availableBytes; byteCounter++) {
Serial.write(connectedClient.read());
}
Serial.println();
connectedClient.stop();
}
}
void NetTest::send(const IPAddress &target, const unsigned int port) {
if (client->connect(target, port)) {
Serial.println("send: connected");
client->println("hi");
client->stop();
}
else {
Serial.println("send: connection failed");
}
}
NetTest1 works as expected. Next I modified it to use the Client class instead of EthernetClient. This version is NetTest2 available in the attachment I have not posted the text here to keep things short but am happy to do so on request. NetTest2 also works as expected. So then I changed the library to use Server instead of EthernetServer:
NetTest3.ino:
#include <SPI.h>
#include <Ethernet.h>
#include "NetTest3.h"
EthernetServer ethernetServer(1024);
EthernetClient ethernetClient;
NetTest netTest(ðernetServer, ðernetClient);
unsigned long sendTimeStamp;
void setup() {
Serial.begin(9600);
byte MACaddress[] = {0, 1, 2, 3, 4, 5};
Ethernet.begin(MACaddress, IPAddress(192, 168, 169, 104));
ethernetServer.begin();
}
void loop() {
netTest.available();
if (millis() - sendTimeStamp > 4000) {
sendTimeStamp = millis();
netTest.send(IPAddress(192, 168, 169, 100), 1024);
}
}
NetTest3.h:
#ifndef NetTest3_h
#define NetTest3_h
#include <Arduino.h>
#include <IPAddress.h>
#include <Client.h>
#include <Server.h>
class NetTest {
public:
NetTest(Server *_server, Client *_client);
void available();
void send(const IPAddress &target, const unsigned int port);
private:
class Server* server;
class Client* client;
};
#endif
NetTest3.cpp:
#include "NetTest3.h"
NetTest::NetTest(Server *_server, Client *_client) {
server = _server;
client = _client;
}
void NetTest::available() {
if (Client connectedClient = server->available()) {
Serial.println("available: client connected");
connectedClient.println("hello client");
if (const byte availableBytes = connectedClient.available()) {
// read the bytes incoming from the client:
Serial.print("available: message from client: ");
for (byte byteCounter = 0; byteCounter < availableBytes; byteCounter++) {
Serial.write(connectedClient.read());
}
Serial.println();
connectedClient.stop();
}
}
}
void NetTest::send(const IPAddress &target, const unsigned int port) {
if (client->connect(target, port)) {
Serial.println("send: connected");
client->println("hi");
client->stop();
}
else {
Serial.println("send: connection failed");
}
}
When I try to compile NetTest3.ino I get "error: 'class Server' has no member named 'available'". I understand this is because there's no available() function in Server and I've looked at EthernetServer.cpp but I'm drawing a blank on how to proceed. Any suggestions on how I can make this work would be much appreciated, thanks.
NetTest.zip (4.7 KB)