Changing program to not loop but only work on my request

Hello everyone. I am making meteorologic station and i am using program made in arduino ide and i am sending info to blynk. My question is, how to make program run only when i send request for updating info instead of looping indefinetly, updating when i dont need to.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "Seeed_BME280.h"
#include <Wire.h>


BME280 bme280;

char auth[] = "...";

char ssid[] = "...";
char pass[] = "...";
int rainPin = A0;

void setup()
{
  
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Serial.begin(9600);
  if(!bme280.init()){
  Serial.println("Device error!");
  pinMode(rainPin, INPUT);
  }
}

void loop()
{
  Blynk.run();
  
  //get and print temperatures
  float temp = bme280.getTemperature();
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.println("C");//The unit for  Celsius because original arduino don't support speical symbols
  Blynk.virtualWrite(0, temp); // virtual pin 0
  Blynk.virtualWrite(4, temp); // virtual pin 4
  
  
  //get and print atmospheric pressure data
  float pressure = bme280.getPressure(); // pressure in Pa
  float p = pressure/100.0 ; // pressure in hPa
  Serial.print("Pressure: ");
  Serial.print(p);
  Serial.println("hPa");
  Blynk.virtualWrite(1, p); // virtual pin 1

  
  //get and print altitude data
  float altitude = bme280.calcAltitude(pressure);
  Serial.print("Altitude: ");
  Serial.print(altitude);
  Serial.println("m");
  Blynk.virtualWrite(2, altitude); // virtual pin 2

  //get and print humidity data
  float humidity = bme280.getHumidity();
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  Blynk.virtualWrite(3, humidity); // virtual pin 3
  
  //read analog rain value
  int rainValue = 1024-analogRead(rainPin);
  Serial.print(rainValue);
  Blynk.virtualWrite(5, rainValue); // virtual pin 5


  delay(15000);
}

this is the program. delay 15s is set so low only to check if it works, so i dont have to wait for too long.

thanks for any responses

This code will not work - the main reason I’ve noticed is the delay(15000) command. Blynk CAN’T wait that long, it needs CPU for it’s own tasks. The same with ESP alone, but in this case delay() gives control to internal tasks. You need to look for examples, where BlynkTimer (a SimpleTimer earlier) takes control of timing.

Also be prepared to see ā€œnot meteo compliantā€ data here. You will see the actual air pressure. Unless you live on an altitude close to ā€˜0’ it will not be equal to those given by meteo stations.

It actually works :slight_smile: I already uploaded it and it sends info every 15sec to my blynk app. Before i added delay this line was there:
ESP.deepSleep(5 * 60 * 1000000); but it didnt work, i just did get some symbols in serial monitor, it wrote the temp and etc once and then went silent.
To that pressure. Can I do something with it? To give real and accurate nubers? Also what would you suggest to use insead of delay if i shouldnt use it

A simple function to ā€˜convert’ the actual pressure to sealevel pressure used in meteo:

float readSealevelPressure(float altitude_meters) {
  float pressure = Sensor.readFloatPressure();
  return (pressure / pow(1.0 - altitude_meters / 44330, 5.255));
}

Call it with argument:

PMSL = readSealevelPressure(132) / 100;

where 132 is height above sea level (where I live :wink: ).

Well, good for you… It never worked for me - ESP resets itself. May it be there were some changes to SDK? I’m not using delays () above some 100ms nowadays.

Just look at the Blynk/GettingStarted/PushData example. There is the mentioned BlynkTimer (SimpleTimer), which is a great tool for timing multitude of tasks

Thanks for a quick response. I took a look on your code and i will try to use it but i gotta understand it well first. Cuz I am going to present this work in school and i gotta be able to reason about every line why is it writen the way it is. I will definietly look into it, thanks again

That isn’t actually my code. It comes from working BMP085 library, which I adopted for BMx280 as it lacks this important calculations. For source look for Barometric formula in the internet.

1 Like

@Rengatharu As with any proper School projects… start with Research :stuck_out_tongue_winking_eye:

Blynk - RE-SEAR-CH

As demonstrated ALL OVER this forum… use timers and dedicated functions. DON’T fill the void loop() with all your running code, and avoid unnecessary delay() commands. Blynk has recently made constant Flood Errors a thing of the past (almost), but it is still not a solution to proper programming practice.

Is the reason for only wanting to send data to Blynk every 15 seconds to save battery power? If so then it makes no real difference to battery life how often you send data, unless you put your ESP to sleep in the meantime. Changing the code to only supply data to Blynk when you press a button on the app won’t save battery life either, as the ESP still needs to be running to respond to those requests for data.

If you do want a battery powered data logger then search for ā€œdeep sleepā€ on this forum (not on google).

Pete.

use a Blynk Button and have all your sensor scanning in the associated BLYNK_WRITE() function.

That way it only scans when you press the button.

As per the links I sent and the Documents, Help Center and Sketch Builder examples, etc…

BLYNK_WRITE(vPin) {  // This function runs whenever you press the associated Button
  if (param.asInt() == 1) { // only run when button state is 1, so as not to run 2nd time when releasing the button

// Put all your sensor scanning code here
// followed by your Blynk.virtualWrite(vPin, value) commands to send the results to the App

  }
}

Won’t help with any battery / sleep mode though, since the device needs to be online and connected to Blynk in order to sense the button press.

Thanks for all replies, i looked into it, and already tried the blynk_write… Sadly now i have some HW problems so i have to postpone this but will be handy