-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworking.cpp
161 lines (131 loc) · 3.54 KB
/
Networking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Networking.cpp
*
* Created on: Jan 18, 2011
* Author: Andy
*/
#include "Networking.h"
#include "Ethernet/Ethernet.h"
//#include "httpclient/HTTPClient.h"
#define TELNET_PORT 23
#define textBuffSize 9 //length of longest command string plus two spaces for CR + LF
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {10,12,34,99 };
byte gateway[] = { 10,12,34, 1 };
byte subnet[] = { 255, 255, 255, 0 };
char textBuff[textBuffSize]; //someplace to put received text
int charsReceived = 0;
boolean connectFlag = 0; //we'll use a flag separate from client.connected
//so we can recognize when a new connection has been created
unsigned long timeOfLastActivity; //time in milliseconds of last activity
unsigned long allowedConnectTime = 300000; //five minutes
int ReadResponse();
//Server server(TELNET_PORT);
byte iptarget[] = {10,12,34,135};
IPAddress ipaddr(10,12,34,135);
static EthernetClient client;
void InitializeNetwork()
{
// start ethernet
Ethernet.begin(mac,ip, gateway, subnet);
delay(1000);
/*
//server.begin();
Serial.println("trying coddnnected");
if (client.connect(ipaddr, 8080)) {
Serial.println("coddnnected");
// Make a HTTP request:
client.println("GET /restInterface/sensor=11,temp=22,datetime=3333/ HTTP/1.1");
client.println();
client.stop();
}
*/
}
void ServiceTelnet()
{
/*
static boolean connected = false; // whether or not you got a message from the client yet
Client client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!connected) {
Serial.println("We have a new client");
client.println("Hello, client!");
connected = true;
}
// read the bytes incoming from the client:
//char thisChar = client.read();
// echo the bytes back to the client:
//server.write(thisChar);
// echo the bytes to the server as well:
//Serial.print(thisChar);
}
static uint8_t buf[MAX_MSG_LENGTH_SERIAL];
static uint8_t index = 0;
Client client = server.available();
while(client.available())
{
//Serial.println("serialAvail");
//Serial.println(Serial.read(), DEC);
if (index > MAX_MSG_LENGTH_SERIAL)
{
client.println("Max Serial buffer reached... Flushing UART");
client.flush();
index = 0;
}
else
{
buf[index] = client.read();
if (buf[index] == 13)
{
char str[MAX_MSG_LENGTH_SERIAL];
Serial.println("Msg Received From telnet client...");
for (int i = 0; i < index; i++)
{
str[i] = buf[i];
//Serial.print(buf[i]);
}
str[index] = '\0';
strcpy(messages[msgCount],str);
msgCount++;
client.println(str);
index = 0;
}
else
{
index++;
}
}
}
*/
}
int GET_asClient(char * msg, IPAddress serverAddr)
{
Serial.println("connecting to server");
if (client.connect(serverAddr, 80))
{
Serial.println("Connected to server!");
Serial.println(msg);
client.print(msg);
client.print(0x0d);
client.print(0x0a);
client.println("Host: 10.12.34.99");
client.println("");
delay(1000);
while (client.available())
{
Serial.print( (char)client.read());
}
client.flush();
Serial.println("Flushing");
client.stop();
}
else
{
Serial.println("Failed to connect");
}
return 0;
}