diff options
Diffstat (limited to '')
-rw-r--r-- | f040_weather.ino | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/f040_weather.ino b/f040_weather.ino new file mode 100644 index 0000000..1786d64 --- /dev/null +++ b/f040_weather.ino @@ -0,0 +1,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(); + } + } +} |