-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvio_ttgo_todo_sd.ino
214 lines (185 loc) · 5.32 KB
/
envio_ttgo_todo_sd.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
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
208
209
210
211
212
213
214
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//libraries for mSD
#include <mySD.h>
//Libraries for ablocks
#include "ABlocks_TinyGPS.h"
#include <HardwareSerial.h>
#include <Adafruit_BMP280.h>
#include "ABlocks_DHT.h"
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 23
#define DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 866E6
//OLED pins
#define OLED_SDA 21
#define OLED_SCL 22
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//creacion de pantalla (objeto display)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
//objetos y variables para mSD
ext::File myFile;
const int chipSelect = 13;
//contador de tiempo
int counter = 0;
//variables creadas por ablocks
double presion;
double altitud;
double temperatura;
double humedad;
double longitud;
double latitud;
double velocidad;
String s_registro;
TinyGPS gps;
float gps_lat = 0;
float gps_long = 0;
float gps_speed_kmph = 0;
float gps_speed_mph = 0;
float gps_altitude = 0;
float gps_course = 0;
bool gps_fixed = false;
byte gps_day = 0;
byte gps_month = 0;
int gps_year = 0;
byte gps_hour = 0;
byte gps_min = 0;
byte gps_sec = 0;
byte gps_hund = 0;
HardwareSerial &gps_serial = Serial;
Adafruit_BMP280 bmp280;
DHT dht2(34, DHT22);
//funciones
void envia_lora() { //Send LoRa packet to receiver
LoRa.beginPacket();
LoRa.print(s_registro);
LoRa.endPacket();
}
void envia_oled() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Club Robotica Granada");
display.setCursor(0, 20);
display.setTextSize(1);
display.print("Paquete LoRa enviado.");
display.setCursor(0, 30);
display.print("Contador: ");
display.setCursor(60, 30);
display.print(counter);
display.display();
}
//funciones creadas en ablocks
void fnc_gps_update() {
unsigned long fix_age;
while (gps_serial.available()) {
if (gps.encode(gps_serial.read())) {
gps_fixed = false;
gps_lat = gps_long = gps_speed_kmph = gps_speed_mph = gps_course = 0;
gps_day = gps_month = gps_year = gps_hour = gps_min = gps_sec = 0;
gps.f_get_position(&gps_lat, &gps_long, &fix_age);
if (fix_age != TinyGPS::GPS_INVALID_AGE && fix_age < 5000) {
gps_fixed = true;
gps.crack_datetime(&gps_year, &gps_month, &gps_day, &gps_hour, &gps_min, &gps_sec, &gps_hund, &fix_age);
gps_altitude = gps.f_altitude();
gps_course = gps.f_course();
gps_speed_kmph = gps.f_speed_kmph();
gps_speed_mph = gps.f_speed_mph();
}
}
}
}
void leesensores() {
presion = (bmp280.readPressure() / 100.0);
altitud = bmp280.readAltitude();
temperatura = dht2.readTemperature();
humedad = dht2.readHumidity();
if (gps_fixed) {
longitud = gps_long;
latitud = gps_lat;
velocidad = gps_speed_kmph;
}
//es importante comentar que si no se pone nada a los valores numéricos te da una precisión de 2 decimales, a los valores del GPS de longitud
//y latitud le vamos a dar 8 decimales, poniendo (valor,8)
s_registro = String(counter) + String(";") + String(presion) + String(";") + String(altitud) + String(";") + String(temperatura) + String(";") + String(humedad) + String(";") + String(longitud,8) + String(";") + String(latitud,8) + String(";") + String(velocidad) + String(";");
}
void envia_sd() {
myFile = SD.open("datos.csv", FILE_WRITE);
if (myFile) {
myFile.println(s_registro);
// close the file:
myFile.close();
}
}
void setup() {
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("LORA SENDER ");
display.display();
/*initialize Serial Monitor
Serial.begin(115200);
Serial.println("Prueba de envio LoRa");*/
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
display.setCursor(0, 10);
display.print("Starting LoRa failed!");
display.display();
//Serial.println("Starting LoRa failed!");
while (1)
;
}
//Serial.println("LoRa Inicializacion OK!");
display.setCursor(0, 10);
display.print("Inicializacion OK!");
display.display();
delay(2000);
//setup mSD
pinMode(SS, OUTPUT);
SD.begin(13, 15, 2, 14); //definición de pines CS,MOSI,MISO,SCK
//setup de ablocks
pinMode(2, INPUT);
bmp280.begin(0x76);
bmp280.setSampling(Adafruit_BMP280::MODE_NORMAL, Adafruit_BMP280::SAMPLING_X2, Adafruit_BMP280::SAMPLING_X16, Adafruit_BMP280::FILTER_X16, Adafruit_BMP280::STANDBY_MS_500);
dht2.begin();
gps_serial.begin(9600);
gps_serial.println("$PMTK220,1000*1F");
}
void loop() {
fnc_gps_update();
leesensores();
envia_lora();
envia_oled();
envia_sd();
counter++;
delay(1000);
}