0
votes

Im using nodeMCU v1.0 ESP-12E module. Im trying to switch 4 SS relays. I have connected the relays to 5v supply separately. The nodeMCU is connected to my laptop USB.

What am I using: I installed the 2.4.2 ESP8266 Arduino Board from the IDE and selected nodeMCU 1.0 board.

Whats Working:

  1. I have Acrobotics OLED on 14,12. They are working perfectly and Im getting all display commands on it.
  2. The onboard RED LED is properly switching on and off.
  3. On start, both the RED LED and the relay switch on right after pinmode is executed.

Whats not working:

  1. I'm not able to change the state of the GPIO pins.
  2. When I connect to D7, D8 ( which were identified as usable on many sites), I was unable to connect through USB to upload the code.
  3. So I disconnect and then connect after loading. Still no joy

Here is the code:

#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);
}

1
There might not be enough current to drive the relays, Connect two LEDs on the pins D7 and D8 and check if they are getting turned on or off.B45i
YES! You are right. the darn relays were looking for more! I had to get an MCP23017 and drive it with 5V and it now works.Balaji V

1 Answers

2
votes

Seems like the nodeMCU didn't have enough current to drive the relay.

Power the relay from an external power source. You can use darlington relay driver IC's like ULN2003.