-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialInterruptTest.ino
161 lines (125 loc) · 3.85 KB
/
serialInterruptTest.ino
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
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
#include <EthernetUdp.h>
#include <Servo.h>
#include <SoftwareSerial.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
Servo rudderServo;
int state;
const int servo_pin = 9;
IPAddress ip(192, 168, 0, 21);
EthernetServer server(80);
EthernetUDP Udp;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
unsigned int localPort = 80;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Set up the server on the arduino
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
server.begin();
// set up the udp connection to send messages to the teensy
Udp.begin(localPort);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.print("UDP communication is located at port ");
Serial.println(localPort);
//inputString.reserve(200);
}
void loop() {
// listen for incoming clients
if(Serial1.available()){
state = Serial1.read() - 48;
Serial.println(state);
}
int packetSize = Udp.parsePacket(); // works here
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort()); // works here
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); // works here
Serial.println("Contents:");
Serial.println(packetBuffer);
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
if(state == 1){
Udp.write("1");
} else if (state == 2){
Udp.write("2");
} else{
Udp.write("3");
}
Udp.endPacket();
Serial.println("sent same UDP packet");
EthernetClient client = server.available();
if (client) {
// create the http page
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 5");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("<h1>Output Values</h1>");
for (int digitalChannel = 2 ; digitalChannel < 10; digitalChannel++) {
int sensorReading = digitalRead(digitalChannel);
client.print("Output value ");
client.print(digitalChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
// close the connection:
client.stop();
}
delay(10);
Serial.print("I got: ");
Serial.println(state);
}
}