int analogPin6 = A6; // Analog-Input Pin6 => Öldruck als 5V-Signal
int analogPin7 = A7; // Analog-Input Pin7 => Öltemperatur als Widerstand
float R1 = 5.14; //Wert des bekannten Widerstands, ehemals 107000 Ohm => R "/1000" nun entfallen da nun in kOhm
float R; //Widerstand des Temperaturfühlers
float SpannungR2; //Spannung über dem zu messenden Widerstand
float oiltemp; // Öltemperatur
float oilpressure; // Öldruck
float Tmittel=0; // gemittelte Öltemperatur
float pmittel=0; //gemittelter Öldruck
//float Tmax = 0; //max. Öltemperatur
//float pmax =0; //max. Öldruck
//float runtime_s = 0;
//float runtime_m = 0;
unsigned long last_oil_send = 0;
unsigned long last_oled_send = 0;
int sendTime = 2000;
#include <U8g2lib.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // Adafruit ESP8266/32u4/ARM Boards + FeatherWing OLED
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
#include <IbusTrx.h> // include the IbusTrx library
IbusTrx ibusTrx; // create a new IbusTrx instance
#define WAKE_PIN 13
#define wake_set(...) digitalWrite(WAKE_PIN, __VA_ARGS__)
void setup(void) {
u8g2.begin();
ibusTrx.begin(Serial); // begin listening for messages
pinMode(WAKE_PIN, OUTPUT);
wake_set(HIGH);
}
void loop()
{
//Berechung Widerstand Temperaturfühler
SpannungR2 = 5 / 1023.0 * analogRead(analogPin7);
R = R1 * (SpannungR2 / (5 - SpannungR2));
//Umrechnung Widerstand in Temperatur
if (R > 45) {
oiltemp = 0.00479709*pow(R, 2) - 1.00497778*R + 61.07424494;
}
else {
oiltemp = 0.0000001326*pow(R, 6) - 0.0000236053*pow(R, 5) + 0.0016840356*pow(R, 4) - 0.0620097366*pow(R, 3) + 1.2759648324*pow(R, 2) - 15.504389816*R + 138.9049501569;
}
//Berechnung Öldruck
if ((analogRead(analogPin6) / 1023.00 * 5) * 1.379 - 0.69 <= 0.05) {
oilpressure = 0;
}
else {
oilpressure = (analogRead(analogPin6) / 1023.00 * 5) * 1.379 - 0.69;
}
//gleitenden Mittelwert bilden
Tmittel = 0.95*Tmittel + 0.05*oiltemp;
pmittel = 0.95*pmittel + 0.05*oilpressure;
//maxwerte
/*if (pmittel > pmax) {
pmax = pmittel;
}
if (Tmittel > Tmax) {
Tmax = Tmittel;
}*/
//Temp auf 0,5er Schritte runden
Tmittel = trunc(Tmittel * 2 + 0.5) / 2;
//Laufzeit berechnen
/*runtime_m = millis() / 1000 / 60;
runtime_s = millis() / 1000 - runtime_m * 60;*/
// an I2C-Display senden alle 200 ms
int sendTimeOLED = 200;
if (millis() - last_oled_send > sendTimeOLED) {
last_oled_send = millis();
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_10x20_tf);
//u8g2.setFont(u8g2_font_fur20_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
u8g2.setCursor(0, 1);
u8g2.print(pmittel, 2);
u8g2.setCursor(72, 1);
if (Tmittel<100) {u8g2.print(Tmittel, 1);
}
else
{u8g2.print(Tmittel, 0);
}
u8g2.setFont(u8g2_font_5x7_tf);
u8g2.drawUTF8(0, 25, "Öldruck bar");
u8g2.drawUTF8(72, 25, "Öltemp. °C");
u8g2.drawArc(40,40,20,128,128+int(round(Tmittel))*2);
u8g2.drawArc(40,40,21,128,128+int(round(Tmittel))*2);
u8g2.sendBuffer();
}
// auf I-BUS senden alle 2000 ms
if (ibusTrx.available()) { // receive and write to I-BUS
IbusMessage m = ibusTrx.readMessage(); // grab incoming messages and clear read buffer
if (millis() - last_oil_send > sendTime) {
last_oil_send = millis();
uint8_t oil[6] = {
0xAA, // sender ID (diagnostic interface)
0x05, // length of the message payload (including destination ID and checksum)
0xAB, // destination ID (body control module)
0xBB, // the type of message (IO manipulation)
byte(pmittel * 10), // "oilpressure"
byte(Tmittel) // "oiltemp"
};
ibusTrx.write(oil); // write message in tx buffer
//Ausgabe auf Serialport
/*Serial.print(oil[0], HEX); // Wert ausgeben
Serial.print(oil[1], HEX); // Wert ausgeben
Serial.print(oil[2], HEX); // Wert ausgeben
Serial.print(oil[3], HEX); // Wert ausgeben
Serial.print(oil[4], HEX); // Wert ausgeben
Serial.print(oil[5], HEX); // Wert ausgeben
Serial.println(oil[6], HEX); // Wert ausgeben*/
}
}
}