This repository was archived by the owner on Feb 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ino
More file actions
140 lines (118 loc) · 3.05 KB
/
Copy pathmain.ino
File metadata and controls
140 lines (118 loc) · 3.05 KB
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
/*
Arduino Ethernet Script Server
Created Mars 4, 2014
Mikael Kindborg, Evothings AB
TCP socket server that accept commands for basic scripting
of the Arduino board.
This example is written for use with an Ethernet shield.
The API consists of the requests listed below.
Requests and responses end with a new line.
The input parameter n is a pin number ranging from 2 to 9.
The response is always a 4-character string with a
hex encoded number ranging from 0 to FFFF.
Possible response string values:
H (result from digital read)
L (result from digital read)
0 to 1023 - Analog value (result from analog read)
Set pin mode to OUTPUT for pin n: On
Response: None
Example: O5
Note: O is upper case letter o, not digit zero (0).
Set pin mode to INPUT for pin n: In
Response: None
Example: I5
Write LOW to pin n: Ln
Response: None
Example: L5
Write HIGH to pin n: Hn
Response: None
Example: H5
READ pin n: Rn
Response: "H" (HIGH) or "L" (LOW)
Example: R5 -> H
ANALOG read pin n: An
Response: int value as string (range "0" to "1023")
Example: A5 -> 42
*/
// Include files.
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below, usually found on a sticker
// on the back of your Ethernet shield.
byte mac[] = { 0x98, 0x4F, 0xFF, 0x01, 0x9B, 0x6F };
// The IP address will be dependent on your local network.
// If you have IP network info, uncomment the lines starting
// with IPAddress and enter relevant data for your network.
// If you don't know, you probably have dynamically allocated IP adresses, then
// you don't need to do anything, move along.
IPAddress ip(192,168,1, 9);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 255, 0);
// Create a server listening on the given port.
EthernetServer server(3300);
void setup()
{
pinMode(8, OUTPUT);
Serial.begin(9600);
// Wait for serial port to connect. Needed for Leonardo only.
while (!Serial) { ; }
Ethernet.begin(mac, ip, gateway, subnet);
// Start the server.
server.begin();
// Print status.
printServerStatus();
}
void loop()
{
// Listen for incoming client requests.
EthernetClient client = server.available();
if (!client)
{
return;
}
Serial.println("Client connected");
String request = readRequest(&client);
executeRequest(&client, &request);
// Close the connection.
//client.stop();
Serial.println("Client disonnected");
}
// Read the request line,
String readRequest(EthernetClient* client)
{
String request = "";
// Loop while the client is connected.
while (client->connected())
{
// Read available bytes.
while (client->available())
{
// Read a byte.
char c = client->read();
// Print the value (for debugging).
Serial.write(c);
// Exit loop if end of line.
if ('\n' == c)
{
return request;
}
// Add byte to request line.
request += c;
}
}
return request;
}
void executeRequest(EthernetClient* client, String* request)
{
if(request->equals("T"))
{
digitalWrite(8, HIGH);
delay(100);
digitalWrite(8, LOW);
}
}
void printServerStatus()
{
Serial.print("Server address:");
Serial.println(Ethernet.localIP());
}