-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.cpp
98 lines (75 loc) · 1.84 KB
/
tx.cpp
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
#ifdef TRANSMIT
#include <RF24.h>
#include <ACS712.h>
#include "common.h"
#define OUTPUT_LED_PIN 4
#define INPUT_SWITCH_PIN 3
#define INPUT_CURRNET_SENSOR_PIN A0
#define CURRENT_THRESHOLD 0.3
#define CURRENT_FREQ 50 // 50Hz
#define LOOP_DELAY 100
// Globals
RF24 g_radio(9, 10);
ACS712 g_current_sensor(ACS712_30A, INPUT_CURRNET_SENSOR_PIN);
void setup(void)
{
#ifdef DEBUG
Serial.begin(9600);
#endif
// Set input pins
pinMode(INPUT_SWITCH_PIN, INPUT_PULLUP);
digitalWrite(INPUT_SWITCH_PIN, HIGH);
pinMode(INPUT_CURRNET_SENSOR_PIN, INPUT);
// Set output pins
pinMode(OUTPUT_LED_PIN, OUTPUT);
// Set up radio
g_radio.begin();
g_radio.enableAckPayload();
g_radio.enableDynamicPayloads();
g_radio.openWritingPipe(PIPE);
g_radio.setPALevel(RF24_PA_MIN);
g_radio.stopListening();
g_current_sensor.calibrate();
}
void loop(void)
{
msg_t msg {0};
bool switch_on = !digitalRead(INPUT_SWITCH_PIN);
float current = g_current_sensor.getCurrentAC(CURRENT_FREQ);
if (switch_on || current >= CURRENT_THRESHOLD)
{
msg |= MSG_ON_BIT;
}
#ifdef DEBUG
Serial.print("Current: ");
Serial.print(current);
Serial.print("\n");
Serial.print("Switch: ");
Serial.print(switch_on);
Serial.print("\n");
Serial.print("Wrote: ");
Serial.print(msg);
Serial.print("\n");
#endif
bool ack = g_radio.write(&msg, sizeof(msg));
if (!ack or !g_radio.available())
{
// No ACK or Empty ACK
msg = MSG_NULL;
}
else
{
while (g_radio.available())
{
g_radio.read(&msg, sizeof(msg));
}
}
#ifdef DEBUG
Serial.print("ACK: ");
Serial.print(msg);
Serial.print("\n");
#endif
digitalWrite(OUTPUT_LED_PIN, msg & MSG_ON_BIT);
delay(LOOP_DELAY);
}
#endif