[SOLVED] ESP8266 - Use Blynk + OTA firmware.... avoid hardcoded Blynk wifi?

I’ve previously used Blynk for a few small ESP8266 projects, and really like the simple bridge to connect an Android device. I’ve also been doing more with the Homie firmware, which is excellent for building a device that could potentially be deployed to customers.

Here’s the catch… as far as I can tell Blynk requires that you hard-code wifi credentials. This puts it in conflict with Homie, which provides a configuration wifi AP that lets you log in and set up wifi and MQTT through a portal page. You can provide Blynk with the uname and pwd that is set up on the Portal, but that is really buggy and generally doesn’t work at all.

Is there a way to use Blynk, but in a way to delay its startup? I’d like to have some way to essentially block Blynk until I get the wifi setup via Homie or some other OTA capable firmware. Another option may instead to let Blynk communicate via MQTT?

Something as simple as allowing for the ESP8266 library to do a blynk.begin(auth) instead of also requiring wifi credentials? I could provide those later, after doing a check to ensure Homie is running in standard mode instead of config mode.

@raiderj are you familiar with WiFiManager? Until recently it was the defacto library to provision MCU’s with WiFi credentials and Blynk tokens on the fly rather than hard coding them in your sketch.

Blynk now have a “better” system that allows you to do the same via the Blynk app rather than via the web browser that WiFi Manager uses.

There are also Blynk.connect(), Blynk.disconnect() and Blynk.config() functions that you might want to take a look at.

1 Like

Earlier I used WiFiManager in a quick test, but felt the Homie firmware was a better system for handling OTA and a configuration portal. But, it sounds like Blynk has a similar setup that you can use, but configuration is done via the Blynk app instead? That’s interesting, I’ll have to look into that more.

Still, I’m primarily interested in Homie for the config portal, MQTT integration, and OTA updates. I also like Blynk since it provided a simple mobile app platform and communication via MQTT.

I’ll take another look at those functions, but when I explored Blynk functions earlier I didn’t see a way to let another solution like Homie handle wifi and setup, and then just let Blynk talk to the Blynk server.

I don’t particularly like having to use the Blynk app to setup the ESP8266… ideally I want a device that can be configured via mobile device or PC just on a web browser.

How did you manage to let it work with MQTT? You got me real interested here…

Sorry I missed this before, but what I did was create a function that ran at a heartbeat interval in Homie. When that triggered, it then also activated Blynk pins. So, I more or less did all the MQTT integration with Homie, and then used that in order to dictate what Blynk did.

My frustration is that both Homie and Blynk want to control the wireless on the ESP8266. Homie is great, lets you do OTA and MQTT very easily. It doesn’t, however, have a slick mobile app companion. That’s why I wanted to integrate Blynk into the mix. However, I’m currently stuck at how to do that. I wish Blynk had a way to just enable the cloud functions without trying to control the wifi. Maybe there’s a way to just use the Blynk API that I’m overlooking.

i think to maintain this in good shape , you can use Node-red in the middle .
node red will take the inputs from Virtual pins and send it to Homie using MQTT.

That wouldn’t solve the problem though. The issue is that Blynk requires you to hard code wifi credentials. That issue is what Homie solves. Deploying devices to customers doesn’t make sense if they can’t update their own connections.

If Blynk could instead just talk directly, without having to set up wifi, then Homie can handle wifi setup. From there, both MQTT and mobile app usage would be simple.

I think you are mistaken.

Everytime it tried this, I got an error…
Need help connecting Node Red with Blynk

What did I miss? It looked to me that when I init Blynk I have to give it an SSID and passphrase. This would be on the ESP8266. The way I read the docs blynk.begin requires the auth code and wifi creds.

@raiderj take a look at Blynk.config() instead of Blynk.begin(). WiFi credentials can be dynamic with WiFiManager and other systems and then you simply request a connection to the Blynk server.

Edit: actually my first post in this thread covered all this.

I guess I didn’t understand your first post. Took a look at my code, and it looks like I interpreted those functions to be that I had to use both config and start. Now, it looks like you could just use config only?

I’ll have to give it another shot. Would be awesome to have both working together.

you should not use Blynk websockets as the developer stopped updating it , it’s better to use HTTP request node
directly.

It works! I now am using Blynk.config(auth_token); to initialize Blynk, then just calling Blynk.run() in my firmware. I’ll have to do a bit more testing, but right now I’m able to get data outputted to the Blynk app. Need to do some tweaking on the MQTT side, but it looks like it’s good there too.

Overall I think this is a very simple setup to get OTA, MQTT, and Blynk all running on an ESP8266. It appears I’m using about 34% of the program space, so that should mean plenty of room for further development. As for initializing a node, it works like this:

  1. Flash Homie firmware (and optionally the ui_bundle) to the ESP8266. The 3MB/1MB split works fine
  2. Power on ESP8266, watch for the SSID to appear, join the network. I’m using my cell phone to do this.
  3. Configuration page auto-loads into mobile browser. Locate nearby wifi network, provide credentials to join. Also provide MQTT server location and optionally enable OTA. Also set device name/ID.
  4. ESP8266 reboots, joins network. Communicated via MQTT, and also to Blynk app.
  5. Verify MQTT updates and Blynk mobile app updates.

I’ll post my code once I have it cleaned up a bit to give others a “boilerplate” starting point for the firmware. The hard part for others might be getting Homie set up, or at least it was for me as a novice user.

1 Like

Here’s some sample code to get going if you’re interested. It’s not as concise as it could be, but hopefully saves you time getting up and running.

// For Homie Firmware
// https://github.com/marvinroger/homie-esp8266
#include <Homie.h>

// For Blynk
// https://www.blynk.cc
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>

// Homie Firmware Settings
// Brand sets wifi AP name instead of "Homie" default
#define FW_NAME "IoT_Keyapd"
#define FW_VERSION "0.0.1"
#define FW_BRAND "Acme_IoT"

/* Magic sequence for Autodetectable Binary Upload */
const char *__FLAGGED_FW_NAME    = "\xbf\x84\xe4\x13\x54" FW_NAME    "\x93\x44\x6b\xa7\x75";
const char *__FLAGGED_FW_VERSION = "\x6a\x3f\x3e\x0e\xe1" FW_VERSION "\xb0\x30\x48\xd4\x1a";
const char *__FLAGGED_FW_BRAND   = "\xfb\x2a\xf5\x68\xc0" FW_BRAND   "\x6e\x2f\x0f\xeb\x2d";
/* End of magic sequence for Autodetectable Binary Upload */

// Using PIN 14 on ESP-8266 for general purpose LED
// Creating a couple LEDs for Blynk testing
const int PIN_LED = 14;
bool statusLEDOn = false;
bool heartbeatLED = false;

// Get Auth token from Blynk mobile app. Go to the Project Settings (nut icon).
char blynkAuth[] = "8675309feedfacefooddeadbeef01234";
WidgetLCD blynkLCD(V1); // Virtual Blynk LCD on PIN 1
BlynkTimer timer; // Create a Timer object called "timer"

const int UPDATE_INTERVAL = 10000;
unsigned long lastUpdateTime = 0;

// Set up nodes for the keypad
HomieNode statusLEDNode("Status LED", "On/Off");

// getLogger() writes to serial, while MQTT output is done via setProperty()
bool statusLEDHandler(HomieRange range, String value) {
  Homie.getLogger() << "Entering statusLEDHandler" << endl;
  if (value != "true" && value != "false") return false;
  bool on = (value == "true");
  digitalWrite(PIN_LED, on ? HIGH : LOW);
  statusLEDNode.setProperty("on").send(value);
  Homie.getLogger() << "Status LED (via MQTT) is " << (on ? "on" : "off") << endl;
  return true;
}

void setupHandler() {
  Homie.getLogger() << "In setupHandler" << endl;
}

// Turns status LED on/off for heartbeat, updates MQTT broker
void loopHandler() {
    if (millis() - lastUpdateTime >= UPDATE_INTERVAL) {
      digitalWrite(PIN_LED, statusLEDOn ? LOW : HIGH);
      Homie.getLogger() << "Status LED On: " << statusLEDOn << endl;
      lastUpdateTime = millis();
      statusLEDOn ^= true;
      statusLEDNode.setProperty("on").send( (statusLEDOn ? "true" : "false") );
      
      // Blink Blynk LED and update Blynk LCD
      if(statusLEDOn == HIGH) {
        Blynk.virtualWrite(14, 255); 
      } else {
        Blynk.virtualWrite(14, 0);
      }
      blynkLCD.clear();
      blynkLCD.print(0,0, "Uptime: ");
      blynkLCD.print(1,1, (lastUpdateTime/1000));
      blynkLCD.print(8,1, "seconds");
  }
}

// Run when broadcasts received from other Homie devices
bool broadcastHandler(const String& level, const String& value) {
  Homie.getLogger() << "Received broadcast level " << level << ": " << value << endl;
  return true;
}

// Visible during config mode, string is adjustable, and default in setup()
HomieSetting<const char*> manufacturerSetting("manufacturer", "Device created by ");  // id, description


void setup() {
  Serial.begin(115200);
  Serial << endl << endl;

  // Set up ESP8266 status LED, turn on
  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, HIGH);

  // Before Homie.setup()
  Homie_setFirmware(FW_NAME, FW_VERSION);
  Homie_setBrand(FW_BRAND);

  // Homie setup functions
  Homie.setup();
  statusLEDNode.advertise("on").settable(statusLEDHandler);               // Set status LED node available to MQTT
  Homie.setBroadcastHandler(broadcastHandler);                            // Handles broadcasts from other Homie devices
  Homie.setSetupFunction(setupHandler).setLoopFunction(loopHandler);      // Defines setup to run once, and a loop to run during the Arduino loop
  Homie.setLedPin(16, HIGH);                                              // Use GPIO 16 on the ESP-8266 as the Homie status LED
  manufacturerSetting.setDefaultValue("Acme IoT Manufacturing");  // Set the default value, viewable during config mode

  // Blynk setup functions
  Blynk.config(blynkAuth);              // Use config instead of begin, since Homie handles wifi
  timer.setInterval(1000L, sendUptime); // Interval of 1sec and which function to call
}

void sendUptime()
{
      // Blink a virtual LED on Blynk for nice visual feedback
      if(heartbeatLED == HIGH) {
        Blynk.virtualWrite(15, 255);
      } else {
        Blynk.virtualWrite(15, 0);
      }
      heartbeatLED ^= true;
}

void loop() {
  // Start Homie, and once connected kick off Blynk
  Homie.loop();
  if (Homie.isConfigured() && Homie.isConnected()) {
    Blynk.run();
  }
  timer.run();  // Timer runs at set interval
}