My first weather station

And I need the sketch :rofl:

I hope that Kastler doesn’t mind. He had attached it here briefly, but now removed it.

#include <Blynk.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define BLYNK_PRINT Serial
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 600 //set deep sleep time (in seconds)
#include <BlynkSimpleEsp32.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <SimpleTimer.h>
BME280I2C bme;
char auth[] = “”; // Blynk projekt token
char ssid[] = “”; // wifi SSID
char pass[] = “”; // wifi password
char server[] = “blynk-cloud.com”; // blynk server
unsigned int port = 8442; // blynk server port
const float pi = 3.14159265; // Pi
int period = 5000; // mĂ©rĂ©si idƑköz (ezred mĂĄsodperc)
int delaytime = 2000; // Time between samples (miliseconds)
int radius = 90; // szélkerék sugara mm-ben
int Wdir = 0;
unsigned int Sample = 0; // Sample number
unsigned int counter = 0; // magnet counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (m/s)
float windspeed = 0; // Wind speed (km/h)
int measurement = 0;
int pinValue = 1;
const int Fesz = 34; // voltage detection pin number
const int rainGauge = 14; // Rain pin number
const int szelseb = 35; // wind speed pin number
const int UVSen = 36; // UV Index detection pin number
const int szelPin = 39; // Wind direction pin number
float rainAmount = .3; // Csapadék növelés értéke
float totalAmount;
float voltage = (((float)analogRead(Fesz)*(3.3/4095))2)+0.24; // battery voltage
float uvindex = ((float)analogRead(UVSen)(3.3/4095))*10; // UV Index GUVA-S12SD
float szelir = ((float)analogRead(szelPin)); // Wind direction
#ifdef __cplusplus
extern “C” {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();
BlynkTimer timer;
void setup()
{
// Debug console
pinMode(rainGauge, INPUT);
pinMode(szelseb, INPUT);
pinMode(szelPin, INPUT);
pinMode(UVSen, INPUT);
pinMode(Fesz, INPUT);
float szelir = ((float)analogRead(szelPin)); // Széliråny analóg adat olvasåsa
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Blynk.connectWiFi(ssid, pass); //conect to wifi
Blynk.config(auth, server, port);
Blynk.connect(); //connect to blynk server
ArduinoOTA.setHostname(“Idojaras”);
ArduinoOTA.begin();
Blynk.setProperty(V1, “rotation”, Wdir);
while(!Serial) {}
Wire.begin();
while(!bme.begin()) //start BME 280
{
Serial.println(“Could not find BME280 sensor!”);
delay(1000);
}
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println(“Found BME280 sensor! Success.”);
break;
case BME280::ChipModel_BMP280:
Serial.println(“Found BMP280 sensor! No Humidity available.”);
break;
default:
Serial.println(“Found UNKNOWN sensor! Error!”);
}
timer.setInterval(300000L, sendSensor); //set timer to execute void sendSensor()
Sample++;
timer.setInterval(500L, WindDir);
timer.setInterval(500L, RainCalc);
timer.setInterval(500L, windvelocity);
timer.setInterval(500L, RPMcalc);
timer.setInterval(500L, WindSpeed);
timer.setInterval(500L, SpeedWind);
}
void loop()
{
Blynk.run(); //start the Blynk sketch
timer.run(); //start the timer
ArduinoOTA.handle();
}
void sendSensor()
{
float temp(NAN), hum(NAN), pres(NAN);
measurement = hallRead();
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); // read BME 280
BME280::PresUnit presUnit(BME280::PresUnit_hPa); // read BME 280
bme.read(pres, temp, hum, tempUnit, presUnit); // read BME 280
float voltage = (((float)analogRead(Fesz)*(3.3/4095))2)+0.24; // battery voltage
float uvindex = ((float)analogRead(UVSen)(3.3/4095))*10; // UV Index
float szelir = ((float)analogRead(szelPin)); // Széliråny
double gamma = log(hum / 100) + ((17.62 * temp) / (243.5 + temp)); // calculate dewpoint
double dp = 243 * gamma / (17.62 - gamma); // calculate dewpoint
long rssi = WiFi.RSSI(); // read signal strength
Blynk.setProperty(V1, “rotation”, Wdir); //SzĂ©lirĂĄny
Blynk.virtualWrite(V1, 1);
Blynk.virtualWrite(V2, temp); //temperature
Blynk.virtualWrite(V3, windspeed); //Wind speed
Blynk.virtualWrite(V4, hum); //humidity
Blynk.virtualWrite(V5, totalAmount); //Rain amount
Blynk.virtualWrite(V6, pres); //pressure
Blynk.virtualWrite(V7, measurement); //hallsensor
Blynk.virtualWrite(V8, szelir); //Wind direction binary (test function)
Blynk.virtualWrite(V9, (temprature_sens_read() - 32) / 1.8); //CPU Temp
Blynk.virtualWrite(V10, (temprature_sens_read() - 32) / 1.8); //CPU Temp
Blynk.virtualWrite(V11, voltage); //Battery Voltage
Blynk.virtualWrite(V12, uvindex); //UV index
Blynk.virtualWrite(V13, dp); //Dew point
Blynk.virtualWrite(V14, dp); //Dew point
Blynk.virtualWrite(V15, rssi); //rssi
Blynk.virtualWrite(V16, rssi); //rssi
// Serial.println(“Mentem aludni
”);
// delay(500);
// esp_sleep_enable_timer_wakeup((uint64_t)TIME_TO_SLEEP * uS_TO_S_FACTOR); //set deepsleep time
// esp_deep_sleep_start(); //deepsleep start
// delay(100);
}
void WindDir()
{
float szelir = ((float)analogRead(szelPin)); // Széliråny analóg adat olvasåsa
if (szelir < 190) Wdir =270; // nyugati
else if (szelir < 420) Wdir = 315; // Ă©szaknyugati
else if (szelir < 700) Wdir = 292.5; // nyugat-Ă©szaknyugati
else if (szelir < 900) Wdir = 0; // Ă©szaki
else if (szelir < 1300) Wdir = 337.5; // Ă©szak-Ă©szaknyugati
else if (szelir < 1500) Wdir = 225; // délnyugati
else if (szelir < 1600) Wdir = 247.5; // nyugat-délnyugati
else if (szelir < 2250) Wdir = 45; // Ă©szakkeleti
else if (szelir < 2400) Wdir = 22.5; // Ă©szak-Ă©szakkeleti
else if (szelir < 2900) Wdir = 180; // déli
else if (szelir < 3100) Wdir = 202.5; // dél-délnyugati
else if (szelir < 3400) Wdir = 135; // délkeleti
else if (szelir < 3600) Wdir = 157.5; // dél-délkeleti
else if (szelir < 3840) Wdir = 90; // keleti
else if (szelir < 3950) Wdir = 67,5; // kelet-Ă©szakkeleti
else if (szelir < 4095) Wdir = 112.5; // kelet-délkeleti
else Wdir = 0;
Blynk.setProperty(V1, “rotation”, Wdir); // V1 image rotating to wind direction
Blynk.virtualWrite(V1, 1); // V1 image number
Blynk.virtualWrite(V8, szelir); // wind direction binary (test function)
}
void RainCalc()
{
attachInterrupt(digitalPinToInterrupt(rainGauge),addrain, RISING);
Blynk.virtualWrite(V5, totalAmount); // Rain amount
}
void addrain(){
totalAmount += rainAmount;
}
void windvelocity(){
speedwind = 0;
windspeed = 0;
counter = 0;
attachInterrupt(digitalPinToInterrupt(szelseb),addcount, RISING);
unsigned long millis();
long startTime = millis();
while(millis() < startTime + period) {
}
}
void RPMcalc(){
RPM=((counter)*60)/(period/1000); // Calculate revolutions per minute (RPM)
}
void WindSpeed(){
windspeed = ((4 * pi * radius * RPM)/60) / 1000; // Calculate wind speed on m/s
}
void SpeedWind(){
speedwind = (((4 * pi * radius * RPM)/60) / 1000)*3.6; // Calculate wind speed on km/h
Blynk.virtualWrite(V3, speedwind); // Wind speed sending
}
void addcount(){
counter++;
}


2 Likes

I scrambled the code from this forum too. This project is not finalized yet.

1 Like

Thank you !

Does this code actually produce sensible results?

In my opinion, the attachInterrupt commands should be in your void setup, and you should be using volatile variable types within your interrupt service routines.
You may also want to use nointerrupts() and interrupts() in your timed wind speed and rainfall calculations to ensure that the processing of these functions isn’t interrupted by an interrupt (if you know what I mean).
As a result, I’d aim to keep these timed calculation routines as slick as possible, so I’d do all the Pi x diameter calculation stuff once and hard code the value rather than recalculating it on the fly every time.

I found that I needed to debounce the contacts on the reed switch of the rainfall tipping bucket gauge, as I was getting multiple interrupts per tip of the bucket. The denouncing was done using standard debounce code.

I struggled a bit with what I wanted to see on my rainfall figures, so eventually settled for calculating rainfall in mm during the last minute, hour and rolling 24 hour periods. This required two arrays, one with 60 elements and another with 24.
This works well, but obviously when your MCU starts-up it takes 24 hours before the rolling rainfall data is fully populated.

Pete.

1 Like

could point to reading rain sensor (instead of Attachinterrupt) sorry my english

like that? https://www.arduino.cc/en/tutorial/debounce

Hi, you’d still attach an interrupt to the rain gauge (and the wind speed meter), but I find that I get multiple consecutive interrupts from the rain gauge when it tips. The debounce code you linked to looks fine, except that the variables should really be volatile as they are used within the ISR.
In theory, timers within ISRs are inaccurate, but in practice I find that they are fine for the short periods that are needed for debouncing.

I’m travelling in Thailand at the moment and don’t have access to my weather station code (which is written to use MQTT rather than Blynk code anyway), so I can’t share code snippets at the moment I’m afraid.

Pete.

1 Like

This is interesting, I like it. How did you measure the battery voltage? I use Wemos D1 mini and R2 boards, the A0 pins are limited to 3.3V - what hardware are you using?
thnx
billd

I’d use a simple two resistor voltage divider.

Pete.

1 Like

Yep, just been playing with the PSU and V divider . . . was hoping for a ‘magic’ bullet, I have my solar battery charger in a very tight enclosure . . . will have to get a bigger box, but will be worth it I reckon. Coming up to the w/e, it will keep me occupied :wink:

Your project looks awesome, but I don’t have hardware to build it.
So I just like to contribute somehow, hopefully I’ll build it later, by modifying your code, posted by @magnum1795, as follows.

It’s compiled OK. Hopefully it’ll run OK as well.

Note
It’s currently written for ESP32, to use Blynk_WM library, SSL as well as local Blynk Server.
Please change the following #define’s (to true or false) to suit your application

#define USE_BLYNK_WM          true
#define USE_SSL               false
#define USE_LOCAL_SERVER      true

The code

#ifndef ESP32
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif

#define BLYNK_PRINT Serial

#define USE_BLYNK_WM          true
#define USE_SSL               false
#define USE_LOCAL_SERVER      true

#if USE_SSL
  #include <WiFiClientSecure.h>
#else
  #include <WiFiClient.h>
#endif
  
#if USE_BLYNK_WM

  // Not use #define USE_SPIFFS  => using EEPROM for configuration data in WiFiManager
  // #define USE_SPIFFS    false => using EEPROM for configuration data in WiFiManager
  // #define USE_SPIFFS    true  => using SPIFFS for configuration data in WiFiManager
  // Be sure to define USE_SPIFFS before #include <BlynkSimpleEsp8266_WM.h>
  
  //#define USE_SPIFFS                  true
  #define USE_SPIFFS                  false
  
  #if (!USE_SPIFFS)
    // EEPROM_SIZE must be <= 4096 and >= CONFIG_DATA_SIZE (currently 172 bytes)
    #define EEPROM_SIZE    (4 * 1024)
    // EEPROM_START + CONFIG_DATA_SIZE must be <= EEPROM_SIZE
    #define EEPROM_START   2048
  #endif
  
  // Force some params in Blynk, only valid for library version 1.0.1 and later
  #define TIMEOUT_RECONNECT_WIFI                    10000L
  #define RESET_IF_CONFIG_TIMEOUT                   true
  #define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET    5
  // Those above #define's must be placed before #include <BlynkSimpleEsp8266_WM.h>
   
  #if USE_SSL
    #include <BlynkSimpleEsp32_SSL_WM.h>          // https://github.com/khoih-prog/Blynk_WM
  #else
    #include <BlynkSimpleEsp32_WM.h>              // https://github.com/khoih-prog/Blynk_WM
  #endif
  
#else
  #if USE_SSL
    #include <BlynkSimpleEsp32_SSL.h>
    #define BLYNK_SERVER_HARDWARE_PORT    9443    // blynk server SSL port
  #else
    #include <BlynkSimpleEsp32.h>
    #define BLYNK_SERVER_HARDWARE_PORT    8080    // blynk server port
  #endif

  char ssid[]   = ""; // wifi SSID
  char pass[]   = ""; // wifi password

  #if USE_LOCAL_SERVER
    char auth[]   = "";                     // Local Blynk server token
    char server[] = "accout.duckdns.org";   // blynk server
  #else
    char auth[]   = "";                     //Cloud Blynk server token
    char server[] = "blynk-cloud.com";      // Cloud Blynk server
  #endif

#endif
#include <WidgetRTC.h>

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BME280I2C.h>                //https://github.com/finitespace/BME280/tree/master/src
#include <Wire.h>

BME280I2C bme;

#define uS_TO_S_FACTOR      1000000
#define TIME_TO_SLEEP       600                       //set deep sleep time (in seconds)

#define COUNT_PERIOD        5000                      // measure interval (ms)
#define RAIN_COUNT_PERIOD   ( 24L * 3600L * 1000L )   // One day

// 50ms debouncing time
#define DEBOUNCE_TIME       50

#define RADIUS            (90.0f)         // in mm
#define PIxRADIUS         (PI * 90.0)
#define RAIN_AMOUNT       (0.3f)          // Precipitation

#define VOLT_DETECTION_PIN    34  // voltage detection pin number
#define RAIN_GAUGE_PIN        14  // Rain pin number
#define WIND_SPEED_PIN        35  // wind speed pin number
#define UV_INDEX_PIN          36  // UV Index detection pin number
#define WIND_DIRECTION_PIN    39  // Wind direction pin number

#define BLYNK_VPIN_WIND_DIRECTION       V1
#define BLYNK_VPIN_TEMP                 V2
#define BLYNK_VPIN_WIND_SPEED           V3
#define BLYNK_VPIN_HUMID                V4
#define BLYNK_VPIN_RAIN_AMOUNT          V5
#define BLYNK_VPIN_PRESSURE             V6
#define BLYNK_VPIN_HALL_SENSOR          V7
#define BLYNK_VPIN_WIND_DIRECTION_BIN   V8
#define BLYNK_VPIN_CPU_TEMP1            V9
#define BLYNK_VPIN_CPU_TEMP2            V10
#define BLYNK_VPIN_BATT_VOLT            V11
#define BLYNK_VPIN_UV_INDEX             V12
#define BLYNK_VPIN_DEW_POINT1           V13
#define BLYNK_VPIN_DEW_POINT2           V14
#define BLYNK_VPIN_RSSI1                V15
#define BLYNK_VPIN_RSSI2                V16

//int delaytime         = 2000;     // Time between samples (miliseconds)

int Wdir              = 0;
int measurement       = 0;
int pinValue          = 1;

unsigned int Sample   = 0; // Sample number
unsigned int RPM      = 0; // Revolutions per minute

float windSpeedMetrePerSec    = 0; // Wind speed (m/s)
float windSpeedKmPerHr        = 0; // Wind speed (km/h)


float voltage                 = (((float)analogRead(VOLT_DETECTION_PIN) * (3.3 / 4095)) * 2) + 0.24; // battery voltage
float UVIndex                 = ((float)analogRead(UV_INDEX_PIN) * (3.3 / 4095)) * 10; // UV Index GUVA-S12SD
float windDirection           = ((float)analogRead(WIND_DIRECTION_PIN)); // Wind direction

volatile unsigned int counter  = 0; // magnet counter for sensor
volatile unsigned int currentCount;
volatile float totalAmount     = 0;
volatile float currRainAmount;
volatile long startTime;
volatile bool windDataReady = false;
volatile bool rainDataReady = false;

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif

uint8_t temprature_sens_read();
BlynkTimer timer;
WidgetRTC  rtc;

BLYNK_CONNECTED()
{
  rtc.begin();
  Blynk.syncAll();
}

void IRAM_ATTR RainSensor();
void IRAM_ATTR WindSpeedSensor();

void setup()
{
  // Debug console
  pinMode(RAIN_GAUGE_PIN, INPUT);
  pinMode(WIND_SPEED_PIN, INPUT);
  pinMode(WIND_DIRECTION_PIN, INPUT);
  pinMode(UV_INDEX_PIN, INPUT);
  pinMode(VOLT_DETECTION_PIN, INPUT);

  float windDirection = ((float)analogRead(WIND_DIRECTION_PIN));

  Serial.begin(115200);
  
  #if USE_BLYNK_WM
    Blynk.begin("ESP32-Weather-Station");
  #else
    WiFi.mode(WIFI_STA);
    Blynk.connectWiFi(ssid, pass);
    Blynk.config(auth, server, BLYNK_SERVER_HARDWARE_PORT);
    Blynk.connect();
  #endif

  ArduinoOTA.setHostname("Weather-Station");
  ArduinoOTA.begin();

  Blynk.setProperty(BLYNK_VPIN_WIND_DIRECTION, "rotation", Wdir);

  Wire.begin();
  while (!bme.begin()) //start BME 280
  {
    Serial.println("Could not find BMP280 sensor!");
    delay(1000);
  }

  switch (bme.chipModel())
  {
    case BME280::ChipModel_BME280:
      Serial.println("Found BMP280 sensor! Success.");
      break;
    case BME280::ChipModel_BMP280:
      Serial.println("Found BMP280 sensor! No Humidity available.");
      break;
    default:
      Serial.println("Found UNKNOWN sensor! Error!");
  }

  timer.setInterval(300000L, sendSensor);
  Sample++;

  timer.setInterval(500L, measureAll);

  attachInterrupt(digitalPinToInterrupt(WIND_SPEED_PIN), WindSpeedSensor, RISING);
  attachInterrupt(digitalPinToInterrupt(RAIN_GAUGE_PIN), RainSensor,  RISING);
  // start timing
  startTime = millis();
}

void loop()
{
  Blynk.run(); //start the Blynk sketch
  timer.run(); //start the timer
  ArduinoOTA.handle();
}

void measureAll()
{
  WindDir();
  RainCalc();
  WindVelocity();
}

void sendSensor()
{
  static float temp(NAN), hum(NAN), pres(NAN);
  static float voltage; // battery voltage
  static float UVIndex; // UV Index
  static float windDirection; // Széliråny
  static double gamma; // calculate dewpoint
  static double dp; // calculate dewpoint
  static long rssi; // read signal strength

  measurement = hallRead();
  
  BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);  // read BME 280
  BME280::PresUnit presUnit(BME280::PresUnit_hPa);      // read BME 280
  bme.read(pres, temp, hum, tempUnit, presUnit);        // read BME 280

  voltage       = (((float)analogRead(VOLT_DETECTION_PIN) * (3.3 / 4095)) * 2) + 0.24; // battery voltage
  UVIndex       = ((float)analogRead(UV_INDEX_PIN) * (3.3 / 4095)) * 10; // UV Index
  windDirection = ((float)analogRead(WIND_DIRECTION_PIN)); // Széliråny

  gamma         = log(hum / 100) + ((17.62 * temp) / (243.5 + temp));   // calculate dewpoint
  dp            = 243 * gamma / (17.62 - gamma);                        // calculate dewpoint

  rssi          = WiFi.RSSI();                                    

  Blynk.setProperty(BLYNK_VPIN_WIND_DIRECTION, "rotation", Wdir);                             // image rotating to wind direction
  Blynk.virtualWrite(BLYNK_VPIN_WIND_DIRECTION, 1);
  Blynk.virtualWrite(BLYNK_VPIN_TEMP,               temp);                                    //temperature
  Blynk.virtualWrite(BLYNK_VPIN_WIND_SPEED,         windSpeedKmPerHr);                        //Wind speed
  Blynk.virtualWrite(BLYNK_VPIN_HUMID,              hum);                                     //humidity
  Blynk.virtualWrite(BLYNK_VPIN_RAIN_AMOUNT,        totalAmount);                             //Rain amount
  Blynk.virtualWrite(BLYNK_VPIN_PRESSURE,           pres);                                    //pressure
  Blynk.virtualWrite(BLYNK_VPIN_HALL_SENSOR,        measurement);                             //hallsensor
  Blynk.virtualWrite(BLYNK_VPIN_WIND_DIRECTION_BIN, windDirection);                           //Wind direction binary (test function)
  Blynk.virtualWrite(BLYNK_VPIN_CPU_TEMP1,          (temprature_sens_read() - 32) / 1.8);     //CPU Temp
  Blynk.virtualWrite(BLYNK_VPIN_CPU_TEMP2,          (temprature_sens_read() - 32) / 1.8);     //CPU Temp
  Blynk.virtualWrite(BLYNK_VPIN_BATT_VOLT,          voltage);                                 //Battery Voltage
  Blynk.virtualWrite(BLYNK_VPIN_UV_INDEX,           UVIndex);                                 //UV index
  Blynk.virtualWrite(BLYNK_VPIN_DEW_POINT1,         dp);                                      //Dew point
  Blynk.virtualWrite(BLYNK_VPIN_DEW_POINT2,         dp);                                      //Dew point
  Blynk.virtualWrite(BLYNK_VPIN_RSSI1,              rssi);                                    //rssi
  Blynk.virtualWrite(BLYNK_VPIN_RSSI2,              rssi);                                    //rssi

  // Serial.println("Sleeping now !);
  // delay(500);
  // esp_sleep_enable_timer_wakeup((uint64_t)TIME_TO_SLEEP * uS_TO_S_FACTOR); //set deepsleep time
  // esp_deep_sleep_start(); //deepsleep start
  // delay(100);
}

void WindDir()
{
  float windDirection = ((float)analogRead(WIND_DIRECTION_PIN));
  
  if (windDirection < 190)
    Wdir = 270;       // W
  else if (windDirection < 420)
    Wdir = 315;       // NW
  else if (windDirection < 700)
    Wdir = 292.5;     // W-NW
  else if (windDirection < 900)
    Wdir = 0;         // N
  else if (windDirection < 1300)
    Wdir = 337.5;     // N-NW
  else if (windDirection < 1500)
    Wdir = 225;       // SW
  else if (windDirection < 1600)
    Wdir = 247.5;     // W-SW
  else if (windDirection < 2250)
    Wdir = 45;        // NE
  else if (windDirection < 2400)
    Wdir = 22.5;      // N-NE
  else if (windDirection < 2900)
    Wdir = 180;       // S
  else if (windDirection < 3100)
    Wdir = 202.5;     // S-SW
  else if (windDirection < 3400)
    Wdir = 135;       // SE
  else if (windDirection < 3600)
    Wdir = 157.5;     // S-SE
  else if (windDirection < 3840)
    Wdir = 90;        // E
  else if (windDirection < 3950)
    Wdir = 67, 5;     // E-NE
  else if (windDirection < 4095)
    Wdir = 112.5;     // E-SE
  else
    Wdir = 0;         // N

  Blynk.setProperty(BLYNK_VPIN_WIND_DIRECTION, "rotation", Wdir);       // image rotating to wind direction
  Blynk.virtualWrite(BLYNK_VPIN_WIND_DIRECTION, 1);                     // image number
  Blynk.virtualWrite(BLYNK_VPIN_WIND_DIRECTION_BIN, windDirection);     // wind direction binary (test function)
}

bool dayStart = false;

void RainCalc()
{
  static float totalRainAmount;
  
  // Reset within 10s after boot or at around beginning of every day (12AM), have 60 s to do
  if ( !dayStart && ( (millis() < 10000 ) || ( (hour() == 0) && (minute() == 0) ) ) )
  {
    dayStart      = true;

    noInterrupts();
    totalAmount   = 0;   
    interrupts();
  }

  // Prepare for next day at 1AM
  if ( dayStart && (hour() == 1) )
  {
     dayStart = false; 
  }

  noInterrupts();
  totalRainAmount = totalAmount;
  interrupts();
  
  Blynk.virtualWrite(BLYNK_VPIN_RAIN_AMOUNT, totalRainAmount); // Rain amount
}

void IRAM_ATTR RainSensor() 
{
  static long lastMillis = 0;
  static long currMillis;

  currMillis = millis();
  
  if (currMillis >= lastMillis + DEBOUNCE_TIME)
  {
    totalAmount += RAIN_AMOUNT;
    lastMillis  = currMillis;
  }
}

void WindVelocity() 
{
  if (windDataReady)
  {
    noInterrupts();
    // COUNT_PERIOD = 5000 = 5s
    windDataReady = false;
    //RPM = ((currentCount) * 60) / (COUNT_PERIOD / 1000);          // Calculate revolutions per minute (RPM)
    RPM = ( currentCount * 12 );                                    
    interrupts();
    
    //windSpeedKmPerHr = ((4 * PIxRADIUS * RPM) / 60) / 1000;       // Calculate wind speed on m/s
    windSpeedMetrePerSec = (RPM * PIxRADIUS) / 15000;               
  
    windSpeedKmPerHr = windSpeedMetrePerSec * 3.6;                  // Calculate wind speed on km/h
    Blynk.virtualWrite(BLYNK_VPIN_WIND_SPEED, windSpeedKmPerHr);    // Wind speed sending
  }
}

void IRAM_ATTR WindSpeedSensor() 
{
  static long lastMillis = 0;
  static long currMillis;

  currMillis = millis();
  
  if (currMillis >= lastMillis + DEBOUNCE_TIME)
  {
    counter++;
    lastMillis = currMillis;
  
    if (currMillis >= startTime + COUNT_PERIOD )
    {
      startTime     = currMillis;
      currentCount  = counter;
      counter       = 0;
      windDataReady = true;
    }
  }
}

Please let us know if it’s running OK.
Regards,

1 Like

Thank you very much, I’ll try and let you know.

Szép munka. :slight_smile:

1 Like

Köszönöm


I tried it, the code looks good. The XL6009 dc converter has not arrived yet, but the converter used instead has given up. :frowning: But when the XL6009 comes, I’ll test the wind speed and rain gauge, thank you very much for your effort.

1 Like

I can’t check the accuracy of the wind speed gauge but it seems good with the rain gauge. The rain gauge counts one tilt looks good. The wind gauge rolls often, I think I should look for a better place. The xl6009 converter hasn’t arrived yet. Thanks again for the code.

1 Like

3 Likes


Slowly but finally the xl6009 dc converter has arrived 


1 Like

Can’t anyone know how to upload the code to esp32 from android?