//kleine Info am Rande, ich habe die richtige ausgabe des Lux-Werts nicht hin bekommen, es wird
//auf der Seriellen Schnittstelle und dem OLEDDISPLAY ausgegeben, aber der Wert der erscheint ist falsch,
// sonst sollte alles gehen.
//MfG
//Batur Ceran
#include "DHTesp.h"
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
const int DHT_PIN = 34;
const int pirPin = 33;
const float BETA = 3950;
const float gama = 0.7;
const float rl10 = 50;
#define PIN_TRIG 27
#define PIN_ECHO 14
DHTesp dhtSensor;
#define breite 128
#define hoehe 64
#define reset -1;
#define adresse 0x3c;
Adafruit_SSD1306 display(breite, hoehe, &Wire, -1);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("display nicht gefunden");
for (;;);
}
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
analogReadResolution(10);
pinMode(25,INPUT);
while (!mpu.begin()) {
Serial.println("MPU6050 not connected!");
delay(1000);
}
pinMode(5, INPUT);
}
void loop() {
if(Serial.available()>0){
String command = Serial.readStringUntil('\n');
if(command=="humi"){
sensorDHT22();
} else if (command=="abstand"){
sensorHCSR04();
}
else if (command == "beweg1") {
sensorPIR();
}
else if (command=="temp2"){
sensorNTC();
}
else if (command == "beweg2") {
sensorMPU6050();
}
else if(command=="licht"){
sensorLDR();
}
}
}
void sensorDHT22(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
}
void sensorHCSR04(){
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
float duration = pulseIn(PIN_ECHO, HIGH);
Serial.print("Abstand in cm: ");
Serial.println(duration / 58.8);
OLEDDISPLAY("Abstand in cm: ",String(duration / 58.8));
}
void sensorPIR() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
Serial.println("Es bewegt sich etwas");
OLEDDISPLAY("Es bewegt sich etwas",String(motion));
} else {
Serial.println("Es bewegt sich nichts");
OLEDDISPLAY("Es bewegt sich nichts",String(motion));
}
}
void sensorNTC(){
int analogValue = analogRead(25);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature in ℃: ");
Serial.println(celsius);
OLEDDISPLAY("Temperature in ℃: ",String(celsius));
}
void sensorMPU6050() {
sensors_event_t acc, gcc, temp;
mpu.getEvent(&acc, &gcc, &temp);
Serial.println("Beschleunigung auf x Achse");
Serial.println(acc.acceleration.x);
OLEDDISPLAY("Beschleunigung auf x Achse",String(acc.acceleration.x));
delay(500);
Serial.println("Beschleunigung auf y Achse");
Serial.println(acc.acceleration.y);
OLEDDISPLAY("Beschleunigung auf y Achse",String(acc.acceleration.y));
delay(500);
Serial.println("Beschleunigung auf z Achse");
Serial.println(acc.acceleration.z);
OLEDDISPLAY("Beschleunigung auf z Achse",String(acc.acceleration.z));
delay(500);
Serial.println("Rotation der x Achse: ");
Serial.println((gcc.gyro.x)*180/3.14);
OLEDDISPLAY("Rotation der x Achse: ",String(gcc.gyro.x*180/3.14));
delay(500);
Serial.println("Rotation der y Achse: ");
Serial.println((gcc.gyro.y)*180/3.14);
OLEDDISPLAY("Rotation der y Achse: ",String(gcc.gyro.y*180/3.14));
delay(500);
Serial.println("Rotation der z Achse: ");
Serial.println((gcc.gyro.z)*180/3.14);
OLEDDISPLAY("Rotation der z Achse: ",String(gcc.gyro.z*180/3.14));
}
void sensorLDR(){
int analogValue = analogRead(5);
float voltage = analogValue / 4095.0 * 5.0;
float resistance = (5.0 * 1000.0) / (5.0 - voltage);
float lux = pow(rl10 * 1e3 * pow(10, gama) / resistance, 1.0 / gama);
Serial.print("Licht in lux: ");
Serial.println(analogValue);
OLEDDISPLAY("Licht in lux: ",String(analogValue));
}
void OLEDDISPLAY(String unit, String value){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 1);
display.println(unit);
display.println(value);
display.display();
}