BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

My first Blynk Project with HX711 & NodeMCU

Hi
I have just started on Blynk and am trying to get my first load cell project done. I am using nodeMCU with HX711 along with a strain gauge load cell and then trying to get it connected to Blynk. While the sketch for load cell is working fine, and the serial monitor is showing the correct weights (screenshot attached with the Blynk commands commented out), connecting and updating the weight in Blynk is posing some challenge. I have set up Blynk and got the Datastream and virtual pin configured. However the Blynk app on the web / mobile is not getting updated.

Another thing I noted is that while the web dashboard for my device is showing online, the console is showing offline.

Will be grateful for any help on this. Am attaching the sketch.

Regards
Somu

#define BLYNK_TEMPLATE_ID "TMPLg_vAXLvt"
#define BLYNK_DEVICE_NAME "IOT WEIGH SCALE PROJECT"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG


#include "BlynkEdgent.h"
#include "HX711.h"


#define DOUT  D2
#define CLK  D1
 
HX711 scale(DOUT, CLK);
 
float weight;
float calibration_factor = -104625; //-104625 worked for my 40Kg max scale setup 


void setup() 
{
  
  Serial.begin(115200);
  delay(1000);
  BlynkEdgent.begin();
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  long zero_factor = scale.read_average(); //Get a baseline reading
}
 



void loop() 
{
  BlynkEdgent.run();
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  weight = scale.get_units(5); 
  Blynk.virtualWrite(V3, weight);
  delay(2000);
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println(" KG");
  Serial.println();
 }

Okay, first things first, you need to remove the code that takes the readings and displays the results from your void loop, and put it into a function that you call with a BlynkTimer.
This will allow you to remove the delay command altogether. Delays are not compatible with Blynk.

Read this for more info…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Also, I think that setting the calibration factor is something you do once, in void setup, not every time you want to take a reading from the scale.

Secondly, you have deleted the lines of code from the Blynk Edgent example that said….

// Uncomment your board, or configure a custom board in Settings.h
//#define USE_SPARKFUN_BLYNK_BOARD
//#define USE_NODE_MCU_BOARD
//#define USE_WITTY_CLOUD_BOARD
//#define USE_WEMOS_D1_MINI

What you should have done was to un-comment the line that said:

//#define USE_NODE_MCU_BOARD

Without this, the custom board settings from Settings.h are used, which assigns pin GPIO4 (pin D2) as the LED pin.
As you are already using this pin in your sketch…

this will cause problems.

Read this for more info…

Thirdly, have you been through the Edgent provisioning process in the app.
If so, did this work as expected (I imagine that the onboard LED didn’t flash as described in the instructions) ?
What do you see in your serial monitor when the board is first rebooted? (Copy/paste the text and use triple backticks in the same way that you did when you posted your sketch, rather than posting a screenshot).

Pete.

1 Like

Thanks Pete for the prompt reply.
I have tried to modify the code. but it’s now throwing this error while compiling. The error says"‘BlynkTimer’ does not name a type"

Not sure what this means.

(P.S It’s a similar code as to the previous one; the calibration code is included in the sketch)

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPLg_vAXLvt"
#define BLYNK_DEVICE_NAME "IOT WEIGH SCALE PROJECT"
#define USE_NODE_MCU_BOARD


#include "HX711.h"  //You must have this library in your arduino library folder

#define DOUT  D2
#define CLK  D1

HX711 scale(DOUT, CLK);
 
float calibration_factor = -104625; //-106600 worked for my 40Kg max scale setup 

#define BLYNK_PRINT Serial

float weight;
bool isconnected = false;

BlynkTimer timer;

void checkBlynkStatus() { // called every 2 seconds by SimpleTimer
    isconnected = Blynk.connected();
    if (isconnected == true) {
    Serial.println("Blynk Connected");
  }
  else{
    Serial.println("Blynk Not Connected");
  }
}

void setup()
{
  
  Serial.begin(115200);
  BlynkEdgent.begin();
  timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
  

  Serial.println("HX711 Calibration");
  Serial.println("Remove all weight from scale");
  Serial.println("After readings begin, place known weight on scale");
  Serial.println("Press a,s,d,f to increase calibration factor by 10,100,1000,10000 respectively");
  Serial.println("Press z,x,c,v to decrease calibration factor by 10,100,1000,10000 respectively");
  Serial.println("Press t for tare");
  scale.set_scale();
  weight = scale.get_units(5); 
  Blynk.virtualWrite(V3, weight);
  scale.tare(); //Reset the scale to 0
 
  long zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);

}



void loop() {
 
  Blynkedgent.run();
  timer.run();
  
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
 
  Serial.print("Reading: ");
  Serial.print(scale.get_units(), 3);
  Serial.print(" kg"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" calibration_factor: ");
  Serial.print(calibration_factor);
  Serial.println();
 
  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 10;
    else if(temp == '-' || temp == 'z')
      calibration_factor -= 10;
    else if(temp == 's')
      calibration_factor += 100;  
    else if(temp == 'x')
      calibration_factor -= 100;  
    else if(temp == 'd')
      calibration_factor += 1000;  
    else if(temp == 'c')
      calibration_factor -= 1000;
    else if(temp == 'f')
      calibration_factor += 10000;  
    else if(temp == 'v')
      calibration_factor -= 10000;  
    else if(temp == 't')
      scale.tare();  //Reset the scale to zero
  }
}

Well, I was going to say that this is because your line of code that reads…

BlynkTimer timer;

needs to be after the line of code that says…

#include "BlynkEdgent.h"

but, it appears that you’ve deleted that line of code from your latest sketch, and without that the sketch will never work.
You also seem to have added more junk to your void loop, and set-up a BlynkTimer to call a function called checkBlynkStatus which contains code that is nothing to do with taking readings from your scale.

I’d suggest that you go back to the original Blynk Edgent ESP8288 sketch and don’t delete anything from it.
Uncomment the NodeMCU board type, add the BlynkTimer code and use it to call a function that contains the stuff that was in the void loop of your original sketch.
Do not include the additional stuff that you’ve added to the void loop of your second sketch.

Pete.

1 Like

Right. So cleared up the mess as advised. The sketch is compiling alright now, but now there is no output in either the Serial Monitor or Blynk web d/b. Have tried all combinations that i have commented out in the sketch below.

#define BLYNK_TEMPLATE_ID "TMPLg_vAXLvt"
#define BLYNK_DEVICE_NAME "IOT WEIGH SCALE PROJECT"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define USE_NODE_MCU_BOARD

#define BLYNK_PRINT Serial

#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

#include "HX711.h"


#define DOUT  D2
#define CLK  D1
 
HX711 scale(DOUT, CLK);
 
float weight;
float calibration_factor = -104625; //-104625 worked for my 40Kg max scale setup 



void setup() 
{
  Serial.begin(115200);
  delay(1000);
  BlynkEdgent.begin();
  timer.setInterval(1000L, weightDataSend); // check if Blynk server is connected every 1 second
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  long zero_factor = scale.read_average(); //Get a baseline reading
  //scale.set_scale(calibration_factor); //Adjust to this calibration factor
 }
 

void weightDataSend()
{
  //scale.set_scale(calibration_factor); //Adjust to this calibration factor
  //weight = scale.get_units(5);
  Blynk.virtualWrite(V3, weight);  // sending weight value to Blynk app
}




void loop() 
{
  //BlynkEdgent.run();
  //timer.run();

  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  weight = scale.get_units(5);
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println(" KG");
  Serial.println();
  
  BlynkEdgent.run();
  timer.run();

}

In all cases, it compiled w/o error, but there was no output.

You shouldn’t be defining variables in the void setup.

I think that this should be in your void setup.

This SHOULD NOT be in your void loop, it should be in weightDataSend

You’d be far better reading what I’ve previously written about the correct code structure, rather than ignoring it and moving random pieces of code into random locations within the sketch.

Pete.

Sure …let me go through then in some more detail. Actually the Arduino sketch w/o Blynk is working perfectly. (Even now when I comment out the Blynk commands, it works without a hitch.) Thought it will be intuitive and easy to hook it up with Blynk, but it seems there are nuances which i am missing. Will revert. Thanks.

Ok…so did some exploration and reading up but unfortunately without success. Here’s the sketch as advised.

#define BLYNK_TEMPLATE_ID "TMPLg_vAXLvt"
#define BLYNK_DEVICE_NAME "IOT WEIGH SCALE PROJECT"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define USE_NODE_MCU_BOARD

#define BLYNK_PRINT Serial

#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

#include "HX711.h"


#define DOUT  D2
#define CLK  D1
 
HX711 scale(DOUT, CLK);
 
float weight;
float calibration_factor = -104625; //-104625 worked for my 40Kg max scale setup 
long zero_factor;


void setup() 
{
  Serial.begin(115200);
  delay(1000);
  BlynkEdgent.begin();
  timer.setInterval(1000L, weightDataSend); // check if Blynk server is connected every 1 second
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
}
 

void weightDataSend()
{
  weight = scale.get_units();
  Blynk.virtualWrite(V3, weight);  // sending weight value to Blynk app

  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println(" KG");
  Serial.println();

  
  }



void loop() 
{

  BlynkEdgent.run();
  timer.run();


}


And here’s the serial monitor output:

Zero factor: -31898
[3935] AP SSID: Blynk IOT WEIGH SCALE PROJECT-76D49
[3935] AP IP:   192.168.4.1
[3936] AP URL:  blynk.setup

The sketch that gives a perfect serial monitor output is as under:



#define BLYNK_TEMPLATE_ID "TMPLg_vAXLvt"
#define BLYNK_DEVICE_NAME "IOT WEIGH SCALE PROJECT"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define USE_NODE_MCU_BOARD

#define BLYNK_PRINT Serial

#define APP_DEBUG

#include "BlynkEdgent.h"

BlynkTimer timer;

#include "HX711.h"


#define DOUT  D2
#define CLK  D1
 
HX711 scale(DOUT, CLK);
 
float weight;
float calibration_factor = -104625; //-104625 worked for my 40Kg max scale setup 
long zero_factor;


void setup() 
{
  Serial.begin(115200);
  delay(1000);
BlynkEdgent.begin();
//timer.setInterval(1000L, weightDataSend); // check if Blynk server is connected every 1 second
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  zero_factor = scale.read_average(); //Get a baseline reading
  Serial.print("Zero factor: "); //This can be used to remove the need to tare the scale. Useful in permanent scale projects.
  Serial.println(zero_factor);
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
}
 


void loop() 
{

//BlynkEdgent.run();
timer.run();

Blynk.virtualWrite(V3, weight);  // sending weight value to Blynk app

  weight = scale.get_units(5);
  Serial.print("Weight: ");
  Serial.print(weight);
  Serial.println(" KG");
  Serial.println();

}

which gives a perfect output in the serial monitor:

Weight: 0.00 KG

Weight: -0.00 KG

Weight: -0.00 KG

Weight: -0.01 KG

Weight: -0.02 KG

Weight: -0.02 KG

Weight: -0.01 KG

Weight: 0.00 KG

Weight: 0.23 KG

Weight: 0.56 KG

Weight: 0.62 KG

Weight: 0.63 KG

Weight: 0.64 KG

Weight: 0.64 KG

Weight: 0.64 KG

Am almost at my wits end and on the verge of quitting this project :frowning:

Neither of these serial outputs is ‘perfect’, as they show no information about your device connecting to Blynk, which is the first thing you should be seeing.

I previously asked you…

which appears to be another of the things that I’ve previously written which you’ve decided to ignore.

Pete.

@Somu when you have a NodeMCU running the Edgent sketch and you’ve correctly provisioned that device, you should see this when you connect it to the serial monitor and hit the RST button on the device…

>[386] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[388] --------------------------
[391] Product:  REDACTED
[393] Firmware: 0.1.0 (build Apr 20 2022 13:42:36)
[398] Token:    ...bGJ7
[400] Device:   ESP8266 @ 80MHz
[403] MAC:      30:83:98:AE:46:5E
[406] Flash:    16384K
[408] ESP core: 3.0.2
[410] ESP SDK:  2.2.2-dev(38a443e)
[413] Boot Ver: 31
[415] Boot Mode:1
[416] FW info:  468320/1626112, MD5:11dadcb61b737a3f738ba622af04f19a
[624] Free mem: 31000
[624] --------------------------
[624] INIT => CONNECTING_NET
[625] Connecting to WiFi: REDACTED
[4768] Using Dynamic IP: 192.168.1.20
[4768] CONNECTING_NET => CONNECTING_CLOUD
[4880] Current time: Wed Apr 20 12:44:33 2022
[4880] Connecting to blynk.cloud:443
[5788] Ready (ping: 11ms).
[5867] CONNECTING_CLOUD => RUNNING

I’d suggest that you start with the Edgent ESP8266 example, add your BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME from the template in the web console, un-comment the #define USE_NODE_MCU_BOARD then upload the sketch to your board with no other changes.

You can then open the app and hit the “+ Add Device” button and go through the provisioning process and get your device connected to Blynk.
Do reboot of the device with the RST button and ensure that you see the same serial output as I’ve posted above, and check that the device shows as online in the app and web console.

Once you have this completed, you can then add-in the additional code that relates to the BlynkTimer and HX711 to the sketch and re-upload it. You won’t need to re-provision that particular board again, provided you upload with the Tools > Erase Flash option set to “Only Sketch”.

Pete.

Hi Pete

My apologies for the late reply. Have been doing some reading and was trying out some options. Alright, got the project to run successfully and its updating both the web and mobile dashboard. However the option of connecting over the air w/o using the user id and password didn’t work. Couldn’t set up the device on the mobile for some reason. Had to do the old fashion way w/o using the edgent functionality. Here’s the sketch for reference. Next I will try to put the the nodemcu to deep sleep and see if i can run this weigh scale off a battery. Many thanks for all the help and suggestions.

/*************************************************************

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// 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"


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


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


char auth[] = BLYNK_AUTH_TOKEN;

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

const char* host = "Hidden";

BlynkTimer timer;

HX711 scale;
// HX711 circuit wiring
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;

boolean alreadyRun = false; 
int upperLimit = 2; 
float calibration_factor = -104625; 
float weight;



void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

   
  Serial.println("Initializing the scale");
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  scale.tare(); //Reset the scale to 0
  long zero_factor = scale.read_average(); //Get a baseline reading
  
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  Serial.println("Readings:"); 

  timer.setInterval(1000L, sensorDataSend); //timer will run every sec 
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

void sensorDataSend()
{
  Serial.print("The weight in kg is:\t");
  weight = (scale.get_units(5));   // Assign Weight to reading
  Serial.println(weight);

  Blynk.virtualWrite(V3, weight);  // sending weight value to Blynk app

  if(weight > upperLimit && alreadyRun == false)
  {
    sendSMS();
    alreadyRun = true;
  }
  
  else 
  {
   Serial.println("Still running loop()");
   delay(1000);
        
   if(weight < upperLimit && alreadyRun == true)
   {
    alreadyRun = false;
   } 
  }

}

void sendSMS() {
    Serial.println("Initiating to send SMS");
    WiFi.begin(ssid, pass);
    Serial.println(" ");
    Serial.print("Waiting to connect to WiFi....");
    while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    Serial.print(".");
    }
    Serial.print("");
    Serial.print("Connected to ");
    Serial.print(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    delay(1000);
   
    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.print("SMS Sent");
    delay(1000);
    }
  
    client.flush();
    client.stop();  
 
  }







And here’s the output:


6798] Connected to WiFi
[6798] IP: 192.168.0.115
[6798] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[6804] Connecting to blynk.cloud:80
[6980] Ready (ping: 56ms).
Initializing the scale
Readings:
The weight in kg is:	0.00
Still running loop()
The weight in kg is:	0.00
Still running loop()
The weight in kg is:	0.00
Still running loop()
The weight in kg is:	0.00
Still running loop()
The weight in kg is:	-0.00
Still running loop()
The weight in kg is:	-0.00
Still running loop()
The weight in kg is:	-0.00
Still running loop()
The weight in kg is:	-0.00
Still running loop()
The weight in kg is:	0.00
Still running loop()
The weight in kg is:	0.00
Still running loop()

You might want to edit your last post to remove your credentials.

If you’re not going to use dynamic provisioning of WiFi credentials and auth token then there is little point in using the Edgent sketch. Edgent does give you Blynk.Air OTA updates, but this won’t work if you use deep sleep anyway.

If you’d have mentioned your desire to use deep sleep earlier then I would have pointed you towards this topic, as it would have been a much better starting point for that type of project (you need to read the whole topic from start to finish)…

Pete.

Thanks again. Have removed the credentials. Related to this, if I use my user ID/PWD and other credentials in the sketch directly, how secure are they ? Is there any chance of data leak or misuse?

Let me go through the post on deep sleep.

I’m not going to get in to a discussion about WiFi security or SSL versus non-SSL data security over the internet - it’s beyond the scope of this forum and is covers in lots of detail on the internet.

Pete.

Hi Pete,

Ok…so got the deep sleep mode working…had to make a slight change to my previous sketch (w/o deepsleep) as the timer.setInterval(1000L, sensorDataSend) function was not running the getSensorData function, but instead going into deep sleep mode before that. So moved the getSensorData function code into the setup() function and disabled the timer.setInterval(1000L, sensorDataSend) . Its working fine, but dont know why the timer.setInterval(1000L, sensorDataSend) function will not work. Any pointers will help for academic interest.

Also wanted to know if the following sketch can be used within the void setup () before the Blynk.begin(auth, ssid, pass) -to set the ssid and pwd externally from a webpage, say, instead of hardcoding it into the sketch. Have used a jumper to connect the RST with D0 pins for enabling deepsleep.

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/kentaylor/WiFiManager

 
void setup() {
   
  Serial.begin(115200);
  Serial.println("\n Starting");
  
 
  WiFi.printDiag(Serial); 
  Serial.println("Opening configuration portal");
     
  WiFiManager wifiManager;  
  
  if (WiFi.SSID()!="") wifiManager.setConfigPortalTimeout(60); 
 
  if (!wifiManager.startConfigPortal("ESP8266","password")) 
  {
     Serial.println("Not connected to WiFi but continuing anyway.");
  } 
  else 
  {
     Serial.println("connected to WiFi");
  }
      


My working sketch as of now…

// 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"


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


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


char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

HX711 scale;
// HX711 circuit wiring
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;

boolean alreadyRun = false; 
int upperLimit = 2; 
float calibration_factor = -104625; 
float weight;



void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

   
  Serial.println("Initializing the scale");
  scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  scale.set_scale();
  //scale.tare(); //Reset the scale to 0
  //long zero_factor = scale.read_average(); //Get a baseline reading
  
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  Serial.println("Readings:"); 

  //timer.setInterval(1000L, sensorDataSend); //timer will run every sec 

  Serial.print("The weight in kg is:\t");
  weight = ((scale.get_units(5))-0.42);   // Assign Weight to reading
  Serial.println(weight);

  Blynk.virtualWrite(V3, weight);  // sending weight value to Blynk app

  if(weight > upperLimit && alreadyRun == false)
  {
    sendSMS();
    alreadyRun = true;
  }
  
  else 
  {
   Serial.println("Still running loop()");
   delay(1000);
        
   if(weight < upperLimit && alreadyRun == true)
   {
    alreadyRun = false;
   } 
  }


  delay(1000);
  Serial.println("Going to Sleep");
  Serial.println();
  ESP.deepSleep(100*1000000); //deep sleep for 1.65 minutes
  
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}


void sendSMS() {
    Serial.println("Initiating to send SMS");
    WiFi.begin(ssid, pass);
    Serial.println(" ");
    Serial.print("Waiting to connect to WiFi....");
    while (WiFi.status() != WL_CONNECTED)
    {
    delay(500);
    Serial.print(".");
    }
    Serial.print("");
    Serial.print("Connected to ");
    Serial.print(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    delay(1000);
   
    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.print("SMS Sent");
    delay(1000);
    }
  
    client.flush();
    client.stop();  
 
  }

i dont like your code structure at all.
Your void loop will never be executed, so you should probably have it empty.
Your delays are wasting battery power, and I’m extremely surprised that pin V3 is being updated at all - I doubt that this update will reliably execute each time.

The alreadyRun flag is also redundant.

If you work on 100 second sleep periods then the code will probably connect quite quickly, but if you extend that then you’ll reach a point where the WiFi connection takes quite a bit longer because the router will have flushed the routing table and the device and router will need to re-negotiate a DHCP address assignment. That’s why it’s better to hard-code the IP address, gateway, DNS etc when using deep sleep.

If you want to avoid hard-coding your WiFi credentials then you’d be better going back to the Edgent sketch as the basis for your project rather than using WiFi Manager.

Pete.

Understand. But what’s the optimal code that you will suggest. If i need deep sleep , I can’t use Edgent as you mentioned in earlier post. The delays can be taken off, if that helps in conserving battery… The void loop is really not doing anything , so I can remove the timer. Can I remove blynk.run also?

The V3 is getting updated alright. It’s running on battery for the last 6 hrs or so. Not sure if it’s by sheer serendipity that it’s working and will fail if some delays, etc are changed.

The alreadyRun is required to check if an SMS has already been sent, when the weight was more than 2 kg. If SMS was sent which actually informs me to reduce weight, don’t resend another SMS. If i took action and reduced the weight to below 2 kg, reset the Boolean to false, in which case no SMS will be sent. If after that , the weight is again increased, send another SMS. That’s the logic. How else can this be handled?

Here’s the screenshot of the app dashboard

I’ve pointed you towards the Beehive Connected project, which works well. There are other good deep sleep examples too, but you threw the deep sleep requirement in at the end of the discussion, rather than at the beginning. I’m also unclear about what else you want, whether dynamic provisioning is essential, and if so why.

Your void loop never gets executed, so yes.

You don’t seem to realise that the void setup is executed just once, at startup. As you put the device to sleep at the end of void setup, there isn’t any looping process. It takes one reading, sends the SMS if needed then goes to sleep.
When it wakes up, it reboots itself and all variables are cleared, and the process repeats. There can be no multiple SMS messages per wake-up period, and the alreadyRun variable is re-initialised when the device wakes again.

Pete.

Yes , understand that part Pete regarding the SMS. It was originally in the void loop , but put it in the void set up as deep sleep required it to be in the set up code. Can you suggest any other way of coding this? The Arduino best practice suggests that deep sleep should be in the setup code and Blynk also suggests that the void loop should be kept as clean as possible.

With regards to the requirement, i am going through a learning process and see what all can be done. I am trying to add features and experiment with this product. I intend to take reading maybe once in an hour and see how it behaves. You have already indicated that this will force a complete clean up of the routing tables, etc. Would like to see how to add tare functionality through a reset button (in the void setup part?), deep sleep, OTA updates, etc.