/
DS18B20_average_measured_values.ino
157 lines (121 loc) · 6.46 KB
/
DS18B20_average_measured_values.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
/*******************************************************************************
* The Things Network - ABP Feather
*
* This uses ABP (Activation by Personalization), where session keys for
* communication would be assigned/generated by TTN and hard-coded on the device.
*
* Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
* Copyright (c) 2018 Terry Moore, MCCI
* Copyright (c) 2018 Brent Rubell, Adafruit Industries
*
* Permission is hereby granted, free of charge, to anyone
* obtaining a copy of this document and accompanying files,
* to do whatever they want with them without any restriction,
* including, but not limited to, copying, modification and redistribution.
* NO WARRANTY OF ANY KIND IS PROVIDED.
*
* Reference: https://github.com/mcci-catena/arduino-lmic
*
* Modified for DS18B20 - average measured values, 8. 4. 2022
*******************************************************************************/
#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 12 // Pin 12 on the Adafruit
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#include<CayenneLPP.h> // Cayenne Low Power Payload (LPP)
CayenneLPP lpp(51);
//---------------------------------------------------------------------------------------------------------------
// Timer
unsigned long previousMillis = 0; // Previous time
const long interval = 1000; // Interval 1000 ms
unsigned int countSeconds = 1; // Counting seconds
unsigned int sendDataEvery = 300; // Send data every 5 minutes
// Temperature sensor DS18B20
float temp = 0.0; // Variable for temperature
float numberOfSamples = 0; // Variable for number of measured samples by DS18B20
// Jobs
static osjob_t measurejob, sendjob;
//--------------------------------------- Here change your keys -------------------------------------------------
static const PROGMEM u1_t NWKSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // LoRaWAN NwkSKey, network session key, MSB
static const u1_t PROGMEM APPSKEY[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // LoRaWAN AppSKey, application session key, MSB
static const u4_t DEVADDR = 0x00000000; // LoRaWAN end-device address (DevAddr), MSB
//---------------------------------------------------------------------------------------------------------------
const lmic_pinmap lmic_pins = { // Pin mapping for the Adafruit Feather 32U4 LoRa
.nss = 8,
.rxtx = LMIC_UNUSED_PIN,
.rst = 4,
.dio = {7, 6, LMIC_UNUSED_PIN},
.rxtx_rx_active = 0,
.rssi_cal = 8,
.spi_freq = 1000000,
};
void onEvent (ev_t ev) {
if(ev == EV_TXCOMPLETE){
os_setCallback(&measurejob, do_measure);
}
}
void do_send(osjob_t* j){
lpp.reset();
lpp.addTemperature(1, temp / numberOfSamples); // Add the average temperature into channel 1
LMIC_setTxData2(1, lpp.getBuffer(), lpp.getSize(), 0); // Prepare upstream data transmission at the next possible time.
resetValues(); // Reset values
}
void resetValues() { // Reset values
temp = 0.0;
numberOfSamples = 0;
}
void measureTempsAndHumi(){
sensors.requestTemperatures(); // Send the command to get temperature readings from DS18B20
temp = temp + sensors.getTempCByIndex(0); // Add the current temperature to all measured temperatures
numberOfSamples++; // Add measured sample
}
void do_measure(osjob_t* j){
unsigned long currentMillis = millis(); // Current millis
if(currentMillis - previousMillis >= interval) { // Timer set to 1 second
previousMillis = currentMillis;
if(countSeconds % 10 == 0){ // Every 10 seconds
measureTempsAndHumi();
}
if(countSeconds % sendDataEvery == 0){ // Every 5 minutes
os_setCallback(&sendjob, do_send); // Call do_send
countSeconds = 0; // Zero seconds counter
}
countSeconds++; // + 1 second
}
if((countSeconds - 1) % sendDataEvery != 0){ // When is called do_send, don't call do_measure
os_setCallback(&measurejob, do_measure);
}
}
void setup() {
os_init();
LMIC_reset();
uint8_t appskey[sizeof(APPSKEY)];
uint8_t nwkskey[sizeof(NWKSKEY)];
memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
LMIC_setSession (0x13, DEVADDR, nwkskey, appskey);
//EU868
LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI); // g-band
LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7), BAND_CENTI); // g-band
LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK, DR_FSK), BAND_MILLI); // g2-band
LMIC_setLinkCheckMode(0); // Disable link check validation
LMIC.dn2Dr = DR_SF9; // TTN uses SF9 for its RX2 window.
LMIC_setDrTxpow(DR_SF9,14); // Set data rate and transmit power for uplink
LMIC_setAdrMode(0);
LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
sensors.begin(); // DS18B20
do_send(&sendjob); // Start sendjob
}
void loop() {
os_runloop_once();
}