1045 |
jacho |
1 |
//#include <Servo.h> |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
//Zapojeni |
|
|
6 |
//Sledování slunce: |
|
|
7 |
//-RC6 - PD1 |
|
|
8 |
//-RC7 - PD0 |
|
|
9 |
|
|
|
10 |
//Měření energie |
|
|
11 |
//-RC6 - PD3 |
|
|
12 |
//-RC7 - PD7 |
|
|
13 |
|
|
|
14 |
//Meteorologická část |
|
|
15 |
//-RC6 - PB1 |
|
|
16 |
//-RC7 - PB0 |
|
|
17 |
|
|
|
18 |
//Zbyle propojeni |
|
|
19 |
//definici zapadleho slunce pro blok mereni energie |
|
|
20 |
//PC1 (A1) -RB1 (log.1 noc) |
|
|
21 |
|
|
|
22 |
//pin pro odpojeni SD karty |
|
|
23 |
//PC0 (A0) - GND(mereni) - VCC (mozno odpojit) |
|
|
24 |
|
|
|
25 |
//GPS modul |
|
|
26 |
//RXD - |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
signed int AZIMUT, azimutOD=0; //promena pro hodnotu azimutu Slunce |
|
|
30 |
int ELEVACE, elevaceOD=0; //promena pro hodnotu elevace Slunce |
|
|
31 |
|
|
|
32 |
#include <TinyGPS.h> |
|
|
33 |
#include <SoftwareSerial.h> |
|
|
34 |
#include <SD.h> |
|
|
35 |
#include <math.h> |
|
|
36 |
#include <stdlib.h> |
|
|
37 |
TinyGPS gps; |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
SoftwareSerial ss(5, 6); //pro pripojeni modulu GPS (PD5 PD6) |
|
|
41 |
char filename[10]; //pro ulozeni naszvu souboru |
|
|
42 |
|
|
|
43 |
char dataGPS[130]; |
|
|
44 |
char dataUvodni[60]; |
|
|
45 |
char dataNATACENI[10]; |
|
|
46 |
float flat, flon; //zemepisna sirka, delka |
|
|
47 |
unsigned long age; |
|
|
48 |
int year; |
|
|
49 |
byte month, day, hour, minute, second, hundredths; |
|
|
50 |
byte day2=0; |
|
|
51 |
unsigned long fix_age; |
|
|
52 |
int c=0; |
|
|
53 |
SoftwareSerial so1Serial(7, 3); |
|
|
54 |
SoftwareSerial so2Serial(8, 9); |
|
|
55 |
File myFile; |
|
|
56 |
|
|
|
57 |
int odchylka=5; //definuje po jakém kroku se má sledovat slunce |
|
|
58 |
|
|
|
59 |
void setup() |
|
|
60 |
{ |
|
|
61 |
int count=0; |
|
|
62 |
Serial.begin(9600); |
|
|
63 |
ss.begin(9600); |
|
|
64 |
pinMode(10, OUTPUT); //sd karta |
|
|
65 |
so1Serial.begin(9600); |
|
|
66 |
so2Serial.begin(9600); |
|
|
67 |
if (!SD.begin(4)) { //inicializace SD karty |
|
|
68 |
//Serial.println("inicializace se nepovedla"); |
|
|
69 |
return; |
|
|
70 |
|
|
|
71 |
//nastaveni preruseni na pinu PD2 |
|
|
72 |
//pro detekci vysouvani karty |
|
|
73 |
attachInterrupt(0, off, HIGH); |
|
|
74 |
interrupts(); |
|
|
75 |
|
|
|
76 |
} |
|
|
77 |
//Serial.println("inicializace provedena"); |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
//uzavre zapis na SD karte |
|
|
81 |
//pro opetovne zapnuti je potreba resetovat |
|
|
82 |
//mikropocitac |
|
|
83 |
void off (void) |
|
|
84 |
{ |
|
|
85 |
while(true) |
|
|
86 |
{ |
|
|
87 |
myFile.close(); |
|
|
88 |
while(true); |
|
|
89 |
} |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
int stupne (float rad) //prevod rad na stupne |
|
|
93 |
{ |
|
|
94 |
int a =(int) ((rad/3.14)*180); |
|
|
95 |
return a; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
float rad (float stup) //prevod stupne na rad |
|
|
101 |
{ |
|
|
102 |
float a = ((stup/180)*3.14); |
|
|
103 |
return a; |
|
|
104 |
} |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
//provede vypocet dnu od zacatku roku |
|
|
109 |
|
|
|
110 |
int den (int m, int d) //vstupem je mesic a den |
|
|
111 |
{ |
|
|
112 |
switch (m) { |
|
|
113 |
case 1: d=d; |
|
|
114 |
break; |
|
|
115 |
|
|
|
116 |
case 2: d=31+d; |
|
|
117 |
break; |
|
|
118 |
|
|
|
119 |
case 3: d=58+d; |
|
|
120 |
break; |
|
|
121 |
|
|
|
122 |
case 4: d=89+d; |
|
|
123 |
break; |
|
|
124 |
|
|
|
125 |
case 5: d=119+d; |
|
|
126 |
break; |
|
|
127 |
|
|
|
128 |
case 6: d=150+d; |
|
|
129 |
break; |
|
|
130 |
|
|
|
131 |
case 7: d=180+d; |
|
|
132 |
break; |
|
|
133 |
|
|
|
134 |
case 8: d=211+d; |
|
|
135 |
break; |
|
|
136 |
|
|
|
137 |
case 9: d=242+d; |
|
|
138 |
break; |
|
|
139 |
|
|
|
140 |
case 10: d=262+d; |
|
|
141 |
break; |
|
|
142 |
|
|
|
143 |
case 11: d=303+d; |
|
|
144 |
break; |
|
|
145 |
|
|
|
146 |
case 12: d=333+d; |
|
|
147 |
break; |
|
|
148 |
|
|
|
149 |
default:printf("chyba"); |
|
|
150 |
break; |
|
|
151 |
} |
|
|
152 |
//Serial.println("Den: "); |
|
|
153 |
//Serial.println(d); |
|
|
154 |
return d; |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
float deklinace (float d) //vstupem je den v roce |
|
|
158 |
{ |
|
|
159 |
float b = 0.98630137; |
|
|
160 |
d=((rad(23.45))*sin(rad(b*(d-81)))); |
|
|
161 |
//Serial.println("Deklinace"); |
|
|
162 |
//Serial.println(d,4); |
|
|
163 |
return d; |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
float lstm (int gmt) //vstupem je GMT dane lokality |
|
|
167 |
{ |
|
|
168 |
float a; |
|
|
169 |
a=((rad(15))*gmt); |
|
|
170 |
//Serial.println("lstm"); |
|
|
171 |
//Serial.println(a,10); |
|
|
172 |
return a; |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
float eot (float d) //vstupem je den v roce |
|
|
176 |
{ |
|
|
177 |
float a; |
|
|
178 |
a=rad(0.98630137*(d-81)); |
|
|
179 |
a=9.87*sin(2*a)-7.53*cos(a)-1.5*sin(a); |
|
|
180 |
//Serial.println("eot"); |
|
|
181 |
// Serial.println(a,10); |
|
|
182 |
return a; |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
float tc (int delka, float lstm, float eot) //vstupem je zemepisná delka, LSTM, EoT |
|
|
186 |
{ |
|
|
187 |
float a; |
|
|
188 |
a=4*(float)(delka-lstm)+eot; |
|
|
189 |
//Serial.println("tc"); |
|
|
190 |
//Serial.println(a); |
|
|
191 |
return a; |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
float lst (float lt, float tc) //vstupem je den GMT dane lokality |
|
|
195 |
{ |
|
|
196 |
float a; |
|
|
197 |
a=lt+(tc/60); |
|
|
198 |
return a; |
|
|
199 |
//Serial.println("lst"); |
|
|
200 |
// Serial.println(a); |
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
float hra (float lst) //vstupem je den v roce |
|
|
204 |
{ |
|
|
205 |
lst=(rad(15))*(lst-12); |
|
|
206 |
//Serial.println("hra"); |
|
|
207 |
//Serial.println(lst,4); |
|
|
208 |
return lst; |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
//vstupem je mesic, den, hodina, minuty, GMT, zemepisna sirka ve stupnich |
|
|
212 |
void poziceSL(int m, int d, int h, int minu, int gmt, float sirka, float delka) |
|
|
213 |
{ |
|
|
214 |
float den1; |
|
|
215 |
float ele,azi,deklin, lstm1, eot1, tc1, lst1, hra1; |
|
|
216 |
float lt = h+((float)minu/60); |
|
|
217 |
//Serial.println("lt"); |
|
|
218 |
//Serial.println(lt); |
|
|
219 |
den1=(float) den(m,d); |
|
|
220 |
deklin=deklinace(den1); |
|
|
221 |
lstm1=lstm(gmt); |
|
|
222 |
eot1=eot(den1); |
|
|
223 |
tc1=tc(delka, lstm1, eot1); |
|
|
224 |
lst1=lst(lt,tc1); |
|
|
225 |
hra1=hra(lst1); |
|
|
226 |
sirka=rad(sirka); |
|
|
227 |
ele=asin((sin(deklin)*sin(sirka))+(cos(deklin)*cos(sirka)*cos(hra1))); |
|
|
228 |
azi = acos(((sin(deklin)*cos(sirka))-(cos(deklin)*sin(sirka)*cos(hra1)))/(cos(ele))); |
|
|
229 |
//printf("Azimut: %6.3f\r\n", azi); |
|
|
230 |
//printf("Elevce: %6.3f\r\n", ele); |
|
|
231 |
ELEVACE=stupne(ele); |
|
|
232 |
AZIMUT= stupne(azi); |
|
|
233 |
if(hra1>0) |
|
|
234 |
{ |
|
|
235 |
AZIMUT= 360-AZIMUT; |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
} |
|
|
239 |
|
|
|
240 |
void datagps() //vycet tat z GPS modulu |
|
|
241 |
{ |
|
|
242 |
bool newData = false; |
|
|
243 |
unsigned long chars; |
|
|
244 |
unsigned short sentences, failed; |
|
|
245 |
|
|
|
246 |
for (unsigned long start = millis(); millis() - start < 1000;) |
|
|
247 |
{ |
|
|
248 |
while (ss.available()) |
|
|
249 |
{ |
|
|
250 |
char c = ss.read(); |
|
|
251 |
if (gps.encode(c)) |
|
|
252 |
newData = true; |
|
|
253 |
} |
|
|
254 |
} |
|
|
255 |
|
|
|
256 |
if (newData) |
|
|
257 |
{ |
|
|
258 |
gps.f_get_position(&flat, &flon, &fix_age); |
|
|
259 |
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &fix_age); |
|
|
260 |
} |
|
|
261 |
} |
|
|
262 |
|
|
|
263 |
void loop () //nekonecna smycka |
|
|
264 |
{ |
|
|
265 |
int az, ele; |
|
|
266 |
byte zk; |
|
|
267 |
ss.listen(); |
|
|
268 |
datagps(); |
|
|
269 |
|
|
|
270 |
//int mesic, den, hodina, minuta, odchylka èasu od GMT, sirka, delka |
|
|
271 |
poziceSL(month, day, hour, minute, 0, flat, flon); |
|
|
272 |
|
|
|
273 |
//vytvori nazev souboru |
|
|
274 |
sprintf(filename, "MET%d%d.txt", month, day); |
|
|
275 |
//Serial.print(filename); |
|
|
276 |
|
|
|
277 |
//pro kazdy den se vytvori novy soubor |
|
|
278 |
//slouzi pro generovani uvodniho nadpisu v souboru |
|
|
279 |
if(!day2==day) |
|
|
280 |
{ |
|
|
281 |
c=0; |
|
|
282 |
day2=day; |
|
|
283 |
} |
|
|
284 |
|
|
|
285 |
if(ELEVACE<0) |
|
|
286 |
{ |
|
|
287 |
analogWrite(A1,255); |
|
|
288 |
} |
|
|
289 |
|
|
|
290 |
//Serial.print("Azimut"); |
|
|
291 |
//Serial.println(AZIMUT); |
|
|
292 |
|
|
|
293 |
//Serial.print("ELevace"); |
|
|
294 |
//Serial.println(ELEVACE); |
|
|
295 |
//Priprava dat pro zapis na SD kartu |
|
|
296 |
sprintf(dataGPS,"\r\n %4.0d %2.0d %2.0d %2.0d %2.0d %3.0d %3.0d %3.0d %3.0d", year, month, day, hour, minute, (int)flon, (int) flat, (int)AZIMUT,(int) ELEVACE); |
|
|
297 |
// Serial.println(dataGPS); |
|
|
298 |
|
|
|
299 |
//zamezi nataceni solarniho panelu v noc |
|
|
300 |
//prez den provede nove nastaveni solaru vzdy |
|
|
301 |
//po prekroceni odchylky od slunce o uhel |
|
|
302 |
//zadaný v promenné odchylka |
|
|
303 |
if(ELEVACE<0) |
|
|
304 |
{ |
|
|
305 |
//Serial.print("noc"); |
|
|
306 |
} |
|
|
307 |
else |
|
|
308 |
{ |
|
|
309 |
//Serial.print("den porovnani"); |
|
|
310 |
az=AZIMUT-azimutOD; |
|
|
311 |
az=abs(az); |
|
|
312 |
ele=ELEVACE-elevaceOD; |
|
|
313 |
ele=abs(ele); |
|
|
314 |
|
|
|
315 |
if((ele>odchylka)||(az>odchylka)) |
|
|
316 |
{ |
|
|
317 |
//Serial.print("odchylka nove nastaveni"); |
|
|
318 |
azimutOD=AZIMUT; |
|
|
319 |
elevaceOD=ELEVACE; |
|
|
320 |
sprintf(dataNATACENI,"%d ", ELEVACE); |
|
|
321 |
Serial.print(dataNATACENI); |
|
|
322 |
delay (100); |
|
|
323 |
sprintf(dataNATACENI,"%d ", AZIMUT); |
|
|
324 |
Serial.print(dataNATACENI); |
|
|
325 |
} |
|
|
326 |
} |
|
|
327 |
|
|
|
328 |
//otevre, nebo vytvory soubor |
|
|
329 |
//s nazvem v promenné filename |
|
|
330 |
File myFile = SD.open(filename, FILE_WRITE); |
|
|
331 |
|
|
|
332 |
//pri privedeni napeti VDD na pin PC0 procesoru, |
|
|
333 |
//dojde k uzavreni soubor a dokud nebude |
|
|
334 |
//na tento pin privede GND program bude pozastaven |
|
|
335 |
//Pro bezpecne odpojeni SD karty |
|
|
336 |
//while(!analogRead(A0)==0) |
|
|
337 |
// { |
|
|
338 |
// myFile.close(); |
|
|
339 |
// while(true); |
|
|
340 |
//} |
|
|
341 |
|
|
|
342 |
if (myFile) |
|
|
343 |
{ |
|
|
344 |
if(c==0) //obsahuje hlavicku souboru |
|
|
345 |
{ |
|
|
346 |
myFile.print("Meteorologicka stanice Jan Chroust 2013 \n"); |
|
|
347 |
sprintf(dataUvodni,"Mereni ze den %2.0d.%2.0d.%4.0d \n", day, month, year); |
|
|
348 |
myFile.print(dataUvodni); |
|
|
349 |
delay(10); |
|
|
350 |
myFile.print(" \n"); |
|
|
351 |
// Serial.print(dataUvodni); |
|
|
352 |
c=1; |
|
|
353 |
} |
|
|
354 |
else |
|
|
355 |
{ |
|
|
356 |
} |
|
|
357 |
|
|
|
358 |
myFile.print(dataGPS); //ulozi data na SD kartu |
|
|
359 |
//Serial.print(dataGPS); |
|
|
360 |
|
|
|
361 |
so1Serial.listen(); |
|
|
362 |
|
|
|
363 |
//cekani na data ze seriove linky |
|
|
364 |
while(so1Serial.available()==0) |
|
|
365 |
{ |
|
|
366 |
} |
|
|
367 |
delay(1000); |
|
|
368 |
|
|
|
369 |
//vycet dat ze seriove linky |
|
|
370 |
//data z blku mereni energie |
|
|
371 |
while (so1Serial.available()) |
|
|
372 |
{ |
|
|
373 |
char baterie = so1Serial.read(); |
|
|
374 |
myFile.print(baterie); |
|
|
375 |
//Serial.print(baterie); |
|
|
376 |
} |
|
|
377 |
|
|
|
378 |
so2Serial.listen(); |
|
|
379 |
|
|
|
380 |
while(so2Serial.available()==0) |
|
|
381 |
{ |
|
|
382 |
} |
|
|
383 |
delay(1000); |
|
|
384 |
//nacteni dat ze seiové linky |
|
|
385 |
//data z meteorologického bloku |
|
|
386 |
while (so2Serial.available()) |
|
|
387 |
{ |
|
|
388 |
char baterie = so2Serial.read(); |
|
|
389 |
myFile.print(baterie); |
|
|
390 |
// Serial.print(baterie); |
|
|
391 |
} |
|
|
392 |
|
|
|
393 |
// uzavreni souboru |
|
|
394 |
myFile.close(); |
|
|
395 |
} |
|
|
396 |
else |
|
|
397 |
{ |
|
|
398 |
//pri neotevreni souboru |
|
|
399 |
//Serial.println("Nepodarilo se otevrit soubor"); |
|
|
400 |
} |
|
|
401 |
|
|
|
402 |
delay(200); //frekvence zapisu na SD |
|
|
403 |
} |
|
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
|
|
|
409 |
|
|
|
410 |
|
|
|
411 |
|
|
|
412 |
|