Esp 8266 blynk and blinking led

Hello. this is my first topic and i need help (sorry my english I use translator)
I bay esp8266 wifi d1 mini module because i want create something like car turn signal (blinking led) in diecast model 1:24 scale, example :
https://www.youtube.com/watch?v=EH4-G8_VDVA
can i must to learn C, C++ language to make create something like this ?, there is no other simple way to create blinking led ?.. of corse so many tutorials in youtube how to turn ON and turn OFF led in mobile app but no one create tutorial how to blink led if i switch button in phone ā€¦

Yes, youā€™ll need to learn a little C++, but itā€™s not that difficult and community members will help guide you.
However, the biggest issue youā€™ll face is the power consumption of the D1 Mini and the LEDs. How are you planning on powering this?

Pete.

I bought battery charger module TP4056 and old phone battery (samsung, nokia, sony ericssonā€¦e.t.c) and voltage step down module. Full charged battery is 4,2v so i need step down module to will change to 3,3v ( module AMS1117) and Iā€™ll see what happensā€¦ ;). Of course first i must solve blinking led problem.

I found something like this: LED Blink With Blynk 2.0 - Hackster.io
and I am curious whether this tutorial maybe works in my project? of corse i must to change pins to D0 or D1ā€¦ hmm ?

The project is designed for a different type of board so this part would need to be differentā€¦

#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>

But, it doesnā€™t look like the code does what you wnat anyway.

Pete.

1 Like

And what ? nobody knows how to create flashing led when I turn on button ?
Only what I can do to connect this module (project) : https://www.youtube.com/watch?v=VXAFg091lpM
but this is not solution for me because this module it takes a lot of space in my toy and

As I said before, people are going to be happy to give some assistance, but so far youā€™ve just confused the situation by posting code that doesnā€™t do what you want (or so it appears).
Without understanding exactly what it is that youā€™re trying to achieve, itā€™s difficult to guide you.
Are you simply wanting to simulate left and right turn indicators named on the press of a couple of buttons in the app?
What about hazard lights, and headlights, and stop/tail lights, front/reat foglights etc etc?
What sort of duty cycle is needed for the indicators?
What other functionality are you looking for?

Until you stop searching the internet for irrelevant code and start focussing on te desired functionality then people arenā€™t going to chip-in with ideas.

Pete.

Hmmā€¦ i need simple example, code or maybe tutorial how to create blink led when i push or switch button in app (blynk), 1 button left sigal and 2 button right signal ā€¦ when i see this example maybe in the future i create another lights just like police strobe lights or something like that. :wink:

okay, here you goā€¦

#define BLYNK_TEMPLATE_ID          "REDACTED"
#define BLYNK_TEMPLATE_NAME        "REDACTED"
#define BLYNK_AUTH_TOKEN           "REDACTED"

#define BLYNK_PRINT Serial

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

// Your WiFi credentials.
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

int blynk_led_switch = 0;

BlynkTimer timer;

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

  pinMode(2,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500, toggle_led); // timer to toggle the LED every 500ms
}


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

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(2);
    digitalWrite(2,!current_state);
  }
}

BLYNK_CONNECTED() // Triggered whne the device connects or re-connects to Blynk
{
  Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup
}

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
{
  blynk_led_switch = param.asInt();
}

If you create a V1 datastream as an integer, and attach a button widget to it, in Switch mode, the add your own firmware configuration and WiFi credentials to the sketch it will flash the onboard LED when the button widget is ON.

Pete.

OMG Peteā€¦ finally some progress :wink: but i see strange thing ā€¦ when led shines and i turn off buttonā€¦ then led still shinesā€¦ so i must wait and click button when led does not light upā€¦ then led is disabled.

Okay, try this version of toggle_led insteadā€¦

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(2);
    digitalWrite(2,!current_state);
  }
else
  {
    digitalWrite(2, HIGH); // turn the LED off
  }
}

Pete.

Hahaā€¦ now it is working but something has must to be changed because I have 2 led, one led in module (d1 mini) and second led connect to pin D4ā€¦ and if i turn ON button led blink:
blue ON/ D4 Offā€¦ blue Off/ D4 ONā€¦ect and if i turn button again (off) then blue led in module off but D4 is Onā€¦ i need reverseā€¦ when i turn button (off) then D4 need offā€¦
or maybe to change code to both led offā€¦ :slight_smile:

(someone somewhere show how to change reverse led on/off , may change the value 1/0 to 0/1)
maybe: int blynk_led_switch = 0; <----- no 0ā€¦ maybe change to 1 and change if(blynk_led_switch == 1) <------no 1 ā€¦ change to 0

It depends how youā€™ve wired your LED.
The onboard LED is active LOW, because the positive side is always connected to VCC and the negative side is switched via GPIO2

if you want to use active HIGH LEDs then you need to change this to LOWā€¦

But, this is what I was saying about explaining your requirements/expectations to begin with. You wanted a basic example and thatā€™s what I provided. What you actually wanted was something else.

Pete.

it works fine now so I was wrong, I thought before when will I change values 1 to 0 or 0 to 1 everything will work as it does now. But it was enough to change high to low if i want reverse led off in pin d4.
Now Iā€™m a bit wondering what I need to change to make the two LEDs flash simultaneously and not alternately.

Edit:
Pete. Thank you very much for helping me learn how to flash the LED

aaaa Iā€™m also curious if flashing the Led only works forā€¦GPIO2 ? if I want to change to GPIO16 (D0) in sketch arduino ide and Blynk ā€¦then the LED will flash D0 ?
is the flashing only on to GPIO2 ?

Edit:
maybe I made a mistake when I created the templateā€¦ maybe I should have chosen digital pin and not virtual pin. does this cause the pin designations to changeā€¦ I may have to check it

Iā€™m not going to get sucked in to a series of incremental feature requests like this.
I said aright at the beginning that your requirement was too vague, and that you needed to be more specific. But, you insisted that you just wanted a basic example.

Writing a sketch is like building a house. If you donā€™t know what the finished item will look like then you donā€™t know what the foundations should look like, or where the utilities need to be.

If you take time to explain your end goal then Iā€™ll help you to get there, but drip feeding requirements doesnā€™t work for me.

Pete.

ok. I understand :wink:
Iā€™m just curious about the answers to various questions that will get me closer to my goal fasterā€¦
of course itā€™s a bit frustrating when I often answer questions from other people like me who have little knowledge of arduino, blynkā€¦ ect
so anyway tnx for your time and help. :wink:

hello againā€¦ i try to duplicate blink effect but i have problemā€¦
i copy and paste line by line code thinking it would work. Unfortunately it doesnā€™t work because when I paste the last command :BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state. I have error " exit status 1"

type or paste code here#define BLYNK_TEMPLATE_ID          "REDACTED"
#define BLYNK_TEMPLATE_NAME        "REDACTED"
#define BLYNK_AUTH_TOKEN           "REDACTED"

#define BLYNK_PRINT Serial

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

// Your WiFi credentials.
char ssid[] = "REDACTED";
char pass[] = "REDACTED";

int blynk_led_switch = 0;

BlynkTimer timer;

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

  pinMode(2,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  pinMode(3,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500, toggle_led); // timer to toggle the LED every 500ms
}


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

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(2);
    digitalWrite(2,!current_state);
    digitalWrite(3,!current_state);
  }
}

BLYNK_CONNECTED() // Triggered whne the device connects or re-connects to Blynk
{
  Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup
  Blynk.syncVirtual(V2);  // Get the state of the V1 widget on startup
}

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
BLYNK_WRITE(V2) // Triggered when the switch widget attache to V2 changes state

{
  blynk_led_switch = param.asInt();
}

Of course I changed the values ā€‹ā€‹so that they are not the same (V2, pinmode3 and digitalWrite(3,!current_state); )

BLYNK_WRITE(V1) needs to be followed by an opening and closing curly bracket, and preferably some code to be executed when the BLYNK_WRITE(V1) callback function is triggeredā€¦

BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
{
  // some code in hereā€¦
}


BLYNK_WRITE(V2) // Triggered when the switch widget attache to V2 changes state
{
  blynk_led_switch = param.asInt();
}

Pete.

firstly. A big thank for you Mr. Pete. (again)ā€¦ and unfortunately I need support again.
i have this code (I modified it a bit)
:

#define BLYNK_TEMPLATE_ID "TMPL4mVINjPLS"
#define BLYNK_TEMPLATE_NAME "lipa"
#define BLYNK_AUTH_TOKEN "JyB55r8vdWNlBZWmZNC3-l6oLLcT06V0"

#define BLYNK_PRINT Serial

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

// Your WiFi credentials.
char ssid[] = "blalalala";
char pass[] = "blalalalala";

int blynk_led_switch = 0;

BlynkTimer timer;

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

  pinMode(16,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  pinMode(5,OUTPUT); // Onboard LED of the Wemos D1 mini is connected to GPIO2
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(500, toggle_led); // timer to toggle the LED every 500ms
}


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

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(16);
    digitalWrite(16,!current_state);
    digitalWrite(5,!current_state);
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}


BLYNK_CONNECTED() // Triggered whne the device connects or re-connects to Blynk
{
  Blynk.syncVirtual(V0);  // Get the state of the V0 widget on startup
  Blynk.syncVirtual(V1);  // Get the state of the V1 widget on startup
}

BLYNK_WRITE(V0) // Triggered when the switch widget attache to V0 changes state
{
  // some code in hereā€¦
}


BLYNK_WRITE(V1) // Triggered when the switch widget attache to V1 changes state
{
  blynk_led_switch = param.asInt();
}

ā€¦ now i have 2 button (app) and 2 led connect to pin D0 and D1 and if i clik one button nothing happens but if i click second button two led bilinking. So the problem is in this lines (probably)
:

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(16);
    digitalWrite(16,!current_state);
    digitalWrite(5,!current_state);
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}

because if I copy and paste

int current_state = digitalRead(16);

just like that :

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(16);
    digitalWrite(16,!current_state);
    int current_state = digitalRead(5);
    digitalWrite(5,!current_state);
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}

or

void toggle_led()
{
  if(blynk_led_switch == 1)
  {
    int current_state = digitalRead(16);
    int current_state = digitalRead(5);
    
    digitalWrite(16,!current_state);
    digitalWrite(5,!current_state);
  }
  else
  {
    digitalWrite(16, LOW); // turn the LED off
    digitalWrite(5, LOW); // turn the LED off
  }
}

then i have error : "Compilation error: redeclaration of ā€˜int current_stateā€™

and i donā€™t know whay.

i try create 1 button one blinking led, 2 button second blinking led

(I hope I didnā€™t make a mess in this message)

Because youā€™re declaring an integer variable called ā€œcurrent_stateā€ then immediately re-declaring another integer variable with exactly the same nameā€¦

Pete.