-
Notifications
You must be signed in to change notification settings - Fork 12
/
ReadSensors-LoRaWAN-LowPower.ino
112 lines (86 loc) · 2.36 KB
/
ReadSensors-LoRaWAN-LowPower.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
/*
MKR ENV Shield - Read Sensors
This example reads the sensors on-board the MKR ENV shield
and prints them to the Serial Monitor once a second.
The circuit:
- Arduino MKR board
- Arduino MKR ENV Shield attached
This example code is in the public domain.
*/
#include <MKRWAN.h>
#include <Arduino_MKRENV.h>
#include <ArduinoLowPower.h>
#include "arduino_secrets.h"
LoRaModem modem;
float temperature;
float humidity;
float pressure;
float illuminance;
void setup() {
Serial.begin(9600);
while (!Serial);
if (!ENV.begin()) {
Serial.println("Failed to initialize MKR ENV shield!");
while (1);
}
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
connectToLoRaWAN();
}
void connectToLoRaWAN(){
Serial.println("Connecting...");
int connected = modem.joinOTAA(APP_EUI, APP_KEY);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
delay(5000);
}
void sendSensorValues(){
Serial.println("Sending message...");
modem.setPort(3);
modem.beginPacket();
modem.write<float>(temperature);
modem.write<float>(humidity);
modem.write<float>(pressure);
modem.write<float>(illuminance);
int error = modem.endPacket(true);
if (error > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
}
Serial.println();
}
void loop() {
// read all the sensor values
temperature = ENV.readTemperature();
humidity = ENV.readHumidity();
pressure = ENV.readPressure();
illuminance = ENV.readIlluminance();
// print each of the sensor values
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(pressure);
Serial.println(" kPa");
Serial.print("Illuminance = ");
Serial.print(illuminance);
Serial.println(" lx");
// print an empty line
Serial.println();
sendSensorValues();
// wait 120 seconds to print again
//LowPower.idle(120000);
LowPower.sleep(120000);
//LowPower.deepSleep(120000);
//LowPower.deepSleep(); // Wake up externally
//SEE: https://www.arduino.cc/en/Reference/ArduinoLowPower
}