NodeMCU - 无法更改 GPIO 输出引脚以关闭

NodeMCU - Unable to change GPIO Output pins to turn off

我正在使用 nodeMCU v1.0 ESP-12E 模块。我正在尝试切换 4 个 SS 继电器。我已将继电器分别连接到 5v 电源。 nodeMCU 连接到我的笔记本电脑 USB。

我在用什么: 我从 IDE 安装了 2.4.2 ESP8266 Arduino 开发板并选择了 nodeMCU 1.0 开发板。

工作原理:

  1. 我在 14,12 有 Acrobotics OLED。它们运行良好,我正在获取所有显示命令。
  2. 板载红色 LED 正确打开和关闭。
  3. 启动时,红色 LED 和继电器都会在执行引脚模式后立即打开。

什么不工作:

  1. 我无法更改 GPIO 引脚的状态。
  2. 当我连接到D7、D8(在许多网站上被标识为可用)时,我无法通过USB连接上传代码。
  3. 所以我断开连接然后加载后连接。还是不开心

代码如下:

#include <ESP8266WiFi.h>
#include <time.h>                       // time() ctime()
#include <sys/time.h>                   // struct timeval
#include <coredecls.h>                  // settimeofday_cb()
#include <Wire.h>
#include <ACROBOTIC_SSD1306.h>

////////////////////////////////////////////////////////

#ifndef STASSID
#define STASSID "somessid"
#define STAPSK  "somepwd"
#endif

#define SSID            STASSID
#define SSIDPWD         STAPSK
#define TZ              -5       // (utc+) TZ in hours
#define DST_MN          60      // use 60mn for summer time in some countries

#define NTP0_OR_LOCAL1  0       // 0:use NTP  1:fake external RTC
#define RTC_TEST     1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC

////////////////////////////////////////////////////////
#define LED 16 
#define STEP1 5
#define STEP2 4
#define STEP3 13
#define OVERRIDE 15
#define SCL 14
#define SDA 12
#define Red_LED 16

#define TZ_MN           ((TZ)*60)
#define TZ_SEC          ((TZ)*3600)
#define DST_SEC         ((DST_MN)*60)

#define TON 0
#define TOFF 1

timeval cbtime;      // time set in callback
bool cbtime_set = false;

bool motorON = false;
bool step1ON = false;
bool step2ON = false;
bool step3ON = false;
bool overRideON = false;

void time_is_set(void) {
  gettimeofday(&cbtime, NULL);
  cbtime_set = true;
  Serial.println("------------------ settimeofday() was called ------------------");
}


String IPAddress2String(IPAddress address)
{
 return String(address[0]) + "." + 
        String(address[1]) + "." + 
        String(address[2]) + "." + 
        String(address[3]);
}


void setup() {
  Wire.begin(SDA,SCL);
  oled.init();
  oled.clearDisplay();
  Serial.begin(115200);
  oled.setTextXY(0,0);              // Set cursor position, start of line 0
  oled.putString("Pool MCU");

  setup_wifi();
  settimeofday_cb(time_is_set);
  configTime(TZ_SEC, DST_SEC, "pool.ntp.org");

  pinMode(STEP1, OUTPUT);
  pinMode(STEP2, OUTPUT);
  pinMode(STEP3, OUTPUT);
  pinMode(OVERRIDE, OUTPUT);
  pinMode(Red_LED, OUTPUT);

  delay(500);
  digitalWrite(STEP1, HIGH);
  digitalWrite(STEP2, HIGH);
  digitalWrite(STEP3, HIGH);
  digitalWrite(OVERRIDE, HIGH);
  digitalWrite(Red_LED, HIGH);

}

// for testing purpose:
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);

#define PTM(w) \
  Serial.print(":" #w "="); \
  Serial.print(tm->tm_##w);

void printTm(const char* what, const tm* tm) {
  Serial.print(what);
  PTM(isdst); PTM(yday); PTM(wday);
  PTM(year);  PTM(mon);  PTM(mday);
  PTM(hour);  PTM(min);  PTM(sec);
}

void setup_wifi()
{

  delay(3);
  Serial.print("Connecting to ");
  Serial.println(SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, SSIDPWD);

  Serial.print("Connecting");
  while (IPAddress2String(WiFi.localIP()) == "0.0.0.0") {
    delay(1000);
    Serial.print(".");
  }
  oled.setTextXY(1,0);  
  oled.putString("IP: ");
  oled.setTextXY(1,3);  
  oled.putString(IPAddress2String(WiFi.localIP()));
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(IPAddress2String(WiFi.localIP()));
}

timeval tv;
timespec tp;
time_t now;
uint32_t now_ms, now_us;
String curTime;

void loop() {
  digitalWrite(Red_LED, LOW);
  gettimeofday(&tv, nullptr);
  clock_gettime(0, &tp);
  now = time(nullptr);
  now_ms = millis();
  now_us = micros();

  if (cbtime_set)
  {
    //The time is now set!
    time_t now;
    struct tm * timeinfo;
    time(&now);
    timeinfo = localtime(&now);
    Serial.print("My Time:");
    Serial.println(timeinfo->tm_hour);

    Serial.println("Setting to LOW");
    digitalWrite(STEP1, LOW);
    digitalWrite(STEP2, LOW);
    digitalWrite(STEP3, LOW);
    digitalWrite(OVERRIDE, LOW);
    delay(3000);
    Serial.println("Setting to HIGH");
    digitalWrite(STEP1, HIGH);
    digitalWrite(STEP2, HIGH);
    digitalWrite(STEP3, HIGH);
    digitalWrite(OVERRIDE, HIGH);
    delay(3000);
    oled.setTextXY(4,0);  
    oled.putString("Time:");
    oled.putNumber(timeinfo->tm_sec);
    //curTime = ctime(&now);
  }
  digitalWrite(Red_LED, HIGH);
  delay(5000);
}

nodeMCU 似乎没有足够的电流来驱动继电器。

从外部电源为继电器供电。您可以使用像 ULN2003 这样的达林顿继电器驱动器 IC。