forked from futureshocked/RaspberryPiFullStack_Raspbian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino-rf24-dht22.ino
148 lines (121 loc) · 3.78 KB
/
Arduino-rf24-dht22.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
/* 1111 - The Arduino Node Sketch
* From Raspberry Pi Full Stack Raspbian
* https://app.techexplorations.com/courses/raspberry-pi-full-stack-raspbian/
*
* This sketch allows an Arduino to transmit DHT22 data to the Raspberry Pi
* base node, using the nRF24 transceiver.
* The sketch places the Arduino in low-power mode when it is not doing
* useful work.
*
*
* Components
* ----------
* - Arduino Uno
* - nRF24 transciever
* - a 22uF or similar bypass capacitor
* - A DHT22 sensor
* - A 10KOhm pull up resistor for the sensor
* -
* -
*
* Libraries
* ---------
* - RF24Network.h
* - RF24.h
* - DHT.h
* - LowPower.h
*
* Connections
* -----------
*
* Refer to lecture 1110.
*
* Created on March 6 2020 by Peter Dalmaris
*
*/
#include "LowPower.h"
#include <RF24Network.h>
#include <RF24.h>
#include <DHT.h>
// The DHT data line is connected to pin 2 on the Arduino
#define DHTPIN 2
#define DHTPIN_POWER 5
// Leave as is if you're using the DHT22. Change if not.
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// Radio with CE & CSN connected to 9 & 10
RF24 radio(9, 10);
RF24Network network(radio);
// Constants that identify this node and the node to send data to
const uint16_t this_node = 3;
const uint16_t parent_node = 0;
// The network header initialized for this node
RF24NetworkHeader header(parent_node);
void send_data() {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(DHTPIN_POWER, HIGH); // Turn on the sensor
delay(2000); // Wait for the sensor and RF22 to power up.
data_tx();
delay(10);
digitalWrite(DHTPIN_POWER, LOW); // Turn off the sensor
digitalWrite(LED_BUILTIN, LOW);
}
void setup(void)
{
// Initialize all radio related modules
SPI.begin();
radio.begin();
delay(5);
network.begin(90, this_node); // In this example, we use channel "90". All members of
// the same netowork must use the same channel.
pinMode(DHTPIN_POWER, OUTPUT);
// Use the internal pull up for the DHT
pinMode(DHTPIN, INPUT);
digitalWrite(DHTPIN, HIGH);
// Initialize the DHT library
dht.begin();
// Set up the Serial Monitor
Serial.begin(9600);
Serial.println("Starting");
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,
SPI_OFF, USART0_OFF, TWI_OFF);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(DHTPIN_POWER, HIGH); // Turn on the sensor
delay(2000); // Wait for the sensor and RF22 to power up.
data_tx();
delay(10);
digitalWrite(DHTPIN_POWER, LOW); // Turn off the sensor
digitalWrite(LED_BUILTIN, LOW);
}
void data_tx()
{
// Update network data
network.update();
// Read humidity (percent)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
String values_string = "";
values_string += String(h);
values_string += ",";
values_string += String(t);
// Headers will always be Temperature for this node
// We set it again each loop iteration because fragmentation of the messages might change this between loops
header.type = 't';
char payload[12]; //Give this char array enough space to accomodate the two float values converted to string
values_string.toCharArray(payload,sizeof(payload));
// Writing the message to the network means sending it
if (network.write(header, payload, sizeof(payload))) { //if (network.write(header, &message, sizeof(message))) {
Serial.print("Message sent\n");
} else {
Serial.print("Could not send message\n");
}
network.update();
}