forked from riyas-org/nrf24pihub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_basic.ino
141 lines (111 loc) · 3.23 KB
/
arduino_basic.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
// A simple sketch to pass multiple values to raspberry pi nrf hub
// For more details see http://blog.riyas.org
// credits to maniacbug,pynrf, and other libraries
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
// For using the temperature sensor DS18S20
#include <OneWire.h>
int DS18S20_Pin = 8; //DS18S20 Signal pin on digital 2
OneWire ds(DS18S20_Pin); // on digital pin 2
//for nrf24 debug
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
//for nrf24 debug
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
//nRF24 set the pin 9 to CE and 10 to CSN/SS
// Cables are:
// SS -> 10
// MOSI -> 11
// MISO -> 12
// SCK -> 13
RF24 radio(9,10);
//we only need a write pipe, but am planning to use it later
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL,0xF0F0F0F0D2LL };
// here we can send up to 30 chars
char SendPayload[31] = "NODE1";
char tempstr[10] = "NODE1";//to store dtostr
void setup(void) {
Serial.begin(57600); //Debug
printf_begin();
//nRF24 configurations
radio.begin();
radio.setChannel(0x4c);
radio.setAutoAck(1);
radio.setRetries(15,15);
radio.setDataRate(RF24_250KBPS);
radio.setPayloadSize(32);
radio.openReadingPipe(1,pipes[0]);
radio.openWritingPipe(pipes[1]);
radio.startListening();
radio.printDetails(); //for Debugging
}
void loop() {
//Get temperature from sensor
float temperature = getTemp();
float humidity =23; // add a sensor here
float pressure=24; // add another sensor here
// Assign temperature to payload, here am sending it as string
//add a temperature in between tag
strcat(SendPayload, "T");
dtostrf(temperature,2,2,tempstr);
strcat(SendPayload,tempstr);
strcat(SendPayload, "T"); // add first string
//Humidity
strcat(SendPayload, "H");
dtostrf(humidity,2,2,tempstr);
strcat(SendPayload,tempstr);
strcat(SendPayload, "H"); // add first string
//Pressure
strcat(SendPayload, "P");
dtostrf(pressure,2,2,tempstr);
strcat(SendPayload,tempstr);
strcat(SendPayload, "P"); // add first string
//send a heartbeat
radio.stopListening();
bool ok = radio.write(&SendPayload,strlen(SendPayload));
radio.startListening();
//Serial.println(SendPayload);
// slow down a bit
delay(1000);
}
// Getting temperature from DS18S20
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}