I tried using the code above for an arduino MKR wifi-1010 but it doesn’t work properly. After losing the internet connection, the code runs normal for a few seconds with the physical buttons and then it doesn’t work anymore.
Then the physical buttons only work again when the internet returns again.
Then it locks for a minute and returns to normal operation.
Can someone help me?
Here is the code:
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <IoAbstraction.h>
#include <IoAbstractionWire.h>
#include <Wire.h>
// create both an Arduino and an IO expander based IO abstraction
IoAbstractionRef ioExpander = ioFrom8574(0x20);
IoAbstractionRef ioExpander1 = ioFrom8574(0x21);
IoAbstractionRef ioExpander2 = ioFrom8574(0x22);
IoAbstractionRef ioExpander3 = ioFrom8574(0x23);
char auth[] = "XXXXXXX";
char ssid[] = "XXXXX";
char pass[] = "XXXX";
char server[] = "blynk-cloud.com"; // URL for Blynk Cloud Server
int port = 8080;
int ReCnctFlag; // Reconnection Flag
int ReCnctCount = 0; // Reconnection counter
// Set your LED and physical button pins here
const int ledPin = 6;
const int btnPin = 7;
int ledState = LOW; int btnState = HIGH;
int ledState2 = LOW; int btnState2 = HIGH;
int ledState3 = LOW; int btnState3 = HIGH;
int blackout = 0;
BlynkTimer timer;
void checkPhysicalButton();
void setup()
{
// Debug console
Serial.begin(9600);
// Blynk.begin(auth, ssid, pass);
WiFi.begin(ssid, pass); // Non-blocking if no WiFi available
Blynk.config(auth, server, port);
Blynk.connect();
Wire.begin();
pinMode(ledPin, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
digitalWrite(ledPin, ledState);
timer.setInterval(100L, checkPhysicalButton); // Setup a function to be called every 100 ms
// here we set the direction of pins on the IO expander
ioDevicePinMode(ioExpander, 0, INPUT);
ioDevicePinMode(ioExpander, 1, INPUT);
ioDevicePinMode(ioExpander, 2, INPUT);
ioDevicePinMode(ioExpander, 3, INPUT);
ioDevicePinMode(ioExpander, 4, OUTPUT);
ioDevicePinMode(ioExpander, 5, OUTPUT);
ioDevicePinMode(ioExpander, 6, OUTPUT);
ioDevicePinMode(ioExpander, 7, OUTPUT);
}
BLYNK_CONNECTED() {
Serial.println("Cconnected");
ReCnctCount = 0;
Blynk.syncVirtual(V2); // Request the latest state from the server
Blynk.syncVirtual(V3); // Request the latest state from the server
Blynk.syncVirtual(V4); // Request the latest state from the server
//Blynk.virtualWrite(V2, ledState); // Alternatively, you could override server state using:
}
// When App button is pushed - switch the state
BLYNK_WRITE(V2) {ledState = param.asInt(); digitalWrite(ledPin, ledState);}
BLYNK_WRITE(V3) {ledState2 = param.asInt(); ioDeviceDigitalWrite(ioExpander, 4, ledState2);}
BLYNK_WRITE(V4) {ledState3 = param.asInt(); ioDeviceDigitalWrite(ioExpander, 5, ledState3);}
void checkPhysicalButton()
{
ioDeviceSync(ioExpander);
if (digitalRead(btnPin) == LOW) {
if (btnState != LOW) {ledState = !ledState;
digitalWrite(ledPin, ledState);
Blynk.virtualWrite(V2, ledState); }
btnState = LOW;} else {btnState = HIGH; }
if (ioDeviceDigitalRead(ioExpander, 0) == LOW) {
if (btnState2 != LOW) {ledState2 = !ledState2;
Blynk.virtualWrite(V3, ledState2);
ioDeviceDigitalWrite(ioExpander, 4, ledState2);}
btnState2 = LOW;} else {btnState2 = HIGH;}
if (ioDeviceDigitalRead(ioExpander, 1) == LOW) {
if (btnState3 != LOW) {ledState3 = !ledState3;
Blynk.virtualWrite(V4, ledState3);
ioDeviceDigitalWrite(ioExpander, 5, ledState3);}
btnState3 = LOW;} else {btnState3 = HIGH;}
}
void loop()
{
timer.run(); //timer2.run();
if (Blynk.connected()) { // If connected run as normal
Blynk.run();
} else if (ReCnctFlag == 0) { // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
ReCnctFlag = 1; // Set reconnection Flag
Serial.println("Starting reconnection timer in 30 seconds...");
timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function
ReCnctFlag = 0; // Reset reconnection Flag
ReCnctCount++; // Increment reconnection Counter
Serial.print("Attempting reconnection #");
Serial.println(ReCnctCount);
WiFi.begin(ssid, pass); // Non-blocking if no WiFi available
Blynk.config(auth, server, port);
Blynk.connect(); // Try to reconnect to the server
}); // END Timer Function
}
}