Arduino:
HTTP-pyynnön lähettäminen
How to: (Kuinka tehdä:)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "yourSSID";
const char* password = "yourPassword";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
HTTPClient http;
http.begin("http://yourserver.com/data"); // Your API endpoint
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
void loop() {
// Nothing to do here
}
Deep Dive (Sukellus syvemmälle)
HTTP-pyyntöjen lähettäminen on webin perustoimintoja, joka dates back to the early days of the internet. Alternatives include using WebSocket for continuous connections or MQTT for IoT applications. Implementation needs a network-capable microcontroller like the ESP8266 or ESP32 and relies on the underlying TCP/IP stack. Libraries like ESP8266HTTPClient simplify the process.