|
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <DHT.h>
- const char ssid[] = "";
- const char password[] = "";
- String url = "";
- String temp;
- #define DHTPIN 2
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- void setup()
- {
- Serial.begin(115200);
- initWifi();
- dht.begin();
- }
- void loop()
- {
- delay(10000);
- if (WiFi.status() == WL_CONNECTED)
- {
- temp = initDht11();
- sendGet(temp);
- }
- else
- {
- initWifi();
- }
-
- }
- void initWifi()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println(WiFi.localIP());
- }
- String initDht11()
- {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- float f = dht.readTemperature(true);
- if (isnan(h) || isnan(t) || isnan(f))
- {
- Serial.println(F("Failed to read from DHT sensor!"));
- return "";
- }
- float hif = dht.computeHeatIndex(f, h);
- float hic = dht.computeHeatIndex(t, h, false);
- Serial.print(F("Humidity: "));Serial.print(h);
- Serial.print(F("% Temperature: "));Serial.print(t);
- Serial.print(F("°C "));Serial.print(f);
- Serial.print(F("°F Heat index: "));Serial.print(hic);
- Serial.print(F("°C "));Serial.print(hif);
- Serial.println(F("°F"));
- return String(h) + "," + String(t) + "," + String(f) + "," + String(hic) + "," + String(hif);
- }
- void sendGet(String str)
- {
- HTTPClient http;
- WiFiClient client;
- if (http.begin(client, url + "&" + str))
- {
- int httpCode = http.GET();
- if (httpCode > 0)
- {
- if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
- {
- String payload = http.getString();
- Serial.println(payload);
- }
- }
- http.end();
- }
- }
复制代码 1.emp8266使用dht11
功能是连接wifi以后,获取dht11的记录,然后把记录通过get请求传送到我个人的微信上。
谢谢各位大神指点
|
|