This project demonstrates how to use the Seeed Studio XIAO ESP32C3 to gather and visualize data from multiple sensors (MCP9808, SHT45, BME680) and a battery voltage measurement using a voltage divider.
- WiFi Connectivity: Connects to a specified WiFi network.
- Web Server: Hosts a web server on port 80 to serve a webpage with sensor data visualization.
- Sensor Data: Reads data from MCP9808 (temperature), SHT45 (temperature and humidity), and BME680 (temperature, humidity, pressure, and gas resistance).
- Battery Voltage Measurement: Reads battery voltage using a voltage divider circuit.
- Calibration Phase: Calibrates BME680 sensor during startup to ensure accurate readings.
- Seeed Studio XIAO ESP32C3
- MCP9808 Temperature Sensor
- SHT45 Temperature and Humidity Sensor
- BME680 Environmental Sensor
- Voltage Divider (100kΩ and 60kΩ resistors)
- Breadboard and jumper wires
- USB Type-C cable
- Resistor 1: 100kΩ
- Resistor 2: 60kΩ
Connect the resistors in series between the battery positive terminal and ground. Connect the midpoint of the resistors to GPIO3 (A0) on the XIAO ESP32C3.
Battery Positive --- 100kΩ ---+--- 60kΩ --- Ground
|
GPIO3
- Arduino IDE with ESP32 support installed.
- Required libraries:
WiFi
,WebServer
,Wire
,Adafruit_MCP9808
,Adafruit_SHT4x
,Adafruit_BME680
,ArduinoJson
.
-
Download and Install Libraries: Ensure the following libraries are installed in your Arduino IDE.
WiFi
WebServer
Wire
Adafruit_MCP9808
Adafruit_SHT4x
Adafruit_BME680
ArduinoJson
-
Clone or Download the Repository: Download the source code from this repository.
-
Configure WiFi Credentials: Update the
ssid
andpassword
variables in the code with your WiFi network credentials. -
Upload the Code: Connect your XIAO ESP32C3 to your computer using a USB Type-C cable and upload the code using the Arduino IDE.
-
Power the XIAO ESP32C3: Connect the XIAO ESP32C3 to a power source via USB or battery.
-
Access the Web Interface: Once the device is connected to WiFi, it will print the IP address to the Serial Monitor. Open a web browser and navigate to the provided IP address to view the sensor data and battery voltage.
During the setup, the BME680 sensor undergoes a calibration phase to stabilize the readings. This ensures accurate environmental data before normal operation begins.
void calibrateBME680() {
Serial.println("Calibrating BME680...");
// Wait for the BME680 to stabilize
for (int i = 0; i < 10; i++) {
if (bme680.performReading()) {
Serial.print("Temperature: ");
Serial.print(bme680.temperature);
Serial.print(" °C, Humidity: ");
Serial.print(bme680.humidity);
Serial.print(" %, Pressure: ");
Serial.print(bme680.pressure / 100.0);
Serial.print(" hPa, Gas: ");
Serial.print(bme680.gas_resistance / 1000.0);
Serial.println(" KΩ");
} else {
Serial.println("Failed to perform reading :(");
}
delay(1000); // Wait 1 second between readings
}
Serial.println("BME680 calibration complete.");
}
The battery voltage is measured using a voltage divider circuit connected to GPIO3. The voltage is then used to calculate the battery percentage.
float readBatteryVoltage() {
const float attenuation = 2.4706;
int raw = analogRead(3);
float voltage = (raw / 4095.0) * 3.3 * attenuation;
return voltage;
}
float calculateBatteryPercentage(float voltage) {
if (voltage > 4.2) return 100.0;
if (voltage < 3.0) return 0.0;
return (voltage - 3.0) / (4.2 - 3.0) * 100.0;
}
This project is licensed under the MIT License - see the LICENSE file for details.