使用dht11传感器的智能灌溉系统中的代码错误

时间:2019-10-03 12:26:56

标签: arduino esp8266 arduino-ide arduino-esp8266 esp8266wifi

我写了一个代码,用于使用esp8266,dht11,湿度传感器自动给植物浇水,但是我的代码有一些错误,我不知道如何解决

#include <DHT.h>
#include <ESP8266WiFi.h>
String apiKey = "X5AQ3EGIKMBYW31H";     //  Enter your Write API key here
const char* server = "api.thingspeak.com";
const char *ssid =  "CircuitLoop";     // Enter your WiFi Name
const char *pass =  "circuitdigest101"; // Enter your WiFi Password
#define DHTPIN D3          // GPIO Pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;

const int moisturePin = A0;             // moisteure sensor pin
const int motorPin = D0;
unsigned long interval = 10000;
unsigned long previousMillis = 0;
unsigned long interval1 = 1000;
unsigned long previousMillis1 = 0;
float moisturePercentage;              //moisture reading
float h;                  // humidity reading
float t;                  //temperature reading

void setup()
{
  Serial.begin(115200);
  delay(10);
  pinMode(motorPin, OUTPUT);
  digitalWrite(motorPin, LOW); // keep motor off initally
  dht.begin();
  Serial.println("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");              // print ... till not connected
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop()
{
  unsigned long currentMillis = millis(); // grab current time

  h = dht.readHumidity();     // read humiduty
  t = dht.readTemperature();     // read temperature

  if (isnan(h) || isnan(t))
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  moisturePercentage = ( 100.00 - ( (analogRead(moisturePin) / 1023.00) * 100.00 ) );

  if ((unsigned long)(currentMillis - previousMillis1) >= interval1) {
    Serial.print("Soil Moisture is  = ");
    Serial.print(moisturePercentage);
    Serial.println("%");
    previousMillis1 = millis();
  }

if (moisturePercentage < 50) {
  digitalWrite(motorPin, HIGH);         // tun on motor
}
if (moisturePercentage > 50 && moisturePercentage < 55) {
  digitalWrite(motorPin, HIGH);        //turn on motor pump
}
if (moisturePercentage > 56) {
  digitalWrite(motorPin, LOW);          // turn off mottor
}

if ((unsigned long)(currentMillis - previousMillis) >= interval) {

  sendThingspeak();           //send data to thing speak
  previousMillis = millis();
  client.stop();
}

}

void sendThingspeak() {
  if (client.connect(server, 80))
  {
    String postStr = apiKey;              // add api key in the postStr string
    postStr += "&field1=";
    postStr += String(moisturePercentage);    // add mositure readin
    postStr += "&field2=";
    postStr += String(t);                 // add tempr readin
    postStr += "&field3=";
    postStr += String(h);                  // add humidity readin
    postStr += "\r\n\r\n";

    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());           //send lenght of the string
    client.print("\n\n");
    client.print(postStr);                      // send complete string
    Serial.print("Moisture Percentage: ");
    Serial.print(moisturePercentage);
    Serial.print("%. Temperature: ");
    Serial.print(t);
    Serial.print(" C, Humidity: ");
    Serial.print(h);
    Serial.println("%. Sent to Thingspeak.");
  }
}

这是我得到的错误

Arduino:1.8.9(Windows 8.1),开发板:“通用ESP8266模块,80 MHz,闪存,已禁用,所有SSL密码(最兼容),ck,26 MHz,40MHz,DOUT(兼容),512K(否) SPIFFS),2,nonos-sdk 2.2.1(旧版),v2较低内存,已禁用,无,仅素描,115200“

sketch_oct03a:7:16:错误:未在此范围内声明“ D3”

#define DHTPIN D3 //连接dht11的GPIO引脚

            ^

C:\ Users \ Shweta \ Desktop \ Libraries \ sketch_oct03a \ sketch_oct03a.ino:8:9:注意:在宏“ DHTPIN”的扩展中

DHT dht(DHTPIN,DHT11);

     ^

sketch_oct03a:12:22:错误:未在此范围中声明'D0'

const int motorPin = D0;

                  ^

退出状态1 未在此范围内声明“ D3”

此报告将包含更多信息 “在编译期间显示详细输出” 文件->首选项中启用了此选项。

1 个答案:

答案 0 :(得分:1)

与模拟引脚不同,数字引脚没有前缀D

#define DHTPIN 3
const int motorPin = 0;