/
rfm69-gateway-sdcard.ino
144 lines (111 loc) · 3.85 KB
/
rfm69-gateway-sdcard.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
/*
Open Hive | Gateway Yun2SD
Copyright (C) 2016 by Clemens Gruber
Changes
-------
2015-10 Clemens Gruber | initial sketch
GNU GPL v3 License
------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Licence can be viewed at
http://www.gnu.org/licenses/gpl-3.0.txt
*/
// Prepare your SD card creating an empty folder in the SD root
// named "arduino". This will ensure that the Yun will create a link
// to the SD to the "/mnt/sd" path.
//
// You can remove the SD card while the Linux and the
// sketch are running but be careful not to remove it while
// the system is writing to it.
// libraries
// SD, Yun
#include <FileIO.h>
// radio
#include <RFM69.h> // RFM69 itself
#include <SPI.h> // SPI (for RTC)
#define NODEID 1 // must be unique for each node on the same network
#define NETWORKID 101 // the same on all nodes that talk to each other
#define FREQUENCY RF69_868MHZ // Match frequency to the hardware version of the radio
#define ENCRYPTKEY "demoKey4openHive" // exactly the same 16 characters/bytes on all nodes!
//#define IS_RFM69HW // uncomment only for RFM69_H_W! Leave out if you have RFM69W!
#define ACK_TIME 30 // max # of ms to wait for an ack
#define LED 13 // Moteino MEGAs have LEDs on D15
RFM69 radio;
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network
void setup() {
// Initialize Yun Bridge and FileSystem
Bridge.begin();
FileSystem.begin();
//debug
Serial.begin(9600);
// radio
delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW // only for RFM69_H_W!
radio.setHighPower();
#endif
radio.encrypt(ENCRYPTKEY);
radio.spyMode(promiscuousMode);
Serial.println(FREQUENCY);
}
void loop() {
if (radio.receiveDone()) {
Serial.print("[Node");Serial.print(radio.SENDERID, DEC);Serial.print("] ");
if (promiscuousMode) {
Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
}
char dataset[radio.DATALEN+1];
for (byte i = 0; i < radio.DATALEN; i++) {
dataset[i] = (char)radio.DATA[i];
}
dataset[radio.DATALEN] = '\0'; // add closing \0
// extract date from timestamp
char dateChar[11];
// starting by position 0 extract 10 characters
memcpy(dateChar, &dataset[0], 10 );
dateChar[10] = '\0'; // and add closing \0
// replace "/" with "-" for propper file´names
char *pch;
do {
pch = strstr (dateChar,"/");
strncpy (pch,"-",1);
puts (dateChar);
} while (pch != NULL);
char path[40];
char pathPrefix[] = "/mnt/sd/arduino/www/";
char folderPrefix[] = "node";
char fileExt[] = ".txt";
sprintf(path,"%s%s%03i",pathPrefix,folderPrefix,radio.SENDERID);
FileSystem.mkdir(path);
char pathAndFile[60];
sprintf(pathAndFile,"%s/%s%s",path,dateChar,fileExt);
Serial.print(pathAndFile);
File dataFile = FileSystem.open(pathAndFile, FILE_APPEND);
// if the file is available, write to it:
if (dataFile) {
dataFile.print(dataset);
dataFile.println();
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening file");
}
Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
Serial.println();
Blink(LED,300);
}
}
void Blink(byte PIN, int DELAY_MS) {
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}