Arduino Wifi w/Relay- Possible conflict

In the process of finalizing a long-drawn-out hydroponic project and everything is working as intended with the exception of Wi-Fi stability on the Arduino Uno Rev 2 Wi-Fi. It is set up to send data to Blynk which works perfectly when everything is stable. However, I am getting multiple disconnects throughout the data and I would shut the power down and start everything back up again. I pulled up the timeline within Blynk to see at what times it was going off-line and noticed it was within a minute of when my 12v pump would turn on. As a test, I disconnected the wire from the 12 pin (controls the submersible pump relay) and left the other relay for the lights connected. I let the system run in this condition and everything worked without any disconnections from the Wi-Fi. Based on this, I'm thinking that I either have something wired up wrong with the pump relay or when the pump kicks on I'm getting some disruptions in the circuit (current, magnetic, etc..). Looking at the electrical sketch below, you will notice some weird wiring on my part for the Pump. I wanted to add a flyback diode but wasn't sure on the best way to wire it up. Not sure if this is the source of my issue or not. I wanted to reach out to you guys for some feedback on what could be possibly causing my issue of when the pump kicks on, what causing my Arduino to drop the Wi-Fi connection. Any help would be appreciated.

I have included the sketch as well. I have tested out changing the Pin 12 to Pin 3 to see if that would have an effect on the behavior and it had the same result (Pump turned on at 7:05 and according to the timeline within Blynk, it went off-line at 7:06). It shows in my router as well as being offline. I have set up as a static IP and going to port 8080. As mentioned, it works fine when I pull the D12 pin from the Arduino. As you can see I am not an electrical or code writing expert so I know there are a lot of things that can be cleaned up and apologize in advance but as I go along and have a better understanding, hopefully it will improve.

The relays are HiLetgo 5V One Channel Relay Module with OPTO Isolation High Low Level Trigger

image

#define BLYNK_TEMPLATE_ID "*******"
#define BLYNK_DEVICE_NAME "*******"
#define BLYNK_AUTH_TOKEN "*******"
#define DHTTYPE DHT22                                                   // DHT 22 (AM2302), AM2321
#define DHTPIN 2
#define LED_PIN 6
#define LED_COUNT 17
#define ONE_WIRE_BUS 7
#define TdsSensorPin A0
#define BLYNK_PRINT Serial

#include <DHT.h>
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>                                                  // Required for 16 MHz Adafruit Trinket
#include <Time.h>
#include <TimeAlarms.h>
#include <Wire.h>;
#include <DS1307RTC.h>
#include <TimeLib.h>                                                     //added from the TimeRTCSet
#include <EEPROM.h>
#include "GravityTDS.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <hd44780.h>                                                    // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h>                              // i2c expander i/o class header

#ifdef __AVR__
#endif

char ssid[] = "*******";
char pass[] = "*******";
char auth[] = "*******";
int Light = 8;
int Pump = 12;
int rawLiquidReading;
int chk;
int status = WL_IDLE_STATUS; ;
float tdsValue = 0;
float hum;
float temp;
float liquidLevel;
const long lowEtapeValue = 0;
const long highEtapeValue = 850;
const byte liquidLevelPin = A1;
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
unsigned long time1;
unsigned long time2 = 0;
char dbg = 1;


Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
DHT dht(DHTPIN, DHTTYPE);
AlarmId id;
OneWire oneWire(ONE_WIRE_BUS);
GravityTDS gravityTds;
DallasTemperature sensors(&oneWire);
hd44780_I2Cexp lcd;                                                    // declare lcd object: auto locate & auto config expander chip
BlynkTimer timer;
BLYNK_WRITE(V5)                                                        // Executes when the value of virtual pin 5 changes
{
  if (param.asInt() == 1) {
    digitalWrite(Light, HIGH);                                        // Set Light HIGH
    Blynk.virtualWrite(V5, 1);                                        // Turn the widget attached to V5 On

  } else {
    // execute this code if the switch widget is now OFF
    digitalWrite(Light, LOW);                                         // Set Light LOW
    Blynk.virtualWrite(V5, 0);                                        // Turn the widget attached to V5 Off
  }
}
BLYNK_WRITE(V8)                                                       // executes when the value of virtual pin V8 changes
{
  if (param.asInt() == 1) {                                           // execute this code if the switch widget is now ON
    digitalWrite(Pump, HIGH);  // Set Pump HIGH
    Blynk.virtualWrite(V8, 1);  // Turn the widget attached to V8 On

  } else {                                                            // execute this code if the switch widget is now OFF
    digitalWrite(Pump, LOW);  // Set digital Pump LOW
    Blynk.virtualWrite(V8, 0);  // Turn the widget attached to V8 Off
  }
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V5);                                              // will cause Light BLYNK_WRITE(V5) to be executed
  Blynk.syncVirtual(V8);                                              // will cause Pump BLYNK_WRITE(V8) to be executed
}

void setup() {

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  if (dbg) Serial.begin(9600);
  pinMode(9, OUTPUT);   // set the LED pin mode
  digitalWrite(9, LOW); //LED OFF to show disconnected
  if (dbg) while (!Serial); // wait for serial port to connect. Needed for Leonardo only
  while (!ScanSSIDs()) WiFiConnect();


int waterValue = analogRead(liquidLevel);
status = lcd.begin(LCD_COLS, LCD_ROWS);
if (status) // non zero status means it was unsuccesful
{
  // hd44780 has a fatalError() routine that blinks an led if possible
  // begin() failed so blink error code using the onboard LED if possible
  hd44780::fatalError(status); // does not return
}
sensors.begin();
while (!Serial);
pinMode(Light, OUTPUT);
digitalWrite(Light, LOW);
pinMode(Pump, OUTPUT);
digitalWrite(Pump, LOW);
pinMode(liquidLevelPin, INPUT);

strip.begin();                                                                 // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show();                                                                  // Turn OFF all pixels ASAP
strip.setBrightness(50);                                                       // Set BRIGHTNESS to about 1/5 (max = 255)

// first run the example from the following path: file, examples, DS1307RTC, to set the time of the RTC, then upload this sketch
setSyncProvider(RTC.get);                                                      // the function to get the time from the RTC
if (timeStatus() != timeSet)
  Serial.println("Unable to sync with the RTC");
else
  Serial.println("RTC has set the system time");

// create the alarms, to trigger at specific times
Alarm.alarmRepeat(6, 0, 0, LightsOn);                                         // Lights on 6:00am every day
Alarm.alarmRepeat(6, 0, 0, PumpOn);                                           // Pump on 6:00am every day
Alarm.alarmRepeat(6, 5, 0, PumpOff);                                          // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(7, 5, 0, PumpOn);                                           // Pump on 7:05am every day
Alarm.alarmRepeat(7, 10, 0, PumpOff);                                         // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(8, 10, 0, PumpOn);                                          // Pump on 8:10am every day
Alarm.alarmRepeat(8, 15, 0, PumpOff);                                         // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(9, 15, 0, PumpOn);                                          // Pump on 9:15am every day
Alarm.alarmRepeat(9, 20, 0, PumpOff);                                         // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(10, 20, 0, PumpOn);                                         // Pump on 10:20am every day
Alarm.alarmRepeat(10, 25, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(11, 25, 0, PumpOn);                                         // Pump on 11:25am every day
Alarm.alarmRepeat(11, 30, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(12, 30, 0, PumpOn);                                         // Pump on 12:30pm every day
Alarm.alarmRepeat(12, 35, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(13, 35, 0, PumpOn);                                         // Pump on 1:35pm every day
Alarm.alarmRepeat(13, 40, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(14, 40, 0, PumpOn);                                         // Pump on 2:40pm every day
Alarm.alarmRepeat(14, 45, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(15, 45, 0, PumpOn);                                         // Pump on 3:45pm every day
Alarm.alarmRepeat(15, 50, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(16, 50, 0, PumpOn);                                         // Pump on 4:50pm every day
Alarm.alarmRepeat(16, 55, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(17, 55, 0, PumpOn);                                         // Pump on 5:55pm every day
Alarm.alarmRepeat(18, 00, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(19, 00, 0, LightsOff);                                      // Lights off at 7:00pm every day, remain off until 6:00am
Alarm.alarmRepeat(19, 00, 0, PumpOn);                                         // Pump on 7:00pm every day
Alarm.alarmRepeat(19, 05, 0, PumpOff);                                        // Pump runs for 5 min. and then off every day
Alarm.alarmRepeat(20, 05, 0, PumpOn);                                         // Pump on 8:05pm every day
Alarm.alarmRepeat(20, 15, 0, PumpOff);                                        // Pump runs for 5 min. and then off unitl 6:00am every day


gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(5.0);                                                      //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(1024);                                                 //1024 for 10bit ADC;4096 for 12bit ADC; 256 for 8 bit
gravityTds.begin();                                                           //initialization

dht.begin();

timer.setInterval(1000L, Liquidlevelvalues);
timer.setInterval(1300L, alarmdelay);
timer.setInterval(1900L, LCDPrintValues);
timer.setInterval(120000L, wifitiming);
timer.setInterval(300000L, TDSValues);
timer.setInterval(300300L, DHT22Values);
timer.setInterval(300600L, Temprequest);
timer.setInterval(300900L, digitalClockDisplay);


lcd.setCursor(2, 0);
lcd.print("Water Level (%)");
lcd.setCursor(0, 2);
lcd.print("Nutrient Level (PPM)");
}

void loop() {
  Blynk.run();
  timer.run();

}

//End Void Loop

void LCDPrintValues() {
  lcd.setCursor(1, 1);                                                        //Set cursor at the begining of row 1 to prepare to clear row 1
  lcd.print("                    ");                                          //Clear Row 1
  lcd.setCursor(9, 1);                                                        //Position cursor to enter value of water level
  lcd.print(liquidLevel, 0);                                                  //Water level
  lcd.setCursor(1, 3);                                                        //Set cursor at the begining of row 3 to prepare to clear row 3
  lcd.print("                    ");                                          //Clear Row 3
  lcd.setCursor(8, 3);                                                        //TDS Level position
  lcd.print(tdsValue, 0);                                                     //tds value displayed on lcd
}

void Temprequest() {
  sensors.requestTemperatures();
}

void alarmdelay() {
  Alarm.delay(500);
}

void Liquidlevelvalues() {
  rawLiquidReading = analogRead(liquidLevelPin);
  Blynk.virtualWrite(V0, liquidLevel);                                       //water level vpin
  Blynk.virtualWrite(V10, rawLiquidReading);                                 //humidity vpin
  liquidLevel = map(rawLiquidReading, lowEtapeValue, highEtapeValue, 0, 100);

  if (liquidLevel > 20 && liquidLevel < 50) {
    for (int i = 0; i < 20; i++)
      strip.setPixelColor(i, strip.Color(0, 150, 0));
    strip.show();
  }

  else if (liquidLevel >= 50) {
    strip.clear();
    for (int i = 0; i < 20; i++)
      strip.setPixelColor(i, strip.Color(0, 0, 150));
    strip.show();
  }

  else if (liquidLevel <= 20) {
    strip.clear();
    for (int i = 0; i < 20; i++)
      strip.setPixelColor(i, strip.Color(255, 0, 0));
    strip.show();
  }
}

void DHT22Values() {
  hum = dht.readHumidity();
  temp = dht.readTemperature() * 1.8 + 32;
  Blynk.virtualWrite(V3, temp);                                             //aTemp vpin
  Blynk.virtualWrite(V2, hum);                                              //humidity vpin
}

void TDSValues() {
  gravityTds.setTemperature(sensors.getTempCByIndex(0));                  //set the temperature and execute temperature compensation
  gravityTds.update();                                                    //sample and calculate
  tdsValue = gravityTds.getTdsValue();                                    //then get the value
  Blynk.virtualWrite(V1, tdsValue);                                       //tds vpin
  Blynk.virtualWrite(V4, sensors.getTempCByIndex(0) * 1.8 + 32);          //wtemp vpin
}

void LightsOn() {
  digitalWrite(Light, HIGH);
  Blynk.virtualWrite(V5, 1);                                            // update the Light button widget
}

void LightsOff() {
  digitalWrite(Light, LOW);
  Blynk.virtualWrite(V5, 0);                                            // update the Light button widget
}

void PumpOn() {
  digitalWrite(Pump, HIGH);
  Blynk.virtualWrite(V8, 1);                                           // update the Pump button widget
}

void PumpOff() {
  digitalWrite(Pump, LOW);
  Blynk.virtualWrite(V8, 0);                                          // update the Pump button widget
}

void digitalClockDisplay() {
  // digital clock display of the time
  printDigits(minute());
  printDigits(second());
}

void printDigits(int digits) {
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {                       //For each pixel in strip...
    strip.setPixelColor(i, color);                                    //Set pixel's color (in RAM)
    strip.show();                                                     //Update strip to match
    delay(wait);                                                      //Pause for a moment
  }
}

void wifitiming() {
  time1 = millis();
 if((time1-time2)>3000) //every 3s
 {
   time2=time1;    
   TestWiFiConnection(); //test connection, and reconnect if necessary
   long rssi=WiFi.RSSI();
   if(dbg) Serial.print("RSSI:");
   if(dbg) Serial.println(rssi);
 }
}

void TestWiFiConnection()
//test if always connected
{
 int StatusWiFi=WiFi.status();
 if(StatusWiFi==WL_CONNECTION_LOST || StatusWiFi==WL_DISCONNECTED || StatusWiFi==WL_SCAN_COMPLETED) //if no connection
 {
  digitalWrite(9, LOW); //LED OFF to show disconnected
  if(ScanSSIDs()) WiFiConnect(); //if my SSID is present, connect
 }
} 

void WiFiConnect()
//connect to my SSID
{
 status= WL_IDLE_STATUS;
 while(status!=WL_CONNECTED)
 {
   status = WiFi.begin(ssid,pass);
   if(status==WL_CONNECTED) digitalWrite(9, HIGH); //LED ON to show connected
   else delay(500);
  }
}

char ScanSSIDs()
//scan SSIDs, and if my SSID is present return 1
{
 char score=0;
 int numSsid = WiFi.scanNetworks();
 if(numSsid==-1) return(0); //error
 for(int thisNet=0;thisNet<numSsid;thisNet++) if(strcmp(WiFi.SSID(thisNet),ssid)==0) score=1; //if one is = to my SSID
 return(score);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.