My first Blynk Project with HX711 & NodeMCU

Wanted to thank you for all the responses and guidance. Cheers, Somu.

That doesn’t apply to deep sleep.

Yes, see the Beehive Connected topic.

Is that a button in Blynk?
If so then you’d need to process Blynk.run multiple times and use BLYNK_CONNECTED to know when the value from that button has been received by the board.

You’d need to stop the device going to sleep, then do the update, then allow sleep again. That’s potentially heavy on battery usage, and your timing for waiting until the device is awake and waiting for the update.

That could mean anything, but you don’t seem to be saying that dynamic provisioning is now a requirement?

One other issue with battery powered deep sleep devices is that if you use Blynk.begin then if the device can’t connect to WiFi or the Blynk server then it will never get to the deep sleep command, as Blynk.begin is a blocking command.
That’s why the Beehive Connected sketch used manual WiFi management and Blynk.config/Blynk.begin as this isn’t blocking.

Pete.

If by dynamic provisioning you are referring to non-hard coded wifi credentials , yes , definitely that’s what I would like to try.

By etc. I mean, things like storing the last few values, say last 1 hr, in eprom so that in case of battery dying out, it stores the last few readings and can start from there when the batteries are recharged.

I will redo the code with the beehive example and see how it turns out. Will keep you posted.

Again, many thanks.

Yes, that’s what Dynamic Provisioning means.
What’s your use-case for requiring this, as it increases the complexity by an order of magnitude, and I don’t think you have the coding skills for that.

Don’t use EEPROM, use SPIFFS OR LittleFS instead.

Life gets much easier if you work-out what your functional requirements and wishlist are to begin with, and communicate these to people who you ask for assistance. It saves you, and us, lots of wasted time.

Pete.

Ok, Pete…redid the code based on the Beehive project…had to make some major changes and reading wrt the esp32 and esp8266 libraries and commands and had to replace / remove some commands which I believe are not supported in esp8266 (for example: RTC_DATA_ATTR int bootCount = 0).

Can you check now if this looks okay now?

I still would want to:

  1. use dynamic provisioning
  2. Hopefully this should work if I want to read the weight say once every 6 or12 hrs only
  3. send SMS once only if the weight exceeds 2kg. Currently it will be sending it every time it wakes up from deep sleep and finds the weight to be more than 2kg
  4. store some of the data like the last 1 hour readings in some memory on the board as back up for battery dying.

Here’s the code:



// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "Hidden"
#define BLYNK_DEVICE_NAME "Hidden "
#define BLYNK_AUTH_TOKEN "Hidden"


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "HX711.h"

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

const char auth[] = BLYNK_AUTH_TOKEN;
const char blynk_server [] =   "blynk.cloud"; // new variable to hold the name of the Blynk server
const int blynk_port =         8080;              // new variable to hold the port used by the Blynk server

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Hidden";
char pass[] = "Hidden";
const char* host = "Hidden";

int wifi_connect_count = 0;          // New variable to keep track of how manty times we've tried to connect to the Wi-Fi
int wifi_connect_max_retries = 50;   // New variable to specify how many attempts we will have at connecting to the Wi-Fi
//float sleep_time_minutes =     1;   // New variable - how long (in minutes) we sleep for. As this is a float variable then it can be 0.5 for a 30 second test

// HX711 circuit wiring
HX711 scale;
const int LOADCELL_DOUT_PIN = 4;
const int LOADCELL_SCK_PIN = 5;

//define the parameters for the IFTTT
#define HOSTIFTTT "Hidden"
#define EVENTO "Hidden"
#define IFTTTKEY "Hidden"

WiFiClient client;

int upperLimit = 2; 
float calibration_factor = -104625; 
float weight = 0;

void getWeight()
{
  Serial.println("Initializing the scale");
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  Serial.print("Reading: ");
  Serial.print("The weight in kg is:\t");
  weight = ((scale.get_units(10))-0.42);   // Assign Weight to reading
  Serial.println(weight);
  Serial.print("calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();
}


void WiFi_Connect() // New functon to handle the connectuon to the Wi-Fi network
{
  Serial.println("Connecting to Wi-Fi");
  if (WiFi.status() != WL_CONNECTED)
  {
      WiFi.begin(ssid, pass); // connect to the network
  }
  while (WiFi.status() != WL_CONNECTED  && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(500);
    wifi_connect_count++;   
    Serial.print("Wi-Fi connection - attempt number ");
    Serial.println(wifi_connect_count);
  }
  
  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("Wi-Fi CONNECTED");
    Serial.println();
  }
} 



void Deep_Sleep_Now()
{
  Serial.println("Going to sleep now");
  Serial.flush(); 
  ESP.deepSleep(60* 1000000);
}


void sendSMS() {
    Serial.println("Initiating to send SMS");
    if(client.connected())
    {
      client.stop();
    }

    client.flush();
    if (client.connect(host,80)) 
    {
    Serial.println("Connected");
    // build the HTTP request
    String toSend = "GET /trigger/";
    toSend += EVENTO;
    toSend += "/with/key/";
    toSend += IFTTTKEY;
    toSend += "?value1=";
    toSend += weight;
    toSend += " HTTP/1.1\r\n";
    toSend += "Host: ";
    toSend += HOSTIFTTT;
    toSend += "\r\n";
    toSend += "Connection: close\r\n\r\n";
    client.print(toSend);
    delay(1000);
  
    Serial.println("SMS Sent");
    delay(1000);
    }
  
    client.flush();
    client.stop();  
   }



void setup()
{
  Serial.begin(115200);
  WiFi_Connect(); // Attempt to connect to Wi-Fi

  if (WiFi.status() == WL_CONNECTED)               // If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
  {
    Blynk.config(auth, blynk_server, blynk_port);  // Initialise the Blynk connection settings
    Blynk.connect();                               // Attempt to connect to Blynk
  }
  else
  {
    Serial.println ("Wi-Fi connection failed - going to sleep");
    Deep_Sleep_Now();
  }
  
  if (Blynk.connected())                          // If we manages to connect to Blynk then carry-on as normal, else go to sleep
  {  
    Serial.println ("Connected to Blynk");
  }
  else
  {  
    Serial.println("Blynk connection failed - going to sleep");
    Deep_Sleep_Now();
  }
   delay(2000);
 }

void loop()
{
  delay(2000);
  getWeight();
  
   if(weight > upperLimit)
  {
     Serial.println("Weight is more than 2 kg, sending SMS....");
     sendSMS();
  }
  
  else 
  {
   Serial.println("Weight is less than 2 kg, not sending SMS");
   delay(1000);
  }      
  Blynk.virtualWrite(V3, weight);
  Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
  delay(100);
  Deep_Sleep_Now();
  
}  
 






I’ve already given you my thoughts on that.

Another previously unmentioned requirement. The maximum deep sleep time, when using latest versions of the ESP core, is around 3.5 hours.

That will require the use of NVR, and as I’ve said earlier you should use SPIFFS or LittleFS for this.

How are you going to do that, if the device is asleep for more than 1 hour?

Why arent you using the Blynk server for storage rather than the device memory?

As I’ve said before, the first task is to define your functional requirements. Once you have these, then they will dictate the code and code structure that needs to be used. You’re approaching the problem from the wrong direction.

Pete.

  1. It would help if you can point to some sketches which allows dynamic provisioning for instances where Edgent is not used.
  2. 3 hrs sleep is alright, if that’s the hardware constraint
  3. For checking if SMS has been sent, it would help if you can point me to some sketches wrt the use of SPIFFS or LittleFS
    4.Blynk server for storage is a good idea. Any pointers to its implementation will help. I will store the last say 24 hrs of weight data ( at 3 hours interval)
  4. I am following an agile development, rather than a waterfall. Planning to add features as I go along, rather than have it all built at one go. The sketch that I have is
    the basic build…i plan to make it better. The objective is simple, a weightscale that is portable (hence dynamic provisioning), run continuously and send data every 3 hrs, run on batteries, send an SMS alert only once when the weight goes beyond a certain value, store latest historical weight values ( in case battery goes down) . Trust i have made the objective and functionality clear.

Now, is the sketch that I have drawn for the basic build looking ok?

Thanks again…

Edgent is the official dynamic provisioning solution for Blynk IoT
There are other potential solutions, such as WiFiManager, but these were developed for Blynk Legacy and there aren’t any off the shelf examples that I’m aware of which incorporate the inclusion of Template ID and Device Name, and which will connect to the correct Blynk server. Also, if you want to speed-up the WiFi connection process with a static IP address then you’d need to add these details to the WiFiManager configuration.
However, as you’ve not shared your use-case for dynamic provisioning then it’s difficult to know if there are any other potential issues preventing the potential use of WiFiManager.

That’s somewhat outside the scope of this forum, as it’s not specific to Blynk. Google is your friend with this one.

Once again, I’m struggling with the use-case for this. Youtr stated requirement was:

You’ve not addressed how the 1 hour readings will be taken, given that your device will be asleep for 3.5 hours or more at one time, and once the data has been uploaded to Blynk then it will be on the Blynk server and you don’t need to worry about your device dying.
If you were approaching it from a different perspective - wanting to store local data in case of the inability to upload data to Blynk because a connection want possible, then this is a totally different proposition.

The first step of an Agile development lifecycle is the Concept phase, where you define the project goals. It’s not Agile if you just randomly skip to the Inception phase without understanding the end goal.
You should ask your ScrumMaster for a bit of re-training :wink:

Not really, as you’ve previously had OTA updates as an option, and now seem to have dropped this. If it’s a requirement then we need to know if this will be from a device connected to teh same WiFi network as the device, or from a remoite location, as the solutions differ enormously. You’ve also mentioned a TARE reset, but it’s not clear if this is via the app or a physical button, ort if it’s even a requirement now.

As with everything, “the devil is in the detail” and we’re almost 30 posts into this conversation (most of which were wasted because you didn’t state your basic requirements) and you still haven’t assembled a proper high level list of functionality so that we can start to flesh-out the detail behind them and develop a plan that could address as many of these requirements as possible.
The hardware approach that you’re taking may not even be the most appropriate. It may be better, or even necessary, to go for a weigh-station plus base unit solution using LoRa or a SIM based hardware solution. It’s impossible to say without information.

You are wasting your time (and mine) by attempting to code a solution at this stage. You need to define each requirement in detail before you start the process.

Pete.