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

问题描述 投票:0回答:1

我写了一个使用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”

此报告将提供有关“在编译期间显示详细输出”文件->首选项中启用的选项。

arduino esp8266 arduino-ide arduino-esp8266 esp8266wifi
1个回答
1
投票

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

#define DHTPIN 3
const int motorPin = 0;
© www.soinside.com 2019 - 2024. All rights reserved.