Yes, compare Blynk’s W5100 sketch and yours.
It’s in the IDE but also shown below:
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
*
* This example shows how to use Arduino Ethernet shield (W5100)
* to connect your project to Blynk.
* Feel free to apply it to any other example. It's simple!
*
* NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
* DON'T use them in your sketch directly!
*
* WARNING: If you have an SD card, you may need to disable it
* by setting pin 4 to HIGH. Read more here:
* https://www.arduino.cc/en/Main/ArduinoEthernetShield
*
**************************************************************/
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";
#define W5100_CS 10
#define SDCARD_CS 4
void setup()
{
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
Serial.begin(9600);
Blynk.begin(auth);
// You can also specify server.
// For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
//Blynk.begin(auth, "your_server.com", 8442);
//Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
}
void loop()
{
Blynk.run();
}
ok I have had the SPI.h and ethernet.h libraries in my sketch and they made no difference. Why would the two define statements have any effect as this is the original code from the blynk getting started page that works just fine ???(shown below)
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
char auth[] = "YourAuthToken";
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth); // Here your Arduino connects to the Blynk Cloud.
}
void loop()
{
Blynk.run(); // All the Blynk Magic happens here...
}
Auth Token
Cheers again would love to get to the bottom of this!!!
Done that exactly as per sketch made no difference
here is a screenshot of the serial monitor as you can see takes for ever to connect then after a couple of goes of the slider it disconnects and then reconnects and so on…
yes disabled sd card yes no v21 set as off so fades as it slides.
still dont under stand though why the basic test sketch loads instantly and works perfect for weeks on end with out a singles disconnect - the ONLY difference is my code added to basic sketch???
Haven’t tried this yet Costas but I have made a bit of a discovery. If I switch the mains of to the dimmer it stops crashing ??? the circuit is very standard and the only connection to the mains that involves the arduino is D2 which is interrupt 0, which in turn is read and does the zero crossing detect part of the circuit. SO, it would appear that it something to do with this??? Again at least we can tell it’s isolated to a bit of code reading pin 2 but why on earth does it matter if pin2 detects zero crossing or not??? I guess it’s because it wont be calling the zero_cross_int procedure as there is no power on so is it somethinh within this loop that is causing the issue ???
I have also been noticing similar issues with involving delayMicroseconds() to control a servo (also on an UNO R3) while using Blynk… Where the same sketch works perfectly outside of Blynk. I came up with a band-aid fix using Blynk.syncVirtual() but that only worked if the delayMicroseconds() call was in a Blynk Loop… and it brought other issues, so not really recommended
As my Ethernet shield died awhile ago, I have been using Blynk’s USB link, and thought that might have been part of the issue, but now I think not so much
Basically I suspect delayMicroseconds() might be getting interrupted at times due internal Blynk timing.
Also, while you don’t appear to be using the analog pins, a known issue with some Ethernet shield clones was that the REF pin was shorted to either ground or 5V and thus caused unreliable analog reads. I had solved that issue by just bending the REF pin out of the way.
Either way, there seems to be a clear issue involveing Arduino (at least the UNO so far) and delayMicroseconds() that cause timing/dropout issues in Blynk. Forgoing use of all arduinos for ESP’s may be a solution (hint taken ) but not ideal for everyone.
Ok… Let’s go
Test this code with your hardware (Uno R3 and ethernet shield W5100)
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0; // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3; // Output to Opto Triac
int brightness = 128; // Dimming level (0-128) 0 = on, 128 = 0ff
int freqStep = 75; // This is the delay-per-brightness step in microseconds.
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
#define W5100_CS 10
#define SDCARD_CS 4
void setup()
{
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0)
Timer1.initialize(freqStep); // Initialize TimerOne library
Timer1.attachInterrupt(dim_check, freqStep);
Serial.begin(9600);
Blynk.begin(auth);
}
BLYNK_WRITE(V1)// slider brillo
{
int brillo = param.asInt();
brightness=brillo;
}
void zero_cross_detect() {
zero_cross = true;
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
if(zero_cross == true) {
if(i>=brightness) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}
void loop()
{
Blynk.run();
}
OK, blond moment here… as I left my TRIAC board in my other jacket pocket, I was going to monitor the output on my DSO (AKA little toy oscilloscope)… and was getting very strange readings until it dawned on me what zero-crossing detection meant (Ah Hah! those YouTube videos I watch all the time are starting to pay off).
So, no Ethernet shield, no TRIAC and no sine wave generator… I am out of this game
But while digging through the code and looking at the picture, I notice you have something going to PIN 2 and I am guessing that is the actual interrupt pin? or is Pin 3 actually used for both functions? (I don’t fully understand how to use interrupts yet - no judging )
I don’t need to try this code as I know it works perfectly on my setup(which is what I stated in my original post that I had another slightly more complex sketch that works fine). I will resort to this if all else fails, but the point of my post was to find the issue that is causing this problem so I can avoid it in future and help others.
Out of interest which boards do you tend to use for these types of applications with Blynk
Cheers Kev
PS sorry for the late reply - it wouldnt let me reply for 2 hours as it was my first day being a newbie and all that lol!!!
That’s fine… the point was clear… and to be honest I didn’t pay too much attention to your code as, according your comments, it doesn’t work properly…That’s why I offered you the piece of code that works fine with or without Blynk…
I prefer to play with stuff that works and change it according my project doing it bigger and bigger instead of keep struggling with faulty code…
I used to play with Arduino and the ESP-01 as shield at the beginning and, after several time, I moved to ESP’s like WeMos Mini D1 or NodeMCU… A lot of fun here with Blynk.
I agree… sometimes finding the root issue is just as important as an finding an alternative way.
I think it just comes down to the angle of the tongue at times … That and competing and/or conflicting timer/interrupt issues withing Blynk libraries and Aduino core… at least when using certain methods (and apparently UNO’s ) that were not really intended for most IoT applications; of which Blynk is.
Yeah agree Gunner and thanks to Psoro for all of his help. I guess it is down the delay and issue and I will just avoid those in future. I did do a lot of reading today on the arduino site and it does mention a lot of issues with interrupts and delays, and as you pointed out is this is fixed in 018 onwards.
I will park it there guys. Thanks for all your help and lets hope it does get fixed good and proper some day!!