หากผมต้องการเพิ่มค่า sid เป็น 2 เเทรกส่วนไหนหรอครับ
ว่งโค้ตที่ใช้ด้วยครับ
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "Office";
const char* password = "xxxxxdddf";
//Your Domain name with URL path or IP address with path
const char* serverName = "https://jaiangelbot.cc.pcs-plp.com/testbot/wtsensor.php";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
// http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
// String httpRequestData = "sid=1&api_key=xxxxxx&light=1100&hum=120&time=20220615113712&type=alert";
// Send HTTP POST request
//int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"sid\":\"1\",\"api_key\":\"xxxxxx\",\"light\":\"1000\",\"hum\":\"120\",\"time\":\"20220615113712\",\"type\":\"alert\"}");
// If you need an HTTP request with a content type: text/plain
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
ที่ยิงขึ้น 400 นี้แก้เพิ่มไปใช่ไหม
ใช่ครับผมลองทำดูก่อน เเต่ติด 400
เราเพื่ออะไรเข้าไป
จริงๆมันเป็นโค้ดคล้ายๆกันครับ ผมเพิ่มไปบางส่วน เอามาจากอีก example นึง
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"sid\":\"1\",\"api_key\":\"xxxxxx\",\"light\":\"1000\",\"hum\":\"120\",\"time\":\"20220615113712\",\"type\":\"alert\"}");
หรือว่ามี example ตัวอื่นเพิ่มเติมนอกเหนือจากนี้อีกไหมครับ
อู้วว ทำได้เเล้วครับ เช็คใน postman เเล้ว status 200 เเล้วครับ
1 Likes
แก้ไขอะไรเพิ่มเติมไปหรอครับถึงได้อธิบายหน่อย
ตรงที่ใส่ host จริงๆไม่ต้องใส่ https ใส่เป็นแค่ http ครับ เเล้วก็ไปผิดที่ error 401 อีก ก็ใส่ key ที่ fig ไว้ที่ url
เยี่ยมมากครับ ขอบคุณที่มาอัพเดท