Hey all,
Working with a MKR1010, and using Blynk app to view readings from board. The problem I am having is about everyday the board loses connection with WiFI, the CHRG light on the board will be be blinking, and with a press of the reset button the board reconnects. I have tried adding code to have the board reconnect, but it seems unchanged. I have another WIFI connected device (Behyve sprinkler timer) on the wall next to the board, and it runs 99.99% with no connection issues. Any ideas on what I can do to get the board to reconnect when it drops WIFI, and why is it dropping WIFI in the first place?
#include <Smoothed.h>
#include <Blynk.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
char ssid[] = "---------";
char pass[] = "---------";
int status = WL_IDLE_STATUS;
char auth[] = "--------";
BlynkTimer timer;
Smoothed <float> sensorData;
Smoothed <float> sensorData2;
float currentPreSensorValue;
float currentPostSensorValue;
float smoothedPreSensorValueExp;
float smoothedPostSensorValueExp;
float preVoltage;
float postVoltage;
float prePsi;
float postPsi;
float pressureDelta;
float preVoltageTotal;
float postVoltageTotal;
float orderThreshold = 3.0;
float changeThreshold = 4.0;
boolean beenNotified = false;
enum FilterStatus {FilterOk, OrderFilter, ChangeFilter};
FilterStatus FilterCondition = FilterOk;
int i;
WidgetLED ledGood(V1);
WidgetLED ledOrder(V2);
WidgetLED ledChange(V3);
byte preSensorPin = A1;
byte postSensorPin = A2;
const byte yellowLEDPin = 7;
const byte redLEDPin = 6;
void setup() {
SerialUSB.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
sensorData.begin(SMOOTHED_EXPONENTIAL, 10);
sensorData2.begin(SMOOTHED_EXPONENTIAL, 10);
pinMode(preSensorPin, INPUT);
pinMode(postSensorPin, INPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
}
void loop() {
Blynk.run();
timer.run();
// Read the value from the sensor
currentPreSensorValue = analogRead(preSensorPin);
currentPostSensorValue = analogRead(postSensorPin);
//Add the new value to both sensor value stores
sensorData.add(currentPreSensorValue);
sensorData2.add(currentPostSensorValue);
// Get the smoothed values
smoothedPreSensorValueExp = sensorData.get();
smoothedPostSensorValueExp = sensorData2.get();
Serial.print(" Pre-filter psi: ");
Serial.println(prePsi);
Serial.print(" Post-filter psi: ");
Serial.println(postPsi);
CheckFilter();
ManageFilterNotify();
ManageFilterLeds();
}
void myTimerEvent()
{
preVoltage = (5.0 / 1023.0) * smoothedPreSensorValueExp;
prePsi = (preVoltage - 0.5) * (100.0) / (4.5 - 0.5);
Blynk.virtualWrite(V5, prePsi);
postVoltage = (5.0 / 1023.0) * smoothedPostSensorValueExp;
postPsi = (postVoltage - 0.5) * (100.0) / (4.5 - 0.5);
Blynk.virtualWrite(V6, postPsi);
pressureDelta = (prePsi - postPsi);
Blynk.virtualWrite(V7, pressureDelta);
}
void CheckFilter() // Filter condition state machine
{
// All we can do is go from OK to order or from Order to change
switch (FilterCondition)
{
case FilterOk:
if (prePsi - postPsi > orderThreshold) // if the pressure drop is greater than Order threshold
{
FilterCondition = OrderFilter;
}
break;
case OrderFilter:
if (prePsi - postPsi > changeThreshold) //if the pressure drop is greater than replace threshold
{
FilterCondition = ChangeFilter;
}
break;
case ChangeFilter: // Once we're in this state, only a reset will change it.
break;
}
}
void ManageFilterNotify()
{
switch (FilterCondition)
{
case FilterOk:
Serial.println("Filter ok");
ledGood.on();
ledOrder.off();
ledChange.off();
break;
case OrderFilter:
Serial.println("Order filter");
if (!beenNotified){
Blynk.notify("It is time to order filter");
Blynk.email("-----------", "FilterLynk", "It is time to order the fitler.");
beenNotified = true;
}
ledGood.off();
ledOrder.on();
break;
case ChangeFilter:
Serial.println("Replace filter");
if(!beenNotified){
Blynk.notify("It is time to replace fitler!");
Blynk.email("---------", "FilterLynk", "It is time to change the fitler.");
beenNotified = true;
}
ledOrder.off();
ledChange.on();
break;
}
}
void ManageFilterLeds()
{
switch (FilterCondition)
{
case FilterOk:
Serial.println("Leds off - filter ok");
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
break;
case OrderFilter:
Serial.println("Show yellow - order filter");
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
break;
case ChangeFilter:
Serial.println("Show red - replace filter");
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
break;
}
}