Have graduated from ESP8266 to ESP32 but having problems

I’m using a ESP32 WROOM-32 ESP32-S development board with the Arduino IDE.

I’ve set up the new device, configured with WiFi with the Blynk mobile app.
The device and the template for the device show up and when I compile it and run it I get the following serial output. However the LED very rapidly and should be 5 seconds on and 5 seconds off. My guess is that it is Blynk trying to tell me something.

While this output show the change of state of the LED every 5 seconds the LED is rapidly blinking. However when I comment our the BlynkEdgent.begin() and the BlynkEdgent.run(); The program behaves normally with 5 seconds on and then 5 seconds off. Skitch is below.

14:01:43.318 -> [1191] CONNECTING_NET => CONNECTING_CLOUD
14:01:43.318 -> [1201] Connecting to blynk.cloud:443
14:01:44.738 -> [2612] Certificate OK
14:01:45.020 -> [2913] Ready (ping: 300ms).
14:01:45.110 -> [2980] CONNECTING_CLOUD => RUNNING
14:01:47.382 -> Blink LED
14:01:52.380 -> Blink LED
14:01:57.370 -> Blink LED
14:02:02.379 -> Blink LED
#define BLYNK_TEMPLATE_ID "TMPL2z-ks3zpD"
#define BLYNK_TEMPLATE_NAME "ESP32 Test"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_ESP32_DEV_MODULE
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_TTGO_T_OI

#include "BlynkEdgent.h"
BlynkTimer timer;

#define LED 2

void blinkLED() {
  Serial.println("Blink LED");
  static bool ledState = LOW;
  ledState = !ledState;
  digitalWrite(LED, ledState);
}


void setup()
{  
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  // Set up a timer to call blinkLED every 500ms
  timer.setInterval(5000L, blinkLED);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}

What’s the reasoning behind you doing this?

Pete.

?
I suppose I could have made ledState a global but since it’s only used in blynkLED() I made it a static variable. That certainly isn’t the problem with the sketch. As mentioned when blynkEdgent.begin() and blynkEdgent.run() are commented out it works fine. Otherwise the LED blinks several times a second and does so if I comment out everything but BlynkEdgent begin() and run().

Your blynkLED function makes no sense to me.

You set ledstate to LOW
You then invert it, so setting it to HIGH
Then you write that value to GPIO2

At no point are you doing anything other than writing HIGH to your GPIO2 pin. That won’t blink the LED attached to GPIO2, it will simply keep turning it on, or keep turning it off, depending on whether your LED is active HIGH or active LOW.

To toggle the LED you need to write HIGH to GPIO2 one time around, then LOW the next time around.

Have you edited the #elif defined(USE_ESP32_DEV_MODULE) section of Settings.h at all?

Pete.

Pete, I don’t think you understand what I’m saying. Commenting out the Blynk stuff it runs as desired. Slow Blinking every 5 seconds. I don’t care about that. My problem is that if all I have is the blynk stuff and comment everything else out then that LED blinks several times a second probably telling me that something is not configured correctly with Blynk or something. The fact that its the same LED is just coincidental.

Setting ledState as static to LOW in the subroutine only initializes it at compile time not every time the function is called. Static protects it from disappearing between function calls as called by the timer in this instance but again that part of my sketch is just for testing. That is not my concern. I’m looking for someone to tell me, Hey idiot you forgot to configure your device properly, you need to do this.

Can you elaborate on what this means, and answer this question…

Pete.

  1. I just meant that my little test program uses the internal LED on my development board assigned as #define LED 2 which just happens to be the same LED that blinks fast with when my blinking code isn’t in there. So apparently Blynk is using that telling me something is missing yet the Serial output doesn’t complain.

  2. No, I haven’t touched Settings.h at all. Don’t see where I was told to do that. The only change besides adding my little test elements that I did was uncomment #define USE_ESP32_DEV_MODULE in the Arduino BlynkEdgent example and I only did that after it was doing it fast blink thinking that may be something I needed to to but that made no difference.

Thanks.

That’s a problem. Blynk is attempting to use this LED, so you can’t also use it.

If you’re using this latest version of Settings.h from the Edgent_ESP32 examp,e then no LED is defined for your board…

#elif defined(USE_ESP32_DEV_MODULE)

  #warning "The LED of this board is not configured"

  #define BOARD_BUTTON_PIN            0
  #define BOARD_BUTTON_ACTIVE_LOW     true

and you should be getting a compiler warning saying “ The LED of this board is not configured”.
For Edgent to be using GPIO2 as the LED pin then I’d assume that either you aren’t using the latest Edgent example, or you’d modified the section of code above to add-in an LED pin on GPIO2.

Either way, the pins selected in Settinsg.h by un-commenting a board type in your .ino file need to be avoided when choosing pins in your main code.

Pete.

Thanks. I changed my led to GPIO13 and now it seems to be doing what I asked it to do. It does seem odd that Blynk would use a valuable available pin and waste CPU cycles flashing an led. I just hope it isn’t a sending a message with every flash to the Blynk servers counting against my allotment.

This is the working code.

#define BLYNK_TEMPLATE_ID "TMPL2z-ks3zpD"
#define BLYNK_TEMPLATE_NAME "ESP32 Test"

#define BLYNK_FIRMWARE_VERSION "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

// Uncomment your board, or configure a custom board in Settings.h
#define USE_ESP32_DEV_MODULE
//#define USE_ESP32C3_DEV_MODULE
//#define USE_ESP32S2_DEV_KIT
//#define USE_WROVER_BOARD
//#define USE_TTGO_T7
//#define USE_TTGO_T_OI

#include "BlynkEdgent.h"
BlynkTimer timer;

#define LED 13

void blinkLED() {
  static bool ledState = LOW;
  Serial.println("Blink LED");
  ledState = !ledState;
  digitalWrite(LED, ledState);
}

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  delay(100);
  BlynkEdgent.begin();
  // Set up a timer to call blinkLED every 500ms
  timer.setInterval(5000L, blinkLED);
}

void loop() {
  BlynkEdgent.run();
  timer.run();
}

My next exercise is seeing if I can figure out OTA to change my blinking rate on my test sketch.

It’s actually two GPIO resources that are being used by Edgent - one for the switch and the other for the LED.
For users in the wild, who don’t plug their device into a serial monitor, LED is their only interface to allow them to understand the status of their device and which mode it is in.

It is not. In fact, the LED will flash even when the device is unable to connect to the Blynk server.

It’s very straightforward, just ensure you manually increment BLYNK_FIRMWARE_VERSION and don’t try to change your BLYNK_TEMPLATE_ID via ORA - it’s not possible.

Pete.