okay so here is the thing…need to be able to put ssid and password in my esp32 without hardcoding the same.
here is my code
#define BLYNK_HEARTBEAT 17 // must be BEFORE BlynkSimpleEsp8266.h works OK as 17s
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxx";
char ssid[] = "N"; // username or ssid of your WI-FI
char pass[] = "N"; // password of your Wi-Fi
char server[] = "blynk-cloud.com";
unsigned int port = 8442;
// Your WiFi credentials.
// Set password to "" for open networks.
// Set your LED and physical button pins here
const int ledPin1 = 23;
const int ledPin2 = 22;
const int ledPin3 = 21;
const int ledPin4 = 19;
const int ledPin5 = 18;
const int ledPin6 = 17;
const int ledPin7 = 16;
const int ledPin8 = 25;
const int btnPin1 = 15;
const int btnPin2 = 04;
const int btnPin3 = 13;
const int btnPin4 = 14;
const int btnPin5 = 27;
const int btnPin6 = 26;
const int btnPin7 = 33;
const int btnPin8 = 32;
void checkPhysicalButton();
int led1State = HIGH;
int btn1State = HIGH;
int led2State = HIGH;
int btn2State = HIGH;
int led3State = HIGH;
int btn3State = HIGH;
int led4State = HIGH;
int btn4State = HIGH;
int led5State = HIGH;
int btn5State = HIGH;
int led6State = HIGH;
int btn6State = HIGH;
int led7State = HIGH;
int btn7State = HIGH;
int led8State = HIGH;
int btn8State = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
// Blynk.syncVirtual(V12);
// Blynk.syncVirtual(V13);
// Blynk.syncVirtual(V14);
// Blynk.syncVirtual(V15);
// Blynk.syncVirtual(V16);
// Blynk.syncVirtual(V17);
// Blynk.syncVirtual(V18);
// Blynk.syncVirtual(V19);
// Alternatively, you could override server state using:
Blynk.virtualWrite(V12, led1State);
Blynk.virtualWrite(V13, led2State);
Blynk.virtualWrite(V14, led3State);
Blynk.virtualWrite(V15, led4State);
Blynk.virtualWrite(V16, led5State);
Blynk.virtualWrite(V17, led6State);
Blynk.virtualWrite(V18, led7State);
Blynk.virtualWrite(V19, led8State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
led1State = param.asInt();
digitalWrite(ledPin1, led1State);
}
BLYNK_WRITE(V13) {
led2State = param.asInt();
digitalWrite(ledPin2, led2State);
}
BLYNK_WRITE(V14) {
led3State = param.asInt();
digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
led4State = param.asInt();
digitalWrite(ledPin4, led4State);
}
BLYNK_WRITE(V16) {
led5State = param.asInt();
digitalWrite(ledPin5, led5State);
}
BLYNK_WRITE(V17) {
led6State = param.asInt();
digitalWrite(ledPin6, led6State);
}
BLYNK_WRITE(V18) {
led7State = param.asInt();
digitalWrite(ledPin7, led7State);
}
BLYNK_WRITE(V19) {
led8State = param.asInt();
digitalWrite(ledPin8, led8State);
}
void checkPhysicalButton()
{
if (digitalRead(btnPin1) == LOW) {
// btn1State is used to avoid sequential toggles
if (btn1State != LOW) {
// Toggle LED state
led1State = !led1State;
digitalWrite(ledPin1, led1State);
// Update Button Widget
Blynk.virtualWrite(V12, led1State);
}
btn1State = LOW;
} else {
btn1State = HIGH;
}
if (digitalRead(btnPin2) == LOW) {
// btnState is used to avoid sequential toggles
if (btn2State != LOW) {
// Toggle LED state
led2State = !led2State;
digitalWrite(ledPin2, led2State);
// Update Button Widget
Blynk.virtualWrite(V13, led2State);
}
btn2State = LOW;
} else {
btn2State = HIGH;
}
if (digitalRead(btnPin3) == LOW) {
// btnState is used to avoid sequential toggles
if (btn3State != LOW) {
// Toggle LED state
led3State = !led3State;
digitalWrite(ledPin3, led3State);
// Update Button Widget
Blynk.virtualWrite(V14, led3State);
}
btn3State = LOW;
} else {
btn3State = HIGH;
}
if (digitalRead(btnPin4) == LOW) {
// btnState is used to avoid sequential toggles
if (btn4State != LOW) {
// Toggle LED state
led4State = !led4State;
digitalWrite(ledPin4, led4State);
// Update Button Widget
Blynk.virtualWrite(V15, led4State);
}
btn4State = LOW;
} else {
btn4State = HIGH;
}
if (digitalRead(btnPin5) == LOW) {
// btnState is used to avoid sequential toggles
if (btn5State != LOW) {
// Toggle LED state
led5State = !led5State;
digitalWrite(ledPin5, led5State);
// Update Button Widget
Blynk.virtualWrite(V16, led5State);
}
btn5State = LOW;
} else {
btn5State = HIGH;
}
if (digitalRead(btnPin6) == LOW) {
// btnState is used to avoid sequential toggles
if (btn6State != LOW) {
// Toggle LED state
led6State = !led6State;
digitalWrite(ledPin6, led6State);
// Update Button Widget
Blynk.virtualWrite(V17, led6State);
}
btn6State = LOW;
} else {
btn6State = HIGH;
}
if (digitalRead(btnPin7) == LOW) {
// btnState is used to avoid sequential toggles
if (btn7State != LOW) {
// Toggle LED state
led7State = !led7State;
digitalWrite(ledPin7, led7State);
// Update Button Widget
Blynk.virtualWrite(V18, led7State);
}
btn7State = LOW;
} else {
btn7State = HIGH;
}
if (digitalRead(btnPin8) == LOW) {
// btnState is used to avoid sequential toggles
if (btn8State != LOW) {
// Toggle LED state
led8State = !led8State;
digitalWrite(ledPin8, led8State);
// Update Button Widget
Blynk.virtualWrite(V19, led8State);
}
btn8State = LOW;
} else {
btn8State = HIGH;
}
}
void setup()
{
// Debug Console
Serial.begin(115200);
Serial.println();
WiFi;
WiFi.begin(ssid, pass);
// line below is blocking code, consider regular WiFi connection with timeout if you have router problems
Blynk.connectWiFi(ssid, pass); // used with Blynk.connect() in place of Blynk.begin(auth, ssid, pass, server, port);
// line below needs to be BEFORE Blynk.connect()
timer.setInterval(11000L,CheckConnection); // check if still connected every 11s
Blynk.config(auth, server, port);
Blynk.connect();
//Serial.println("");
//Serial.println("wifi connected...yeey :");
//Serial.println("IP address:");
//Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
// Debug console
//Serial.begin(9600);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
pinMode(ledPin1, OUTPUT);
pinMode(btnPin1, INPUT_PULLUP);
digitalWrite(ledPin1, led1State);
pinMode(ledPin2, OUTPUT);
pinMode(btnPin2, INPUT_PULLUP);
digitalWrite(ledPin2, led2State);
pinMode(ledPin3, OUTPUT);
pinMode(btnPin3, INPUT_PULLUP);
digitalWrite(ledPin3, led3State);
pinMode(ledPin4, OUTPUT);
pinMode(btnPin4, INPUT_PULLUP);
digitalWrite(ledPin4, led4State);
pinMode(ledPin5, OUTPUT);
pinMode(btnPin5, INPUT_PULLUP);
digitalWrite(ledPin5, led5State);
pinMode(ledPin6, OUTPUT);
pinMode(btnPin6, INPUT_PULLUP);
digitalWrite(ledPin6, led6State);
pinMode(ledPin7, OUTPUT);
pinMode(btnPin7, INPUT_PULLUP);
digitalWrite(ledPin7, led7State);
pinMode(ledPin8, OUTPUT);
pinMode(btnPin8, INPUT_PULLUP);
digitalWrite(ledPin8, led8State);
// Setup a function to be called every 100 ms
timer.setInterval(500L, checkPhysicalButton);
}
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
Blynk.connect(); // try to connect to server with default timeout
}
else{
Serial.println("Connected to Blynk server");
}
}
void loop()
{
Blynk.run();
timer.run();
}
same like wifimanager with esp8266…
WiFiManager wifiManager;
wifiManager.autoConnect("MCU1", "1397");
tried this code but it isnt working (guessing it only supports esp8266 ) .
any help would be apprecaited…thank you…!