I am in the process of migrating my hydroponics controller from an Arduino Mega to several ESP8266 boards. With research I was able to piece together a sketch that uses the ESP and a DHT22 Temp/humidity sensor, so I am now trying to utilize the same framework of that sketch, and just switch out the functions. In this sketch, I merely wish to control an 8 channel relay module that was used for more than a year on the Mega with Blynk but for some reason, it seems this new sketch is getting hung up somewhere in the setup() function. Usually, after a fresh upload, all the Blynk widget values are returned to zero as instructed in the code, but this is not happening now.
Could someone kindly read my sketch and offer pointers for what’s needed and what’s not needed? Thank you in advance.
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "auth"; // Cloud Server
//char auth[] = "auth"; // Cloud Server
//char auth[] = "auth"; // Local Server
char ssid[] = "netName";
char pass[] = "password";
byte arduino_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress device_ip ( 192, 168, 0, 80);
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
BlynkTimer timer;
byte blynkInterval = 3000;
uint32_t msPerGallon = 33000; //ms per gallon
uint32_t ROstart = 0;
uint32_t countGallons;
boolean runningRO = false;
int ROpumpOn = 0; //Blynk Triggered RO Pump
float totalGallons; //Number of RO Gallons Selected in Widget
BLYNK_WRITE(V1) {
ROpumpOn = param.asInt(); // ROpump remote
}
BLYNK_WRITE(V2) {
totalGallons = param.asFloat(); // ROpump remote
}
// Digital Pins - 8 Channel Relay Assignments - ESP8266 Pins - 120VAC~ switching
#define lightA 0
#define lightB 2
#define pumpA 4
#define pumpB 5
#define ROpump 12
#define scrubberFan 13
#define VentA 14
#define VentB 15
#define TURN_ON 0 // TURN_ON and TURN_OFF are defined to account for Active LOW relays
#define TURN_OFF 1 // Used to switch relay states for on/off of 120VAC~ devices
//WidgetLCD lcdA(V7); //Set LCD widget to Advanced Mode - Widget to display project clock
WidgetTerminal terminal(V8);
//---------------------------------------------------------------//
//----------------------------Functions--------------------------//
void checkBlynk() {
unsigned long startConnecting = millis();
unsigned int Timeout = 5000;
while (!Blynk.connected())
{
Blynk.connect();
if (millis() > startConnecting + Timeout)
{
break;
}
}
}
void reconnectBlynk()
{
if (!Blynk.connected())
{
if (Blynk.connect())
{
Serial.print("Blynk Reconnected");
}
else
{
Serial.print("Blynk Not reconnected");
}
}
}
void ROcheck() //RO Pump = 34 seconds on time per gallon
{
if (ROpumpOn == 1 && runningRO == false) // Activates when Blynk button is toggled
{
digitalWrite(ROpump, TURN_ON);
Blynk.virtualWrite(V1, 1); // Illuminates Blynk button widget
runningRO = true;
ROstart = millis();
countGallons = msPerGallon * totalGallons; // Calculates length of runtime for pump
Blynk.virtualWrite(V2, 0);
terminal.print("Pumping:");
terminal.print(totalGallons);
terminal.println(" Gallons of RO");
}
if (millis() - ROstart > countGallons) // Determines when runtime ends
{
ROpumpOn = 0;
runningRO = false;
digitalWrite(ROpump, TURN_OFF);
Blynk.virtualWrite(V1, 0);
//Blynk.virtualWrite(V22, 0);
}
terminal.flush();
}
//------------------------Functions------------------------------//
//---------------------------------------------------------------//
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); // Cloud Server
//Blynk.begin(auth, ssid, pass, server_ip, 8442); // Local Server
WiFi.begin(ssid, pass);
Blynk.config(auth);
//Blynk.connect(); // this is OK
//WiFi.config(device_ip, gateway_ip, subnet_mask);
for (int allRelays = lightA; allRelays <= VentB; allRelays++)
{
pinMode(allRelays, OUTPUT);
digitalWrite(allRelays, TURN_OFF);
}
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V8, 0);
timer.setInterval(1007L, ROcheck);
//timer.setInterval(blynkInterval, checkBlynk);
//timer.setInterval(60000, reconnectBlynk);
while (Blynk.connect() == false) {}
delay(1000);
Serial.println("setup complete.");
}
void loop()
{
// only attempt Blynk-related functions when connected to Blynk
if (Blynk.connected())
{
Blynk.run();
}
timer.run();
}
As a reference point, here is the functioning DHT22 sketch;
//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "auth"; // Cloud Server
//char auth[] = "auth"; // Local Server
char ssid[] = "netName";
char pass[] = "password";
byte arduino_mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress device_ip ( 192, 168, 0, 80);
//IPAddress server_ip ( 192, 168, 0, 1 ); //IP of machine server runs on
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
#include <DHT.h>
#define DHTPIN 2 //pin gpio 12 in sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
byte blynkInterval = 3000;
void readSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(true);
Blynk.virtualWrite(V0, t); // virtual pin
Blynk.virtualWrite(V1, h); // virtual pin
}
void checkBlynk() {
unsigned long startConnecting = millis();
unsigned int Timeout = 5000;
while (!Blynk.connected())
{
Blynk.connect();
if (millis() > startConnecting + Timeout)
{
break;
}
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442); // Cloud Server
//Blynk.begin(auth, ssid, pass, server_ip, 8442); // Local Server
// Setup WiFi network
//WiFi.config(device_ip, gateway_ip, subnet_mask);
WiFi.begin(ssid, pass);
// Setup Blynk
Blynk.config(auth);
dht.begin();
timer.setInterval(3000, readSensor); //Read every 3 secs
timer.setInterval(blynkInterval, checkBlynk); // check connection to server per blynkInterval
}
void loop()
{
// only attempt Blynk-related functions when connected to Blynk
if (Blynk.connected())
{
Blynk.run();
}
timer.run();
}
Edit - Note, because all of these sketches have the same credentials, none of them are powered on at the same time. I will give each unique credentials upon successful connection and operation.