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:
- I have Acrobotics OLED on 14,12. They are working perfectly and Im getting all display commands on it.
- The onboard RED LED is properly switching on and off.
- On start, both the RED LED and the relay switch on right after pinmode is executed.
Whats not working:
- I'm not able to change the state of the GPIO pins.
- 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.
- 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);
}
D7
andD8
and check if they are getting turned on or off. – B45i