乐鑫技术交流
直播中

何夏庄

8年用户 1057经验值
私信 关注
[问答]

带5V继电器的NodeMcu,继电器状态始终为ON是为什么?如何解决?

试图实施 SMART 家庭自动化,卡在最后一步,我的继电器需要根据 GPIO 引脚状态进行切换。详细信息如下:
问题:我已经刷新了标准 ADAFRUIT MQTT 库。并且能够根据从 io.adafruit.com 发送的数据源成功切换 LED 灯泡。
当我用 5V 继电器更换 LED 灯泡时,问题就开始了。尽快连接到节点 MCU 的 VCC 和 GND 到继电器的 vcc 和 gnd。红灯亮了很好。但是将 Relay 的 IN 引脚连接到 mcu 的 D2 引脚。绿灯亮了。但它从未将数据馈送更改为关闭。将继电器的 IN 引脚连接到 mcu 的许多其他引脚可保持灯亮。
我对这种行为完全感到困惑,非常感谢任何帮助,
下面是 Flashed mqtt 代码:
代码:全选/***************************************************
  Adafruit MQTT Library ESP8266 Example

  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino

  Works great with Adafruit's Huzzah ESP board & Feather

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Tony DiCola for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
****************************************************/
#include
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "Sunilwifi"
#define WLAN_PASS       "14311431"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "sunildm4u"
#define AIO_KEY         "xxxx dummy api code"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: /feeds/
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/lightsdata");

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
   pinMode(D3, OUTPUT);
  Serial.begin(115200);
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

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

  // Setup MQTT subscription for onoff feed.
  mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(5000))) {
    if (subscription == &onoffbutton) {
      Serial.print(F("Got: "));
      Serial.println((char *)onoffbutton.lastread);
      if (strcmp((char *)onoffbutton.lastread, "ON") == 0) {
        digitalWrite(D3, LOW);
      }
      if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) {
        digitalWrite(D3, HIGH);
      }
      
    }
  }

  // Now we can publish stuff!
  Serial.print(F("\nSending photocell val "));
  Serial.print(x);
  Serial.print("...");
  if (! photocell.publish(x++)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }

  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }
  */
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}





回帖(1)

王艳

2024-4-10 16:32:16
可能有几个原因导致继电器状态始终为ON:

1. 继电器使用的电源电压可能不正确。确保您使用的继电器是5V的,并将其正确连接到NodeMcu的5V引脚和GND引脚。

2. 继电器的IN引脚可能是低电平触发(Active LOW)。这意味着输入低电平(0V)将触发继电器关闭,而输入高电平(5V)将触发继电器打开。尝试在代码中更改GPIO引脚的状态,例如将GPIO引脚设置为LOW,则继电器应关闭。

3. 您可能没有正确设置GPIO引脚的状态或未正确使用GPIO引脚。确保您正确初始化GPIO引脚,并将其设置为输出模式,并向它们写入正确的值来控制继电器。

如果您仍然无法解决问题,请重新检查您的电路连接和代码,并尝试重新烧写NodeMcu的固件。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分