Hi,
I’ve a 5 motors that when turned on energize a relay which N/O contactors are wired to an Arduino Yun board (using ethernet). Meaning that when the motors are ON, a DC ground is applied to the board’s pin. A LOW signal is detected and a LED is turned on in the Blynk app. I’m using analog lines because the digitals are all used up.
This process has been working for months but lately I had the brilliant idea to count the times the motors are turned on during a day and displaying the totals on the Blynk app as well. The counters are then reseted in the morning starting so a new process all over again. For troubleshooting purposes, I’m also storing, on a SD card, the counts (relays activations).
Well, since the addition of the counters, the LEDs on the Blynk app are not always turning on/off but the counters are adding up correctly.
I just cannot seem to find my error and I was wondering if it could be a timing issue between my Yun and the Blynk server. I also noticed that when I turn on the app, the Yun shows offline but always recovers within seconds. The latter it always been this way even before adding the countes to the app.
Here’s a test code…
TIA
//#define BLYNK_DEBUG // Optional, this enables lots of prints
//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <Bridge.h>
#include <BlynkSimpleYun.h>
#include <Process.h>
#include <FileIO.h>
String dataString;
/* Blynk */
char auth[] = "*******";
BlynkTimer timer;
//LEDs on counters
// S XL XXL S1 S2
int opCounter[5] = {0, 0, 0, 0, 0};
int lastRelayState[5] = {0, 0, 0, 0, 0};
//Blynk LEDs
WidgetLED ledS(V11);
WidgetLED ledXL(V12);
WidgetLED ledXXL(V13);
WidgetLED ledS1(V14);
WidgetLED ledS2(V15);
//hardware arduino Yun
//lines connected to N/O relays contactors
const int statusPinS = A5;
const int statusPinXL = A4;
const int statusPinXXL = A3;
const int statusPinS1 = A2;
const int statusPinS2 = A1;
void setup() {
Bridge.begin();
Console.begin();
pinMode(statusPinS, INPUT_PULLUP);
pinMode(statusPinXL, INPUT_PULLUP);
pinMode(statusPinXXL, INPUT_PULLUP);
pinMode(statusPinS1, INPUT_PULLUP);
pinMode(statusPinS2, INPUT_PULLUP);
timer.setInterval(5000L, ledTimerS); //check lavadora x status
timer.setInterval(5000L, ledTimerXL); //check lavadora x status
timer.setInterval(5000L, ledTimerXXL); //check lavadora x status
timer.setInterval(5000L, ledTimerS1); //check lavadora x status
timer.setInterval(5000L, ledTimerS2); //check lavadora x status
Blynk.begin(auth);
}
void loop() {
//--if it's 9AM resetCounters()---
Blynk.run();
timer.run();
}
void ledTimerS() {
bool pinState = (digitalRead(statusPinS) == HIGH);
if (pinState != lastRelayState[0]) {
if (pinState) {
ledS.off(); // maquina x led state
}
else {
ledS.on();
opCounter[0] += 1;
dataString = "";
dataString += getTimeStamp();
dataString += " S cnt: ";
dataString += opCounter[0];
writeSD(dataString);
Blynk.virtualWrite(V18, opCounter[0]);
}
delay(50); //for debounce
}
lastRelayState[0] = pinState;
}
void ledTimerXL() {
bool pinState = (digitalRead(statusPinXL) == HIGH);
if (pinState != lastRelayState[1]) {
if (pinState) {
ledXL.off();
}
else {
ledXL.on();
opCounter[1] += 1;
dataString = "";
dataString += getTimeStamp();
dataString += " XL cnt: ";
dataString += opCounter[1];
writeSD(dataString);
Blynk.virtualWrite(V20, opCounter[1]);
}
delay(50); //for debounce
}
lastRelayState[1] = pinState;
}
void ledTimerXXL() {
bool pinState = (digitalRead(statusPinXXL) == HIGH);
if (pinState != lastRelayState[2]) {
if (pinState) {
ledXXL.off(); // maquina x led status
}
else {
ledXXL.on();
opCounter[2] += 1;
dataString = "";
dataString += getTimeStamp();
dataString += " XXL cnt: ";
dataString += opCounter[2];
writeSD(dataString);
Blynk.virtualWrite(V21, opCounter[2]);
}
delay(50); //for debounce
}
lastRelayState[2] = pinState;
}
void ledTimerS1() {
bool pinState = (digitalRead(statusPinS1) == HIGH);
if (pinState != lastRelayState[3]) {
if (pinState) { //led off
ledS1.off(); // maquina x led status
}
else {
ledS1.on();
opCounter[3] += 1;
dataString = "";
dataString += getTimeStamp();
dataString += " S1 cnt: ";
dataString += opCounter[3];
writeSD(dataString);
Blynk.virtualWrite(V22, opCounter[3]);
}
delay(50); //for debounce
}
lastRelayState[3] = pinState;
}
void ledTimerS2() {
bool pinState = (digitalRead(statusPinS2) == HIGH);
if (pinState != lastRelayState[4]) {
if (pinState) {
ledS2.off();
}
else {
ledS2.on();
opCounter[4] += 1;
dataString = "";
dataString += getTimeStamp();
dataString += " S2 cnt: ";
dataString += opCounter[4];
writeSD(dataString);
Blynk.virtualWrite(V23, opCounter[4]);
}
delay(50); //for debounce
}
lastRelayState[4] = pinState;
}
void writeSD(String data) {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
// The FileSystem card is mounted at the following "/mnt/FileSystema1"
File dataFile = FileSystem.open("/mnt/sd/cntlog.txt", FILE_APPEND);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(data);
dataFile.close();
// print to the Console port too:
Console.print("Data written to SD: ");
Console.println(data);
}
// if the file isn't open, pop up an error:
else {
Console.println("error opening datalog.txt");
}
}
void resetCounters() {
//reset machines use counters & status in the morning
for (int i = 0; i < 6; i++) {
opCounter[i] = 0;
lastRelayState[i] = 0;
}
Blynk.virtualWrite(V18, opCounter[0]);
Blynk.virtualWrite(V20, opCounter[1]);
Blynk.virtualWrite(V21, opCounter[2]);
Blynk.virtualWrite(V22, opCounter[3]);
Blynk.virtualWrite(V23, opCounter[4]);
}
// This function return a string with the time stamp
String getTimeStamp() {
String result;
Process time;
// date is a command line utility to get the date and the time
// in different formats depending on the additional parameter
time.begin("date");
time.addParameter("+%D-%T"); // parameters: D for the complete date mm/dd/yy
// T for the time hh:mm:ss
time.run(); // run the command
// read the output of the command
while (time.available() > 0) {
char c = time.read();
if (c != '\n') {
result += c;
}
}
return result;
}