本站改版新增arduino频道
arduino DHT 使用WiFi模块上传气温和湿度数据到云平台
#include <DHT.h> #include <WiFi.h> #include <HTTPClient.h> #define DHT_PIN 2 #define WIFI_SSID "your_wifi_ssid" #define WIFI_PASSWORD "your_wifi_password" #define API_ENDPOINT "http://your_api_endpoint" DHT dht(DHT_PIN, DHT11); WiFiClient wifiClient; void setup() { WiFi.begin(WIFI_SSID, WIFI_PASSWORD); dht.begin(); } void loop() { delay(2000); float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); if (WiFi.status() == WL_CONNECTED) { HTTPClient http; String payload = "temperature=" + String(temperature) + "&humidity=" + String(humidity); http.begin(wifiClient, API_ENDPOINT); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpResponseCode = http.POST(payload); if (httpResponseCode > 0) { Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); } else { Serial.println("HTTP Request failed."); } http.end(); } else { Serial.println("WiFi not connected."); } }
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0095131397247314 seconds