Alamat
Tampin, 73000 Negeri Sembilan

Waktu Bekerja
Isnin to Jumaat: 8AM - 6PM

nodemcu esp8266

Tutorial – HTTP POST Request dengan ESP8266

Spread the love

Berikut ditunjukkan contoh untuk http post request dengan menggunakan NodeMcu (ESP8266) dan Arduino IDE.

#include <ESP8266WiFi.h>
 
const char* ssid = your wifi ssid;
const char* password = your wifi password;
 
const char* host =  your hostname;
 
void setup() {
Serial.begin(115200);
delay(10);
 
// We start by connecting to a WiFi network
 
Serial.println();
Serial.println();
Serial.print(&quot;Connecting to &quot;);
Serial.printl
 
WiFi.begin(ssid, password);
 
while (WiFi.status() ! WL_CONNECTED) {
delay(500);
Serial.print(&quot;.&quot;);
}
 
Serial.println(&quot;&quot;);
Serial.println(&quot;WiFi connected&quot;);
Serial.println(&quot;IP address: &quot;);
Serial.println(WiFi.localIP());
}
 
void loop() {
delay(5000);
Serial.print(&quot;connecting to &quot;);
Serial.println(host);
 
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println(&quot;connection failed&quot;);
return;
}
 
String input_data = &quot;&quot;;
input_data += &quot;email=faiz.lop@gmail.com&amp;&quot;;
input_data += &quot;status=KELUAR&amp;&quot;;
input_data += &quot;nama=Faiz&quot;;
 
Serial.print(&quot;Requesting POST: &quot;);
// Send request to the server:
client.println(&quot;POST /url link HTTP/1.1&quot;);
client.println(&quot;Host: hostname&quot;);
client.println(&quot;User-Agent: ESP8266/1.0&quot;);
client.println(&quot;Connection: close&quot;);
client.println(&quot;Content-Type: application/x-www-form-urlencoded&quot;);
client.print(&quot;Content-Length: &quot;);
client.println(input_data.length());
client.println();
client.print(input_data);
 
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout &gt; 5000) {
Serial.println(&quot;&gt;&gt;&gt; Client Timeout !&quot;);
client.stop();
return;
}
}
 
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
 
Serial.println();
Serial.println(&quot;closing connection&quot;);
}