Skip to content

Commit f21165d

Browse files
committed
implement connect with domain name
1 parent ef120fa commit f21165d

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Arduino Serial to TCP Bridge Client
22
Arduino client for the [Serial To TCP Bridge Protocol](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) gateway service.
33

4-
Open a TCP connection to a server from the Arduino using just serial. (No shields or network HW)
4+
Open a TCP connection to a server from the Arduino using just serial. No Ethernet/WiFi shields necessary.
5+
Quickly communicate with other servers and make network apps using minimal hardware.
56
See [this](https://github.com/RoanBrand/SerialToTCPBridgeProtocol) for more information on the protocol and for the **Protocol Gateway** you will need to run on the host the Arduino is connected to serially.
67

78
## Dependencies

src/ArduinoSerialToTCPBridgeClient.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ ArduinoSerialToTCPBridgeClient::ArduinoSerialToTCPBridgeClient() {
100100
ser0 = this;
101101
}
102102

103+
/*
104+
SUCCESS 1
105+
TIMED_OUT -1
106+
INVALID_SERVER -2
107+
TRUNCATED -3
108+
INVALID_RESPONSE -4
109+
*/
103110
int ArduinoSerialToTCPBridgeClient::connect(IPAddress ip, uint16_t port) {
104111
uint8_t destination[6] = {
105112
(uint8_t) ((uint32_t) ip),
@@ -109,6 +116,7 @@ int ArduinoSerialToTCPBridgeClient::connect(IPAddress ip, uint16_t port) {
109116
(uint8_t) port,
110117
(uint8_t) (port >> 8)
111118
};
119+
112120
writePacket(PROTOCOL_CONNECT, destination, 6);
113121
lastInAct = millis();
114122
while (state != STATE_CONNECTED) {
@@ -119,17 +127,30 @@ int ArduinoSerialToTCPBridgeClient::connect(IPAddress ip, uint16_t port) {
119127
}
120128
lastInAct = millis();
121129
return 1;
122-
/*
123-
SUCCESS 1
124-
TIMED_OUT -1
125-
INVALID_SERVER -2
126-
TRUNCATED -3
127-
INVALID_RESPONSE -4
128-
*/
129130
}
130131

131132
int ArduinoSerialToTCPBridgeClient::connect(const char *host, uint16_t port) {
133+
uint8_t destination[64];
134+
uint8_t len = 0;
132135

136+
while (host[len] != '\0') {
137+
destination[len] = host[len];
138+
len++;
139+
}
140+
141+
destination[len++] = (uint8_t) port;
142+
destination[len++] = (uint8_t) (port >> 8);
143+
144+
writePacket(PROTOCOL_CONNECT | 0x80, destination, len);
145+
lastInAct = millis();
146+
while (state != STATE_CONNECTED) {
147+
uint32_t now = millis();
148+
if (now - lastInAct >= 5000) {
149+
return -1;
150+
}
151+
}
152+
lastInAct = millis();
153+
return 1;
133154
}
134155

135156
size_t ArduinoSerialToTCPBridgeClient::write(uint8_t) {

0 commit comments

Comments
 (0)