forked from HyperloopatVCU/FlightComputer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmsjsonserial.ino
213 lines (176 loc) · 7.86 KB
/
bmsjsonserial.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//#include <Ethernet.h>
//#include <OneWire.h>
#define NUMBER_OF_DS18B20 30
#define NUMBER_OF_PL200 8
#define ID_TAG 69
#define VOLTAGE 0
#define CURRENT 1
#define str_l2(X) #X
#define str(X) str_l2(X)
#define HP_TEMP_IND_START 1
#define HP_TEMP_IND_END 2
#define HP_PL200_IND_START 1
#define HP_PL200_IND_END 2
#define LP_TEMP_IND_START 3
#define LP_TEMP_IND_END 4
#define LP_PL200_IND_START 3
#define LP_PL200_IND_END 4
#define BA_TEMP_IND_START 5
#define BA_TEMP_IND_END 6
#define BA_PL200_IND_START 7
#define BA_PL200_IND_END 7
#define BB_TEMP_IND_START 7
#define BB_TEMP_IND_END 8
#define BB_PL200_IND_START 8
#define BB_PL200_IND_END 8
byte IVDataArray[2][NUMBER_OF_PL200];
byte tempDataArray[NUMBER_OF_DS18B20];
uint16_t ds18b20ErrorArray[NUMBER_OF_DS18B20];
uint16_t pl200ErrorArray[NUMBER_OF_PL200];
//EthernetSerial Serial;
//JSON FILE STRUCTURE
struct {
const String prelude = "{";
const String info = "\n\t\"identity\":\"BMS\"";
const String highpower = "\n\t\"high power\":{";
const String lowpower = "\n\t\"low power\":{";
const String brakeayy = "\n\t\"brake a\":{";
const String brakebee = "\n\t\"brake b\":{";
const String locsuff = "\n\t},";
//TEMP DATA TRANSMISSION STRUCTURE
struct {
const String prelude = "\n\t\t\"temperature-";
const String presuff = "\":{";
const String error_prelude = "\n\t\t\t\"error\":";
const String info = "\n\t\t\t\"units\":\"C\"";
const String temp_prelude = ",\n\t\t\t\"temp\":";
const String suff = "\n\t\t},";
}DS18B20;
//VOLTAGE AND CURRENT DATA TRANSMISSION STRUCTURE
struct{
const String prelude = "\n\t\t\"voltage¤t-";
const String presuff = "\":{";
const String error_prelude = "\n\t\t\t\"error\":";
struct {
const String prelude = "\n\t\t\t\"voltage\":{";
const String info = "\n\t\t\t\t\"units\":\"V\"";
const String V_prelude = ",\n\t\t\t\t\"V\":";
const String suff = "\n\t\t\t},";
} V;
struct {
const String prelude = "\n\t\t\t\"current\":{";
const String info = "\n\t\t\t\t\"units\":\"A\"";
const String I_prelude = ",\n\t\t\t\t\"A\":";
const String suff = "\n\t\t\t}";
} I;
const String suff = "\n\t\t},";
} PL200;
const String suff = "\n}\n";
} json;
void errorClear(uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer){
for(uint16_t deviceIndex = 0; deviceIndex < NUMBER_OF_DS18B20; ++deviceIndex){
ds18b20ErrorArrayPointer[deviceIndex] = 0;
}
for(uint16_t deviceIndex = 0; deviceIndex < NUMBER_OF_PL200; ++deviceIndex){
pl200ErrorArrayPointer[deviceIndex] = 0;
}
}
//TEMP DATA TRANSMISSION FUNCTION
void send_temp_data(byte *tempDataArrayPointer, uint16_t sensIndStart, uint16_t sensIndEnd, uint16_t *ds18b20ErrorArrayPointer) {
for(uint16_t deviceIndex = sensIndStart; deviceIndex <= sensIndEnd; ++deviceIndex){
Serial.print(String(json.DS18B20.prelude).c_str());
Serial.print(deviceIndex, DEC);
Serial.print(String(json.DS18B20.presuff).c_str());
Serial.print(String(json.DS18B20.error_prelude).c_str());
Serial.print(ds18b20ErrorArrayPointer[deviceIndex], DEC);
Serial.print(String(json.DS18B20.info).c_str());
Serial.print(String(json.DS18B20.temp_prelude).c_str());
Serial.print(String(tempDataArrayPointer[deviceIndex], DEC).c_str());
Serial.print(String(json.DS18B20.suff).c_str());
}
}
//VOLATAGE AND CURRENT TRANSMISSION FUCNTION
void send_pl200_data(byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t pl200IndStart, uint16_t pl200IndEnd, uint16_t *pl200ErrorArrayPointer){
for(uint16_t deviceIndex = pl200IndStart; deviceIndex <= pl200IndEnd; ++deviceIndex){
Serial.print(String(json.PL200.prelude + String(deviceIndex, DEC) + String(json.PL200.presuff)).c_str());
Serial.print(String(json.PL200.error_prelude).c_str());
Serial.print(pl200ErrorArrayPointer[deviceIndex], DEC);
Serial.print(String(json.PL200.V.prelude).c_str());
Serial.print(String(json.PL200.V.info).c_str());
Serial.print(String(json.PL200.V.V_prelude).c_str());
Serial.print(String(IVDataArrayPointer[VOLTAGE][deviceIndex], DEC).c_str());
Serial.print(String(json.PL200.V.suff).c_str());
Serial.print(String(json.PL200.I.prelude).c_str());
Serial.print(String(json.PL200.I.info).c_str());
Serial.print(String(json.PL200.I.I_prelude).c_str());
Serial.print(String(IVDataArrayPointer[CURRENT][deviceIndex], DEC).c_str());
Serial.print(String(json.PL200.I.suff).c_str());
Serial.print(String(json.PL200.suff).c_str());
}
Serial.print("\b");
}
void send_highpwr(byte *tempDataArrayPointer, byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer){
Serial.print(String(json.highpower).c_str());
send_temp_data(tempDataArrayPointer, HP_TEMP_IND_START, HP_TEMP_IND_END, ds18b20ErrorArrayPointer);
send_pl200_data(IVDataArrayPointer, HP_PL200_IND_START, HP_PL200_IND_END, pl200ErrorArrayPointer);
Serial.print(String(json.locsuff).c_str());
}
void send_lowpwr(byte *tempDataArrayPointer, byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer){
Serial.print(String(json.lowpower).c_str());
send_temp_data(tempDataArrayPointer, LP_TEMP_IND_START, LP_TEMP_IND_END, ds18b20ErrorArrayPointer);
send_pl200_data(IVDataArrayPointer, LP_PL200_IND_START, LP_PL200_IND_END, pl200ErrorArrayPointer);
Serial.print(String(json.locsuff).c_str());
}
void send_brake_a(byte *tempDataArrayPointer, byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer){
Serial.print(String(json.brakeayy).c_str());
send_temp_data(tempDataArrayPointer, BA_TEMP_IND_START, BA_TEMP_IND_END, ds18b20ErrorArrayPointer);
send_pl200_data(IVDataArrayPointer, BA_PL200_IND_START, BA_PL200_IND_END, pl200ErrorArrayPointer);
Serial.print(String(json.locsuff).c_str());
}
void send_brake_b(byte *tempDataArrayPointer, byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer){
Serial.print(String(json.brakebee).c_str());
send_temp_data(tempDataArrayPointer, BB_TEMP_IND_START, BB_TEMP_IND_END, ds18b20ErrorArrayPointer);
send_pl200_data(IVDataArrayPointer, BB_PL200_IND_START, BB_PL200_IND_END, pl200ErrorArrayPointer);
Serial.print(String(json.locsuff).c_str());
}
//JSON FILE TRANSMISSION FUNCTION
void send_json(byte *tempDataArrayPointer, byte (*IVDataArrayPointer)[NUMBER_OF_PL200], uint16_t *ds18b20ErrorArrayPointer, uint16_t *pl200ErrorArrayPointer) {
Serial.print(String(json.prelude).c_str());
Serial.print(String(json.info).c_str());
send_highpwr(tempDataArrayPointer, IVDataArrayPointer, ds18b20ErrorArrayPointer, pl200ErrorArrayPointer);
send_lowpwr(tempDataArrayPointer, IVDataArrayPointer, ds18b20ErrorArrayPointer, pl200ErrorArrayPointer);
send_brake_a(tempDataArrayPointer, IVDataArrayPointer, ds18b20ErrorArrayPointer, pl200ErrorArrayPointer);
send_brake_b(tempDataArrayPointer, IVDataArrayPointer, ds18b20ErrorArrayPointer, pl200ErrorArrayPointer);
Serial.print("\b");
Serial.print(String(json.suff).c_str());
}
void setup() {
/*
const byte mac[] = {0xde, 0xad, 0xbe, 0xef, 0xfe, ID_TAG};
const byte gateway[] = {192,168,1,20};
const byte netmask[] = {255,255,255,0};
const byte dns[] = {192,168,1,20};
const byte ip[] = {192,168,1,49};
const byte server[] = {192,168,1,20};
const int port = 23;
*/
Serial.begin(9600);
while (!Serial);
/*
Ethernet.begin(mac, ip, dns, gateway, netmask);
delay(1000);
Serial.setSerialTimeout(99000);
Serial.println(String(Serial.connect(server, port), DEC));
if (Serial.connected())
Serial.println("Connected");
else {
Serial.println("Could not connect to server");
fail(1);
}
*/
}
void loop() {
errorClear(ds18b20ErrorArray, pl200ErrorArray);
send_json(tempDataArray, IVDataArray, ds18b20ErrorArray, pl200ErrorArray);
delay(10000);
}