Skip to content

Commit 85bdc6e

Browse files
authored
Update PostHttpClient.ino
style, comments
1 parent ddb2faf commit 85bdc6e

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed
Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,84 @@
11
/**
2-
* PostHTTPClient.ino
3-
*
4-
* Created on: 21.11.2016
5-
*
6-
*/
2+
PostHTTPClient.ino
73
8-
#include <Arduino.h>
4+
Created on: 21.11.2016
95
10-
#include <ESP8266WiFi.h>
11-
#include <ESP8266WiFiMulti.h>
6+
*/
127

8+
#include <ESP8266WiFi.h>
139
#include <ESP8266HTTPClient.h>
1410

1511
#define USE_SERIAL Serial
1612

17-
ESP8266WiFiMulti WiFiMulti;
13+
/* this can be run with an emulated server on host:
14+
cd esp8266-core-root-dir
15+
cd tests/host
16+
make ../../libraries/ESP8266WebServer/examples/PostServer/PostServer
17+
bin/PostServer/PostServer
18+
then put your PC's IP address in SERVER_IP below, port 9080 (instead of default 80):
19+
*/
20+
//#define SERVER_IP "10.0.1.7:9080" // PC address with emulation on host
21+
#define SERVER_IP "192.168.1.42"
22+
23+
#ifndef STASSID
24+
#define STASSID "your-ssid"
25+
#define STAPSK "your-password"
26+
#endif
1827

1928
void setup() {
2029

21-
USE_SERIAL.begin(115200);
22-
// USE_SERIAL.setDebugOutput(true);
30+
USE_SERIAL.begin(115200);
2331

24-
USE_SERIAL.println();
25-
USE_SERIAL.println();
26-
USE_SERIAL.println();
32+
USE_SERIAL.println();
33+
USE_SERIAL.println();
34+
USE_SERIAL.println();
2735

28-
for(uint8_t t = 4; t > 0; t--) {
29-
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
30-
USE_SERIAL.flush();
31-
delay(1000);
32-
}
36+
WiFi.begin(STASSID, STAPSK);
3337

34-
WiFiMulti.addAP("SSID", "PASSWORD");
38+
while (WiFi.status() != WL_CONNECTED) {
39+
delay(500);
40+
USE_SERIAL.print(".");
41+
}
42+
USE_SERIAL.println("");
43+
USE_SERIAL.print("Connected! IP address: ");
44+
USE_SERIAL.println(WiFi.localIP());
3545

3646
}
3747

3848
void loop() {
39-
// wait for WiFi connection
40-
if((WiFiMulti.run() == WL_CONNECTED)) {
41-
42-
WiFiClient client;
43-
HTTPClient http;
44-
45-
USE_SERIAL.print("[HTTP] begin...\n");
46-
// configure traged server and url
47-
http.begin(client, "http://192.168.1.12/post/"); //HTTP
48-
http.addHeader("Content-Type", "application/json");
49-
50-
USE_SERIAL.print("[HTTP] POST...\n");
51-
// start connection and send HTTP header and body
52-
int httpCode = http.POST("{\"hello\":\"world\"}");
53-
54-
// httpCode will be negative on error
55-
if(httpCode > 0) {
56-
// HTTP header has been send and Server response header has been handled
57-
USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);
58-
59-
// file found at server
60-
if(httpCode == HTTP_CODE_OK) {
61-
String payload = http.getString();
62-
USE_SERIAL.println(payload);
63-
}
64-
} else {
65-
USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
66-
}
67-
68-
http.end();
49+
// wait for WiFi connection
50+
if ((WiFiMulti.run() == WL_CONNECTED)) {
51+
52+
WiFiClient client;
53+
HTTPClient http;
54+
55+
USE_SERIAL.print("[HTTP] begin...\n");
56+
// configure traged server and url
57+
http.begin(client, "http://" SERVER_IP "/postplain/"); //HTTP
58+
http.addHeader("Content-Type", "application/json");
59+
60+
USE_SERIAL.print("[HTTP] POST...\n");
61+
// start connection and send HTTP header and body
62+
int httpCode = http.POST("{\"hello\":\"world\"}");
63+
64+
// httpCode will be negative on error
65+
if (httpCode > 0) {
66+
// HTTP header has been send and Server response header has been handled
67+
USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode);
68+
69+
// file found at server
70+
if (httpCode == HTTP_CODE_OK) {
71+
const String& payload = http.getString();
72+
USE_SERIAL.println("received payload:\n<<");
73+
USE_SERIAL.println(payload);
74+
USE_SERIAL.println(">>");
75+
}
76+
} else {
77+
USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
6978
}
7079

71-
delay(10000);
80+
http.end();
81+
}
82+
83+
delay(10000);
7284
}

0 commit comments

Comments
 (0)