#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <math.h>
// ---------------- PIN CONFIG ----------------
#define ONE_WIRE_BUS 4
#define SDA_PIN 8
#define SCL_PIN 9
#define ANEMO_PIN 5
#define WIND_DIR_PIN 6
// ---------------- OBJECTS ----------------
Adafruit_BMP085 bmp;
Adafruit_SSD1306 display(128, 64, &Wire);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// ---------------- VELOCITY ----------------
volatile unsigned long lastPulseTime = 0;
volatile unsigned long period = 0;
float velocity = 0;
// ---------------- VARIABLES ----------------
float temperature = 0;
float pressure = 0;
float angle = 0;
// ---------------- INTERRUPT ----------------
void IRAM_ATTR countPulse() {
unsigned long now = micros();
period = now - lastPulseTime;
lastPulseTime = now;
}
// ---------------- ANGLE ----------------
float getAngle(int val) {
return (val / 4095.0) * 360.0; // calibrate if needed
}
// ---------------- DRAW LARGE VECTOR ----------------
void drawBigVector(float angle, float velocity) {
int cx = 64;
int cy = 40;
float rad = angle * PI / 180.0;
float maxVel = 10.0;
int length = (velocity / maxVel) * 25;
if (length > 25) length = 25;
if (length < 5) length = 5;
int dx = length * cos(rad);
int dy = length * sin(rad);
// Main arrow
display.drawLine(cx, cy, cx + dx, cy - dy, SSD1306_WHITE);
// Arrow head
display.drawLine(cx + dx, cy - dy, cx + dx - dy/2, cy - dy - dx/2, SSD1306_WHITE);
display.drawLine(cx + dx, cy - dy, cx + dx + dy/2, cy - dy + dx/2, SSD1306_WHITE);
display.fillCircle(cx, cy, 2, SSD1306_WHITE);
}
// ---------------- SETUP ----------------
void setup() {
Serial.begin(115200);
Wire.begin(SDA_PIN, SCL_PIN);
sensors.begin();
if (!bmp.begin()) {
Serial.println("BMP180 not found!");
while (1);
}
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED not found!");
while (1);
}
pinMode(ANEMO_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ANEMO_PIN), countPulse, FALLING);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
}
// ---------------- LOOP ----------------
void loop() {
// 🌪️ Velocity (REAL-TIME)
if (period > 0) {
velocity = 500000.0 / period; // calibrate this constant
}
// 🌡️ Temperature
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
// 🌬️ Pressure
pressure = bmp.readPressure() / 100.0;
// 🧭 Direction
int dirValue = analogRead(WIND_DIR_PIN);
angle = getAngle(dirValue);
// ---------------- OLED ----------------
display.clearDisplay();
// TOP LINE
display.setCursor(0, 0);
display.print("T:");
display.print(temperature, 1);
display.print("C");
display.setCursor(70, 0);
display.print("P:");
display.print(pressure, 0);
// SECOND LINE
display.setCursor(0, 12);
display.print("V:");
display.print(velocity, 2);
display.print("m/s");
display.setCursor(70, 12);
display.print("A:");
display.print(angle, 0);
display.print((char)247);
// BIG VECTOR
drawBigVector(angle, velocity);
display.display();
// SERIAL LOG (for analysis)
Serial.print(temperature); Serial.print(",");
Serial.print(pressure); Serial.print(",");
Serial.print(velocity); Serial.print(",");
Serial.println(angle);
delay(200);
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1
Loading
bmp180
bmp180
Loading
ssd1306
ssd1306