从 bool 获取值时未在此范围内声明 Temp

Temp was not declared in this scope when getting values from bool

您好,我想将一些空值更改为布尔值,但我有点迷茫。我知道如果你写一个 void 或 bool 并想将值添加到下一个 void 你只需插入代码以添加前一个函数

我不知道怎么解释,我只想告诉你我想做什么:

创建了一个新的 bool getValues 并添加了所有从传感器获取代码的值,然后我想将数据发送到 void 循环,该循环将通过 mqqt 将数据发送到树莓派。

我知道 bool 代表 true 和 false。但是我不太明白使用它的礼节

所以我在 void 循环函数 'temp' was not declared in this scope 遇到的问题

我用 // 突出显示了这个函数,我得到的错误几乎在底部

#include "DHT.h"
#include <WiFi.h>

#define DHTPIN 25  // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11

//MQTT Setup Start
#include <PubSubClient.h>
#define mqtt_server "192.168.1.210"
WiFiClient espClient;
PubSubClient client(espClient);
#define mqttlightReadingpercent "greenHouse/light"
#define mqttsoilmoisturepercent "greenHouse/soil"
#define mqtttemp "greenHouse/temp"
#define mqtthum "greenHouse/hum"
//MQTT Setup End

const char* ssid = "Cgates_E031F1"; // ESP32 and ESP8266 uses 2.4GHZ wifi only
const char* password = "60E541C32F";

DHT dht(DHTPIN, DHTTYPE);
const byte lightPin = 33;
int lightReading;
int lightReadingpercent=0;
//const int RELAY_PIN = 15;  // the Arduino pin, which connects to the IN pin of relay

// soil moisture
const int AirValue = 4095;   //you need to replace this value with Value_1
const int WaterValue = 2200;  //you need to replace this value with Value_2
const int SensorPin = 32;
int soilMoistureValue = 0;
int soilmoisturepercent=0;

const int Lightvalue = 0;
const int Darkvalue = 4095;

unsigned long millisNow = 0; //for delay purposes
unsigned int sendDelay = 20000; //delay before sending sensor info via MQTT
 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println();

  // begin Wifi connect
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(2000);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //end Wifi connect

  client.setServer(mqtt_server, 1883);
 
  //  pinMode(RELAY_PIN, OUTPUT);//relay

  pinMode(lightPin, INPUT);
  pinMode(SensorPin, INPUT);
  
  Serial.println(F("DHTxx test!")); //dht
  ; 

  dht.begin();
}

void reconnect() {
  // Loop until we're reconnected
  int counter = 0;
  while (!client.connected()) {
    if (counter == 5) {
      ESP.restart();
    }
    counter+=1;
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
   
    if (client.connect("greenHouseController")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

bool getValues() {
  lightReading = analogRead(lightPin); //0-4095 12bit -- esp8266 10bit 0-1023 -- arduino 8bit 0-254
 
  Serial.print("Light reading = ");
  
  lightReadingpercent = map(lightReading, Darkvalue, Lightvalue,  0, 100 );
  Serial.print(lightReadingpercent);
  Serial.println(" %");
  Serial.println();

  soilMoistureValue = analogRead(SensorPin);  //put Sensor insert into soil
  soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);

  if (soilmoisturepercent > 100) {
    Serial.println("Soil moisture ");
    Serial.println("100 %");
    delay(500);
  }
  else if(soilmoisturepercent <0) {
    Serial.println("Soil moisture ");
    Serial.println("0 %");
    delay(500);
  }
  else if (soilmoisturepercent >=0 && soilmoisturepercent <= 100) {
    Serial.println("Soil moisture "); //go to next line
    Serial.print(soilmoisturepercent);
    Serial.println("%");
    delay(500); // soil end
  }

  delay(500);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float hum = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float temp = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(hum) || isnan(temp) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return 1;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, hum);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(temp, hum, false);

  Serial.print(F(" Humidity: "));
  Serial.print(hum);
  Serial.print(F("%  Temperature: "));
  Serial.print(temp);
  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"));

  delay(500); //wait 0.5seconds
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }

  if (millis() > millisNow + sendDelay) {
    if (getValues()) {
      client.publish(mqttlightReadingpercent, String(lightReadingpercent).c_str(),true);
      client.publish(mqttsoilmoisturepercent, String(soilmoisturepercent).c_str(),true);
      client.publish(mqtttemp, String(temp).c_str(),true); // the problem is here
      client.publish(mqtthum, String(hum).c_str(),true);
      millisNow = millis();
    } 
  }

  client.loop();
 
  /*if (moisture_level < 10) {
    digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds
    delay(5000);
  } 
  else {
    digitalWrite(RELAY_PIN, LOW);  // turn off pump 5 seconds
    delay(5000);
  }*/
}

通过将您的代码移动到 getValues,您还更改了 temp 变量所在的范围。变量不会自动全局可用。如果你在一个函数中声明一个变量(getValues 是),它只在这个函数中可用。

当您尝试在 loop 函数中访问 temp 变量时,编译器正确地告诉您,没有这样的变量可用。

您可以通过将 temp 声明为全局变量来解决问题,您可以通过在顶部添加 float temp = 0 来实现,您还可以在其中声明 soilMoistureValue 等变量。确保不要在 getValues 中重新声明变量,所以不要像 float temp = dht.readTemperature(); 那样声明,而是像 temp = dht.readTemperature();

那样分配一个新值

关于你的第一段的快速说明:voids 和 bools 你如何称呼它,定义函数的 return 类型。如果你的函数没有 return 任何东西,你就把它定义为 void。如果它 return 是一个布尔值(真或假),你定义为布尔值。对于 getValues 函数,因为它没有 return 任何东西,所以它应该是 void getValues.