-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGEN STARTER V2
395 lines (320 loc) · 7.98 KB
/
GEN STARTER V2
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Servo.h>
Servo servo;
int fmr;
byte batSense = A0;
byte servoPort = 2;
byte genRelay2 = 4;
byte genRelay1 = 5;
byte acSense = 12;
byte manualButton = 13;
byte powerSwitchRelay = 14;
bool stat = false;
byte retry = 0;
const int numReadings = 20;
int flag = 0;
unsigned long initTime; //some global variables available anywhere in the program
unsigned long timeNow;
const unsigned long setTime = 10000; //the value is a number of milliseconds
unsigned long inits; //some global variables available anywhere in the program
unsigned long now;
const unsigned long sTime = 3500; //the value is a number of milliseconds
unsigned long startTime; //some global variables available anywhere in the program
unsigned long currentTime;
const unsigned long waitTime = 2000; //the value is a number of milliseconds
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
float input_voltage = 0.0;
float temp = 0.0;
float r1 = 10000.0;
float r2 = 1060.0;
uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};
uint8_t broadcastAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x65};
// Define variables to store DHT readings to be sent
float temperature;
float bat;
// Define variables to store incoming readings
int genStat = 0;
float batLvl;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message
{
int genStatus; //// send 1 0r zero
float batlvl; ///0-100
} struct_message;
// Create a struct_message called DHTReadings to hold sensor readings
struct_message valuesToSend;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus)
{
//Serial.print("Last Packet Send Status: ");
if (sendStatus == 0)
{
Serial.println("Delivery success");
}
else
{
Serial.println("Delivery fail");
}
}
// Callback when data is received
void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len)
{
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
genStat = incomingReadings.genStatus;
Serial.println(incomingReadings.genStatus);
if (fmr!= genStat){
retry=0;
fmr=genStat;
}
//batLvl = incomingReadings.batlvl;
}
float batlvl()
{
total = total - readings[readIndex];
readings[readIndex] = analogRead(batSense);
total = total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
{
readIndex = 0;
}
average = total / numReadings;
// temp = ( average * 5.4567) / 1023.0;
temp = (analogRead(A0) * 1) / 1023.0;
input_voltage = temp / (r2 / (r1 + r2));
return input_voltage;
}
void batLow()
{
batlvl();
if (batlvl() < 3.2)
{
digitalWrite(powerSwitchRelay, HIGH);
}
else
{
digitalWrite(powerSwitchRelay, LOW);
}
}
bool GenStart( int retry)
{ flag = 1;
if (retry == 0) {
servo.write(120); ////OPEN CHOKE
digitalWrite(genRelay1, HIGH);
delay(200);
digitalWrite(genRelay2, HIGH);
delay(2000);
digitalWrite(genRelay2, LOW);
servo.write(0); ////close CHOKE
delay(3000);
if (digitalRead(acSense) == 0)
{
return true;
}
else
{
return false;
}
}
else if (retry == 1) {
servo.write(0); ////OPEN CHOKE
digitalWrite(genRelay1, HIGH);
delay(200);
digitalWrite(genRelay2, HIGH);
delay(2000);
digitalWrite(genRelay2, LOW);
servo.write(0); ////close CHOKE
delay(3000);
if (digitalRead(acSense) == 0)
{
return true;
}
else
{
return false;
}
}
else if (retry == 2) {
servo.write(120); ////OPEN CHOKE
digitalWrite(genRelay1, HIGH);
delay(200);
digitalWrite(genRelay2, HIGH);
delay(3000);
digitalWrite(genRelay2, LOW);
servo.write(0); ////close CHOKE
delay(3000);
if (digitalRead(acSense) == 0)
{
return true;
}
else
{
return false;
}
}
else if (retry == 3) {
servo.write(0); ////OPEN CHOKE
digitalWrite(genRelay1, HIGH);
delay(200);
digitalWrite(genRelay2, HIGH);
delay(3000);
digitalWrite(genRelay2, LOW);
servo.write(0); ////close CHOKE
delay(3000);
if (digitalRead(acSense) == 0)
{
return true;
}
else
{
return false;
}
}
}
bool GenStop()
{
flag = 0;
digitalWrite(genRelay1, LOW);
delay(3000);
if (digitalRead(acSense) == HIGH)
{
return true;
}
else
{
return false;
}
}
void setup()
{
Serial.begin(9600);
// esp.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
wifi_set_macaddr(STATION_IF, &newMACAddress[0]);
// Init ESP-NOW
if (esp_now_init() != 0)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW Role
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
pinMode(manualButton, INPUT_PULLUP);
pinMode(acSense, INPUT);
pinMode(genRelay2, OUTPUT);
pinMode(genRelay1, OUTPUT);
pinMode(batSense, INPUT);
pinMode(powerSwitchRelay, OUTPUT);
digitalWrite(powerSwitchRelay, HIGH);
servo.attach(servoPort); //D4
servo.write(0);
// servo.detach(servoPort);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
{
readings[thisReading] = 0;
}
}
void loop()
{
timeNow = millis(); //get the current "time" (actually the number of milliseconds since the program started)
now = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (digitalRead(manualButton) == LOW)
{
//Serial.print("in");
startTime = millis(); //initial start time
while (digitalRead(manualButton) == LOW)
{
currentTime = millis();
delay(1);
if (currentTime - startTime >= waitTime) //test whether the period has elapsed
{
if (flag == 0 && digitalRead(acSense) == HIGH ) //if gen is off
{
GenStart(0);
}
else if (flag == 1 && digitalRead(acSense) == LOW) //if gen is on
{
GenStop();
flag = 0;
}
else if (flag == 1 && digitalRead(acSense) == HIGH ) //if gen is off
{
GenStart(0);
}
else if (flag == 0 && digitalRead(acSense) == LOW ) //if gen is off
{
GenStop();
}
startTime = millis();
}
}
}
//wait for elapsed time
if (timeNow - initTime >= setTime) //test whether the period has elapsed
{
if (genStat == 1 && retry < 4 && stat == 0) // if genstat variable is ==1 and retry hasnt timed out
{
if (digitalRead(acSense) == HIGH)
{
if (GenStart(retry) == true)
{
retry = 0;
stat = 1;
}
else
{
stat = 0;
retry++;
}
}
else {
retry = 0;
stat = 1;
}
}
initTime = timeNow;
}
if (genStat == 0.0) // if genstat variable is ==0
{
if (digitalRead(acSense) == LOW) // if gen is on
{
GenStop();
}
}
if (digitalRead(acSense) == HIGH) // if gen is off set the stat vatiable to 0
{
stat = 0;
}
else
{
stat = 1;
}
//Set values to send
valuesToSend.genStatus = stat;
valuesToSend.batlvl = batlvl();
Serial.println(genStat);
if (now - inits >= sTime) //test whether the period has elapsed
{
esp_now_send(broadcastAddress, (uint8_t *)&valuesToSend, sizeof(valuesToSend));
inits = now;
}
}