{"id":531,"date":"2026-03-09T07:03:08","date_gmt":"2026-03-09T07:03:08","guid":{"rendered":"https:\/\/iotnoob.com\/wordpress\/?p=531"},"modified":"2026-03-09T07:03:08","modified_gmt":"2026-03-09T07:03:08","slug":"esp8266-%e0%b8%81%e0%b8%b1%e0%b8%9a%e0%b8%81%e0%b8%b2%e0%b8%a3-connect-http","status":"publish","type":"post","link":"https:\/\/iotnoob.com\/wordpress\/2026\/03\/09\/esp8266-%e0%b8%81%e0%b8%b1%e0%b8%9a%e0%b8%81%e0%b8%b2%e0%b8%a3-connect-http\/","title":{"rendered":"ESP8266 \u0e01\u0e31\u0e1a\u0e01\u0e32\u0e23 connect HTTP"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;ESP8266WiFi.h>\n#include &lt;ESP8266HTTPClient.h>\n#include &lt;DHT11.h>\n\n#ifndef STASSID\n#define STASSID \"XXXXXXXXXX\"\n#define STAPSK  \"XXXXXXXXXX\"\n#endif\n\nDHT11 dht11(D2);\n\nvoid setup() {\n\n  Serial.begin(115200);\n\n  Serial.println();\n  Serial.println();\n  Serial.println();\n\n  WiFi.begin(STASSID, STAPSK);\n\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(\".\");\n  }\n  Serial.println(\"\");\n  Serial.print(\"Connected! IP address: \");\n  Serial.println(WiFi.localIP());\n\n}\n\nvoid loop() {\n  int temperature = 0;\n  int humidity = 0;\n\n  int result = dht11.readTemperatureHumidity(temperature, humidity);\n\n  if (result != 0) {\n    temperature = -99999;\n    humidity = 99999;\n  }\n\n  \/\/ wait for WiFi connection\n  if ((WiFi.status() == WL_CONNECTED)) {\n\n    WiFiClient client;\n    HTTPClient http;\n    Serial.print(\"[HTTP] begin...\\n\");\n    http.begin(client, \"http:\/\/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\"); \/\/HTTP API\n    http.addHeader(\"Content-Type\", \"application\/json\");\n\n    Serial.print(\"[HTTP] POST...\\n\");\n    \/\/ start connection and send HTTP header and body\n    String payload = \"{\\\"sensor_t_m\\\":\\\"\" + String(temperature) + \"\\\", \\\"sensor_h_m\\\": \\\"\" + String(humidity) + \"\\\"}\";\n\n    int httpCode = http.POST(payload);\n\n    \/\/ httpCode will be negative on error\n    if (httpCode > 0) {\n      \/\/ HTTP header has been send and Server response header has been handled\n      Serial.printf(\"[HTTP] POST... code: %d\\n\", httpCode);\n\n      \/\/ file found at server\n      if (httpCode == HTTP_CODE_OK) {\n        const String&amp; payload = http.getString();\n        Serial.println(\"received payload:\\n&lt;&lt;\");\n        Serial.println(payload);\n        Serial.println(\">>\");\n      } else {\n        Serial.printf(\"[HTTP] POST... Not failed?, %s\\n\", http.errorToString(httpCode).c_str());\n      }\n    } else {\n      Serial.printf(\"[HTTP] POST... failed, error: %s\\n\", http.errorToString(httpCode).c_str());\n    }\n\n    http.end();\n  }\n\n  delay(60000);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":1,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/posts\/531\/revisions\/532"}],"wp:attachment":[{"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotnoob.com\/wordpress\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}