//#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#include <Wire.h>
#include <SPI.h>;
#include <WiFiNINA.h>;
#include <BlynkSimpleWiFiNINA.h>;
#include <OneWire.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#define MIXswitchHIGH 2
#define MIXswitchLOW 3
#define FINALswitchHIGH 4
#define FINALswitchLOW 5
#define RODI2MIXrelay 6
#define MIX2FINALrelay 7
#define MIXswitchEM 8
#define FINALswitchEM 9
int MixLevelHigh = 0;
int FinalLevelHigh = 0;
int MixLevelLow = 0;
int FinalLevelLow = 0;
int MixEmergency = 0;
int FinalEmergency = 0;
int pinValue1 = 0;
int pinValue2 = 0;
int pinValue3 = 0;
int wifi_attempt_count=0;
int max_wifi_connect_attempts=20;
int wait_between_wifi_attempts_millis=500;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "************************";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "**************";
char pass[] = "*****************";
//Both used for Blynk
BlynkTimer timer;
WidgetRTC rtc;
WidgetLED led1(V5);
WidgetLED led2(V6);
BLYNK_CONNECTED()
{
rtc.begin();
//Blynk.syncVirtual(V11);
}
void RODIpumpRUN()
{
digitalWrite (RODI2MIXrelay,HIGH);
}
void RODIpumpIDLE()
{
digitalWrite (RODI2MIXrelay,LOW);
}
void MIXpumpRUN()
{
digitalWrite (MIX2FINALrelay,HIGH);
}
void MIXpumpIDLE()
{
digitalWrite (MIX2FINALrelay,LOW);
}
void checkLevels()
{
MixLevelHigh = digitalRead(MIXswitchHIGH);
MixLevelLow = digitalRead(MIXswitchLOW);
FinalLevelHigh = digitalRead(FINALswitchHIGH);
FinalLevelLow = digitalRead(FINALswitchLOW);
FinalEmergency = digitalRead(FINALswitchEM);
MixEmergency = digitalRead(MIXswitchEM);
//Serial.print("Low level mix: ");
//Serial.println(MixLevelLow);
//Serial.print("High level mix: ");
//Serial.println(MixLevelHigh);
//Serial.print("Emergency level mix: ");
//Serial.println(MixEmergency);
//Serial.print("Low level final: ");
//Serial.println(FinalLevelLow);
//Serial.print("High level final: ");
//Serial.println(FinalLevelHigh);
//Serial.print("Emergency level final: ");
//Serial.println(FinalEmergency);
if (MixLevelLow == HIGH)
{
if (MixLevelHigh == LOW)
{
led1.on();
Blynk.setProperty(V5, "color", "#1af002");
}
}
if (MixLevelLow == LOW)
{
led1.on();
Blynk.setProperty(V5, "color", "#f81605");
}
if (MixEmergency == HIGH)
{
led1.on();
Blynk.setProperty(V5, "color", "#f81605");
}
if (FinalLevelLow == HIGH)
{
if (FinalLevelHigh == LOW)
{
led2.on();
Blynk.setProperty(V6, "color", "#1af002");
}
}
if (FinalLevelLow == LOW)
{
led2.on();
Blynk.setProperty(V6, "color", "#f81605");
}
if (FinalEmergency == HIGH)
{
led2.on();
Blynk.setProperty(V6, "color", "#f81605");
}
}
BLYNK_WRITE(V1)
{
pinValue1 = param.asInt();
Serial.println("Fill Mix Bin");
}
void RODI2MixSet()
{
//Serial.print("High level switch: ");
//Serial.println(MixLevelHigh);
//Serial.println(pinValue1);
if (MixLevelHigh == HIGH)
{
RODIpumpIDLE();
//pinValue1 = 0;
Blynk.virtualWrite(V1,0);
pinValue1 = 0;
Serial.println("Mix bin is already full, cannot proceed.");
//Blynk.virtualWrite(V4, "Mix bin full");
}
if (MixLevelHigh == LOW & pinValue1 == 1)
{
RODIpumpRUN();
Serial.println("Filling mix bin with RODI water");
Blynk.virtualWrite(V4, "Filling mix bin");
}
if (MixLevelHigh == LOW & pinValue1 == 0)
{
RODIpumpIDLE();
Serial.println("RODI to mix pump idle");
}
}
BLYNK_WRITE(V2)
{
pinValue2 = param.asInt();
Serial.println("Fill Final Bin");
}
void Mix2FinalSet()
{
if (MixLevelLow == LOW)
{
MIXpumpIDLE();
Blynk.virtualWrite(V2,0);
pinValue2 = 0;
Serial.println("Mix bin is too low to proceed.");
Blynk.virtualWrite(V4,"Mix Bin Low");
Blynk.email("********************@gmail.com", "Mix Bin Low");
}
if (FinalLevelHigh == HIGH)
{
MIXpumpIDLE();
Blynk.virtualWrite(V2,0);
pinValue2 = 0;
Serial.println("Final bin is already full, cannot proceed.");
//Blynk.virtualWrite(V4, "Final bin full");
}
if (FinalLevelHigh == LOW & pinValue2 == 1)
{
MIXpumpRUN();
Serial.println("Filling final bin with saltwater");
Blynk.virtualWrite(V4, "Filling final bin");
}
if (FinalLevelHigh == LOW & pinValue2 == 0)
{
MIXpumpIDLE();
Serial.println("Mix to final pump idle");
}
}
void FillFinalBin()
{
if (FinalLevelLow == LOW)
{
Serial.println("Final bin is too low.");
Blynk.virtualWrite(V4,"Final Bin Low");
Blynk.email("******************@gmail.com", "Final Bin Low");
}
}
BLYNK_WRITE(V3)
{
pinValue3 = param.asInt();
Serial.println("Emergency Shut off");
Blynk.virtualWrite(V4, "Emergency STOP");
}
void EmergencyStop()
{
if (pinValue3 == 1)
{
MIXpumpIDLE();
RODIpumpIDLE();
Blynk.virtualWrite(V1,0);
pinValue1 = 0;
Blynk.virtualWrite(V2,0);
pinValue2 = 0;
Serial.println("All pumps stopped");
}
if (FinalEmergency == HIGH)
{
//MIXpumpIDLE();
RODIpumpIDLE();
//Blynk.virtualWrite(V1,0);
//pinValue1 = 0;
Blynk.virtualWrite(V2,0);
pinValue2 = 0;
Serial.println("Final bin emergency high level stop");
}
if (MixEmergency == HIGH)
{
MIXpumpIDLE();
//RODIpumpIDLE();
Blynk.virtualWrite(V1,0);
pinValue1 = 0;
//Blynk.virtualWrite(V2,0);
//pinValue2 = 0;
Serial.println("Mix bin emergency high level stop");
}
if (pinValue3 == 0)
{
Blynk.virtualWrite(V4,"Status normal");
}
}
bool standalone_mode = true;
void Connect_to_WiFi()
{
Serial.println(F("Connecting to WiFi..."));
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // connect to the network
}
while (WiFi.status() != WL_CONNECTED && wifi_attempt_count < max_wifi_connect_attempts) // Loop until we've connected, or reached the maximum number of attemps allowed
{
delay(wait_between_wifi_attempts_millis);
wifi_attempt_count++;
Serial.print(F("WiFi connection - attempt # "));
Serial.print(wifi_attempt_count);
Serial.print(F(" of "));
Serial.println(max_wifi_connect_attempts);
}
// we get to this point when either we're connected to Wi-Fi, or we've tried too many times. We need to do differnet things, depending which it is...
if (WiFi.status() == WL_CONNECTED)
{
//WiFi.mode(WIFI_STA);
Serial.println(F("WiFi CONNECTED"));
Serial.println();
}
else
{
// we get here if we tried multiple times, but can't connect to WiFi. We need to go into stand-alone mode and wait a while before re-trying...
standalone_mode = true; // We can't be in connected mode if no wifi
Serial.println();
Serial.println(F("................ In Stand-alone mode ................"));
timer.setInterval(1000*60*60,delayedWIFIconnect);
}
}
void delayedWIFIconnect()
{
if (standalone_mode = true)
{
wifi_attempt_count=0;
Connect_to_WiFi();
}
}
//void reconnectBlynk()
//{
// if (!Blynk.connected())
// {
// Serial.println("Lost connection");
// Blynk.disconnect();
// WiFi.begin(ssid, pass);
// Blynk.connect();
// if (Blynk.connect()) Serial.println("Reconnected");
// else Serial.println("Not reconnected");
// }
// else if (Blynk.connected())
// {
// Serial.println("Connected");
// }
//}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Connect_to_WiFi();
Blynk.config(auth);
Blynk.connect();
timer.setInterval(1000L, checkLevels);
timer.setInterval(5000L, Mix2FinalSet);
timer.setInterval(6000L, RODI2MixSet);
timer.setInterval(7000L, EmergencyStop);
//timer.setInterval(300000L, reconnectBlynk);
timer.setInterval(8000L, FillFinalBin);
pinMode(MIXswitchHIGH, INPUT_PULLUP);
pinMode(MIXswitchLOW, INPUT_PULLUP);
pinMode(FINALswitchHIGH, INPUT_PULLUP);
pinMode(FINALswitchLOW, INPUT_PULLUP);
pinMode(RODI2MIXrelay, OUTPUT);
pinMode(MIX2FINALrelay, OUTPUT);
pinMode(FINALswitchEM, INPUT_PULLUP);
pinMode(MIXswitchEM, INPUT_PULLUP);
}
void loop() {
timer.run();
if (WiFi.status() != WL_CONNECTED)
{
Connect_to_WiFi();
}
if (WiFi.status() == WL_CONNECTED)
{
if (!Blynk.connected())
{
Blynk.connect();
}
}
if (Blynk.connected())
{
Blynk.run();
}
//Blynk.run();
}
[5312] Connecting to blynk-cloud.com:80
[5470] Ready (ping: 74ms).
[5669] Time sync: OK
Hour: 14
Deep sleep pin value: 0
Deep sleep mode
[75] Connecting to *************
[7307] Connected to WiFi
[7307] IP: 192.168.4.143
[7307]
[5311] Connecting to blynk-cloud.com:80
[5479] Ready (ping: 68ms).
[5698] Time sync: OK
Hour: 14
Deep sleep pin value: 0
Deep sleep mode
[75] Connecting to **************
[5306] Connected to WiFi
[5307] IP: 192.168.4.143
[5307]
I’m assuming the number in the brackets have some meaning, and could potentially tell me how many times I’m losing connection. These two outputs are not back-to-back, just two separate instances I copied.