I've noticed that the ethernet library has a while() function in Ethernet.connect that it uses when returning connection status. My project involves some analog to digital conversions that I'd like to run as often as possible while connecting and sending data to clients. I am very aware that there are the setRetransmissionTime and setRetransmissionCount functions in the library itself, but i'm trying to pass my analog to digital function into the while loop of the connect function, so the ethernet chip can still run the 8 retries at 200ms...
What i've come up with is this...
In the EthernetClient.h
virtual int connect(IPAddress ip, uint16_t port, virtual void(*function)());
virtual int connect(const char *host, uint16_t port, virtual void(*function)());
In the EthernetClient.cpp
int EthernetClient::connect(const char* host, uint16_t port, virtual void (*function)()) {
// Look up the host first
int ret = 0;
DNSClient dns;
IPAddress remote_addr;
dns.begin(Ethernet.dnsServerIP());
ret = dns.getHostByName(host, remote_addr, function);
if (ret == 1) {
return connect(remote_addr, port);
} else {
return ret;
}
}
int EthernetClient::connect(IPAddress ip, uint16_t port, virtual void (*function)() ) {
if (_sock != MAX_SOCK_NUM)
return 0;
for (int i = 0; i < MAX_SOCK_NUM; i++) {
uint8_t s = w5500.readSnSR(i);
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
_sock = i;
break;
}
}
if (_sock == MAX_SOCK_NUM)
return 0;
_srcport++;
if (_srcport == 0) _srcport = 1024;
socket(_sock, SnMR::TCP, _srcport, 0);
if (!::connect(_sock, rawIPAddress(ip), port)) {
_sock = MAX_SOCK_NUM;
return 0;
}
while (status() != SnSR::ESTABLISHED) {
function();
delay(1);
if (status() == SnSR::CLOSED) {
_sock = MAX_SOCK_NUM;
return 0;
}
}
return 1;
}
So in theory, I should be able to run something like this from the webclient.ino example...
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80, AnalogToDigital)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
void AnalogToDigital(){
//analog to digital function I wish to pass goes here
}
As you can see, I modified my .connect function to show client.connect(server, 80, AnalogToDigital)
which should pass the AnalogToDigital function at the bottom.
When i try to compile, all i get is errors regarding pure virtual functions and what not. Again I don't have enough programming experience to solve this myself... I'm hoping its even possible lol... Any help/comments much appreciated!!
-Matt