-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
339 lines (303 loc) · 10.8 KB
/
Copy pathmain.cpp
File metadata and controls
339 lines (303 loc) · 10.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include <Adafruit_NeoPixel.h>
#include <NodeControllerCore.h>
#define NEOPIXEL_PIN 10
#define NUMPIXELS 6 // Update to 6 NeoPixels
#define RELAY_1_NC_PIN 0
#define RELAY_2_NO_PIN 1
#define NODE_ID 164 // 164
#define RELAY_1_NC_MESSAGE_ID 0x0A00 // 2560
#define RELAY_2_NO_MESSAGE_ID 0x0A01 // 2561
#define CURRENT_MESSAGE_ID 0x0A02 // 2562
#define RELAY_1_ALERT_NODE_MESSAGE_ID 2563 // 2563
#define RELAY_2_ALERT_NODE_MESSAGE_ID 2564 // 2564
#define RELAY_1_IN_ALERT_MESSAGE_ID 2565 // 2565
#define RELAY_2_IN_ALERT_MESSAGE_ID 2566 // 2566
#define RELAY_1_ALERT_INVERTED_MESSAGE_ID 2567 // 2567
#define RELAY_2_ALERT_INVERTED_MESSAGE_ID 2568 // 2568
bool Relay_1_Alert_Inverted = false;
bool Relay_2_Alert_Inverted = false;
bool is_Relay_1_in_Alert = false;
bool is_Relay_2_in_Alert = false;
int relay_1_alert_node = 163;
int relay_2_alert_node = 163;
bool relay1PreviousState = true;
bool relay2PreviousState = true;
float total_current = 0;
Adafruit_ADS1115 ads;
Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
int I2C_SDA = 3;
int I2C_SCL = 2;
const float FACTOR = 15; // 15A/1V from the CT
const float multiplier = 0.00005;
bool relay0State = true;
bool relay1State = true;
NodeControllerCore core;
//-------------------------------------------------- Function Prototypes --------------------------------------------------
void receive_message(uint8_t nodeID, uint16_t messageID, uint64_t data);
void getcurrent(void *parameters);
void printMeasure(String prefix, float value, String postfix);
//-------------------------------------------------- setup() --------------------------------------------------
void setup()
{
Serial.begin(115200);
// Set initial relay states to ON
core = NodeControllerCore();
if (core.Init(receive_message, NODE_ID))
{
Serial.println("Driver device initialized");
}
else
{
Serial.println("Failed to initialize driver");
}
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
// initialize digital pins 0 and 1 as outputs
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
// Initialize I2C and ADS1115
Wire.begin(I2C_SDA, I2C_SCL); //
ads.setGain(GAIN_FOUR); // +/- 1.024V 1bit = 0.5mV
ads.setDataRate(RATE_ADS1115_128SPS); // 250 samples per second
ads.begin();
Serial.println("Getting data rate");
Serial.println(ads.getDataRate());
// Initialize NeoPixel
pixels.begin();
pixels.setBrightness(3); // Set brightness to 1% (90% less than current setting) // Set brightness to 10%
pixels.clear();
pixels.setPixelColor(0, pixels.Color(0, 255, 0)); // Set first NeoPixel to green (always on)
pixels.setPixelColor(5, pixels.Color(0, 255, 0)); // Set sixth NeoPixel to green (always on)
pixels.setPixelColor(1, pixels.Color(0, 255, 0)); // Set second NeoPixel to green (relay 1 active)
pixels.setPixelColor(4, pixels.Color(0, 255, 0)); // Set fifth NeoPixel to green (relay 1 active)
pixels.setPixelColor(2, pixels.Color(255, 0, 0)); // Set third NeoPixel to green (relay 2 active)
pixels.setPixelColor(3, pixels.Color(255, 0, 0)); // Set fourth NeoPixel to green (relay 2 active)
pixels.show();
// Create a task to get the current
xTaskCreate(getcurrent, "getcurrent", 4096, NULL, 1, NULL);
core.sendMessage(RELAY_1_IN_ALERT_MESSAGE_ID, (uint64_t)0);
core.sendMessage(RELAY_2_IN_ALERT_MESSAGE_ID, (uint64_t)0);
}
//-------------------------------------------------- loop() --------------------------------------------------
void loop()
{
// float current = getcurrent();
// printMeasure("Irms: ", current, "A");
if (Serial.available() > 0)
{
char key = Serial.read();
if (key == '1')
{
relay0State = !relay0State; // Toggle relay 0 state
digitalWrite(0, relay0State ? HIGH : LOW);
if (relay0State)
{
pixels.setPixelColor(1, pixels.Color(0, 255, 0)); // Set first NeoPixel to green (relay 0 active)
pixels.setPixelColor(4, pixels.Color(0, 255, 0)); // Set second NeoPixel to green (relay 0 active)
}
else
{
pixels.setPixelColor(1, pixels.Color(255, 0, 0)); // Set first NeoPixel to red (relay 0 inactive)
pixels.setPixelColor(4, pixels.Color(255, 0, 0)); // Set second NeoPixel to red (relay 0 inactive)
}
pixels.show();
}
else if (key == '2')
{
relay1State = !relay1State; // Toggle relay 1 state
digitalWrite(1, relay1State ? HIGH : LOW);
if (relay1State)
{
pixels.setPixelColor(2, pixels.Color(0, 255, 0)); // Set third NeoPixel to green (relay 1 active)
pixels.setPixelColor(3, pixels.Color(0, 255, 0)); // Set fourth NeoPixel to green (relay 1 active)
}
else
{
pixels.setPixelColor(2, pixels.Color(255, 0, 0)); // Set third NeoPixel to red (relay 1 inactive)
pixels.setPixelColor(3, pixels.Color(255, 0, 0)); // Set fourth NeoPixel to red (relay 1 inactive)
}
pixels.show();
}
}
delay(100);
}
//-------------------------------------------------- Function Definitions --------------------------------------------------
void getcurrent(void *parameters)
{
Serial.println("Getting current");
while (1)
{
float voltage;
float current;
float sum = 0;
long time_check = millis();
int counter = 0;
while (millis() - time_check < 1000)
{
voltage = ads.readADC_Differential_0_1() * multiplier;
current = voltage * FACTOR;
sum += sq(current);
counter = counter + 1;
delay(7);
}
total_current = sqrt(sum / counter);
core.sendMessage(CURRENT_MESSAGE_ID, &total_current);
Serial.print("Current: ");
Serial.println(total_current);
delay(4000);
}
}
void printMeasure(String prefix, float value, String postfix)
{
Serial.print(prefix);
Serial.print(value, 3);
Serial.println(postfix);
}
void receive_message(uint8_t nodeID, uint16_t messageID, uint64_t data)
{
Serial.println("Message received callback");
// Check if the message is for this node
if (nodeID == NODE_ID)
{
Serial.println("Message received to self");
// Check the message ID for the LED control messages
switch (messageID)
{
// ---------------------Demo override control messages-------------------------
case RELAY_1_NC_MESSAGE_ID:
if (data == 1)
{
if (!is_Relay_1_in_Alert)
{
relay1PreviousState = true;
digitalWrite(RELAY_1_NC_PIN, LOW);
pixels.setPixelColor(1, pixels.Color(0, 255, 0));
pixels.setPixelColor(4, pixels.Color(0, 255, 0));
pixels.show();
Serial.println("Relay 1 is ON");
//delay(mesageDelay);
}
}
else
{
if (!is_Relay_1_in_Alert)
{
relay1PreviousState = false;
digitalWrite(RELAY_1_NC_PIN, HIGH);
pixels.setPixelColor(1, pixels.Color(255, 0, 0));
pixels.setPixelColor(4, pixels.Color(255, 0, 0));
pixels.show();
Serial.println("Relay 1 is OFF");
// delay(mesageDelay);
}
break;
case RELAY_2_NO_MESSAGE_ID:
if (data == 1)
{
if (!is_Relay_2_in_Alert)
{
relay2PreviousState = true;
digitalWrite(RELAY_2_NO_PIN, HIGH);
pixels.setPixelColor(2, pixels.Color(0, 255, 0));
pixels.setPixelColor(3, pixels.Color(0, 255, 0));
pixels.show();
Serial.println("Relay 2 is ON");
}
//delay(mesageDelay);
}
else
{
if (!is_Relay_2_in_Alert)
{
relay2PreviousState = false;
digitalWrite(RELAY_2_NO_PIN, LOW);
pixels.setPixelColor(2, pixels.Color(255, 0, 0));
pixels.setPixelColor(3, pixels.Color(255, 0, 0));
pixels.show();
Serial.println("Relay 2 is OFF");
// delay(mesageDelay);
}
}
break;
case RELAY_1_ALERT_NODE_MESSAGE_ID:
relay_1_alert_node = data;
Serial.print("Relay 1 alert node: ");
Serial.println(relay_1_alert_node);
break;
case RELAY_2_ALERT_NODE_MESSAGE_ID:
relay_2_alert_node = data;
Serial.print("Relay 2 alert node: ");
Serial.println(relay_2_alert_node);
break;
case RELAY_1_ALERT_INVERTED_MESSAGE_ID:
Relay_1_Alert_Inverted = data;
Serial.print("Relay 1 alert inverted: ");
Serial.println(Relay_1_Alert_Inverted);
break;
case RELAY_2_ALERT_INVERTED_MESSAGE_ID:
Relay_2_Alert_Inverted = data;
Serial.print("Relay 2 alert inverted: ");
Serial.println(Relay_2_Alert_Inverted);
break;
default:
break;
}
}
}
if (nodeID == relay_1_alert_node)
{
if (messageID == 901 || messageID == 900)
{
if (data == 1)
{
is_Relay_1_in_Alert = true;
core.sendMessage(RELAY_1_IN_ALERT_MESSAGE_ID, (uint64_t)1);
digitalWrite(RELAY_1_NC_PIN, !Relay_1_Alert_Inverted);
pixels.setPixelColor(1, pixels.Color(Relay_1_Alert_Inverted ? 0 : 255, Relay_1_Alert_Inverted ? 255 : 0, 0));
pixels.setPixelColor(4, pixels.Color(Relay_1_Alert_Inverted ? 0 : 255, Relay_1_Alert_Inverted ? 255 : 0, 0));
pixels.show();
Serial.println("Relay 1 is in alert");
}
else
{
is_Relay_1_in_Alert = false;
core.sendMessage(RELAY_1_IN_ALERT_MESSAGE_ID, (uint64_t)0);
digitalWrite(RELAY_1_NC_PIN, !relay1PreviousState);
pixels.setPixelColor(1, pixels.Color(relay1PreviousState ? 0 : 255, relay1PreviousState ? 255 : 0, 0));
pixels.setPixelColor(4, pixels.Color(relay1PreviousState ? 0 : 255, relay1PreviousState ? 255 : 0, 0));
pixels.show();
Serial.println("Relay 1 is out of alert");
}
}
}
if (nodeID == relay_2_alert_node)
{
if (messageID == 901)
{
if (data == 1)
{
is_Relay_2_in_Alert = true;
core.sendMessage(RELAY_2_IN_ALERT_MESSAGE_ID, (uint64_t)1);
digitalWrite(RELAY_2_NO_PIN, Relay_2_Alert_Inverted);
pixels.setPixelColor(2, pixels.Color(Relay_2_Alert_Inverted ? 0 : 255, Relay_2_Alert_Inverted ? 255 : 0, 0));
pixels.setPixelColor(3, pixels.Color(Relay_2_Alert_Inverted ? 0 : 255, Relay_2_Alert_Inverted ? 255 : 0, 0));
pixels.show();
Serial.println("Relay 2 is in alert");
}
else
{
is_Relay_2_in_Alert = false;
core.sendMessage(RELAY_2_IN_ALERT_MESSAGE_ID, (uint64_t)0);
digitalWrite(RELAY_2_NO_PIN, relay2PreviousState);
pixels.setPixelColor(2, pixels.Color(relay2PreviousState ? 0 : 255, relay2PreviousState ? 255 : 0, 0));
pixels.setPixelColor(3, pixels.Color(relay2PreviousState ? 0 : 255, relay2PreviousState ? 255 : 0, 0));
pixels.show();
Serial.println("Relay 2 is out of alert");
}
}
}
}