forked from food-future/skyline-sbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada_m0_lora_dht_analog_tx.ino
181 lines (122 loc) · 3.85 KB
/
ada_m0_lora_dht_analog_tx.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
#include <RTCZero.h>
#include <SPI.h>
#include <RH_RF95.h>
#include "DHT.h"
#define DHTPIN 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#include <ArduinoJson.h>
#define DEVID 6
#define RESOLUTION 4096
// for feather m0
#define RFM95_CS 8
#define RFM95_RST 4
#define RFM95_INT 3
//#define RF95_FREQ 915.0
#define RF95_FREQ 868.0
RTCZero zerortc;
// Set how often alarm goes off here
const byte alarmSeconds = 10;
const byte alarmMinutes = 0;
const byte alarmHours = 0;
volatile bool alarmFlag = false; // Start awake
// Singleton instance of the radio driver
RH_RF95 rf95(RFM95_CS, RFM95_INT);
// Blinky on receipt
#define LED 13
// Voltage PIN
#define VBATPIN A7
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
dht.begin(); //begin the humidity tester
analogReadResolution(12); // on M0 only ..
//Serial.begin(9600);
//delay(3000); // Wait for console
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//radio manual reset
digitalWrite(RFM95_RST, LOW);
delay(10);
digitalWrite(RFM95_RST, HIGH);
delay(10);
while (!rf95.init()) {
//Serial.println("LoRa radio init failed");
while (1);
}
//Serial.println("LoRa radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
if (!rf95.setFrequency(RF95_FREQ)) {
//Serial.println("setFrequency failed");
while (1);
}
//Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
// The default transmitter power is 13dBm, using PA_BOOST.
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
// you can set transmitter powers from 5 to 23 dBm:
rf95.setTxPower(23, false);
zerortc.begin(); // Set up clocks and such
resetAlarm(); // Set alarm
zerortc.attachInterrupt(alarmMatch); // Set up a handler for the alarm
}
int16_t packetnum = 0; // packet counter, we increment per xmission
void loop()
{
if (alarmFlag == true) {
alarmFlag = false; // Clear flag
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
//Serial.println("Alarm went off - I'm awake!");
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
//float t = 3.2;
//float h = 2.;
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["id"] = DEVID;
root["packetnum"] = packetnum;
JsonObject& data = root.createNestedObject("data");
data["temp"] = double_with_n_digits(t,2);
data["humidity"] = double_with_n_digits(h,2);
data["A0"] = analogRead(A0);
data["A1"] = analogRead(A1);
data["A2"] = analogRead(A2);
data["A3"] = analogRead(A3);
data["A4"] = analogRead(A4);
data["A5"] = analogRead(A5);
char buf[251];
root.printTo(buf, sizeof(buf));
buf[sizeof(buf)-1] = 0;
//Serial.print("Sending "); Serial.println(buf);
rf95.send((uint8_t *)buf, sizeof(buf));
//Serial.println("Waiting for packet to complete..."); delay(10);
rf95.waitPacketSent();
packetnum++; // update the packet number
digitalWrite(LED,HIGH);
delay(50);
digitalWrite(LED, LOW);
}
resetAlarm(); // Reset alarm before returning to sleep
//Serial.println("Alarm set, going to sleep now.");
//digitalWrite(LED_BUILTIN, LOW);
delay(1000);
zerortc.standbyMode(); // Sleep until next alarm match
}
void alarmMatch(void)
{
alarmFlag = true; // Set flag
}
void resetAlarm(void) {
byte seconds = 0;
byte minutes = 0;
byte hours = 0;
byte day = 1;
byte month = 1;
byte year = 1;
zerortc.setTime(hours, minutes, seconds);
zerortc.setDate(day, month, year);
zerortc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds);
zerortc.enableAlarm(zerortc.MATCH_HHMMSS);
}