forked from belba/123Display
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path123Display.ino
145 lines (121 loc) · 3.16 KB
/
123Display.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <SoftwareSerial.h>
#define OLED_RESET 4 // not used / nicht genutzt bei diesem Display
Adafruit_SSD1306 display(OLED_RESET);
SoftwareSerial BMSserial(8,9);
extern HardwareSerial Serial;
int ledPin = 7;
const int msgBuffSize = 58; // max. Zeichen im "Telegramm"
int msgBuffer[msgBuffSize];
int msgBufferIndex = 0;
float Batteryvoltage = 0;
char Current3signHEX = 0;
float Current3 = 0;
int SOC = 0;
void checkForNewData() {
digitalWrite(ledPin, HIGH);
delay(10);
digitalWrite(ledPin, LOW);
BMSserial.listen();
msgBufferIndex = 0;
while (msgBufferIndex <= msgBuffSize-1){
if (BMSserial.available()){
msgBuffer[msgBufferIndex] = BMSserial.read();
msgBufferIndex++;
}
}
BMSserial.stopListening();
}
boolean checkChecksum(){
// create own checksum
int checksum = 0;
for (int count=0; count <= msgBuffSize-2; count++){
checksum = checksum + msgBuffer[count];
}
checksum = checksum & 0xFF;
if ( checksum == msgBuffer[57]) {
Serial.println ("Checksum is OK");
return true;
}else {
Serial.println ("Checksum failed");
return false;
}
}
void decBatteryvoltage(){
int BatteryvoltageHEX = 0;
byte byte1 = msgBuffer[0];
byte byte2 = msgBuffer[1];
byte byte3 = msgBuffer[2];
BatteryvoltageHEX = (byte1<<16) | (byte2<<8)| byte3;
Batteryvoltage = BatteryvoltageHEX * 0.005;
}
void decCurrentsensor3(){
byte byte9 =msgBuffer[9];
byte byte10 =msgBuffer[10];
byte byte11 =msgBuffer[11];
int Current3HEX = (byte10<<8)| byte11;
Current3signHEX = byte9;
Current3 = Current3HEX * 0.125;
}
void decSOC(){
SOC = msgBuffer[40];
}
void print2LCDstatus(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.println("Voltage: ");
display.setCursor(54,0);
display.print(Batteryvoltage);
display.print(" V");
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,9);
display.println("SOC: ");
display.setCursor(60,9);
display.print(SOC);
display.print(" %");
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,25);
display.println("Current: ");
display.setCursor(54,25);
display.print(Current3signHEX);
display.print(Current3);
display.print(" A");
display.display();
}
void print2LCDChkErr(){
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0,0);
display.println("Checksum");
display.println("Error...");
display.display();
}
void setup() {
// initialize with the I2C addr 0x3C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
BMSserial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
checkForNewData();
if (checkChecksum()){
decBatteryvoltage();
decCurrentsensor3();
decSOC();
print2LCDstatus();
}else{
print2LCDChkErr();
}
}