AC Dimmer ESP8266, Invalid Auth token

Hi, I am working on a AC dimmer project using NodeMCU with Blynk. I have setup the circuit and added the code to the micro-controller. Now the board is connecting to the Wifi but Blynk IoT app shows the device is offline and serial monitor is giving the error “Invalid Auth Token”. I have tried to fix it using the related topics already addressed but those are not working for me. I am pretty new to this stuff. Therefore, your help is much appreciated. Thankyou.

Here is the code, I am using. Kindly check.

#include <Stepper.h>

/*
   This is the code for AC Dimmer using Arduino Board.

   To watch it's full tutorial video, head on to my YouTube Channel

   http://www.youtube.com/techiesms

*/


#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL8ThXENcI"
#define BLYNK_DEVICE_NAME "AC dimmer"

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

#define triacPulse 4 //D2
#define ZVC 12 //D6

int Slider_Value;
int dimming;
int x = 0;


char auth[] = "eMxFT2EHj2syTEt2KDPLT2vB96Gp93O1";        // You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon) in the Blynk app or check your email for auth token.

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


BLYNK_WRITE(V1)   // function to assign value to variable Slider_Value whenever slider changes position
{
  Slider_Value = param.asInt(); // assigning incoming value from pin V1 to a variable
}


void setup()
{

  pinMode(ZVC, INPUT_PULLUP);
  //digitalWrite(2, INPUT_PULLUP); // pull up
  pinMode(triacPulse, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com");
  attachInterrupt(digitalPinToInterrupt(ZVC), acon, FALLING); // attach Interrupt at PIN2
}



void loop()
{
  Blynk.run();
  // When the switch is closed
  dimming = map(Slider_Value, 0, 100, 7200, 200); //0.2ms 7.2 ms

}

void acon()
{
  // Serial.println("REad");

  delayMicroseconds(dimming); // read AD0
  digitalWrite(triacPulse, HIGH);

  delayMicroseconds(50);  //delay 50 uSec on output pulse to turn on triac
  digitalWrite(triacPulse, LOW);

  // Serial.println(digitalRead(triacPulse));
}

Are you using the latest version of blynk library ( 1.1.0 ) ?

Must be on the top of your sketch, the first lines.

blynk-cloud.com” is the old blynk server, the new blynk server is “blynk.cloud”

Thanks @John93, it is showing online in the app but serial monitor is still running “connecting to wifi” and the bulb also didn’t light up. Here is the image of the serial monitor. Kindly check.

first of all, you should read this
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Sorry John, I am pretty new to Arduino coding. Can you please clarify a bit. Am I supposed to add BlynkTimer to the code. Or can you provide a sample for my case. Thankyou

Yes.

When you’re posting serial monitor output it’s always best to copy that text and paste it between triple backticks (the same as when you post code) rather than posting a screenshot.

You’re getting an “ISR not in IRAM!” error, because your sketch was written for an earlier version of the ESP8266 core.

To solve this you should add this line of code after the BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME lines of code, but before your void setup…

void IRAM_ATTR acon();

Pete.

@AhmadR Hello,

The code what you are using is not stable. Even i used the same a while ago and there was frequent disconnect issue / crash.

This code may work properly on a arduino uno or nano. But not on esp8266. I tried

Adding this line like @PeteKnight said ! But this did not work too.

This is something to do with the core we are using. Core 2.4.2 works ok with this code, but blynk iot needs a higher core to work.

So i had to drop this code.

Thankyou @PeteKnight, but even after adding void IRAM_ATTR acon();, it is still not working.

Thanks @Madhukesh for helping. You are right it’s working with core 2.4.2 but blynk needs higher one. Kindy, can you tell me how did you fix it or if there is an other piece of code which is compatible with latest ESP8266 core and blynk. Thankyou.

Post your latest code and serial monitor output.

Pete.

Here is it.
Code

#include <Stepper.h>

/*
   This is the code for AC Dimmer using Arduino Board.

   To watch it's full tutorial video, head on to my YouTube Channel

   http://www.youtube.com/techiesms

*/


#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL8ThXENcI"
#define BLYNK_DEVICE_NAME "AC dimmer"

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

#define triacPulse 4 //D2
#define ZVC 12 //D6

int Slider_Value;
int dimming;
int x = 0;


char auth[] = "eMxFT2EHj2syTEt2KDPLT2vB96Gp93O1";        // You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon) in the Blynk app or check your email for auth token.

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


BLYNK_WRITE(V1)   // function to assign value to variable Slider_Value whenever slider changes position
{
  Slider_Value = param.asInt(); // assigning incoming value from pin V1 to a variable
}


void setup()
{

  pinMode(ZVC, INPUT_PULLUP);
  //digitalWrite(2, INPUT_PULLUP); // pull up
  pinMode(triacPulse, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk.cloud");
  attachInterrupt(digitalPinToInterrupt(ZVC), acon, FALLING); // attach Interrupt at PIN2
}



void loop()
{
  Blynk.run();
  // When the switch is closed
  dimming = map(Slider_Value, 0, 100, 7200, 200); //0.2ms 7.2 ms

}

void acon()
{
  // Serial.println("REad");

  delayMicroseconds(dimming); // read AD0
  digitalWrite(triacPulse, HIGH);

  delayMicroseconds(50);  //delay 50 uSec on output pulse to turn on triac
  digitalWrite(triacPulse, LOW);

  // Serial.println(digitalRead(triacPulse));
}

Serial Monitor

#StandWithUkraine    https://bit.ly/swua


[7915] Connecting to blynk.cloud:80
[8170] Ready (ping: 86ms).
ISR not in IRAM!

User exception (panic/abort/assert)
--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Abort called

>>>stack>>>

ctx: cont
sp: 3ffffee0 end: 3fffffc0 offset: 0000
3ffffee0:  00000000 4bc6a7f0 6c8b4395 0025335d  
3ffffef0:  000000fe 00000000 00000000 00000000  
3fffff00:  00000000 00000000 00000000 00ff0000  
3fffff10:  5ffffe00 5ffffe00 00000000 00001eea  
3fffff20:  00000000 00000002 0000000c 402045aa  
3fffff30:  40100525 0000202c 3ffeeb64 402045bc  
3fffff40:  0000464f 3ffeeb64 0000000c 40204afd  
3fffff50:  00000000 3ffeeb40 00000000 00001eea  
3fffff60:  0000464f 3ffeeb40 00000000 40204b9c  
3fffff70:  0000464f 3ffeeb40 00000000 40201fc2  
3fffff80:  402075cc 532ba8c0 feefeffe feefeffe  
3fffff90:  feefeffe feefeffe feefeffe 3ffeed14  
3fffffa0:  3fffdad0 00000000 3ffeed00 402040e4  
3fffffb0:  feefeffe feefeffe 3ffe8610 40100de1  
<<<stack<<<

--------------- CUT HERE FOR EXCEPTION DECODER ---------------
⸮⸮⸮[65] Connecting to ahmad
[7791] Connected to WiFi
[7791] IP: 192.168.43.83
[7791]

Have you posted the wrong sketch, or just ignored my advice…

Pete.

Oh sorry, it was by mistake. Actually I was trying it on latest core as it didn’t work on 2.4.2. Here is the latest one.
Code

#include <Stepper.h>

/*
   This is the code for AC Dimmer using Arduino Board.

   To watch it's full tutorial video, head on to my YouTube Channel

   http://www.youtube.com/techiesms

*/


#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "TMPL8ThXENcI"
#define BLYNK_DEVICE_NAME "AC dimmer"
void IRAM_ATTR acon();

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

#define triacPulse 4 //D2
#define ZVC 12 //D6

int Slider_Value;
int dimming;
int x = 0;


char auth[] = "eMxFT2EHj2syTEt2KDPLT2vB96Gp93O1";        // You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon) in the Blynk app or check your email for auth token.

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


BLYNK_WRITE(V1)   // function to assign value to variable Slider_Value whenever slider changes position
{
  Slider_Value = param.asInt(); // assigning incoming value from pin V1 to a variable
}


void setup()
{

  pinMode(ZVC, INPUT_PULLUP);
  //digitalWrite(2, INPUT_PULLUP); // pull up
  pinMode(triacPulse, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk.cloud");
  attachInterrupt(digitalPinToInterrupt(ZVC), acon, FALLING); // attach Interrupt at PIN2
}



void loop()
{
  Blynk.run();
  // When the switch is closed
  dimming = map(Slider_Value, 0, 100, 7200, 200); //0.2ms 7.2 ms

}

void acon()
{
  // Serial.println("REad");

  delayMicroseconds(dimming); // read AD0
  digitalWrite(triacPulse, HIGH);

  delayMicroseconds(50);  //delay 50 uSec on output pulse to turn on triac
  digitalWrite(triacPulse, LOW);

  // Serial.println(digitalRead(triacPulse));
}

Serial Monitor

[4289] Connected to WiFi
[4289] IP: 192.168.43.83
[4289] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on ESP8266

 #StandWithUkraine    https://bit.ly/swua


[4415] Connecting to blynk.cloud:80
[4739] Ready (ping: 88ms).

@PeteKnight, but still it didn’t light up the bulb and blynk interface also doing nothing.

So, you no longer have the invalid Auth token message, or the ISR not in IRAM message, and your device is successfully connecting to Blynk.

I’d check how you’ve configured your V1 datastream - in particular the min/max values.

I’d also use-comment the serial print lines in the acon function to see if it’s actually being called,

You really should move this…

From your void loop into your BLYNK_WRITE(V1) function too.

Pete.

Thanks a lot @PeteKnight. Its working now. Still have some issue with dimming as it blinks sometime but that is I guess because of the void loop you already mentioned. I really appreciate your help. Thanks again.