Error: 't' was not declared in this scope ไม่ทราบว่าเกิดจากสาเหตุใดครับ

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
 int pinPump1 = D3;
 int pinPump2 = D3;
 int pinPump3 = D4;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "31XmVGrutN6PTd-fcSTcQLEPLZnSJNMR";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Ammara_2.4GHz";
char pass[] = "0866922890";

#define DHTPIN 5          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

int setTemp;
int setHumid;

BLYNK_WRITE(V4) //temperature slider in app on pin V22
{
setTemp = param.asFloat();
}
BLYNK_WRITE(V3) //temperature slider in app on pin V23
{
setHumid = param.asFloat();
}


// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V1, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  Blynk.begin(auth, ssid, pass, "oasiskit.com", 8080);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  pinMode(pinPump1, OUTPUT);
  pinMode(pinPump2, OUTPUT);
  pinMode(pinPump3, OUTPUT);
}

void loop()
{
    if (t >= setTemp)
  {
    digitalWrite(pinPump1, HIGH);
    digitalWrite(pinPump2, HIGH);
    digitalWrite(pinPump3, HIGH);
  }
  else
  {
    digitalWrite(pinPump1, LOW);
    digitalWrite(pinPump2, LOW);
    digitalWrite(pinPump3, LOW);
  }
  if (h <= setHumid)
  {
    digitalWrite(pinPump1, HIGH);
    digitalWrite(pinPump2, HIGH);
    digitalWrite(pinPump3, HIGH);
  }
  else
  {
    digitalWrite(pinPump1, LOW);
    digitalWrite(pinPump2, LOW);
    digitalWrite(pinPump3, LOW);
  }
  Blynk.run();
  timer.run();
}

โคดerror

Arduino:1.8.13 (Windows 10), บอร์ด:"NodeMCU 1.0 (ESP-12E Module), 160 MHz, Flash, Disabled, 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 921600"

C:\Users\huawe\Documents\Arduino\sketch_sep29a\sketch_sep29a.ino: In function 'void loop()':

sketch_sep29a:82:9: error: 't' was not declared in this scope

     if (t >= setTemp)

         ^

sketch_sep29a:94:7: error: 'h' was not declared in this scope

   if (h <= setHumid)

       ^

Multiple libraries were found for "Adafruit_Sensor.h"

 Used: C:\Users\huawe\Documents\Arduino\libraries\Adafruit_Sensor-master

 Not used: C:\Users\huawe\Documents\Arduino\libraries\Adafruit_Unified_Sensor

exit status 1

't' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

ลืมประกาศตัวแปร t กับ h นะครับ

ตอนนี้ผมแก้เป็นแบบนี้ละครับ คอมพลายผ่าน
โดยใช้ humidity และ temperature
และประกาศตัวแปร
float temperature = 0;
float humidity = 0;

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
 int pinPump1 = D3;
 int pinPump2 = D3;
 int pinPump3 = D4;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "31XmVGrutN6PTd-fcSTcQLEPLZnSJNMR";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Ammara_2.4GHz";
char pass[] = "0866922890";

#define DHTPIN 5          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

float temperature = 0;
float humidity = 0;

int setTemp;
int setHumid;

BLYNK_WRITE(V4) //temperature slider in app on pin V22
{
setTemp = param.asFloat();
}
BLYNK_WRITE(V3) //temperature slider in app on pin V23
{
setHumid = param.asFloat();
}


// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, h);
  Blynk.virtualWrite(V1, t);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  Blynk.begin(auth, ssid, pass, "oasiskit.com", 8080);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
  pinMode(pinPump1, OUTPUT);
  pinMode(pinPump2, OUTPUT);
  pinMode(pinPump3, OUTPUT);
}

void loop()
{
    if (temperature >= setTemp)
  {
    digitalWrite(pinPump1, HIGH);
    digitalWrite(pinPump2, HIGH);
    digitalWrite(pinPump3, HIGH);
  }
  else
  {
    digitalWrite(pinPump1, LOW);
    digitalWrite(pinPump2, LOW);
    digitalWrite(pinPump3, LOW);
  }
  if (humidity <= setHumid)
  {
    digitalWrite(pinPump1, HIGH);
    digitalWrite(pinPump2, HIGH);
    digitalWrite(pinPump3, HIGH);
  }
  else
  {
    digitalWrite(pinPump1, LOW);
    digitalWrite(pinPump2, LOW);
    digitalWrite(pinPump3, LOW);
  }
  Blynk.run();
  timer.run();
}
1 Like

ยอดเยี่ยมมากครับ ฝึกทักษะพวกนี้ไว้ ต่อไปถ้าเจออีกก็จะแก้ได้ทันที