各位大神,请帮忙看看这段程序是否有问题esp8266-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 330|回复: 0

各位大神,请帮忙看看这段程序是否有问题esp8266

[复制链接]
发表于 2022-6-14 10:08 | 显示全部楼层 |阅读模式
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <DHT.h>

  4. const char ssid[]     = "";
  5. const char password[] = "";
  6. String url = "";
  7. String temp;
  8. #define DHTPIN 2
  9. #define DHTTYPE DHT11
  10. DHT dht(DHTPIN, DHTTYPE);
  11. void setup()
  12. {
  13.   Serial.begin(115200);
  14.   initWifi();
  15.   dht.begin();
  16. }

  17. void loop()
  18. {
  19.   delay(10000);
  20.   if (WiFi.status() == WL_CONNECTED)
  21.   {
  22.     temp = initDht11();
  23.     sendGet(temp);
  24.   }
  25.   else
  26.   {
  27.     initWifi();
  28.   }
  29.   
  30. }
  31. void initWifi()
  32. {
  33.   WiFi.mode(WIFI_STA);
  34.   WiFi.begin(ssid, password);

  35.   while (WiFi.status() != WL_CONNECTED)
  36.   {
  37.     delay(500);
  38.     Serial.print(".");
  39.   }
  40.   Serial.println(WiFi.localIP());
  41. }

  42. String initDht11()
  43. {
  44.   float h = dht.readHumidity();
  45.   float t = dht.readTemperature();
  46.   float f = dht.readTemperature(true);
  47.   if (isnan(h) || isnan(t) || isnan(f))
  48.   {
  49.     Serial.println(F("Failed to read from DHT sensor!"));
  50.     return "";
  51.   }
  52.   float hif = dht.computeHeatIndex(f, h);
  53.   float hic = dht.computeHeatIndex(t, h, false);

  54.   Serial.print(F("Humidity: "));Serial.print(h);
  55.   Serial.print(F("%  Temperature: "));Serial.print(t);
  56.   Serial.print(F("°C "));Serial.print(f);
  57.   Serial.print(F("°F  Heat index: "));Serial.print(hic);
  58.   Serial.print(F("°C "));Serial.print(hif);
  59.   Serial.println(F("°F"));
  60.   return String(h) + "," + String(t) + "," + String(f) + "," + String(hic) + "," + String(hif);
  61. }

  62. void sendGet(String str)
  63. {
  64.   HTTPClient http;
  65.   WiFiClient client;
  66.   if (http.begin(client, url + "&" + str))
  67.   {
  68.     int httpCode = http.GET();
  69.     if (httpCode > 0)
  70.     {
  71.       if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
  72.       {
  73.         String payload = http.getString();
  74.         Serial.println(payload);
  75.       }
  76.     }
  77.     http.end();
  78.   }
  79. }
复制代码
1.emp8266使用dht11
功能是连接wifi以后,获取dht11的记录,然后把记录通过get请求传送到我个人的微信上。
谢谢各位大神指点

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 09:34 , Processed in 0.093100 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表