-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNRF24_receiver_depth_temperature_megaPro.ino
More file actions
207 lines (187 loc) · 5.52 KB
/
NRF24_receiver_depth_temperature_megaPro.ino
File metadata and controls
207 lines (187 loc) · 5.52 KB
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
#define DEBUG
#ifdef DEBUG
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x) // define empty, so macro does nothing
#define debugln(x)
#endif
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
#include <Wire.h>
#include <DHT12.h>
#include "RTClib.h"
// Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8
RF24 radio(46,47);
// Topology
//const uint64_t pipes = 0xABCDABCD71LL; // Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[] = {0xABCDABCD71LL, 0xB3B4B5B6F1LL};
const long RFUpdatePeriod = 600000L; //300000=5min 600000=10min
long RFLastUpdate = 0;
// A single byte to keep track of the data being sent back and forth
typedef struct {
int dist; //distante
int perc; //percentage
int volt; //battery reading
} RxMsg;
RxMsg packet;
typedef struct {
int lauko;
int svetai;
int zidinys;
int garazas;
int hidroforas;
int palepe;
} TempMsg;
TempMsg packet1;
char LCDTxt[20];
unsigned int fVolt = 0;
unsigned int heaVolt = 0;
unsigned int decVolt = 0;
//LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define button_LCD 19
byte backlightState = 0;
const char* LCDLine0 = "%02u:%02u %s\xDF""C/%02d%%";
const char* LCDLine1 = "Back:%03dcm %02d%% %d.%02dV";
const char* LCDLine1Error = "Back:nera duom. 10m ";
const char* LCDLine2 = "Lauk Sv Zi Ga Hi Pa ";
const char* LCDLine3 = "%02d %02d %02d %02d %02d %02d";
char str_temp[6];
//RTC
RTC_DS1307 RTC;
DateTime dt_now;
const long Temp_Period = 60000L; //30000=30sec
long previousMillis = 0;
//DHT12
DHT12 dht12;
float dht_temp;
int dht_hum;
const float dht_adj = 3.5;
void setup() {
Serial.begin(115200);
debugln(F("Starting init"));
//RTC INIT
Wire.begin();
RTC.begin();
if (! RTC.begin()) {
debugln(F("Couldn't find RTC"));
while (1);
}
if (RTC.isrunning()) {
debugln(F("Nustatom RTC laika!!!"));
//RTC.adjust(DateTime(__DATE__, __TIME__));
}
lcd.begin();
lcd.backlight();
lcd.home ();
lcd.setCursor(0,0);
lcd.print(F("Temperaturos/backos"));
lcd.setCursor(0,1);
lcd.print(F("sistema v.1"));
Wire.begin();
//NRF24 init
printf_begin();
// Setup and configure rf radio
radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.enableAckPayload(); // Allow optional ack payloads
radio.setRetries(0,15); // Smallest time between retries, max no. of retries
//radio.setPayloadSize(2); // Here we are sending 1-byte payloads to test the call-response speed
radio.enableDynamicPayloads();
radio.setDataRate(RF24_250KBPS); // reducing bandwidth
radio.openReadingPipe(0,pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.startListening(); // Start listening
radio.printDetails();
pinMode(button_LCD, INPUT_PULLUP);
dht12.begin();
delay(1000);
lcd.clear();
debugln(F("Init complete"));
readDHT12();
}
void readDHT12(){
if(backlightState == 0){
dht_temp = dht12.readTemperature() - dht_adj;
}else{
dht_temp = dht12.readTemperature();
}
dht_hum = dht12.readHumidity();
debugln("Temp: "+String(dht_temp)+ " , Hum: " + String(dht_hum));
}
void loop() {
dt_now = RTC.now();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > Temp_Period) {
previousMillis = currentMillis;
readDHT12();
}
backlightState = digitalRead(button_LCD);
if(backlightState == 0){
lcd.backlight();
}else{
lcd.noBacklight();
}
uint8_t pipe;
while(radio.available(&pipe) ){
unsigned long tim = micros();
//reading packet from Tank
delay(50);
//printf("Got response %d, round-trip delay: %lu microseconds\n\r",RX_msg,tim-time);
if(pipe == 0){
radio.read( &packet, sizeof(RxMsg));
Serial.println("Gautas paketas: " +String(sizeof(RxMsg)));
debugln(packet.dist);
debugln(packet.perc);
debugln(packet.volt);
debugln("-------");
int tempVolt = packet.volt;
debug("tempVolt:");
debugln(tempVolt);
fVolt = map(tempVolt, 0, 1023, 0, 803);
heaVolt = (fVolt/100);
decVolt = ((fVolt-heaVolt) % 100);
//debug("decVolt:");
//debugln(decVolt);
RFLastUpdate = millis();
}
delay(100);
//reading packet from Temperature
if(pipe == 1){
radio.read( &packet1, sizeof(TempMsg));
Serial.println("Gautas paketas: " +String(sizeof(TempMsg)));
debugln(packet1.lauko);
debugln(packet1.palepe);
debugln(packet1.svetai);
debugln(packet1.zidinys);
debugln(packet1.garazas);
debugln(packet1.hidroforas);
}
}
//output to LCD
//const char* LCDLine0 = "%02u:%02u %d.%02d \xB0C/%d%%";
lcd.setCursor(0,0);
dtostrf(dht_temp, 4, 2, str_temp);
sprintf(LCDTxt,LCDLine0,dt_now.hour(),dt_now.minute(), str_temp, dht_hum);
lcd.print(LCDTxt);
if(currentMillis - RFLastUpdate < RFUpdatePeriod){
//const char* LCDLine1 = "Back:%03dcm %02d%% %d.%02dV";
sprintf(LCDTxt,LCDLine1,packet.dist, packet.perc, heaVolt, decVolt);
lcd.setCursor(0,1);
lcd.print(LCDTxt);
}else{
lcd.setCursor(0,1);
lcd.print(LCDLine1Error);
}
lcd.setCursor(0,2);
lcd.print(LCDLine2);
//const char* LCDLine2 = "Lauk Sv Zi Ga Hi Pa ";
sprintf(LCDTxt,LCDLine3,packet1.lauko, packet1.svetai, packet1.zidinys, packet1.garazas, packet1.hidroforas, packet1.palepe);
lcd.setCursor(0,3);
lcd.print(LCDTxt);
delay(50);
}