summaryrefslogtreecommitdiffstats
path: root/f040_weather.ino
blob: 1786d64a2fe3cafac01f1d58239ce49176146c92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define SEALEVELPRESSURE_HPA (1021)
bool weatherInited = false;
int previousWeather = -69420;
int weatherUpdateInterval = 10;
Adafruit_BME280 bme1;
Adafruit_BME280 bme2;
unsigned long delayTime;
BH1750 lightMeter1(0x23);
BH1750 lightMeter2(0x5C);
void weatherInit() {
  Wire.begin(D3, D4);
  if (razhroscevanje) Serial.println(F("BME280 initialising ..."));
  unsigned status[2];
  status[0] = bme1.begin(0x76);
  status[1] = bme2.begin(0x77);
  if (!status[0]) {
    if(razhroscevanje) {
      Serial.println("[1] Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
      Serial.print("SensorID was: 0x"); Serial.println(bme1.sensorID(),16);
      Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
      Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
      Serial.print("        ID of 0x60 represents a BME 280.\n");
      Serial.print("        ID of 0x61 represents a BME 680.\n");
      Serial.println();
    }
  }
  if (!status[1]) {
    if(razhroscevanje) {
      Serial.println("[1] Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
      Serial.print("SensorID was: 0x"); Serial.println(bme2.sensorID(),16);
      Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
      Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
      Serial.print("        ID of 0x60 represents a BME 280.\n");
      Serial.print("        ID of 0x61 represents a BME 680.\n");
      Serial.println();
    }
  }
  float lux1 = lightMeter1.readLightLevel();
  float lux2 = lightMeter2.readLightLevel();
  if(razhroscevanje) {
    Serial.print("[1] Temperature = ");
    Serial.print(bme1.readTemperature());
    Serial.println(" *C");
    Serial.print("[1] Pressure = ");
    Serial.print(bme1.readPressure() / 100.0F);
    Serial.println(" hPa");
    Serial.print("[1] Approx. Altitude = ");
    Serial.print(bme1.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");
    Serial.print("[1] Humidity = ");
    Serial.print(bme1.readHumidity());
    Serial.println(" %");
    Serial.print("[1] Luminance= ");
    Serial.print(lightMeter1.readLightLevel());
    Serial.println(" lux");
    Serial.println();
    Serial.print("[2] Temperature = ");
    Serial.print(bme2.readTemperature());
    Serial.println(" *C");
    Serial.print("[2] Pressure = ");
    Serial.print(bme2.readPressure() / 100.0F);
    Serial.println(" hPa");
    Serial.print("[2] Approx. Altitude = ");
    Serial.print(bme2.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");
    Serial.print("[2] Humidity = ");
    Serial.print(bme2.readHumidity());
    Serial.println(" %");
    Serial.print("[2] Luminance= ");
    Serial.print(lightMeter2.readLightLevel());
    Serial.println(" lx");
    Serial.println();
  }
  if (lightMeter1.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2)) {
    if (razhroscevanje )Serial.println(F("[1] BH1750 Advanced begin"));
  }  else {
    if (razhroscevanje )Serial.println(F("[1] Error initialising BH1750"));
  }
  if (lightMeter2.begin(BH1750::CONTINUOUS_HIGH_RES_MODE_2)) {
    if (razhroscevanje )Serial.println(F("[2] BH1750 Advanced begin"));
  }  else {
    if (razhroscevanje )Serial.println(F("[2] Error initialising BH1750"));
  }
  if(!SPIFFS.exists("/www/var/weather.csv")) {
    writefile("/www/var/weather.csv", "ura,temperatura1,pritisk1,visina1,vlaznost1,temperatura2,pritisk2,visina2,vlaznost2,svetlost1,svetlost2\n");
  }
  if(readfile("/403/weatherUpdateInterval.txt").length() < 1) {
    writefile("/403/weatherUpdateInterval.txt", "10");
  }
  weatherInited = true;
  previousWeather = now();
}
void weatherHeartbeat () {
  if(weatherInited == false) {
    weatherInit();
  } else {
    if(previousWeather + weatherUpdateInterval < now()) {
      weatherUpdateInterval = readfile("/403/weatherUpdateInterval.txt").toInt(); // for updating the time
      File file = SPIFFS.open("/www/var/weather.csv", "a+");
      if (!file) {
        if (razhroscevanje) Serial.println("There was an error opening the file for writing (weather.csv)");
        return;
      }
      String temperatura1 = String(bme1.readTemperature());
      String pritisk1 = String(bme1.readPressure() / 100.0F);
      String visina1 = String(bme1.readAltitude(SEALEVELPRESSURE_HPA));
      String vlaznost1 = String(bme1.readHumidity());
      String temperatura2 = String(bme2.readTemperature());
      String pritisk2 = String(bme2.readPressure() / 100.0F);
      String visina2 = String(bme2.readAltitude(SEALEVELPRESSURE_HPA));
      String vlaznost2 = String(bme2.readHumidity());
      String svetlost1 = String(lightMeter1.readLightLevel());
      String svetlost2 = String(lightMeter2.readLightLevel());
      if (!file.println(String(now())+","+temperatura1+","+pritisk1+","+visina1+","+vlaznost1+","+temperatura2+","+pritisk2+","+visina2+","+vlaznost2+","+svetlost1+","+svetlost2)) {
        if (razhroscevanje) Serial.println("File write failed (weather.csv)");
      }
      previousWeather = now();
    }
  }
}