diff --git a/README.org b/README.org index ffd7248..f00ca91 100644 --- a/README.org +++ b/README.org @@ -1,12 +1,6 @@ * Muraqib -Temperature and humidity sensor that sends its data via MQTT. The project is implemented using a Wemos D1 mini board with the following components: - -- DHT22 temperature and humidity sensor -- Battery shield -- 3.7V 1000mAh Lithium battery - -The sensor captures the data every 5 minutes and sends it to a MQTT broker and then enters deep sleep, it is important to save power as the device runs off a battery. This sensor is part of a project to grow mushrooms in a controlled environment. +CO2 sensor that collects and sends its data via MQTT to a server. The board of the sensor is the Wemos D1 mini and the sensor is a SCD41. ** Pinout of the board @@ -14,8 +8,7 @@ The sensor captures the data every 5 minutes and sends it to a MQTT broker and t [[./pinout.png]] ** Dependencies -- [[https://github.com/adafruit/DHT-sensor-library][Adafruit DHT sensor library]] -- [[https://github.com/adafruit/Adafruit_Sensor][Adafruit Unified Sensor Driver]] +- [[https://github.com/Sensirion/arduino-i2c-scd4x][Sensirion I²C SCD4X Arduino Library]] - [[https://github.com/knolleary/pubsubclient][PubSubClient]] - [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]] ** Configuration diff --git a/platformio.ini b/platformio.ini index df3ccbd..12e5591 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,8 +13,9 @@ platform = espressif8266 board = d1_mini board_build.filesystem = littlefs framework = arduino +monitor_filters = esp8266_exception_decoder lib_deps = - adafruit/DHT sensor library@^1.4.6 - adafruit/Adafruit Unified Sensor@^1.1.15 + sensirion/Sensirion I2C SCD4x@^1.1.0 knolleary/PubSubClient@^2.8 bblanchon/ArduinoJson@^7.4.1 + diff --git a/src/main.cpp b/src/main.cpp index 08738e7..70820e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,38 +1,64 @@ #include "config.h" #include "wlan.h" #include -#include +#include +#include #define DHTTYPE DHT22 #define DHTPIN 4 -DHT dht(DHTPIN, DHTTYPE); +SensirionI2cScd4x sensor; + +int error; +static char errorMessage[64]; const char *config_file_path = "/config.json"; +long inactivity_delay; Config *config; WiFiClient wifi_client; PubSubClient mqtt_client(wifi_client); -bool check_valid_value(float value) { - return (!isnan(value) && value >= 0 && value <= 100); +int check_error(int error) { + if (error) { + Serial.println("Error while initializing sensor"); + errorToString(error, errorMessage, sizeof errorMessage); + return 1; + } + return 0; } void setup() { - Serial.begin(9600); - dht.begin(); - config = (Config *)malloc(sizeof(Config)); - if (!load_config_file(config_file_path, config)) - Serial.println("ERROR: The config file could not be loaded"); - connect_wlan(config); + Serial.begin(9600); + Wire.begin(); + sensor.begin(Wire, SCD41_I2C_ADDR_62); + error = sensor.wakeUp(); + error = sensor.stopPeriodicMeasurement(); + error = sensor.reinit(); + error = sensor.startPeriodicMeasurement(); + inactivity_delay = 60000; } void loop() { - float temperature = dht.readTemperature(); - float humidity = dht.readHumidity(); - float data[2] = {temperature, humidity}; - if (check_valid_value(temperature) && check_valid_value(humidity)) { - mqtt_transfer(mqtt_client, config, data); - } - disconnect_mqtt(mqtt_client, config->topic); - free(config); - enter_deep_sleep(false, config->sleep_time); + uint16_t co2_concentration = 0.0; + float temperature = 0.0; + float humidity = 0.0; + bool data_available = false; + + error = sensor.getDataReadyStatus(data_available); + error = sensor.readMeasurement(co2_concentration, temperature, humidity); + + if (data_available) { + Serial.print("CO2 concentration [ppm]: "); + Serial.print(co2_concentration); + Serial.println(); + Serial.print("Temperature [°C]: "); + Serial.print(temperature); + Serial.println(); + Serial.print("Relative Humidity [RH]: "); + Serial.print(humidity); + Serial.println(); + } else { + Serial.println("."); + } + + delay(inactivity_delay); }