-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdevice1.ino
155 lines (145 loc) · 5.09 KB
/
device1.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
/*
* This example show how possible control Arduino PINs or send messages to RPi/PC via 'router'
*
* Examples (use python client 'pjon_grpc_client.py' on RPi/PC side):
* Read Hardware Digital PIN (digitalRead(13)): ./pjon_grpc_client.py 1 21 H-13
* Read Hardware Analog PIN (analogRead(14)): ./pjon_grpc_client.py 1 21 H-14
* Write Hardware Digital PIN (digitalWrite(13, 1)): ./pjon_grpc_client.py 1 21 H-13-1
* Read Virtual PIN: ./pjon_grpc_client.py 1 21 V-0
* Write Virtual PIN: ./pjon_grpc_client.py 1 21 V-0-1
* Increase number with each request: ./pjon_grpc_client.py 1 21 N
* Enable sending messages every 1 second from Arduino to RPi/PC: ./pjon_grpc_client.py 1 21 M-1
*
*/
#define PJON_INCLUDE_PACKET_ID true
#define PJON_INCLUDE_SWBB true // Include SoftwareBitBang only
#include <PJON.h>
PJON<SoftwareBitBang> bus(21);
// For test virtual PINs
uint8_t vPIN[2] = { 0, 0 };
int master_id = 1; // Master ID (appication 'pjon_grpc_server' running on RPi)
int send_msg_repeatedly = 0;
unsigned long prevMillis = millis();
int n = 0;
void bus_reply(String response_str) {
int response_str_len = response_str.length();
char response[response_str_len + 1];
response_str.toCharArray(response, response_str_len + 1);
bus.reply(response, response_str_len);
bus.update();
}
void wrong_command(uint8_t *payload, uint16_t length) {
String message_in = "";
for (uint8_t i = 0; i != length; i++){
message_in += (char)payload[i];
}
String response_str = message_in + ">wrong command";
bus_reply(response_str);
bus.update();
}
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
// Hardware PINs
if ((char)payload[0] == 'H') {
if ((char)payload[1] == '-') {
String pin_num = String((char)payload[2]);
if ((char)payload[3] != '-')
pin_num += String((char)payload[3]);
int pin_num_int = pin_num.toInt();
if (pin_num_int > 1 && pin_num_int < 14) {
int pin_status;
if ((char)payload[3] == '-' || (char)payload[4] == '-') {
if ((char)payload[3] == '-') {
pin_status = (char)payload[4] - '0';
} else {
pin_status = (char)payload[5] - '0';
}
if (pin_status == 0 || pin_status == 1) {
digitalWrite(pin_num_int, pin_status);
String response_str = "H-" + pin_num + "-" + String(pin_status) + ">" + String(digitalRead(pin_num_int));
bus_reply(response_str);
} else {
wrong_command(payload, length);
}
} else {
String response_str = "H-" + pin_num + ">" + String(digitalRead(pin_num_int));
bus_reply(response_str);
}
} else if (pin_num_int > 13 && pin_num_int < 22) {
String response_str = "H-" + pin_num + ">" + String(analogRead(pin_num_int));
bus_reply(response_str);
} else {
wrong_command(payload, length);
}
} else {
wrong_command(payload, length);
}
// Virtual PINs
} else if ((char)payload[0] == 'V') {
if ((char)payload[1] == '-') {
int pin_num_int = String((char)payload[2]).toInt();
if (pin_num_int >= 0 && pin_num_int < 2) {
if ((char)payload[3] == '-') {
int pin_status = (char)payload[4] - '0';
if (pin_status == 0 || pin_status == 1) {
vPIN[pin_num_int] = pin_status;
String response_str = "V-" + String(pin_num_int) + "-" + String(pin_status) + ">" + String(vPIN[pin_num_int]);
bus_reply(response_str);
} else {
wrong_command(payload, length);
}
} else {
String response_str = "V-" + String(pin_num_int) + ">" + String(vPIN[pin_num_int]);
bus_reply(response_str);
}
} else {
wrong_command(payload, length);
}
} else {
wrong_command(payload, length);
}
} else if ((char)payload[0] == 'M') {
if ((char)payload[1] == '-') {
if ((char)payload[2] == '0') {
send_msg_repeatedly = 0;
String response_str = "M-0>0";
bus_reply(response_str);
} else if ((char)payload[2] == '1') {
send_msg_repeatedly = 1;
String response_str = "M-1>1";
bus_reply(response_str);
} else {
wrong_command(payload, length);
}
} else {
String response_str = "M>" + String(send_msg_repeatedly);
bus_reply(response_str);
}
} else if ((char)payload[0] == 'N') {
n += 1;
String response_str = "N>" + String(n);
bus_reply(response_str);
} else {
wrong_command(payload, length);
}
};
void loop() {
bus.receive();
bus.update();
unsigned long curMillis = millis(); // time now in ms
if (send_msg_repeatedly == 1 and curMillis - prevMillis >= 1000) {
bus.send(master_id, "<Incoming message every 1 sec", 29);
bus.update();
prevMillis = millis();
}
};
void setup() {
// Initialize LED 13 to be off (for tests)
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
bus.strategy.set_pin(12);
bus.set_receiver(receiver_function);
bus.set_synchronous_acknowledge(true);
bus.set_crc_32(true);
bus.set_packet_id(true);
bus.begin();
};