ESP32 Sensor Data Collection, Static Data After Sometime

DJDhrub
Posts: 1
Joined: Mon Feb 20, 2023 3:36 pm

ESP32 Sensor Data Collection, Static Data After Sometime

Postby DJDhrub » Mon Feb 20, 2023 3:41 pm

I am trying to create a Room Environment Monitor System using ESP32, using TEMT600, AHT10, BMP280, MPU6050 sensor modules. Everything seems to work fine, but the data from BMP280 & MPU6050 stops coming. The BMP280 sensor gives me Temp of 180.73°C & Pressure of -127hPa. The MPU6050 gives similar static readings, Accelerometer -9.20, 0.00, -77.8 & Gyro -1194°, 0°, 0°. Both of them start giving static reading at same time but can vary between 5-60 min in my test.

I am also getting timeouts from BMP280 sometimes, may be that causing something?

I am attaching my code below, this is my first time trying with ESP32, so if someone help me out what's wrong with the code, that will be great. All of the sensors SDA are SDA GPIO 21, SCL GPIO 22.

Code
  1. #include <WiFi.h>
  2. #include <WiFiMulti.h>
  3. #include <math.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <Adafruit_AHTX0.h>
  6. #include <Adafruit_BMP280.h>
  7. #include <Adafruit_MPU6050.h>
  8. #include <InfluxDbClient.h>
  9.  
  10.  
  11. // Wi-Fi AP SSID
  12. #define WIFI_SSID "Wi-Fi"
  13. // Wi-Fi Password
  14. #define WIFI_PASSWORD "Password"
  15. // InfluxDB v2 Server URL, e.g. https://eu-central-1-1.aws.cloud2.influxdata.com (Use: InfluxDB UI -> Load Data -> Client Libraries)
  16. #define INFLUXDB_URL "URL"
  17. // InfluxDB v2 Server or Cloud API Token (Use: InfluxDB UI -> Data -> API Tokens -> Generate API Token)
  18. #define INFLUXDB_TOKEN "TOEKN"
  19. // InfluxDB v2 Organization ID (Use: InfluxDB UI -> User -> About -> Common Ids )
  20. #define INFLUXDB_ORG "Test"
  21. // InfluxDB v2 Bucket Name (Use: InfluxDB UI ->  Data -> Buckets)
  22. #define INFLUXDB_BUCKET "Weather"
  23. // Set Timezone String According to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
  24. #define TZ_INFO "UTC +10"
  25.  
  26.  
  27. // InfluxDB Client Instance with Preconfigured InfluxCloud Certificate
  28. InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
  29.  
  30. WiFiMulti wifiMulti;
  31. Adafruit_AHTX0 aht;
  32. Adafruit_BMP280 bmp;
  33. Adafruit_MPU6050 mpu;
  34.  
  35. // Setup Code to Run Once
  36. void setup() {
  37.   Serial.begin(115200);
  38.  
  39.   // Connect to Wi-Fi
  40.   WiFi.mode(WIFI_STA);
  41.   wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
  42.  
  43.   Serial.print("Connecting to Wi-Fi");
  44.   while (wifiMulti.run() != WL_CONNECTED) {
  45.     Serial.print(".");
  46.     delay(100);
  47.   }
  48.   Serial.println();
  49.  
  50.   // Initialize the AHT10 Sensor
  51.   if (!aht.begin()) {
  52.     Serial.println("Could Not Find AHT10 Sensor!");
  53.     while (1);
  54.   }
  55.  
  56.   // Initialize the BMP280 Sensor
  57.   if (!bmp.begin(0x76)) {
  58.   Serial.println("Could Not Find BMP280 Sensor");
  59.   while (1);
  60.   }
  61.  
  62.   // Initialize the MPU6050 Sensor
  63.   if (!mpu.begin()) {
  64.   Serial.println("Could Not Find MPU6050 Sensor");
  65.   while (1);
  66.   }
  67.  
  68.   // MPU 6050 Details
  69.   mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  70.   Serial.print("Accelerometer range set to: ");
  71.   switch (mpu.getAccelerometerRange()) {
  72.   case MPU6050_RANGE_2_G:
  73.     Serial.println("+-2G");
  74.     break;
  75.   case MPU6050_RANGE_4_G:
  76.     Serial.println("+-4G");
  77.     break;
  78.   case MPU6050_RANGE_8_G:
  79.     Serial.println("+-8G");
  80.     break;
  81.   case MPU6050_RANGE_16_G:
  82.     Serial.println("+-16G");
  83.     break;
  84.   }
  85.   mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  86.   Serial.print("Gyro range set to: ");
  87.   switch (mpu.getGyroRange()) {
  88.   case MPU6050_RANGE_250_DEG:
  89.     Serial.println("+- 250 deg/s");
  90.     break;
  91.   case MPU6050_RANGE_500_DEG:
  92.     Serial.println("+- 500 deg/s");
  93.     break;
  94.   case MPU6050_RANGE_1000_DEG:
  95.     Serial.println("+- 1000 deg/s");
  96.     break;
  97.   case MPU6050_RANGE_2000_DEG:
  98.     Serial.println("+- 2000 deg/s");
  99.     break;
  100.   }
  101.  
  102.   mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  103.   Serial.print("Filter bandwidth set to: ");
  104.   switch (mpu.getFilterBandwidth()) {
  105.   case MPU6050_BAND_260_HZ:
  106.     Serial.println("260 Hz");
  107.     break;
  108.   case MPU6050_BAND_184_HZ:
  109.     Serial.println("184 Hz");
  110.     break;
  111.   case MPU6050_BAND_94_HZ:
  112.     Serial.println("94 Hz");
  113.     break;
  114.   case MPU6050_BAND_44_HZ:
  115.     Serial.println("44 Hz");
  116.     break;
  117.   case MPU6050_BAND_21_HZ:
  118.     Serial.println("21 Hz");
  119.     break;
  120.   case MPU6050_BAND_10_HZ:
  121.     Serial.println("10 Hz");
  122.     break;
  123.   case MPU6050_BAND_5_HZ:
  124.     Serial.println("5 Hz");
  125.     break;
  126.   }
  127.  
  128.  
  129.   // Accurate time is necessary for certificate validation and writing in batches
  130.   // For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/
  131.   // Syncing progress and the time will be printed to Serial.
  132.   timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
  133.  
  134.   // Check Server Connection
  135.   if (client.validateConnection()) {
  136.     Serial.print("Connected to InfluxDB: ");
  137.     Serial.println(client.getServerUrl());
  138.   }
  139.   else {
  140.     Serial.print("InfluxDB Connection Failed: ");
  141.     Serial.println(client.getLastErrorMessage());
  142.   }
  143.  
  144.   /* Default Settings from Datasheet */
  145.   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode */
  146.                   Adafruit_BMP280::SAMPLING_X2,     /* Temperature Oversampling */
  147.                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure Oversampling */
  148.                   Adafruit_BMP280::FILTER_X16,      /* Filtering */
  149.                   Adafruit_BMP280::STANDBY_MS_500); /* Standby Time */  
  150. }
  151.  
  152. // Code to Run in Loop
  153. void loop() {
  154.   // Read Sensor Values
  155.  
  156.   // Sensor Event  
  157.   sensors_event_t aht10humidity, aht10temp, mpu6050accelerometer, mpu6050gyroscope, mpu6050temp;
  158.  
  159.   // AHT 10
  160.   aht.getEvent(&aht10humidity, &aht10temp);
  161.  
  162.   // TEMP 6000
  163.   int const TEMP6000_PIN = 34; // Analog Pin 34
  164.   int const AREF = 3.3;        // set for 5.0 or 3.3 depending on voltage of uC
  165.   // Luminescence Math from Analog Reading (0-4095) for ESP32 12 bit
  166.   float sensor_value = analogRead(TEMP6000_PIN); // Get Raw Sensor Reading
  167.   float volts = sensor_value * AREF / 4095.0;    // Convert Reading to Voltage
  168.   float amps = volts / 10000.0;                  // Convert to Amps Across 10K Resistor
  169.   float microamps = amps * 1000000.0;            // Convert Amps to Microamps
  170.   float lux = microamps * 2.0;                   // 2 Microamps = 1 lux
  171.  
  172.   //BMP 280
  173.   float bmpTemp = bmp.readTemperature();
  174.   float bmpPressure = bmp.readPressure()/100;             // Pascal to Hectopascal
  175.   float bmpAltitude = bmp.readAltitude(1013.25);          //The "1013.25" is the Pressure(hPa) at Sea Level
  176.  
  177.   // MPU 6050
  178.   mpu.getEvent(&mpu6050accelerometer, &mpu6050gyroscope, &mpu6050temp);
  179.  
  180.   // Create Data Points
  181.   Point temp6000("TEMP6000");
  182.   Point aht10("AHT10");
  183.   Point bmp280("BMP280");
  184.   Point mpu6050("MPU6050");
  185.  
  186.   // Field for AHT10
  187.   aht10.addField("Temperature", aht10temp.temperature);
  188.   aht10.addField("Relative Humidity", aht10humidity.relative_humidity);
  189.   // Field for TEMP6000
  190.   temp6000.addField("Lux", lux);
  191.   temp6000.addField("Raw ADC Data", (int)sensor_value);
  192.   temp6000.addField("Volts", volts);
  193.   // Field for BMP280
  194.   bmp280.addField("Temperature", bmpTemp);
  195.   bmp280.addField("Pressure", bmpPressure);
  196.   bmp280.addField("Approx Altitude", bmpAltitude);
  197.   // Field for MPU6050
  198.   mpu6050.addField("Temperature", mpu6050temp.temperature);
  199.   mpu6050.addField("Accelerometer X", mpu6050accelerometer.acceleration.x);
  200.   mpu6050.addField("Accelerometer Y", mpu6050accelerometer.acceleration.y);
  201.   mpu6050.addField("Accelerometer Z", mpu6050accelerometer.acceleration.z);
  202.   mpu6050.addField("Gyroscope X", mpu6050gyroscope.gyro.x * 180 / PI);
  203.   mpu6050.addField("Gyroscope Y", mpu6050gyroscope.gyro.y * 180 / PI);
  204.   mpu6050.addField("Gyroscope Z", mpu6050gyroscope.gyro.z * 180 / PI);
  205.  
  206.   // Check Wi-Fi Connection and Reconnect if Needed
  207.   if (wifiMulti.run() != WL_CONNECTED)
  208.   {
  209.     Serial.println("Wi-Fi Connection Lost!");
  210.   }
  211.  
  212.   // Write Point
  213.   if (!client.writePoint(temp6000))
  214.   {
  215.     Serial.print("InfluxDB Write Failed For TEMP6000: ");
  216.     Serial.println(client.getLastErrorMessage());
  217.   }
  218.  
  219.   if (!client.writePoint(aht10))
  220.   {
  221.     Serial.print("InfluxDB Write Failed For AHT10: ");
  222.     Serial.println(client.getLastErrorMessage());
  223.   }
  224.  
  225.   if (!client.writePoint(bmp280))
  226.   {
  227.     Serial.print("InfluxDB Write Failed For BMP280: ");
  228.     Serial.println(client.getLastErrorMessage());
  229.   }
  230.  
  231.   if (!client.writePoint(mpu6050))
  232.   {
  233.     Serial.print("InfluxDB Write Failed For MPU6050: ");
  234.     Serial.println(client.getLastErrorMessage());
  235.   }
  236.  
  237.   delay(10000);
  238. }
I am pushing this data to Oracle Cloud, Tried doing it locally as well, faced similar issues but again when it's going to happen is uncertain, but when it happens happens for MPU6050, BMP280 at the same time.

Who is online

Users browsing this forum: No registered users and 91 guests