This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
arduinoWebClient.ino
166 lines (140 loc) · 4.4 KB
/
arduinoWebClient.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
162
163
164
165
166
#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "d1wt7yy3y78u4b.cloudfront.net"; // name address for Google (using DNS)
const char* pullingResource = "/puzzle?action=arduinoPull";
const char* updateMovingStatusResource = "/puzzle?action=arduinoMove";
struct PuzzleStatus {
char action[32];
bool toMove;
int startingPos[2];
int endingPos[2];
};
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
PuzzleStatus puzzleStatus;
void initSerial() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to conect. Needed for native USB port only
}
Serial.println("Serial ready");
}
void initEthernet() {
if (!Ethernet.begin(mac)) {
Serial.println("Failed to configure Ethernet");
return;
}
Serial.print("Ethernet ready at ");
Serial.println(Ethernet.localIP());
delay(1000);
}
bool sendRequest(const char* host, const char* resource) {
Serial.print("GET ");
Serial.println(resource);
client.print("GET ");
client.print(resource);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
client.println();
return true;
}
bool skipResponseHeaders() {
char endOfHeaders[] = "\r\n\r\n";
client.setTimeout(10000);
bool ok = client.find(endOfHeaders); // HTTP headers end with an empty line
if (!ok) {
Serial.println("No response or invalid response!");
}
return ok;
}
void readReponseContent(char* content, size_t maxSize) {
size_t length = client.readBytes(content, maxSize);
content[length] = 0;
Serial.println(content);
}
bool parsePuzzleStatus(char* content, PuzzleStatus* puzzleStatus) {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(content);
if (!root.success()) {
Serial.println("JSON parsing failed!");
return false;
}
strcpy(puzzleStatus->action, root["action"]);
puzzleStatus->toMove = root["toMove"];
puzzleStatus->startingPos[0] = root["startingPos"][0];
puzzleStatus->startingPos[1] = root["startingPos"][1];
puzzleStatus->endingPos[0] = root["endingPos"][0];
puzzleStatus->endingPos[1] = root["endingPos"][1];
return true;
}
void printPuzzleStatus(const PuzzleStatus* puzzleStatus) {
Serial.print("Action = ");
Serial.println(puzzleStatus->action);
Serial.print("toMove = ");
Serial.println(puzzleStatus->toMove);
Serial.print("startingPos = ");
Serial.print(puzzleStatus->startingPos[0]);
Serial.print(" , ");
Serial.println(puzzleStatus->startingPos[1]);
Serial.print("endingPos = ");
Serial.print(puzzleStatus->endingPos[0]);
Serial.print(" , ");
Serial.println(puzzleStatus->endingPos[1]);
}
void updateMovingStatus(const PuzzleStatus* puzzleStatus){
if (client.connect(server, 80)) {
Serial.println("connected for moving status update!");
// send an update request informing the moving status.
if (sendRequest(server, updateMovingStatusResource) && skipResponseHeaders()) {
char response[512];
readReponseContent(response, sizeof(response));
}
disconnect();
// MOVE MOVE Move!!!
// Move robotic arm here based on startingPos and endingPos
}
}
void disconnect() {
Serial.println("Disconnect");
client.stop();
}
void wait() {
Serial.println("Wait 10 seconds");
delay(10000);
}
void setup() {
initSerial();
initEthernet();
}
void loop() {
boolean currentLineIsBlank = true;
if (client.connect(server, 80)) {
Serial.println("connected for status pulling!");
if (sendRequest(server, pullingResource) && skipResponseHeaders()) {
char response[512];
readReponseContent(response, sizeof(response));
if (parsePuzzleStatus(response, &puzzleStatus)) {
printPuzzleStatus(&puzzleStatus);
}
}
disconnect();
}
// robotic arm moving based on status
if (puzzleStatus.toMove) {
updateMovingStatus(&puzzleStatus);
}
wait();
}