本站改版新增arduino频道

Micropython
Arduino

arduino DHT 使用WiFi模块上传气温和湿度数据到云平台


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