#include <WiFi.h> // Wi-Fi library
#include <Wire.h> // I2C library
#include <SPI.h> // SPI library
#include <Adafruit_Sensor.h> // Sensor library (use a compatible sensor)
#include <Adafruit_BME280.h> // BME280 sensor library
#include <Servo.h> // Servo library
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// BME280 sensor I2C address
#define BME280_I2C_ADDRESS 0x76
Adafruit_BME280 bme; // create BME280 object
Servo myservo; // create a Servo object
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // Built-in LED (GPIO2)
pinMode(4, INPUT); // Button input (GPIO4)
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nConnected to WiFi");
// Initialize I2C and BME280 sensor
if (!bme.begin(BME280_I2C_ADDRESS)) {
Serial.println("Could not find BME280 sensor!");
while (1);
}
// Initialize the Servo
myservo.attach(13); // Attach servo to GPIO13
// Start an ADC (Analog to Digital Conversion)
analogReadResolution(10); // 10-bit ADC resolution
Serial.println("Setup complete");
}
void loop() {
// Toggle the LED
digitalWrite(2, HIGH); // Turn the LED on
delay(500);
digitalWrite(2, LOW); // Turn the LED off
delay(500);
// Read and display button state
int buttonState = digitalRead(4);
Serial.print("Button state: ");
Serial.println(buttonState);
// Read and display sensor data (temperature, humidity, pressure)
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F; // convert to hPa
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
// ADC read (Analog sensor input)
int analogValue = analogRead(34); // ADC pin 34
Serial.print("Analog Value: ");
Serial.println(analogValue);
// Control servo based on analog input
int servoAngle = map(analogValue, 0, 1023, 0, 180); // map ADC value to servo angle
myservo.write(servoAngle);
// Display IP address
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
// SPI Communication (Example of sending data)
SPI.begin();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
digitalWrite(SS, LOW);
SPI.transfer(0x42); // Send a byte
digitalWrite(SS, HIGH);
SPI.endTransaction();
// Simple task creation example (non-blocking delay)
delay(1000); // Wait for a second
}