How to call Blynk function using Blynk parameters saved in EEPROM?
Thanks
Dear friends,
How to use Blynk dynamic custom parameters to call Blynk function in a code.
Regards
@terzika I’ve moved these two questions from @khoih’s lightweight credentials manager topic into a new one.
I’ve done this so that it’s easier for other users to follow @khoih’s original topic and not have it cluttered-up with off-topic questions.
Your questions don’t provide much detail about what you are trying to achieve, but I’m guessing that you want to use a credentials manager to store a value which will then be used within your code.
The simple way is to read the value into a global variable and use it from there.
I’d caution against using EEPROM for this, as it has a finite life in terms of the maximum number of read/write operations that it can tolerate.
Also, as you’re using Blynk anyway then a better solution may be to write the value you which to save/retrieve to a virtual pin on the Blynk server then retrieve it on startup using the BLYNK_CONNECTED callback and the Blynk.syncVirtual(vPin) command.
Pete.
Dear Pete, Thank you for quick answer. I understand the reason for moving the topic I just did not know how to ask the question.
I want to use ESP_AT_WM_Lite configuration portal to input wifi and Blynk credentials and then using them to call Blynk function. But when I include ESP_AT_WM_Lite library in my code Blynk.config(wifi, auth, server, port) does not work anymore ( I can provide report from Arduino IDE).
So here is my code on Arduino Mega using ESP01 as a WiFi shiled:
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#define ESP8266_BAUD 115200
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxx";
char pass[] = "xxxxxx";
char server[] = "blynk-cloud.com";
uint16_t port = 8080;
#define EspSerial Serial3
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
void setup() {
timer.setInterval(15000L, SendtoBlynk);
timer.setInterval(60000L, CheckConnection);
Serial.begin(115200);
EspSerial.begin(ESP8266_BAUD);
Blynk.config(wifi, auth, server, port);
if (Blynk.connectWiFi(ssid, pass)) {
Blynk.connect();
}
if(Blynk.connected()){
Serial.println("Connected in Setup");
}
} // ***********************End Setup*************************
void loop() {
if(Blynk.connected()) {
Blynk.run();
}
timer.run();
}//****************** End Loop************
void CheckConnection(){
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
Blynk.connect();
if(Blynk.connected()){
Serial.println("Reconnected to Blynk server");
}
}
} //***************End CheckConnection()*************************
void SendtoBlynk() {
//My Blynk Sending Code
} // **************End SendtoBlynk************
Thank you in advance,
BR,
Terzika
@terzika 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.
But if I add the recommended Esp8266_AT_WM_Lite piece of code in my existing code for using Esp8266_AT_WM_Lite configuration portal Arduino IDE reports problem with Blynk.config(wifi, auth, server, port) syntax.
// Select depending on board
#define EspSerial Serial3
// Must be before #include <Esp8266_AT_WM_Lite.h>
// Start location in EEPROM to store config data. Default 0
#define EEPROM_START (0)
#include <Esp8266_AT_WM_Lite.h>
ESP_AT_WiFiManager_Lite* ESP_AT_WiFiManager;
// Your Mega <-> ESP8266 baud rate:
#define ESP8266_BAUD 115200
#define USE_DYNAMIC_PARAMETERS true
/////////////// Start dynamic Credentials ///////////////
//Defined in <Esp8266_AT_WM_Lite_Teensy.h>
/**************************************
#define MAX_ID_LEN 5
#define MAX_DISPLAY_NAME_LEN 16
typedef struct
{
char id [MAX_ID_LEN + 1];
char displayName [MAX_DISPLAY_NAME_LEN + 1];
char *pdata;
uint8_t maxlen;
} MenuItem;
**************************************/
#if USE_DYNAMIC_PARAMETERS
#define MAX_BLYNK_SERVER_LEN 34
#define MAX_BLYNK_TOKEN_LEN 34
char Blynk_Server [MAX_BLYNK_SERVER_LEN + 1] = "";
char Blynk_Token [MAX_BLYNK_TOKEN_LEN + 1] = "";
#define MAX_BLYNK_PORT_LEN 6
char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "";
MenuItem myMenuItems [] =
{
{ "sv", "Blynk Server", Blynk_Server, MAX_BLYNK_SERVER_LEN },
{ "tk", "Token", Blynk_Token, MAX_BLYNK_TOKEN_LEN },
{ "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN },
};
uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize;
#else
MenuItem myMenuItems [] = {};
uint16_t NUM_MENU_ITEMS = 0;
#endif
/////// // End dynamic Credentials ///////////
I have to somehow combine my code with this ESP_AT_WM_Lite Example code.
/****************************************************************************************************************************
Mega_ESP8266Shield.ino
For AVR or Generic boards using ESP8266 AT WiFi Shields, using much less code to support boards with smaller memory
ESP_AT_WM_Lite is a library for the Mega, Teensy, SAM DUE, SAMD and STM32 boards (https://github.com/khoih-prog/ESP_AT_WM_Lite)
to enable store Credentials in EEPROM to easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi and other services
without Hardcoding.
Built by Khoi Hoang https://github.com/khoih-prog/ESP_AT_WM_Lite
Licensed under MIT license
Version: 1.0.2
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 09/03/2020 Initial coding
1.0.1 K Hoang 20/03/2020 Add feature to enable adding dynamically more Credentials parameters in sketch
1.0.2 K Hoang 17/04/2020 Fix bug. Add support to SAMD51 and SAMD DUE. WPA2 SSID PW to 63 chars.
Permit to input special chars such as !,@,#,$,%,^,&,* into data fields.
*****************************************************************************************************************************/
/* Comment this out to disable prints and save space */
#define ESP_AT_DEBUG_OUTPUT Serial
#define ESP_AT_DEBUG true
#if !( defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) )
#error This code is intended to run only on the Arduino Mega 1280/2560 boards ! Please check your Tools->Board setting.
#endif
// For Mega, use Serial1 or Serial3
#define EspSerial Serial3
#if defined(ARDUINO_AVR_MEGA2560)
#define BOARD_TYPE "AVR Mega2560"
#else
#define BOARD_TYPE "AVR Mega"
#endif
// Start location in EEPROM to store config data. Default 0
// Config data Size currently is 116 bytes)
#define EEPROM_START 512
#include <Esp8266_AT_WM_Lite.h>
#define USE_DYNAMIC_PARAMETERS true
/////////////// Start dynamic Credentials ///////////////
//Defined in <Esp8266_AT_WM_Lite.h>
/**************************************
#define MAX_ID_LEN 5
#define MAX_DISPLAY_NAME_LEN 16
typedef struct
{
char id [MAX_ID_LEN + 1];
char displayName [MAX_DISPLAY_NAME_LEN + 1];
char *pdata;
uint8_t maxlen;
} MenuItem;
**************************************/
#if USE_DYNAMIC_PARAMETERS
#define MAX_BLYNK_SERVER_LEN 34
char Blynk_Server [MAX_BLYNK_SERVER_LEN + 1] = "";
#define MAX_BLYNK_TOKEN_LEN 34
char Blynk_Token [MAX_BLYNK_TOKEN_LEN + 1] = "";
#define MAX_BLYNK_PORT_LEN 6
char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "";
#define MAX_MQTT_SERVER_LEN 34
char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "";
MenuItem myMenuItems [] =
{
{ "sv", "Blynk Server", Blynk_Server, MAX_BLYNK_SERVER_LEN },
{ "tk", "Token", Blynk_Token, MAX_BLYNK_TOKEN_LEN },
{ "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN },
{ "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN },
};
uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize;
#else
MenuItem myMenuItems [] = {};
uint16_t NUM_MENU_ITEMS = 0;
#endif
/////// // End dynamic Credentials ///////////
// Your Mega <-> ESP8266 baud rate:
#define ESP8266_BAUD 115200
void heartBeatPrint(void)
{
static int num = 1;
if (WiFi.status() == WL_CONNECTED)
Serial.print(F("H")); // H means connected to WiFi
else
Serial.print(F("F")); // F means not connected to WiFi
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define HEARTBEAT_INTERVAL 600000L
// Print hearbeat every HEARTBEAT_INTERVAL (600) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
ESP_AT_WiFiManager_Lite* ESP_AT_WiFiManager;
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
//delay(1000);
Serial.print(F("\nStart Mega_ESP8266Shield on "));
Serial.println(BOARD_TYPE);
// initialize serial for ESP module
EspSerial.begin(115200);
ESP_AT_WiFiManager = new ESP_AT_WiFiManager_Lite(&EspSerial, ESP8266_BAUD);
// Optional to change default AP IP(192.168.4.1) and channel(10)
//ESP_AT_WiFiManager->setConfigPortalIP(IPAddress(192, 168, 120, 1));
ESP_AT_WiFiManager->setConfigPortalChannel(1);
ESP_AT_WiFiManager->begin();
}
#if USE_DYNAMIC_PARAMETERS
void displayCredentials(void)
{
Serial.println("Your stored Credentials :");
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.println(String(myMenuItems[i].displayName) + "=" + myMenuItems[i].pdata);
}
}
#endif
void loop()
{
ESP_AT_WiFiManager->run();
check_status();
#if USE_DYNAMIC_PARAMETERS
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
#endif
}
And presumably that ‘problem’ is that wifi, auth, server and port are not defined in this scope?
The variables being used to hold the server IP/URL, auth token and port are Blynk_Server
, Blynk_Token
and Blynk_Port
Pete.
This is my test code (which is now compiled and uploaded) but Blynk is not connected.
#define ESP_AT_DEBUG_OUTPUT Serial
#define BLYNK_PRINT Serial
#define BLYNK_DEBUG
#define EEPROM_START 512
#include <Esp8266_AT_WM_Lite.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//#define ESP_AT_DEBUG true
#if !( defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) )
#error This code is intended to run only on the Arduino Mega 1280/2560 boards ! Please check your Tools->Board setting.
#endif
#if defined(ARDUINO_AVR_MEGA2560)
#define BOARD_TYPE "AVR Mega2560"
#else
#define BOARD_TYPE "AVR Mega"
#endif
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#define USE_DYNAMIC_PARAMETERS true
/////////////// Start dynamic Credentials ///////////////
#if USE_DYNAMIC_PARAMETERS
#define MAX_BLYNK_SERVER_LEN 34
char Blynk_Server [MAX_BLYNK_SERVER_LEN + 1] = "";
#define MAX_BLYNK_TOKEN_LEN 34
char Blynk_Token [MAX_BLYNK_TOKEN_LEN + 1] = "";
#define MAX_BLYNK_PORT_LEN 6
char Blynk_Port [MAX_BLYNK_PORT_LEN + 1] = "";
#define MAX_MQTT_SERVER_LEN 34
char MQTT_Server [MAX_MQTT_SERVER_LEN + 1] = "";
MenuItem myMenuItems [] =
{
{ "sv", "Blynk Server", Blynk_Server, MAX_BLYNK_SERVER_LEN },
{ "tk", "Token", Blynk_Token, MAX_BLYNK_TOKEN_LEN },
{ "pt", "Port", Blynk_Port, MAX_BLYNK_PORT_LEN },
{ "mq", "MQTT Server", MQTT_Server, MAX_MQTT_SERVER_LEN },
};
uint16_t NUM_MENU_ITEMS = sizeof(myMenuItems) / sizeof(MenuItem); //MenuItemSize;
#else
MenuItem myMenuItems [] = {};
uint16_t NUM_MENU_ITEMS = 0;
#endif
/////// // End dynamic Credentials ///////////
void heartBeatPrint(void)
{
static int num = 1;
if (WiFi.status() == WL_CONNECTED)
Serial.print(F("H")); // H means connected to WiFi
else
Serial.print(F("F")); // F means not connected to WiFi
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define HEARTBEAT_INTERVAL 600000L
// Print hearbeat every HEARTBEAT_INTERVAL (600) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
ESP_AT_WiFiManager_Lite* ESP_AT_WiFiManager;
BlynkTimer timer;
int param1 = 1 ;
int param2 = 2;
float param3 = 3.3;
float param4= 4.4;
float param5 = 5.5;
float param6 = 6.6;
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
timer.setInterval(5000L, SendtoBlynk);
//delay(1000);
Serial.print(F("\nStart Mega_ESP8266Shield on "));
Serial.println(BOARD_TYPE);
// initialize serial for ESP module
EspSerial.begin(ESP8266_BAUD);
ESP_AT_WiFiManager = new ESP_AT_WiFiManager_Lite(&EspSerial, ESP8266_BAUD);
// Optional to change default AP IP(192.168.4.1) and channel(10)
//ESP_AT_WiFiManager->setConfigPortalIP(IPAddress(192, 168, 120, 1));
ESP_AT_WiFiManager->setConfigPortalChannel(1);
ESP_AT_WiFiManager->begin();
Blynk.config(wifi, Blynk_Token, Blynk_Server, Blynk_Port);
Blynk.connect();
}
#if USE_DYNAMIC_PARAMETERS
void displayCredentials(void)
{
Serial.println("Your stored Credentials :");
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.println(String(myMenuItems[i].displayName) + "=" + myMenuItems[i].pdata);
}
}
#endif
void loop()
{
ESP_AT_WiFiManager->run();
check_status();
if (WiFi.status() == WL_CONNECTED){
Blynk.config(wifi, Blynk_Token, Blynk_Server, Blynk_Port);
Blynk.connect();
}
if (Blynk.connected()){
Blynk.run();
timer.run();
}
#if USE_DYNAMIC_PARAMETERS
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (int i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
#endif
}
void SendtoBlynk() {
Blynk.virtualWrite(V0, param1);
Blynk.virtualWrite(V1, param2);
Blynk.virtualWrite(V2, param3);
Blynk.virtualWrite(V3, param4);
Blynk.virtualWrite(V4, param5);
Blynk.virtualWrite(V5, param6);
}
I add two lines in the code to check WiFi and Blynk status:
Serial.println (Blynk.connected());
Serial.println (WiFi.status());
and it returns 0 and 1 that means that ESP is connected to wifi but Blynk is not connected.
I’m not sure where your wifi object is coming from, but I don’t think it’s what you should be using here.
Pete.
It is comming from BlynkSimpleShieldEsp8266.h library which is in the needed Blynk library. Do You have any sugestion what to use instead?
And Pete, I really appreciate Your time spend on this topic.
I thought the Wi-Fi object was called ESP_AT_WiFiManager
but I might be wrong, as I’ve not used this library.
Pete.
Hi @terzika
Sorry for late reply and thanks to @PeteKnight to help. I got problem with Internet and have limited acess now.
You are using the wrong library for your current purpose. You can achieve what you’d like with this lib, but have to write extra code.
It’s better to use this library for your Mega to connect to Blynk
Thanks a lot. I hopped the lite version will work because of the limitation of Mega. I will try.
BR