ESP8266 main loop not executed with 80 MHz

Hello everybody and first of all thank you for all the great info in the forum and the docs. However, I am facing an error which I cannot solve right now. I am having the ESP8266 Board from Amazon (https://www.amazon.com/gp/product/B010N1SPRK) and am programming it via Arduino IDE. I first flashed the Board with the flasher from (github.com/nodemcu/nodemcu-flasher/tree/master/Win64/Release ) and default settings as written on the Amazon site. Now I already started to write some Blynk code and it uploaded nicely. But somehow I think that the main loop is not executed with the 80MHz as it is supposed to be.
As an easy example I went back to the ESp8266 Blink file from arduino and added some lines for RS232 output.


int ledState = LOW;

unsigned long previousMillis = 0;
unsigned long counter = 0;
const long interval = 1000;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(57600);
}

void loop() {
  unsigned long currentMillis = millis();
  counter++;
  if (currentMillis - previousMillis >= interval) {
    Serial.print("Counter: ");
    Serial.println(counter);
    counter=0;
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;  // Note that this switches the LED *off*
    } else {
      ledState = LOW;  // Note that this switches the LED *on*
    }
    digitalWrite(LED_BUILTIN, ledState);
    Serial.print("LED toggle at: ");
    Serial.println(millis());
  }
}

What I now get as a result is the following:
LED toggle at: 34837
Counter: 94800
LED toggle at: 35837
Counter: 94711
LED toggle at: 36837
Counter: 94828
LED toggle at: 37837
Counter: 94695

So the LED is toggled every 1000 miliseconds as it is supposed to be. But the counter is just counting up to about 94700 which is not 80 MHz at all.Is there any clock divider which could be set? Or where is the problem located?
If I do change the Speed to 160 MHz in Arduino it is running almost twice that fast but still too slow.

LED toggle at: 6530
Counter: 176941
LED toggle at: 7530
Counter: 180174
LED toggle at: 8530
Counter: 179521
LED toggle at: 9530
Counter: 180586
LED toggle at: 10530
Counter: 180487
LED toggle at: 11530

Thank you so much for the help

Just because a MCU’s internal clock runs at a specified speed, doesn’t mean that is exactly the end result speed… there are lots of overhead, internal MCU processing, WiFi, background libraries, foreground commands like Serial.print() & digitalWrite(), etc… even your own written code efficiency… All needs to be taken into account.

Besides… this is the Blynk forum, not the general programming and “how do microcontrollers work” forum :stuck_out_tongue: For example… your example has nothing to do with Blynk… or if somehow used in Blynk… will just crash due to your use of the void loop()

1 Like

Hey Gunner and thank you for your reply! I was just curious about the large amount of background tasks leading to that “slow execution”.
Anyway, I just started with that easy example as I thought I programmed something wrong or so. But indeed now it doesn’t seem to be the case. My first Blynk example was the LED fader and there I noticed the long fading time which made me create this post :wink:

If I upload the code shown next a fading from one Widget in Blynk takes about 2.5 seconds. And in this case the stepsize is even set to 5. I would like to build a RGB crossfading application with DMX Output so there will be way more computation done on the ESP and I am just wondering about the Speed. What is the main limiting factor regarding execution time for Blynk applications? Is the ping of the server connection also having an impact? I have already set up my own Blynk server and hoped to improve it with that.

*************************************************************

  Fade using a LED widget on your phone!

  App project setup:
    LED widget on V2
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

WidgetLED led2(V2);

BlynkTimer timer;

// V2 LED Widget is fading
void fadeLedWidget()
{
  static int value = 0;
  static int delta = 5;
  value += delta;
  if (value > 255 || value < 0) {
    delta = -delta;
    Blynk.run();
    Serial.println(millis());
  } else {
    Serial.print("LED on V2: ");
    Serial.println(value);
    led2.setValue(value);
  }
}

void setup()
{
  // Debug console
  Serial.begin(115200);

 // Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  Blynk.begin(auth, ssid, pass, IPAddress(), );

 // timer.setInterval(1, fadeLedWidget);
}

void loop()
{
  fadeLedWidget();
  //timer.run();
} 

[10932] Ready (ping: 57ms).
LED on V2: 255
14416
LED on V2: 255
LED on V2: 0
17901

Running this in the void loop() may trigger a form of governor in Blynk that prevents overt server spamming… as you are trying to process that function thousands of times a second… or even if doing it the strange way you are, it may cause other issue?? hard to say as I do NOT run code that way.

The proper way is to use BlynkTimer and timed functions with reasonable intervals for needed rates.

As for will Blynk run fast… yes, yes it can… see here…

With Cloud it WILL depend on internet latency, but with Local Server then generally only your own network latency matters.

Here is another old example I made over a year ago for fading up and down both a virtual and Physical LED via timers.

I could certainly improve on this with a little clear headed effort… More efficient coding, allowing better control , properly disabling the timers if somehow process is cancelled, etc.

//===== White LED & Widget Button - Fade Up/Down - BLYNK Functions =====
BLYNK_WRITE(V0)  // Triggered from a virtual Button on V0, Virtual LED on V1 and a Physical LED on whatever PWM pin you want (in my case Arduino pin 6)
{
  int LEDFadeFlag = param.asInt();
  if (LEDFadeFlag == 1) {  // If Button ON Fade up
    FadeLvl = -1;
    timer.setTimer(18, FadeLEDUP, 16);  // Run every 18ms for 16 iterations
  } else {  // If Button OFF Fade down
    FadeLvl = 256;
    timer.setTimer(18, FadeLEDDOWN, 16);  // Run every 18ms for 16 iterations
  }
}

void FadeLEDUP() {
  FadeLvl += 16;  // Fade up in 16 increments
  Blynk.virtualWrite(V1, FadeLvl);
  analogWrite(6, FadeLvl);
}

void FadeLEDDOWN() {
  FadeLvl -= 16;  // Fade down in 16 increments
  Blynk.virtualWrite(V1, FadeLvl);
  analogWrite(6, FadeLvl);
}
1 Like