Blynk.Edgent WiFi AutoConnect/AutoReconnect?

I finally got the Blynk.Edgent example integrated into my ESP32 project and it is working ok.
But, it is an automotive application, and I need a way to store The wifi credentials of different locations and be able to automatically reconnect to those Access points when in range.
Is there anyone who could point me to code that addresses this need?
I have searched, and can not seem to find anything.
If you need it, I can provide my Project Code.
Thank You for any help.


void loop()

Edgent doesn’t support multiple wifi credentials to be stored. Either you have to reconfigure the device every time(not a practical thing to do) or you can use 3rd party library to have this functionality.

But then you will not have the functionality of the edgent. You will have to manage everything on your code. You can search multiWifi esp32 and get lots of examples.

If you go down the WiFiMulti route then I would advise against doing this in the Edgent sketch.
Start with a non-Edgent sketch. Once you have that working with WiFi Multi then if you want to add-in the Blynk.Air functionality you can do this.

Pete.

I do not really have to have OTA function. But, how would I create a new device on the Blynk App if I use WiFiMulti? Would it still go into AP mode to allow you to Add New Device?

No, you create a new device in the web console, based on the template you’ve created.

Pete.

Doesn’t Blynk link your Auth code to the particular network your device is connected to?
Just curious, I am still learning.

The Auth token is the security token that relates to your hardware device.
When you use Edgent that is assigned dynamically, when you use normal Static Provisioning it is allocated when you create the device from a template.

It has no links whatsoever to the network and connection method you are using, except that in Edgent the Auth token is stored in EEPROM and so are your WiFi credentials- but the two aren’t connected in any way.

Pete.

Thank You.
But, just wondering why I have to pass the SSID and PASS to Blynk without Edgent?

You have to pass them with Edgent too, it’s just that this is done via the app, and they are stored in EEPROM rather than hard-coded in the sketch.

Pete.

I see. Do you know where I could find an example of how to pass the current ssid and password to Blynk?

The Blynk examples installed with the library, or the sketch builder link at the top of this page.

Pete.

Ok, Thank You.

I would like to add OTA via Blynk capability to my Blynk project which currently is set up to use WiFiMulti and Blynk.config for the auth code. My question, is it possible to include Blynk Edgent to this project and not lose the ability to connect to Blynk without passing an ssid & password?

Hope this is formatted correctly
And, it has not been cleaned up yet.

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <BlynkSimpleEsp32.h>


#define BLYNK_TEMPLATE_ID "*************"
#define BLYNK_TEMPLATE_NAME "ESP32 TRUCK"
#define BLYNK_AUTH_TOKEN "*********************************"
#define BLYNK_PRINT Serial
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define USE_ESP32_DEV_MODULE

#define rainLED 21 // Yellow
#define stallPin 32  //Blue/White
#define battV 39  //Orange
int updn=16;  //Green/Wht 
#define ign 17  // Gray 
#define rollupPin 18 //Green 
#define rolldnPin 23  //Blu/Wht

#define rainSensor 36 //Purple
#define DHTTYPE DHT11
#define DHTPIN 19  //Sensor Green

DHT dht(DHTPIN, DHTTYPE);

WiFiMulti wifiMulti;

char auth[] = BLYNK_AUTH_TOKEN;

int runOnce = 1; // starts void loop() once, upon loading program changes.
volatile int stopRoll = 0;
int trendCnt = 1;
int trendSet = 5; // length of time to develop trend
int baroTrig = 0;
int roll_pin = 0;
int waitFlag = 0;
int ventTime = 500; // set roll dn interval for vent
int venteD = 0;
volatile int up_dn = 0;
volatile int doneFlag = 1;
int raineD = 0;
int RS = 955;
int RSlow = 1045;  //lowest rain sensor value recorded
int RSthresh = 800; //rain sensor threshold
int RSoffset = 0;
int changed = 0;
int stallMid = 400;
int stallMax = 500;
int Maxhumidity = 0;
float Mintemp = 110;
float Maxtemp = 0;
int Hyst = 100; //rain sensor hysteresis setting
int humidHyst = 5; // Humidity sensor Hysteresis
int ct; // counter variable


float pressMax = 0; // variables for BMP sensor
float pressMin;
float humeCal = 1.5; // Humidity sensor calibration offset multiplier
float pressNow = 0;
float tempC = 0; 
float tempF = 0;
float tempCal = 0;

float stallValue  = 0;
float stallSet = 480; //threshold value for stall detect

float humid =85;
float tempSet = 50; //set temprature threshold in F
float battSet = 11.0; //battery alarm threshhold 
int Debug = 1; // set to 0 to skip delay

int EnableIntr = 1; // enable interrupts = 1
// float tempSet = ((ftempSet-32)/5)/9; //convert to C for bme085

unsigned long cntNow = 0; // VARIABLES FOR Millis()
unsigned long cntInit = millis();
unsigned long cntInterv = 300000; // 5 min.

unsigned long cntNow1 = 0; // VARIABLES FOR Millis(1)
unsigned long cntInit1 = millis();
unsigned long cntInterv1 = 86400000; // 24 hrs

unsigned long fanNow = 0; // VARIABLES FOR Millis(fan)
unsigned long fanInit = millis();
unsigned long fanInterv = 180000; // 1/2 hr

volatile int rain = 0;
float vout = 0.0;
float correctionfactor = -1.75;
float vin = 0;
float Vtot = 0;
int Vcc = 0;
//int vcc = 39;
int R1 = 68000; //volt div resistor
int R2 = 22000; //volt div resistor
int h = 0;
float t = 0;
volatile int test;
// Declare global variables for sensor data
int RAINSENSOR = 1023; 
float TEMP = 32;
float HUMIDITY = 0;
volatile int ROLLUP;
volatile int ROLLDN;
volatile int STALLVAL;
float BATTV = 13.5;
int RAINING;
int VENTED;
int tempValue;
volatile int UpDn;
int UPDN = 0;
int BATTALARM = 0;


 BLYNK_WRITE(V8)
{   
  tempSet = param.asInt(); // Get value as integer
  }

BLYNK_WRITE(V10)
{   
  humid = param.asInt(); // Get value as integer
}

BLYNK_WRITE(V12)
{   
  up_dn = param.asInt(); // Get value as integer
  RollPin();
}


BLYNK_CONNECTED() { // when device is connected to Blynk Cloud... 
//Blynk.syncVirtual(V8, V10,V12);
 Blynk.syncAll(); // request the values for all datastreams that has "sync" setting enabled 
  }

// This function creates the timer object. It's part of Blynk library 
BlynkTimer timer; 

void myTimer() 
{
  // This function describes what will happen with each timer tick
  // e.g. writing sensor value to datastream V5
  //VENTED=venteD;
  //Blynk.run();
  //timer.run();


  Blynk.virtualWrite(V0, RAINSENSOR);  
  Blynk.virtualWrite(V1, TEMP);
  Blynk.virtualWrite(V2, HUMIDITY);
  Blynk.virtualWrite(V6, BATTV);
  Blynk.virtualWrite(V4, ROLLUP);
  Blynk.virtualWrite(V5, ROLLDN);
  Blynk.virtualWrite(V3, STALLVAL);
  Blynk.virtualWrite(V7, RAINING);
  Blynk.virtualWrite(V11, BATTALARM);
  Blynk.virtualWrite(V9, VENTED);
  Blynk.virtualWrite(V13, WiFi.SSID());

  if (ct < 11) {
    Vtot = Vtot+vin;
    ct=ct+1;
  }
  if (ct==11) {
    ct=0;
  BATTV = Vtot/10;
  Vtot=0;
  }

}

void RollPin() // roll window up or down alternately
{
  //up_dn = !digitalRead(updn); 
  if (digitalRead(ign)==1 || stopRoll==0) goto endIt;  //if ign is ON or Motors active, exit RollPin, goto label
Serial.println("switch(rollpin) = 1 -press any key");
        //while (Serial.available()==0); {Serial.read();}
      
      switch(up_dn) // check up_dn flag
        {
          case 0:
          
          Serial.println("switch up/dn = 0 -press any key");
         // while (Serial.available()==0); {Serial.read();}
       Serial.println("Rolling UP");
       digitalWrite(rollupPin,HIGH);
       ROLLUP = 1;
       ROLLDN = 0;
       //venteD = 0;
       up_dn = 1;
       doneFlag = 0;
       stopRoll = 0;
       //while (Serial.available()==0); {Serial.read();}
       break;
          

          case 1:
        if(rain==0)  {
          Serial.println("switch up/dn = 1 -press any key");
         // while (Serial.available()==0); {Serial.read();}
        Serial.println("Rolling DN");
        digitalWrite(rolldnPin,HIGH); 
        ROLLUP = 0;
        ROLLDN = 1;
        //venteD = 0;
        up_dn = 0;
        doneFlag = 0;
        stopRoll = 0;
        //while (Serial.available()==0); {Serial.read();}
        break;
        } // close if raining
        } // close switch up_dn
       
       endIt: // Label for Goto
       Serial.println("switch up/dn check finished -press any key");

}


void setup() { // open setup
//dubug_init();
  pinMode(rolldnPin, OUTPUT);        //rolldn pin
  pinMode(stallPin,INPUT_PULLUP);          //Stall shunt voltage input pin
  pinMode(updn, INPUT_PULLUP);         //connected to "Trunk Unlock" output from remote start -updn
  pinMode(rollupPin,OUTPUT);          //rollup pin
  pinMode(ign,INPUT);           //ign sense pin
  pinMode(rainLED,OUTPUT);         //rain led ;
  pinMode(battV,INPUT);        // Battery Voltage
  pinMode(rainSensor,INPUT);        //rain sensor input
  pinMode(DHTPIN,INPUT);        // DHT11 Data In
 
  
   Serial.begin(115200);
    delay(10);
    pinMode(RAINING,OUTPUT);

    wifiMulti.addAP("NETGEAR44-pro", "*****");
    wifiMulti.addAP("NETGEAR44", "******");
    wifiMulti.addAP("DottieL", "*****");
    wifiMulti.addAP("DottieL_EXT", "*****");
    wifiMulti.addAP("Bay Ridge Resort", "*****");
    wifiMulti.addAP("Bay Ridge Resort Guest", "*****");
    wifiMulti.addAP("Bay Ridge Resort 5G", "*****");
    wifiMulti.addAP("oyowifi", "*****");
    wifiMulti.addAP("oyo wifi", "*****");
    wifiMulti.addAP("John's Galaxy A02s", "*****");

    Serial.println("Connecting Wifi...");
    if(wifiMulti.run() == WL_CONNECTED) {
        Serial.println("");
        Serial.print("Connected to : ");
        Serial.println(WiFi.SSID());
        //string(NETWORK) = string(WiFi.SSID());
        //Serial.println("");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
        
    }
  timer.setInterval(700L, myTimer);
  Blynk.config(auth);
  Blynk.connect(0);
  
  
  runOnce = 1;
  dht.begin();
  
  raineD = 0;
 if (EnableIntr==1) attachInterrupt(digitalPinToInterrupt(updn),RollPin, FALLING);
 
} //close setup

void disable() {
  while (digitalRead(ign)== 1){ //if ign is ON, chase your tail
   digitalWrite(rollupPin,LOW);
   //digitalWrite(rollupLed, LOW);
   digitalWrite(rolldnPin,LOW);
   waitFlag = 1;
   raineD = 0;
   rain = 0;
   Vcc = analogRead(battV);
   vout = (Vcc * 3.3) / 4092;
   vin = vout / (R2/float(R1+R2));
   vin = vin - correctionfactor;
   stallValue = analogRead(stallPin)/4;
   Blynk.run();
   //Blynk.virtualWrite(V0, RAINSENSOR);  
   //Blynk.virtualWrite(V1, TEMP);
   //Blynk.virtualWrite(V2, HUMIDITY);
   Blynk.virtualWrite(V6, vin);
   //Blynk.virtualWrite(V4, ROLLUP);
   //Blynk.virtualWrite(V5, ROLLDN);
   Blynk.virtualWrite(V3, stallValue);
   //Blynk.virtualWrite(V7, RAINING);
   //Blynk.virtualWrite(V11, BATTALARM);
   //Blynk.virtualWrite(V9, VENTED);
   delay(700);
   doneFlag = 1;
if (EnableIntr==1) detachInterrupt(digitalPinToInterrupt(updn));
     ESP.restart();
   } 
if (EnableIntr==1) attachInterrupt(digitalPinToInterrupt(updn),RollPin, FALLING);

}



void initialize() {
  if (runOnce == 1) {
    runOnce = 0;
    waitFlag = 0;
    Serial.print("runOnce = ");
    Serial.println(runOnce);
    stopRoll = 1; //ch to 1
    // venteD = 0;
    doneFlag = 1;
  }

 if (waitFlag == 1) {
 delay(30000) ;
 waitFlag = 0;
 stopRoll = 1; //ch to 1
 venteD = 0;
 doneFlag = 1;
}      
}

void stall() {
   
 if (stallValue <= stallSet) { // open stall test, check for end of window motors travel
  digitalWrite(rollupPin,LOW); // turn off window motors
  //digitalWrite(rollupLed, LOW);
  digitalWrite(rolldnPin,LOW); // turn off window motors
  // Serial.println(dht.readHumidity());   
  stopRoll = 1;
  Serial.print("Stalled!!!!!!!!!!!!!! ");   
  Serial.println(stallValue);
  test = 0;
  doneFlag = 1;
  //return;  
  } // close stall test
}

void rainchk() {        
    
    if (stopRoll == 1){ 
      RS = analogRead(rainSensor)/4;
      if (RS<RSlow) RSlow = RS;
   if (RS<RSthresh) {
     rain = 1;
      if (raineD == 0) {
      digitalWrite(rollupPin,HIGH);
      ROLLUP = 1;
      ROLLDN = 0;
      venteD = 0;
      doneFlag = 0;
      raineD = 1; 
 
  if (RS<=RSthresh-Hyst) {
     venteD=0; //reset venteD
     rain = 1;
  }
      //rollcheck()
      }
   }   
    
  if (dht.readHumidity()*humeCal>=humid) { // open humidity test
  Serial.print("rain is ");
  Serial.println(rain);
   rain = 1;
      if (raineD == 0) {
      digitalWrite(rollupPin,HIGH);
      ROLLUP = 1;
      ROLLDN = 0;
      venteD = 0;
      doneFlag = 0;
      raineD = 1; 
      //venteD = 0;
      }

     if (dht.readHumidity()*humeCal>=humid) {
     venteD=0; //reset venteD
     rain = 1;
  } 
   } // close humid test
 else
 {
 if(RS>RSthresh+Hyst && dht.readHumidity()*humeCal<=humid-humidHyst)
 {
  rain = 0;
  raineD = 0;
  
 } 
  
 } // close else
  }// close stopRoll
  
 
}

void Vent() {
if (dht.readTemperature(true)>=tempSet && raineD ==0) { //check for Vent temp 
  if (venteD == 0) {
  Serial.println("Over Temp!!!!!!!!!");
  digitalWrite(rolldnPin,HIGH); // roll down for ventTime seconds
  delay(ventTime);
  digitalWrite(rolldnPin,LOW); // stop roll down
  venteD = 1; // set flag
  //raineD = 0;
  ROLLUP = 0;
  ROLLDN = 0;
  stopRoll = 1;
  } // close vented test
 } // close overtemp test

//Serial.print ("roll_pin state = ");
}

void rollcheck() {
roll_pin = digitalRead(updn);
 
Serial.println("IN ROLL CHECK ROUTINE"); // !!!!!!!!!!!
Serial.print("Stall Value = ");
Serial.println(stallValue);
Serial.print("Rain = ");
Serial.println(RS);  

if (doneFlag == 1) {   
   roll_pin = !digitalRead(updn);   
   switch(roll_pin)
   {
    case (0): {
    Serial.println("switch rollpin = 0 - Idling");
    Serial.print(F("Humidity: "));
    Serial.println(dht.readHumidity()*humeCal);
    if (dht.readHumidity()*humeCal>Maxhumidity) Maxhumidity = dht.readHumidity()*humeCal;
    if (dht.readTemperature(true)<Mintemp) Mintemp = dht.readTemperature(true);
    Serial.print("Maxhumidity = ");
    Serial.print(Maxhumidity);
    Serial.println("%");
    Serial.print("Temp = ");
    Serial.println(dht.readTemperature(true)+tempCal);
    Serial.print("Max Temp = ");
    Serial.println(Maxtemp);
    Serial.print("Mintemp = ");
    Serial.println(Mintemp);
    Serial.print(" ");
    Serial.print(tempSet);
    Serial.print(" ");
    Serial.println(humid);
    Serial.print("rain sensor = ");
    Serial.print(RAINSENSOR);
    Serial.println("%");
    if (RAINSENSOR<RSlow) RSlow = RAINSENSOR;
    Serial.print("Max Rain Sensor Value = ");
    Serial.print(RSlow);
    Serial.println("%");
    Serial.print("Raw Rain Sensor = ");
    Serial.println(RS+RSoffset, 0);
    Serial.print("rain = ");
    Serial.println(rain);
    Serial.print("raineD = ");
    Serial.println(raineD);
    Serial.print("venteD = ");
    Serial.println(venteD);
    Serial.print("up_dn = ");
    Serial.println(up_dn);
    Serial.print("Battery Voltage = ");
    Serial.print(BATTV);
    Serial.println("v");
    Serial.print("Connected to ");
    Serial.println(WiFi.SSID());
    Serial.println("switch rollpin finished");
    Serial.println("");
   //Vent(); // call Vent Function !!
  break;
    }
    
    case (1): {// else check up_dn flag
    Serial.println("IN UP/DN TEST");
    Serial.print("A1 triggered ******* = ");
    Serial.println(digitalRead(updn));
    RollPin();
    break;
    }
       //while (Serial.available()<=0); {Serial.read();}
   } // CLOSE ROLL_PIN TEST
  //delay(100);
  test = 0;

} // close doneflag
} //close rollcheck

void update() {
if (test == 1){ // open test humidity/temp
    int h = dht.readHumidity();
    // Read temperature as Celsius (the default)
   float f = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
   float t = dht.readTemperature(true);
    // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) { // open check
     Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  } // close check
}

void loop() { //open MAIN loop !!!!!!!!!!!!!!!!!!!!
Blynk.run();
timer.run();

if(wifiMulti.run() != WL_CONNECTED) {
        Serial.println("WiFi not connected!");
        delay(1000);
    }

RS = analogRead(rainSensor)- RSoffset;
//if (RS<RSlow) RSlow = RS;
RAINSENSOR = (100-((RS)/40));
TEMP = dht.readTemperature(true)+tempCal;
HUMIDITY = dht.readHumidity()*humeCal;
//BATTV = vin;
STALLVAL = analogRead(stallPin)/4;
RAINING = raineD;
VENTED = venteD;
 stallValue = analogRead(stallPin)/4;
 digitalWrite(rainLED,rain);

 Vcc = analogRead(battV);
 vout = (Vcc * 3.3) / 4092;
 vin = vout / (R2/float(R1+R2));
 vin = vin - correctionfactor;
  if (BATTV<battSet) {
   BATTALARM = 1; 
 }
 else
 {
   BATTALARM = 0;
 }
    

  
 //Serial.print("INPUT V = ");
 //Serial.println(Vcc);

  if (cntNow < cntInit) cntInit = cntNow; //allow for millis() rollover every 39 days
  if (dht.readHumidity()*humeCal>Maxhumidity) Maxhumidity = dht.readHumidity()*humeCal;
  if (dht.readTemperature(true)<Mintemp) Mintemp = dht.readTemperature(true);
  if (dht.readTemperature(true)>Maxtemp) Maxtemp = dht.readTemperature(true);


      //Serial.println("switch rollpin finished");
      Serial.print("stall Value is ");
      Serial.println(stallValue);
      //Serial.print("rain sensor = ");
      //Serial.println(digitalRead(rainSensor));
      Serial.print(F("Humidity: "));
      Serial.println(dht.readHumidity()*humeCal);
      Serial.print("Temp = ");
      Serial.println(dht.readTemperature(true));
      Serial.println();

    delay(100);
        //while (Serial.available()<=0); {Serial.read();}
      
    disable();
    initialize();
    //update();
    stall();
    rainchk();
    Vent();
    rollcheck();

 
  
 }// close loop
  


@johnt60 you keep creating new topics about what is essentially the same project, so I’ve merged your latest one into this existing topic.

You DO NOT want to add Blynk Edgent into your existing sketchy.
If you want to add the Blynk OTA functionality (known as Blynk.Air) then you can do this, as I said earlier…

This is fairly easy to do, and there’s a topic about it here…

Pete.