Skip to content

Commit 3dca365

Browse files
added experimental StandardFirmataEthernet example
1 parent b1a39bc commit 3dca365

File tree

3 files changed

+955
-0
lines changed

3 files changed

+955
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
EthernetClientStream.cpp
3+
An Arduino-Stream that wraps an instance of Client reconnecting to
4+
the remote-ip in a transparent way. A disconnected client may be
5+
recognized by the returnvalues -1 from calls to peek or read and
6+
a 0 from calls to write.
7+
8+
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
See file LICENSE.txt for further informations on licensing terms.
16+
17+
formatted using the GNU C formatting and indenting
18+
*/
19+
20+
#include "EthernetClientStream.h"
21+
#include <Arduino.h>
22+
23+
#define MILLIS_RECONNECT 5000
24+
25+
EthernetClientStream::EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port)
26+
: ip(ip),
27+
host(host),
28+
port(port),
29+
connected(false),
30+
client(client),
31+
localip(localip)
32+
{
33+
}
34+
35+
int
36+
EthernetClientStream::available()
37+
{
38+
return maintain() ? client.available() : 0;
39+
}
40+
41+
int
42+
EthernetClientStream::read()
43+
{
44+
return maintain() ? client.read() : -1;
45+
}
46+
47+
int
48+
EthernetClientStream::peek()
49+
{
50+
return maintain() ? client.peek() : -1;
51+
}
52+
53+
void EthernetClientStream::flush()
54+
{
55+
if (maintain())
56+
client.flush();
57+
}
58+
59+
size_t
60+
EthernetClientStream::write(uint8_t c)
61+
{
62+
return maintain() ? client.write(c) : 0;
63+
}
64+
65+
void
66+
EthernetClientStream::maintain(IPAddress localip)
67+
{
68+
if (this->localip!=localip)
69+
{
70+
this->localip = localip;
71+
if (connected)
72+
stop();
73+
}
74+
}
75+
76+
void
77+
EthernetClientStream::stop()
78+
{
79+
client.stop();
80+
connected = false;
81+
time_connect = millis();
82+
}
83+
84+
bool
85+
EthernetClientStream::maintain()
86+
{
87+
if (client && client.connected())
88+
return true;
89+
90+
if (connected)
91+
{
92+
stop();
93+
}
94+
else if (millis()-time_connect >= MILLIS_RECONNECT)
95+
{
96+
connected = host ? client.connect(host,port) : client.connect(ip,port);
97+
if (!connected) {
98+
time_connect = millis();
99+
Serial.println("connection failed");
100+
} else {
101+
Serial.println("connected");
102+
}
103+
}
104+
return connected;
105+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
EthernetClientStream.h
3+
An Arduino-Stream that wraps an instance of Client reconnecting to
4+
the remote-ip in a transparent way. A disconnected client may be
5+
recognized by the returnvalues -1 from calls to peek or read and
6+
a 0 from calls to write.
7+
8+
Copyright (C) 2013 Norbert Truchsess. All rights reserved.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
See file LICENSE.txt for further informations on licensing terms.
16+
17+
formatted using the GNU C formatting and indenting
18+
*/
19+
20+
#ifndef ETHERNETCLIENTSTREAM_H
21+
#define ETHERNETCLIENTSTREAM_H
22+
23+
#include <inttypes.h>
24+
#include <stdio.h>
25+
#include <Stream.h>
26+
#include <Client.h>
27+
#include <IPAddress.h>
28+
29+
class EthernetClientStream : public Stream
30+
{
31+
public:
32+
EthernetClientStream(Client &client, IPAddress localip, IPAddress ip, const char* host, uint16_t port);
33+
int available();
34+
int read();
35+
int peek();
36+
void flush();
37+
size_t write(uint8_t);
38+
void maintain(IPAddress localip);
39+
40+
private:
41+
IPAddress localip;
42+
IPAddress ip;
43+
const char* host;
44+
uint16_t port;
45+
Client &client;
46+
bool connected;
47+
uint32_t time_connect;
48+
bool maintain();
49+
void stop();
50+
};
51+
52+
#endif

0 commit comments

Comments
 (0)