Update 2018-08-31: Scroll down for solution!
Hello,
I’m having some difficulties with three widgets, namely the terminal widget, the rtc widget and the email widget.
Hardware: Arduino UNO with ENC29J60 Ethernet module
App: Android
Server: Blynk Cloud
Library Version: 0.5.3
Terminal widget: As soon as I add the line terminal.flush(); to my code, my Arduino is unable to connect to the Blynk server.
Working Code:
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <RCSwitch.h>
char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Censored
boolean rcswitchstatus1 = false; // Pump 1
boolean rcswitchstatus2 = false; // Computer 1 Bedroom
boolean starttime_sent = false;
int blynkRead1; // Pump 1
int blynkRead2; // Computer 1 Bedroom
int led_status;
unsigned long blink_timer = millis();
WidgetTerminal terminal(V0);
RCSwitch mySwitch = RCSwitch();
BLYNK_WRITE(V4){
blynkRead1 = param.asInt();
}
BLYNK_WRITE(V5){
blynkRead2 = param.asInt();
}
void switchit(char tristate[20]){
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
}
void status_led(){
if ((millis() - blink_timer) >= 2000){
led_status = !digitalRead(9);
digitalWrite(9, led_status);
if (led_status == 1){
Blynk.virtualWrite(V1, 1023);
}
else{
Blynk.virtualWrite(V1, 0);
}
blink_timer = millis();
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(9, OUTPUT);
mySwitch.enableTransmit(6);
mySwitch.setPulseLength(309);
Blynk.begin(auth);
}
void loop()
{
Blynk.run();
status_led();
if (starttime_sent == false and millis() > 1000){
terminal.println("System Started");
starttime_sent = true;
}
if (blynkRead1 == 1 && rcswitchstatus1 == false){
terminal.println("Pump 1 ON");
switchit("XXXXXXXXXXXX");
rcswitchstatus1 = true;
}
if (blynkRead1 == 0 && rcswitchstatus1 == true){
terminal.println("Pump 1 OFF");
switchit("XXXXXXXXXXXX"); // Censored
rcswitchstatus1 = false;
}
if (blynkRead2 == 1 && rcswitchstatus2 == false){
terminal.println("Computer 1 ON");
switchit("XXXXXXXXXXXX"); // Censored
rcswitchstatus2 = true;
}
if (blynkRead2 == 0 && rcswitchstatus2 == true){
terminal.println("Computer 1 OFF");
switchit("XXXXXXXXXXXX"); // Censored
rcswitchstatus2 = false;
}
}
Adding terminal.flush to my code:
void loop()
{
Blynk.run();
status_led();
if (starttime_sent == false and millis() > 1000){
terminal.println("System Started");
terminal.flush();
starttime_sent = true;
}
if (blynkRead1 == 1 && rcswitchstatus1 == false){
terminal.println("Pump 1 ON");
terminal.flush();
switchit("XXXXXXXXXXXX");
rcswitchstatus1 = true;
}
if (blynkRead1 == 0 && rcswitchstatus1 == true){
terminal.println("Pump 1 OFF");
terminal.flush();
switchit("XXXXXXXXXXX");
rcswitchstatus1 = false;
}
if (blynkRead2 == 1 && rcswitchstatus2 == false){
terminal.println("Computer 1 ON");
terminal.flush();
switchit("XXXXXXXXXX");
rcswitchstatus2 = true;
}
if (blynkRead2 == 0 && rcswitchstatus2 == true){
terminal.println("Computer 1 OFF");
terminal.flush();
switchit("XXXXXXXXXX");
rcswitchstatus2 = false;
}
}
Serial Output:
[0] Getting IP...
[5334] IP:192.168.178.21
[5335]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.3 on Arduino Uno
Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
[5531] Connecting to blynk-cloud.com:80
[6
Serial output stops here. No connection can be established and as soon as I remove the terminal.flush(); lines again, the code works fine.
Email Widget:
void loop()
{
Blynk.run();
status_led();
if (starttime_sent == false and millis() > 1000){
terminal.println("System Started");
Blynk.email("XXXXXXXXXXXX@gmail.com", "Arduino 1 Connected", "Arduino 1 Successfully Connected");
starttime_sent = true;
}
if (blynkRead1 == 1 && rcswitchstatus1 == false){
terminal.println("Pump 1 ON");
switchit("XXXXXXXXXXXXXXXXXXXXXX");
rcswitchstatus1 = true;
}
if (blynkRead1 == 0 && rcswitchstatus1 == true){
terminal.println("Pump 1 OFF");
switchit("XXXXXXXXXXXXXXXXXXXXXXX");
rcswitchstatus1 = false;
}
if (blynkRead2 == 1 && rcswitchstatus2 == false){
terminal.println("Computer 1 ON");
switchit("XXXXXXXXXXXXXXXXXXXXXXX");
rcswitchstatus2 = true;
}
if (blynkRead2 == 0 && rcswitchstatus2 == true){
terminal.println("Computer 1 OFF");
switchit("XXXXXXXXXXXXXXXXXXXXXXX");
rcswitchstatus2 = false;
}
}
Serial Monitor:
[0] Getting IP...
[5334] IP:192.168.178.21
[5335]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.3 on Arduino Uno
Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
[1] Getting IP...
[5329] IP:192.168.178.21
[5330]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.3 on Arduino Uno
Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
[1] Getting IP...
[5347] IP:192.168.178.21
[5348]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.3 on Arduino Uno
Give Blynk a Github star! => https://github.com/blynkkk/blynk-library
[1] Getting IP...
This happens over and over again. After removing the line, the Arduino connects instantly and works like expected.
RTC Widget:
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <RCSwitch.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;
char auth[] = "XXXXXXXXXXXXXXXXXXXXXX";
boolean rcswitchstatus1 = false; // Pump 1
boolean rcswitchstatus2 = false; // Computer 1 Bedroom
boolean starttime_sent = false;
int blynkRead1; // Pump 1
int blynkRead2; // Computer 1 Bedroom
int led_status;
unsigned long blink_timer = millis();
WidgetTerminal terminal(V0);
WidgetRTC rtc;
RCSwitch mySwitch = RCSwitch();
BLYNK_WRITE(V4){
blynkRead1 = param.asInt();
}
BLYNK_WRITE(V5){
blynkRead2 = param.asInt();
}
BLYNK_CONNECTED() {
rtc.begin();
}
void switchit(char tristate[20]){
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
mySwitch.sendTriState(tristate);
}
void status_led(){
if ((millis() - blink_timer) >= 2000){
led_status = !digitalRead(9);
digitalWrite(9, led_status);
if (led_status == 1){
Blynk.virtualWrite(V1, 1023);
}
else{
Blynk.virtualWrite(V1, 0);
}
blink_timer = millis();
}
}
void setup()
{
// Debug console
// Serial.begin(9600);
pinMode(9, OUTPUT);
mySwitch.enableTransmit(6);
mySwitch.setPulseLength(309);
Blynk.begin(auth);
setSyncInterval(10 * 60);
}
void loop()
{
Blynk.run();
status_led();
if (starttime_sent == false and millis() > 1000){
terminal.println("System Started");
starttime_sent = true;
}
if (blynkRead1 == 1 && rcswitchstatus1 == false){
terminal.println("Pump 1 ON");
switchit("XXXXXXXXXXXX");
rcswitchstatus1 = true;
}
if (blynkRead1 == 0 && rcswitchstatus1 == true){
terminal.println("Pump 1 OFF");
switchit("XXXXXXXXXXXXXX");
rcswitchstatus1 = false;
}
if (blynkRead2 == 1 && rcswitchstatus2 == false){
terminal.println("Computer 1 ON");
switchit("XXXXXXXXXXXXXX");
rcswitchstatus2 = true;
}
if (blynkRead2 == 0 && rcswitchstatus2 == true){
terminal.println("Computer 1 OFF");
switchit("XXXXXXXXXXXXXX");
rcswitchstatus2 = false;
}
}
Same issue, no connection to Blynk server possible with the minium required lines for the RTC widget. I also tried a version without any other code, which didn’t work without removing the following function from the code, either. This doesn’t fix the issue with my code above, though.
BLYNK_CONNECTED() {
rtc.begin();
}
I would really appreciate some help from you guys!