forked from chrisys/mini-lora-weatherstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-lora-weatherstation.ino
140 lines (119 loc) · 3.26 KB
/
mini-lora-weatherstation.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
#include <LoRaWan_APP.h>
#include <Arduino.h>
#include <Seeed_BME280.h>
#include <Wire.h>
#include "ttnparams.h"
bool ENABLE_SERIAL = false; // enable serial debug output here if required
uint32_t appTxDutyCycle = 300000; // the frequency of readings, in milliseconds(set 300s)
uint16_t userChannelsMask[6]={ 0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000 };
LoRaMacRegion_t loraWanRegion = ACTIVE_REGION;
DeviceClass_t loraWanClass = LORAWAN_CLASS;
bool overTheAirActivation = LORAWAN_NETMODE;
bool loraWanAdr = LORAWAN_ADR;
bool keepNet = LORAWAN_NET_RESERVE;
bool isTxConfirmed = LORAWAN_UPLINKMODE;
uint8_t appPort = 2;
uint8_t confirmedNbTrials = 4;
int temperature, humidity, batteryVoltage, batteryLevel;
long pressure;
BME280 bme280;
static void prepareTxFrame( uint8_t port )
{
// This enables the output to power the sensor
pinMode(Vext, OUTPUT);
digitalWrite(Vext, LOW);
delay(500);
if(!bme280.init()){
if(ENABLE_SERIAL){
Serial.println("Device error!");
}
}
// This delay is required to allow the sensor time to init
delay(500);
temperature = bme280.getTemperature() * 100;
humidity = bme280.getHumidity();
pressure = bme280.getPressure();
Wire.end();
// Turn the power to the sensor off again
digitalWrite(Vext, HIGH);
batteryVoltage = getBatteryVoltage();
batteryLevel = (BoardGetBatteryLevel() / 254) * 100;
appDataSize = 12;
appData[0] = highByte(temperature);
appData[1] = lowByte(temperature);
appData[2] = highByte(humidity);
appData[3] = lowByte(humidity);
appData[4] = (byte) ((pressure & 0xFF000000) >> 24 );
appData[5] = (byte) ((pressure & 0x00FF0000) >> 16 );
appData[6] = (byte) ((pressure & 0x0000FF00) >> 8 );
appData[7] = (byte) ((pressure & 0X000000FF) );
appData[8] = highByte(batteryVoltage);
appData[9] = lowByte(batteryVoltage);
appData[10] = highByte(batteryLevel);
appData[11] = lowByte(batteryLevel);
if(ENABLE_SERIAL){
Serial.print("Temperature: ");
Serial.print(temperature / 100);
Serial.print("C, Humidity: ");
Serial.print(humidity);
Serial.print("%, Pressure: ");
Serial.print(pressure / 100);
Serial.print(" mbar, Battery Voltage: ");
Serial.print(batteryVoltage);
Serial.print(" mV, Battery Level: ");
Serial.print(batteryLevel);
Serial.println(" %");
}
}
void setup()
{
boardInitMcu();
if(ENABLE_SERIAL){
Serial.begin(115200);
}
deviceState = DEVICE_STATE_INIT;
LoRaWAN.ifskipjoin();
}
void loop()
{
switch( deviceState )
{
case DEVICE_STATE_INIT:
{
printDevParam();
LoRaWAN.init(loraWanClass,loraWanRegion);
deviceState = DEVICE_STATE_JOIN;
break;
}
case DEVICE_STATE_JOIN:
{
LoRaWAN.join();
break;
}
case DEVICE_STATE_SEND:
{
prepareTxFrame( appPort );
LoRaWAN.send();
deviceState = DEVICE_STATE_CYCLE;
break;
}
case DEVICE_STATE_CYCLE:
{
// Schedule next packet transmission
txDutyCycleTime = appTxDutyCycle + randr( 0, APP_TX_DUTYCYCLE_RND );
LoRaWAN.cycle(txDutyCycleTime);
deviceState = DEVICE_STATE_SLEEP;
break;
}
case DEVICE_STATE_SLEEP:
{
LoRaWAN.sleep();
break;
}
default:
{
deviceState = DEVICE_STATE_INIT;
break;
}
}
}