简体   繁体   English

arduino 传感器在 wifi 上的故障

[英]arduino sensor's misfunction over wifi

I have an esp32 firmware that uses 4 sensors (DHT11, PIR, water leakage, MQ2).我有一个使用 4 个传感器(DHT11、PIR、漏水、MQ2)的 esp32 固件。 All sensors work fine and give accurate output but when I try to include Wi-Fi connection and Wi-Fi client the water and mq2 sensors give wrong and random data.所有传感器工作正常并提供准确的 output 但是当我尝试包含 Wi-Fi 连接和 Wi-Fi 客户端时,水和 mq2 传感器提供错误和随机数据。 Is there anybody who experienced this issue before?有没有人以前遇到过这个问题?

Here is an example of the output without Wi-Fi:以下是没有 Wi-Fi 的 output 的示例:

  • temperature: 18温度:18
  • humidity: 58湿度:58
  • water level: 97水位:97
  • motion: 1运动:1
  • smoke: 6烟雾:6
  • LPG: 12液化石油气:12
  • CO: 8一氧化碳:8

and with the Wi-Fi commands it shows like this:并使用 Wi-Fi 命令显示如下:

  • temperature: 18温度:18
  • humidity: 58湿度:58
  • water level: 4065水位:4065
  • motion: 1运动:1
  • smoke: 26145582395.16烟雾:26145582395.16
  • LPG: 26145582395.16液化石油气:26145582395.16
  • CO: 26145582395.16公司:26145582395.16

and here is the code used:这是使用的代码:

#include <DHTesp.h>
#include <WiFi.h>
#include <MQ2.h>

#ifndef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP32 ONLY!)
#error Select ESP32 board.
#endif

const char* ssid = "Mynetwork";
const char* password = "********";

const uint16_t port = 8090;
const char * host = "***********";

const int dhtPin = 18;
const int waterPin = 26;
const int mq2Pin   = 2;
const int pirPin = 25;

int factor=0;

DHTesp dht;
MQ2 mq2(mq2Pin);

void setup() {


  pinMode(pirPin,INPUT);
  pinMode(waterPin,INPUT);
  pinMode(mq2Pin,INPUT);
  pinMode(pirPin,INPUT);


  Serial.begin(115200);
  mq2.begin();
  dht.setup(dhtPin, DHTesp::DHT11);
  factor = analogRead(waterPin);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  delay(2000);
  Serial.println("...");

  }


}

void loop() {

  WiFiClient client;

  if (!client.connect(host, port)) {

        Serial.println("Connection to host failed");

        delay(500);
        return;
    }

    Serial.print("WiFi connected with IP: ");
    Serial.println(WiFi.localIP());


    float *values= mq2.read(true);


    Serial.print("Temperature:\t");
  Serial.println(getTemp());
    client.println(getTemp());

    Serial.print("Humidity:\t");
  Serial.println(getHum());
    client.println(getHum());

    Serial.print("movement:\t");
    Serial.println(getPir());
    client.println(getPir());

    Serial.print("Water state:\t");
  Serial.println(getWater());
    client.println(getWater());

    Serial.print("LPG:\t");
  Serial.println(getLPG());
    client.println(getLPG());

    Serial.print("Smoke:\t");
  Serial.println(getSmoke());
    client.println(getSmoke());

   Serial.print("CO:\t");
  Serial.println(getCO());
   client.println(getCO());

  delay(3000);


}
float getTemp(){
  return dht.getTemperature();
}
float getHum(){
  return dht.getHumidity();
}
int getWater(){
   int sensorValue = analogRead(waterPin)-factor;  
   int per= (sensorValue*100/4094);
   return map(per,0,33,0,100);
}


float getSmoke(){
  return mq2.smoke;
}
float getLPG(){     
  return mq2.lpg;
}
float getCO(){
  return mq2.co;
}
int getPir(){
  return (digitalRead(pirPin));
}

ESP32 internally has two ADCs, but ADC2 is shared between other resources within ESP32. ESP32 内部有两个 ADC,但 ADC2 在 ESP32 内的其他资源之间共享。 They are mapped as:它们映射为:

ADC1_CH0 -- GPIO36
ADC1_CH1 -- Not available on some ESP32 development board
ADC1_CH2 -- NA
ADC1_CH3 -- GPIO39
ADC1_CH6 -- GPIO34
ADC1_CH7 -- GPIO35
ADC1_CH4 -- GPIO32
ADC1_CH5 -- GPIO33

ADC2_CH0 --- GPIO0
ADC2_CH1 --- Not available on some boards
ADC2_CH2 --- GPIO2
ADC2_CH3 --- GPIO15
ADC2_CH4 --- GPIO13
ADC2_CH5 --- GPIO12
ADC2_CH6 --- GPIO14
ADC2_CH7 --- GPIO27
ADC2_CH8 --- GPIO25
ADC2_CH9 --- GPIO26

ADC2 pins can not be used when WiFi is used.使用 WiFi 时不能使用 ADC2 引脚。 On the other hand, ADC1 pins can be used even when WiFi is enabled.另一方面,即使启用了 WiFi,也可以使用 ADC1 引脚。 Source of reference .参考来源

Therefore, instead of using pin2 for your MQ sensor, try to use pin 36 or 34.因此,不要为您的 MQ 传感器使用 pin2,而是尝试使用 pin 36 或 34。

Also ESP32 is well-known for its noise, try to add a 10uF and 0.1uF capacitors between 3v3 and GND.另外 ESP32 以噪音着称,尝试在 3v3 和 GND 之间加一个 10uF 和 0.1uF 的电容。

One more thing you need to aware if you are planning to use ESP32 for your analog measurement in your project that ESP32 ADCs are not linear, I have a blog talk about the non-linearity of ESP32 ADC and how I use uses a Look-up table to correct the non-linearity .如果您打算在项目中使用 ESP32 进行模拟测量,您还需要注意一件事,即 ESP32 ADC 不是线性的,我有一篇关于ESP32 ADC 的非线性以及我如何使用查找的博客讨论表来校正非线性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM