AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 会员列表

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
     
Title: arduino通过post查询XXX.PHP文件,输出0或1,并根据0关闭,1打开设备  
 
sky999
天山茗客



UID 181291
Digest 2
Points 10
Posts 3937
码币MB 2542 Code
黄金 0 Catty
钻石 884 Pellet
Permissions 10
Register 2020-11-28
Status offline
arduino通过post查询XXX.PHP文件,输出0或1,并根据0关闭,1打开设备



#include <SPI.h> #include <WiFiNINA.h> const char* ssid = "your_ssid"; // 你的WiFi网络名称 const char* password = "your_password"; // 你的WiFi网络密码 int ledPin = 13; // 设备LED引脚 int status = 0; // 设备状态,0表示关闭,1表示打开 void setup() { // 初始化WiFi库 WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } // 输出连接成功信息 Serial.println("WiFi connected."); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // 构造POST请求数据 String postData = "status=" + String(status); // 发送POST请求 HTTPClient http; http.begin("http://example.com/XXX.php"); // 替换为你的PHP文件URL http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 设置请求头 int httpCode = http.POST(postData); // 发送POST请求 http.end(); // 检查HTTP响应状态码 if (httpCode == 200) { // 解析PHP文件响应 String payload = http.getString(); if (payload == "0") { // 关闭设备 digitalWrite(ledPin, LOW); status = 0; } else if (payload == "1") { // 打开设备 digitalWrite(ledPin, HIGH); status = 1; } else { // 无效的响应 Serial.println("Invalid response from PHP file."); } } else { // HTTP请求失败 Serial.println("HTTP request failed."); } // 等待一段时间后再次发送请求 delay(10000); // 10秒后再次发送请求,你可以根据需要调整这个时间间隔 }




CAFFZ.com
2023-11-27 15:06#1
View profile  Blog  Send a short message  Top
     


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2026-1-24 05:30 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE