Keep getting invalid auth token

I keep getting "invalid auth token when trying to connect to Blynk server after a small software change, allthough previously the program ran fine. The program controls an arduino uno to control a heater project. I copied the data from the blynk console and incorporated them at the start of the program. I iinclude the start of my program.I can give the whoile program but it is rather lengthy. Any clue what has changed since a couple of months since it was running fine than.

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/ done, no solution
  3. Add details :
    • Arduino UNO with Ethernet Shield
    • Smartphone OS iOS + iphone SE version 15.1
    • Blynk server
    • Blynk Library version 0.6.1 by volodymyr
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

/*
 * rev 4: new blynk module
 * rev 5: remote reset added
 * rev 6: temperature bypass button added in case temp reading fails
 * rev 7: eliminating 30° test as dallastemp allways reports 0 after a while
 * 
 * Central heating Prégermain
 * 
 * Gets a signal from the nest heatlink and start heater and circulation pump. 
 * If heatlink sends a stop signal , turns off the circulation and stops the heater after a while
 * 
 * The circuit:
  - heatlink switch attached to pin 6
  - start stop circulation pump on pin 5
  - start stop heater on pin 3
  - heater temperature onewire DS18B20 on pin 2
  */
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial  
#define BLYNK_TEMPLATE_ID "TMPL0e7VvgzM"
#define BLYNK_DEVICE_NAME "Prégermain Heater Control"
#define BLYNK_AUTH_TOKEN "XqXokF-XkYGjdXSjEbqlDkZSZiEH2Ypq"

//  include onwire libraries
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
//DeviceAddress insideThermometer = {0x28, 0x63, 0xCB, 0x79, 0x97, 0x00, 0x03, 0x5D };  // mac address of temperature probe DS18B20 at PG
//DeviceAddress insideThermometer = {0x28, 0x56, 0xE9, 0x79, 0x97, 0x00, 0x03, 0x20 };    // mac address of temperature probe DS18B20 home

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

//include blink libraries
#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[] = BLYNK_AUTH_TOKEN;
//char auth[] = "XqXokF-XkYGjdXSjEbqlDkZSZiEH2Ypq";
//char auth[] = "dn-rx1VqwK_7gCWouL8Bue6Kpsbe5m0I"; //testopstelling thuis (old blynk)
//char auth[] = "M2KWX7fILaf9VUhx9X_PbsD-CAV1Izq1"; //installatie PG (old blynk)

#define W5100_CS  10
#define SDCARD_CS 4

WidgetLED circulatiepomp(V2);
WidgetLED ketel(V3);

BlynkTimer timer;               // main timer to control heater/blynk communication/connection handling

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"
#define BLYNK_YELLOW    "#FBAD00"

// define input /outputs
#define heatlink  6      // heatlink input
#define ststcirc  5      // Start stop circultation pump on pin 5
#define ststheat  3      // Start sto of heater on pin 3
#define heartbeat 7      // heartbeat pin in pin 7
#define echoPin   8      // attach pin D8 Arduino to pin Echo of HC-SR04
#define trigPin   9      //attach pin D9 Arduino to pin Trig of HC-SR04

// variables

int noconnectcount=0;             //  counter when not connected
int resetcount=0;                 //  counts the number of reset button presses
int mazouttlevel = 0;             //  initialise level of mazouttank
bool heatlinkstate = false;       //  status van nest heatlink      
int flag_circ = false;            //  flag to start circulation pump 
int flag_heater = false;          //  flag to start heater
bool restart_inhibit = true;      //  flag (true) not to restart heater immediately, false heater can start
bool stopflag = false;            //  flag (true) to indicate heater was started and should not restart immediately after stop
unsigned long stop_time = 0;      //  indicates heater reset or heater stop time

//initialise Virtual pins

int automanueel = 0;        // blynk selector auto in auto (V8)
int tbypass = 0;            // pushbutton to bypass Temp reading
int flag_mazoutt = 0;       // blynk flag to measurelevel mazouttank  

float tempC = 0;
float tempsaved = 0;

void setup() {
  
  // initialise inputs-outputs
  pinMode(ststcirc, OUTPUT);
  pinMode(ststheat, OUTPUT); 
  pinMode(heatlink, INPUT);
  pinMode(heartbeat, OUTPUT);
  pinMode(echoPin, INPUT); 
  pinMode(trigPin, OUTPUT);

  digitalWrite(ststcirc,LOW);    // stop circulationpump
  digitalWrite(ststheat,LOW);    // stop heater
  digitalWrite(heartbeat,LOW);   // set heartbeat low
  digitalWrite(trigPin, LOW);   // set trigpin low to start

  restart_inhibit = true;       // set flag true so heater cannot start  immediately after reset
  stopflag = false;             // set flag false to indicate heate was not yet started
  stop_time = millis();         // save stop time, 0 after reset
  resetcount = 0;               // initialise resetcounter to 0
  
  // initialise serial communication
  
   Serial.begin(9600);

   pinMode(SDCARD_CS, OUTPUT);
   digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

   //Blynk.begin(auth);
    byte mac[]= {0xDE,0xAD,0xBE,0xEF,0xFE,0xEF};
  
    if (Ethernet.begin(mac) == 0)
    //Ethernet.begin(mac);
    {
     Serial.println("Failed to configure ethernet using DHCP");
    }
    
    Blynk.config(auth);
    Blynk.connect();

   // Setup a function to be called every 5 seconds
   timer.setInterval(5000L, Heatercontrol);
   // Setup timer for communication with blynk
   timer.setInterval(5500L, blinkcomm);
   // Setup a function to be called every 10 seconds for connection control and watchdog feeding
   timer.setInterval(10000L, heaterreset);
   // Setup a function to be called every minute to check if mazouttank level has to be measured
   timer.setInterval(60000L, mazouttank);
   // Setup a function to be called every 1,5 minute to check if distance reset has to be given
   timer.setInterval (90000L, distancereset);
   
   // Start up the library (one wire)
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  //sensors.setResolution(insideThermometer, 10);
  sensors.setResolution(9);
  sensors.requestTemperatures();
  //tempC = sensors.getTempC(insideThermometer);
  tempC = sensors.getTempCByIndex(0);
  if (tempC == -127.00) 
  {
    Serial.println("Error getting temperature");
  }
  tempsaved = tempC;
  Blynk.setProperty(V5, "color", BLYNK_GREEN);
  
}   // end void setup

// define actions on virtual pins

  // if resetbutton pressed increment resetcounter
  BLYNK_WRITE (V0)
  {
  if (param.asInt())
    {
      resetcount = resetcount+1;
    }
  } 
 // read manual auto selector
  BLYNK_WRITE (V8)
  {
  if (param.asInt())
    {
      automanueel = !automanueel; //toggle flag
    }
  }
  // read temp bypass button
  BLYNK_WRITE (V1)
  {
  if (param.asInt())
    {
      tbypass = !tbypass; //toggle flag
    }
  }
  // read ketel push button
  BLYNK_WRITE (V7)
  {
  if (param.asInt())
    {
      flag_heater = !flag_heater; //toggle flag
    }
  }
  // read circulatie push button
  BLYNK_WRITE (V6)
  {
  if (param.asInt())
    {
      flag_circ = !flag_circ; //toggle flag
    }
  }
  // read flag to determine if level mazouttank has to be measured
  BLYNK_WRITE (V9)
  {
  if (param.asInt())
    {
      flag_mazoutt = !flag_mazoutt; //toggle flag
    }
  }
// end definition of actions
 
void Heatercontrol() {

  // set elapsed time to delay restart heater after reset or stop to two minutes
  
  const unsigned long elapsed = (1000UL*60*2); 

  // set restart_inhibit flag false after elapsed time
   
  if (restart_inhibit==true)
  {
    Serial.println(millis()- stop_time);
    if (millis()- stop_time > elapsed)
    {
      restart_inhibit=false;
    }
  }
  
  // read heater temperature and convert to real temp

  sensors.requestTemperatures();
  //tempC = sensors.getTempC(insideThermometer);
  tempC = sensors.getTempCByIndex(0);
  if (tempC == -127.00) 
  {
    Serial.println("Error getting temperature");
  } else 
  {
    Serial.print("C: ");
    Serial.println(tempC); 
  }
  
  // if keuzeselector is auto
  
  if (automanueel==false)
  
  {
     
    // read heatlinkstate and check if heatlink sends start signal

    if (digitalRead(heatlink) == HIGH)
    { 
      flag_heater = true; // set flag heater
      flag_circ = true; // set flag circulation pump 
      
      // check heater temperature
      //if ((tempC > 30)||(tbypass == true))
      //{
        //while(1);
       // flag_circ = true; // set flag circulation pump       
      //}
      //else 
      //{    
        //flag_circ = false; // clear flag circulation pump 
      //}
    }
    else 
    {
      flag_circ = false;    // clear flag circulation pump
      flag_heater = false;  // clear flag heater
    }
  }
  // Start or stop heater and circulation pump according to flags
  
  if (flag_heater == true)                  //if start heater condition is true
  {
    if (restart_inhibit==false)             // and inhibit is not valid
    {
      digitalWrite (ststheat,flag_heater);  // then start heater
      stopflag = true;                      // and set flag when heater will be stopped
    }
  }
  else 
  {
    digitalWrite (ststheat,flag_heater);    // else stop heater
        
    if (stopflag == true)                   // if heater has been started
    {
     restart_inhibit = true;                // set restart_inhibit ture not to restart heater immediately
     stop_time = millis();                  // take time stamp to calculate delay
     Serial.println(stop_time);
     stopflag = false;                      // clear flag to execute this once 
    }
  }
  //digitalWrite (ststheat,flag_heater);  
    
  digitalWrite (ststcirc,flag_circ);
  
  //Serial.print("ketelstst:" );
  //Serial.println (flag_heater);
  //Serial.print("ciculatiestst:" );
  //Serial.println (flag_circ);
}
void mazouttank(){
  long duration; // variable for the duration of sound wave travel
  float distance; // variable for the distance measurement

  if (flag_mazoutt == true)    //if flag true, level will be measured
  {
    for (int i = 0; i <= 5; i++) 
    {
      // Clears the trigPin condition
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      // Reads the echoPin, returns the sound wave travel time in microseconds
    }
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2.0; // Speed of sound wave divided by 2 (go and back)
    //mazouttlevel = 137 - distance ;
    mazouttlevel = round(134 - distance) ;
  // Displays the distance on the Serial Monitor
  //Serial.print(distance);
  Serial.print(mazouttlevel);
  Serial.println(" cm");
 }
  flag_mazoutt = false;   //reset flag
}
void blinkcomm() {
  if (Blynk.connected())              // test is arduino still connected to blynk server
  {
    Blynk.virtualWrite(V8, automanueel);  //update auto manueel selector
    Blynk.virtualWrite(V6, flag_circ);    //update circulation pushbutton    
    Blynk.virtualWrite(V7, flag_heater);  //update heater pushbutton
    Blynk.virtualWrite(V1, tbypass);      //update tbypass pushbutton

    
    if(automanueel==true && digitalRead(ststheat) == false && digitalRead(ststcirc) == false)  // if reset conditions are met, set color to red
    {
     Blynk.setProperty(V0, "color", BLYNK_RED);
     Blynk.setProperty(V0, "offLabel", "Ja    "+String(resetcount));  //show resetcounter in resetbutton
    }
    else
    {
     Blynk.setProperty(V0, "offLabel", "Neen  "+String(resetcount));  //show resetcounter in resetbutton 
     if (resetcount == 0)                                         //if resetcounter zero set collor of button to green
      {
        Blynk.setProperty(V0, "color", BLYNK_GREEN);      
      }
      else                                                         //set color to yellow
      {
        Blynk.setProperty(V0, "color", BLYNK_YELLOW);     
      }     
    }    
    if (restart_inhibit==false)
    {
     Blynk.setProperty(V7, "color", BLYNK_RED); 
    }
    else
    {
      Blynk.setProperty(V7, "color", BLYNK_YELLOW);
    }
        
    //set circulation led according to flag
    if (flag_circ == true)
    {
      circulatiepomp.on(); 
    }
    else
    {
      circulatiepomp.off();
    }
    //set heater led according to heater status
    if (tempC-tempsaved > 0.25 && digitalRead (ststheat)==true)
    {
     ketel.on();
    }
    if (tempC-tempsaved < 0)
    {
     ketel.off();
    }
    tempsaved = tempC;
    
    // set color of gauge depending on heatlinkstate (red if on, green if off)
    if (digitalRead(heatlink) == HIGH)
    {
    Blynk.setProperty(V5, "color", BLYNK_RED);  
    }
    else
    {
      Blynk.setProperty(V5, "color", BLYNK_GREEN);
    }
    //update temperature gauge in Blynk
    if (tbypass == false)
    {
      Blynk.virtualWrite(V5, tempC); 
    }
    else
    {
      Blynk.virtualWrite(V5, 0); 
    }
    
    // send level mazouttank to blynk app
    //Blynk.setProperty(V9, "onLabel", String(mazouttlevel)+ " cm");
    Blynk.setProperty(V9, "offLabel", String(mazouttlevel*24)+ " l   /    "+String(mazouttlevel)+ " cm");
  
  }
}
void distancereset(){
  if (automanueel==true)        // check if in manual mode
  {
   if (digitalRead(ststheat) == false)  //check if ketel off
   {
    if (digitalRead(ststcirc) == false)  // check circulation off
    {
     if (resetcount >= 2)               // check if resetbutton has been pressed at least twice
     {
      Serial.print("resetting by stalling");
      //asm volatile ("  jmp 0");       // restart arduino
      while(1);                         // loop eternal to activate watchdog  
     }
    }
   }
  }
 resetcount = 0;              // initialise resetcounter
}
void heaterreset() {
  const int maxnoconnect = 2750;   // maximum time no internet connection 12h*60min*19pulses on 5 minutes
  if (!Blynk.connected())              // test is arduino still connected to blynk server, if not ...
  {
   noconnectcount=noconnectcount+1; // increment reset counter 
   if ((noconnectcount % 7) == 0) // check per minute (7-1)*10 sec
   {
    byte mac[]= {0xDE,0xAD,0xBE,0xEF,0xFE,0xEF};
    Ethernet.begin(mac) == 0;         // try and reconnect ethernet
    bool result = Blynk.connect();   // try and reconnect to blynk
   }
  }
  else
  {
   noconnectcount=0;                // yes, reset counter 
  } 
  Serial.print("connection count = ");
  Serial.println(noconnectcount);
  
  // test if 
  if (noconnectcount < maxnoconnect)           // 12 uur 
  {
   // send heartbeat on 
   send_heartbeat();    //reset watchdog
  }
}

void send_heartbeat(){
    digitalWrite(heartbeat,HIGH);  // Heartbeat sent to watchdog
    delay(10);              // can be as low as 50 microsecond
    digitalWrite(heartbeat,LOW);   // pull the heartbeat pin low again
    Serial.println("Heartbeat");
}   

void loop() {  

 Blynk.run();
 timer.run(); // Initiates BlynkTimers
 
}


…

@Robert please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

Editing seem not to work ??? I’ll try again. Hope this is better. So I get an invalid auth token message and a no connection to the Blynk server. Previously, this ran fine. Iincorporate only the beginning until the program tries to connect to the Blynk server. I do not want to use the Blynk.begin command since this blocks if the connection fails.

[Unformatted code removed by moderator]

In what way?
If you cant find the pencil icon then click the three dots to reveal it.

No, despite the text that was present when you first created this project showing you how to use triple backticks, and me subsequently giving you triple backticks to copy/paste, you’ve used three commas instead.

I’ve deleted your second piece of unformatted code.
Please go back and edit your original post and place the correct triple backtick characters at the beginning and end of your code.
Also, these triple backticks need to be on a separate line.

When you’ve done this, I’ll engage in a conversation with you about your issue.
But I’ll give you a hint - the first thing I’ll be asking you for is the information that you were requested to supply when you first created this topic.

Pete.

I’m sorry , Pete. I should read your replies better. Hope this time it is ok ?
About your hint ? I missed something, that’s clear

No, it is not.

Do you know how to copy and paste?

Go through the process as if you were going to create a new topic with the category of “Need help with my project”. You’ll see the information that is requested, and which you haven’t included.
Abandon that topic creation process, then when you’re editing your original post you can add-in the missing information at the same time that you’re fixing your code formatting.

Pete.

That’s your problem.
You should be using version 1.0.1

The old (Legacy) library version 0.6.1 is totally unaware of the new IoT servers and doesn’t have any idea what to do with the Template ID info that you’ve added to the beginning of your sketch.

The 1.0.1 library will see the Template ID data and understand that it needs to use blynk.cloud as the server url, rather than the legacy blynk-cloud.com url.
Your IoT project, and it’s corresponding Auth tokens, only exist on the blynk.cloud server, which is why the legacy server is reporting that the Auth token you are using is unknown.

Pete.

Hi Pete,

Thanks for your reply. Indeed I was using the old Blynk Library. I did an update to version 1.0.1 and recompiled the program. After uploading though the program reconnected several times to the version 0.6.1 and thus generaring the same error invalid auth token.
Any clues why ? When I lookup in the installed librariies it says version 1.0.1 is installed???

Rob

What exactly does this mean?

What does your serial monitor show? (Copy/paste the text from your serial monitor and use triple backticks as you did with your sketch).

Pete.

Hi Pete,

The application, I 'm running here is for a holidayhome I have in France. I can control the central heating from home in Belgium and even measure the level of fuel in the tank. It all worked fine until short.
The issues I’m facing here are mainly unreliable intenet connections, so I programmed the application to deal with this and continue to control the heating even without internet connection.
A couple of days ago I made a small change in the software and suddenly the program would no longer reconnect to the new blynk server. So suddenly two unpredictables and in fact I need the application running, because we will leave shortly.
But anyway, you set me on the right track, I believe, because indeed the app tries and connect to the wrong server. So today I updated the blynk library to version you mentionned, but no change.
What I did today, I rebuilded the app in the old blynk mode and it is running now. I know it is a step back, but my firsrt priority was to get it running.
I will test the app with the new blynk server on a test system at home to solve the problem and I update the software in my holidayhome once it is working again. I will keep you posted.
The odd thing I found this evening was: in the arduinofolder on my laptop I found two Blynk libraries (folders). One was named blynk0.6.1, the other just blynk.
I deleted the 0.6.1 folder and opened the arduino ide again. It showed me that the blynk 1.0.1 was still installed. Maybe there lies the problem: the double blynk libraries ???
But as I said I will test this when I’m home , probably net week.

Thank you anyway for your support so far.

Rob

When you compile the code it will tell you the path and version of each library that is used, although you may need to turn on verbose compiler messages in Preferences.

This will allow you to understand exactly which Blynk library is being used.

I’d strongly suggest that you look at migrating your project to either a NodeMCU or ESP32 based system.
This, in conjunction with the Blynk Edgent sketch would allow you to remotely update the code running on your device using Blynk.Air.

I have a similar system running at my holiday home in Spain and am fully aware of the benefits (and hassles) that go with this arrangement.
Having a duplicate system at home certainly helps with testing critical updates before deploying them, and simple tricks like having the same WiFi SSID and password at both locations can make remote updates less likely to end in disaster.

Pete.

Hi Pete,

I come back to my former problem of getting the invalid auth token error. I tested the program at home on a test system and it works perfectly, even with the same laptop I had in my holiday home. So you were right : the wrong blynk library was selected, even after updating this library. The update apparently was installed in a different folder, and the arduino IDE continued to select the old library, thus generating the same error. Only after deleting the old library folder , it selected the right one.

Thanks again for your advice. It was clearly the one over my shoulder looking at it , who got me out of my blind spot. :slight_smile: :wink:

1 Like