-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.ino
More file actions
136 lines (128 loc) · 3.5 KB
/
http.ino
File metadata and controls
136 lines (128 loc) · 3.5 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
/*
*
* HTTP Client
*
**/
#if PROXYSET == 1
static const int serverPORT = PROXYPORT;
static const char serverIP[] = PROXY;
static const char baseUrl[] = SERVER;
#else
static const int serverPORT = SERVERPORT;
static const char serverIP[] = SERVER;
#endif
void httpReq() {
client.stop();
#if VERBOSE > 2
serialMessage('d',F("HTTP:SND"),serverIP);
#endif
if (client.connect(serverIP,serverPORT)) {
#if PROXYSET == 1
// Craft PROXY HTTP request :
client.print(F("GET http://"));
client.println(baseUrl);
client.print(F("/lights/"));
client.print(mac2Str(mac));
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(baseUrl);
#else
// craft DIRECT GET HTTP request:
client.print(F("GET /lights/"));
client.print(mac2Str(mac));
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(serverIP);
#endif
client.println(F("Connection: close"));
client.println();
#if VERBOSE > 2
serialMessage('d',F("HTTP:SND:OK"),serverIP);
#endif
} else {
#if VERBOSE >= 1
serialMessage('e',F("HTTP:SND:KO"),serverIP);
#endif
}
}
void httpRead() {
int len = client.available();
if (len > 0) {
#if RESET_ON_FAIL > 0
ErrorCount = 0;
#endif
#if VERBOSE > 2
serialMessage('d',F("HTTP:RCV:OK"),ip2Str(client.remoteIP()));
#endif
char buffer[160];
if (len > 160) len = 160;
client.read(buffer, len);
#if VERBOSE > 2
serialMessage('d',F("HTTP:RCV"),F("BFR:"));
Serial.write(buffer, len);
Serial.println();
#endif
for (int i = 0; i < len; i++){
if (buffer[i] == '>'){
if ( isDigit(buffer[i-1]) && buffer[i-1] != 0 ){
eventId = buffer[i-1];
}
}
}
}
}
/*
void readHttpEvent(){
unsigned long start = micros();
const int kNetworkTimeout = 500;
const int kNetworkDelay = 100;
int statusCode = http.responseStatusCode();
unsigned long timeoutStart = millis();
if ( (statusCode >= 200) && (statusCode < 300)
&& ((millis() - timeoutStart) < kNetworkTimeout) )
{
int skipHeaders = http.skipResponseHeaders();
if (skipHeaders >= 0){
int bodyLen = http.contentLength();
if (bodyLen == 4){
timeoutStart = millis();
char c;
// Whilst we haven't timed out & haven't reached the end of the body
while ( (http.connected() || http.available()) &&
((millis() - timeoutStart) < kNetworkTimeout) )
{
if (http.available()){
c = http.read();
// Print out this character
//Serial.print(c);
bodyLen--;
// We read something, reset the timeout counter
timeoutStart = millis();
}
}
}
}
}
http.stop();
unsigned long end = micros();
unsigned long delta = end - start;
Serial.print("FUNC11::TIME: ");
Serial.println(delta);
}
void getHttpEvent(){
unsigned long start = micros();
http.stop();
int statusConnect = http.get(SERVER, SERVERPORT, "/lights/0000deadbeef", "fluolight");
if (statusConnect == 0){
//Serial.println("startedRequest ok");
}
else{
Serial.print("Connect failed: ");
Serial.println(statusConnect);
}
unsigned long end = micros();
unsigned long delta = end - start;
Serial.print("FUNC10::TIME: ");
Serial.println(delta);
}
*/