首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当WiFi工作时,你能使用所有use 32的GPIO引脚吗?

当WiFi工作时,你能使用所有use 32的GPIO引脚吗?
EN

Stack Overflow用户
提问于 2020-12-03 11:41:34
回答 1查看 3.1K关注 0票数 4

我有一个奇怪的现象,当我添加WiFi库和所有设置时,每3个传感器中就有2个停止工作。当我删除WiFi代码时,它的工作方式与以前一样。

我有一个ESP32 devkit v1板和连接的3个传感器,它们是光敏电阻(ky-018)、dht-11和电容性土壤水分传感器。

  • dht-11连接到D14 (works);
  • 连接到D13的光敏电阻(Ky-018)(不工作);
  • 电容土壤水分传感器连接到D15 (不工作)。

我试着换针脚,但没帮上忙。

以下是代码:

代码语言:javascript
复制
#include "DHT.h"
#include <WiFi.h>

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

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

DHT dht(DHTPIN, DHTTYPE);
const byte lightPin = 13;
int lightReading;
int lightReadingpercent=0;
const int RELAY_PIN = 15;    // the Arduino pin, which connects to the IN pin of relay
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 = 15;    // Soil moisture

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

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

  // 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
 
  pinMode(RELAY_PIN, OUTPUT);//relay
  
  Serial.println(F("DHTxx test!")); //dht
  ; 
  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:

  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();
  
  delay(500);

  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
  }
 
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = 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(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  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"));

  delay(500); //wait 0.5seconds
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-03 12:13:04

问题可能是,根据这个评论 on 问题 on GitHub:

ADC2引脚在使用WiFi时不能使用。另一方面,即使启用了ADC1,也可以使用WiFi引脚。

这可能是因为运行在具有ADC2外围设备的核心上的WiFi固件使用了ADC2。

这并不能解释为什么GPIO14适用于您,但是您仍然可以尝试只使用使用ADC1的GPIO引脚,看看它是否适合您。

另外,WiFi可以拉出相当多的电流,确保电源达到它的要求,这样你就不会有电压下降。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65125302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档