-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdot335-helium-http-adafruit-connect.ino
213 lines (156 loc) · 5.62 KB
/
dot335-helium-http-adafruit-connect.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
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
/*
Helium Send And Receive
For Adafruit non-CayeenLPP connections for simple sensor Base 64 encoded
In Helium "functions" section use this decoder
function Decoder(bytes, port, uplink_info) {
var decoded={};
try{
var result = String.fromCharCode.apply(null, bytes);
decoded.value = result;
return decoded;
} catch (err) {
return 'Decoder: ' + err.name + " : " + err.message;;
}
}
And in the integration section of helium use this JSON
{ "value": {{decoded.payload.value}} }
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
note: Helium must be setup for what it does with the data
*/
#include <Arduino.h> // Only needed by https://platformio.org/
#include <MKRWAN.h>
LoRaModem modem;
bool connected = false;
bool myWaitForDownlink = false;
bool myDownLink = false;
unsigned long myStoredMillisA;
unsigned long myStoredMillisB;
const unsigned long myTimerDurationA = 30000; // delay between sending data
const unsigned long myTimerDurationB = 5000; // delay to wait for a downlink
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// Note: Best to have the App_Device hard coded. Run the program once to see the value.
//#include "arduino_secrets.h"
#define SECRET_APP_EUI "00000000000000000"
#define SECRET_APP_KEY "00000000000000000000000000000000"
String appEui = SECRET_APP_EUI; // just strings of the above
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LEDR,OUTPUT);
pinMode(LEDG,OUTPUT);
pinMode(LEDB,OUTPUT);
digitalWrite(LEDR, HIGH); // new boards HIGH = off
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
//while (!Serial); // don't wait for serial
Serial.println("Wait 4");
delay(3000); // delay instead, so it works when disconnected
digitalWrite(LEDG, HIGH);// allows time to connect serial monitor
Serial.println("Wait 3");
delay(3000);
digitalWrite(LEDG, LOW);
Serial.println("Wait 2");
delay(3000);
digitalWrite(LEDG, HIGH);
Serial.println("Wait 1");
delay(3000);
digitalWrite(LEDG, LOW);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(US915)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
Serial.println("Now Disabling all channels and enable channel 1 only for Helium ");
modem.disableChannel(0);
modem.enableChannel(1); // only one enabled for Helium
modem.disableChannel(2);
modem.disableChannel(3);
modem.disableChannel(4);
modem.disableChannel(5);
modem.disableChannel(6);
delay(5000);
Serial.println("Now Joining the Helium Network ");
}
void loop() {
while (!connected) {
Serial.println("trying to reconnect");
digitalWrite(LEDR, HIGH); // new boards HIGH = off
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, LOW);
connected = modem.joinOTAA(appEui, appKey);
delay(5000);
digitalWrite(LEDR, HIGH); // new boards HIGH = off
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
delay(1000);
}
if (myWaitForDownlink){
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
myDownLink = true;
}
if (millis() - myStoredMillisB >= myTimerDurationB){ // Test whether the period has elapsed
myStoredMillisB = millis();
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
myWaitForDownlink = false;
}
}
if (myDownLink) {
myWaitForDownlink = false;
myDownLink = false;
Serial.print("Received: ");
Serial.write(rcv, i);
// for (unsigned int j = 0; j < i; j++) {
// Serial.print(rcv[j] >> 4, HEX);
// Serial.print(rcv[j] & 0xF, HEX);
// Serial.print(" ");
// }
Serial.println();
digitalWrite(LEDR, LOW); // new boards HIGH = off
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
}
}
if (millis() - myStoredMillisA >= myTimerDurationA){ // Test whether the period has elapsed
myStoredMillisA = millis(); // IMPORTANT to save the next stored time
String msg = String(analogRead(A0)); // program expecting numbers!
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
digitalWrite(LEDR, HIGH); // new boards HIGH = off
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, LOW);
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
digitalWrite(LEDR, LOW); // new boards HIGH = off
digitalWrite(LEDG, LOW);
digitalWrite(LEDB, HIGH);
}
myStoredMillisB = millis();
myWaitForDownlink = true;
//delay(1000);
// delay(30000); // delay 30 seconds for testing
} // end timerA
}