BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to tidy and write code properly

Hi all, I am very new to this.
I have written some code basically by trial and error and have got it to work and send data to my phone app, however it disconnects on a regular basis and I know its my poor coding.
I learn quicker by seeing how something is done / written.

Would it be possible for someone to show me how my code should be written rather than the way I have written it?

         #include "DHT.h"
     #include <ESP8266WiFi.h>
     #include <BlynkSimpleEsp8266.h>
     #include <SimpleTimer.h>

     #include <OLEDDisplay.h>
     #include <OLEDDisplayFonts.h>
     #include <OLEDDisplayUi.h>
     #include <SH1106.h>

    #include <SH1106Wire.h>
     #include <SSD1306.h>

     #include <SSD1306Spi.h>
     #include <SSD1306Wire.h>

     #include "HX711.h"

     #define DOUT D6
     #define CLK  D5

     #define BLYNK_PRINT Serial    
     #define DHTPIN D4     // what pin we're connected to
     #define DHTTYPE DHT11   // DHT 11

     DHT dht(DHTPIN, DHTTYPE);
     SimpleTimer timer;

     int tempSet;
     int tempHigh;
     int tempValue;
     int humidValue;
     int readTemp;
     //int checkTemp;
     int readHumid;

     // Display Settings
     const int I2C_DISPLAY_ADDRESS = 0x3c;
     const int SDA_PIN = D2;
     const int SDC_PIN = D3;


    HX711 scale(DOUT, CLK);
     float calibration_factor = 104050;

     // You should get Auth Token in the Blynk App.
     // Go to the Project Settings (nut icon).
     char auth[] = "";

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




     SSD1306Wire      display(I2C_DISPLAY_ADDRESS, SDA_PIN, SDC_PIN);


     void setup()
     {
      Serial.begin(9600);
       Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

      display.init();
       display.flipScreenVertically();
       // Setup a function to be called every second
    //  timer.setInterval(1000L, sendUptime);



      scale.set_scale();
     scale.tare(); //Reset the scale to 0
     long zero_factor = scale.read_average(); //Get a baseline reading

       //BLYNK_WRITE(V8){
        // int pinData = param.asInt(); //pinData variable will store value that came via Bridge
     //}
     }





      









     void loop()
     {
        scale.set_scale(calibration_factor); //Adjust to this calibration factor
        readHumid = (dht.readHumidity()+ 30);
        readTemp = (dht.readTemperature()- 12);
      Blynk.virtualWrite(2, readTemp);
      Blynk.virtualWrite(3, readHumid);

       
      // Blynk.run();
      int c = (char)233;
     float kegRead = scale.get_units();
     String kegDisplay;
     kegDisplay = String(kegRead);

     float tempRead = readTemp;
     String tempDisplay;
     tempDisplay = String(tempRead);

     Blynk.virtualWrite(6, kegDisplay);

       display.setFont(ArialMT_Plain_24);
       display.setTextAlignment(TEXT_ALIGN_LEFT);
       display.clear();
       display.drawString(0, 0, "Temp:");
       display.drawString(70, 0, tempDisplay);
       display.drawString(50, 35, kegDisplay);
       display.drawString(110, 35, "L");
       display.drawString(0, 35, "Keg:");

       

       Serial.print("Reading: ");
       Serial.print(scale.get_units(),0);
       Serial.print(" KG"); 

       Serial.println();

       
       display.display();

       Blynk.run();
     delay(1000);

     }

Start by looking at this and then change your loop() to 2 lines of code.

Thanks, I have cleaned up my code but I still get a Disconect on my app all the time

#include <DHT.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>



#include <SH1106Wire.h>
#include <SSD1306.h>

#include <SSD1306Spi.h>
#include <SSD1306Wire.h>

#include <HX711.h>

#define DOUT D6
#define CLK  D5


#define DHTPIN D4     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE);

SimpleTimer timer;


int readTemp;
int readHumid;
int Kegtwo;




HX711 scale(DOUT, CLK);
float calibration_factor = 104050;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

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


void myTimerEvent()
{
  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  readHumid = (dht.readHumidity()+ 30);
  readTemp = (dht.readTemperature()- 12);
 Blynk.virtualWrite(6, scale.get_units());
 Blynk.virtualWrite(2, readTemp);
 Blynk.virtualWrite(3, readHumid);
 Blynk.virtualWrite(8, Kegtwo);
}



void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

 timer.setInterval(1000, myTimerEvent);

 scale.set_scale();
 scale.tare(); //Reset the scale to 0
 long zero_factor = scale.read_average(); //Get a baseline reading

}





 
 BLYNK_WRITE(V8)
{
     int pinData = param.asInt(); 
 pinData = Kegtwo;


 }








void loop()
{


  Blynk.run();
   timer.run(); // Initiates SimpleTimer


}

@plotto looks like you are using an ESP in standalone mode, which ESP?

By default Blynk set the widgets that have a refresh frequency to 1 second intervals. Your system will run better if you change all these widgets to PUSH frequency in the app. You can do this because you are using SimpleTimer at 1s intervals.

You should probably increase the 1s to 10s or more as surely you don’t need to know temperature and humidity values every second.

What widget is V8?

Could be an issue with the OLED library as some don’t work too well with ESP’s. Try disabling the OLED to see if that improves things.

I have OLED’s here and if you are still struggling I can probably load up your revised sketch to see how it runs on my ESP’s.

Hi, Thank you for the quick reply.
I am using the Wemos D1 mini.
I have found, through trial and error, that its the ‘bridge’ function that causes the disconnect.

Even when the Bridge function does work, its very erratic with its readings. It would read correct then jump to a random number then back again.

I think the best option will be to use the same Auth code for each device and assign different virtual pins. As I only read from them and do not send commands to the devices.

I have also removed the OLED function. Which didnt make a difference either.

Thanks again

1 Like