-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.ino
135 lines (109 loc) · 3.09 KB
/
union.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
#define MAX_MESSAGE_LEN 50
#include <SteamLink.h>
#include <SteamLinkLoRa.h>
#define VER "8"
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3
#define LORALED 13
#define VBATPIN A7
#define SL_ID 0x127
// minimum transmisison gap (ms)
#define MINTXGAP 125
// test LED and button
#define LED 5
#define BUTTON 6
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
struct SL_NodeCfgStruct config = {
1, // version
SL_ID, // slid
"Union", // name
"P2P Workshop Node", // description
43.645, // gps_lat 43.6452211,-79.3827583
-79.383, // gps_lon
180, // altitude
150, // max_silence in s
false, // battery powered
1 // radio_params
};
SteamLinkLoRa sl(&config);
struct SteamLinkLoRaConfig slconfig = { false, NULL, 0 };
/* Packet building */
char data[100];
int packet_num = 0;
// button state
int8_t bLast, bCurrent = 2;
void sl_on_receive(uint8_t *buf, uint8_t len) ;
// Don't put this on the stack:
uint8_t buf[MAX_MESSAGE_LEN];
int beforeTime = 0, afterTime = 0, nextSendTime = 0;
int waitInterval = 90000;
int getBatInfo() {
return int(analogRead(VBATPIN) * 6.45); // = *2*3.3/1024*1000
}
// Setup
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.print(F("!ID SL_testclient " VER " slid "));
Serial.println(SL_ID, HEX);
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(LED, HIGH);
pinMode(LORALED, OUTPUT);
sl.set_pins(RFM95_CS, RFM95_RST, RFM95_INT);
sl.init((void *)&slconfig, sizeof(slconfig));
sl.register_receive_handler(sl_on_receive);
Serial.println("Steamlink init done");
bLast = 2;
}
void loop()
{
uint8_t len = sizeof(buf);
sl.update();
bCurrent = digitalRead(BUTTON);
if ((millis() > nextSendTime) || (bCurrent != bLast)) {
packet_num += 1;
if (bCurrent != bLast) {
int8_t value = (bCurrent == LOW ? 1 : 0);
snprintf(data, sizeof(data), "Button %i pkt: %d", value, packet_num);
bLast = bCurrent;
} else {
snprintf(data, sizeof(data), "Hello World! pkt: %d vBat: %dmV", packet_num, getBatInfo());
}
beforeTime = millis();
digitalWrite(LORALED, HIGH);
Serial.print("Sending \"");
Serial.print(data);
Serial.println("\"");
bool rc = sl.send((uint8_t* )data);
digitalWrite(LORALED, LOW);
if (rc) {
afterTime = millis() - beforeTime;
// It has been reliably delivered to the next node.
Serial.print("tx time: ");
Serial.println(afterTime);
} else {
Serial.println("send failed");
}
nextSendTime = millis() + waitInterval;
}
}
void sl_on_receive(uint8_t *buf, uint8_t len) {
Serial.print("sl_on_receive: len: ");
Serial.print(len);
Serial.print(" msg: ");
Serial.println((char*)buf);
int v = atoi((char *)buf);
if (v == 0) {
digitalWrite(LED, LOW);
} else if (v == 1) {
digitalWrite(LED, HIGH);
} else {
waitInterval = MAX(MINTXGAP, v);
Serial.print("waitInterval now ");
Serial.println(waitInterval);
}
}