-
Notifications
You must be signed in to change notification settings - Fork 1
/
stazioneOK.ino
119 lines (101 loc) · 3.1 KB
/
stazioneOK.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
#include<ESP8266WiFi.h>
#include "DHT.h"
#include <ThingSpeak.h>
char ssid[] = "Wi-Fi House 2,4G";
char pass[] = "";
int status = WL_IDLE_STATUS;
WiFiClient client;
//ANEMOMETRO
#define WIND_SPD_PIN 14
volatile unsigned long timeSinceLastTick = 0;
volatile unsigned long lastTick = 0;
long secsClock = 0;
float vento=0;
//THINGSPEAK
unsigned long myChannelNumber = 1512831 ;
const char * myWriteAPIKey = "V";
const int updateThingSpeakInterval = 1 * 1000;
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//Variabili
float temperatura = 0.0;
float umidita = 0.0;
//Prototipi funzioni
void getDati();
void VisualizzaSeriale();
void InviaDati();
float Vento();
void windTick(void)
{
timeSinceLastTick = millis() - lastTick;
lastTick = millis();
}
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid,pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connesso");
Serial.println("indirizzo IP: ");
Serial.println(WiFi.localIP());
while (!Serial) continue;
ThingSpeak.begin(client);
pinMode(WIND_SPD_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(WIND_SPD_PIN), windTick, FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
getDati();
VisualizzaSeriale();
InviaDati();
vento=0;
Serial.println("Vado a dormire per 10 minuti circa");
ESP.deepSleep(6e8);
}
void getDati(){
temperatura = dht.readTemperature();
umidita = dht.readHumidity();
vento=Vento()*1.61;
}
void VisualizzaSeriale(){
Serial.print("Temperatura: "); Serial.print(temperatura); Serial.println(" °C");
Serial.print("Umidita': "); Serial.print(umidita); Serial.println(" %");
Serial.print("Velocita' vento: "); Serial.print(vento); Serial.println(" km/h");
Serial.println();
}
void InviaDati(){
Serial.println("Invio dati a Thingspeak");
ThingSpeak.setField(1, (float)temperatura);
ThingSpeak.setField(2, (float)umidita);
ThingSpeak.setField(3, (float)vento);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(updateThingSpeakInterval);
}
float Vento(){
static unsigned long outLoopTimer = 0;
static unsigned long wundergroundUpdateTimer = 0;
static unsigned long clockTimer = 0;
static unsigned long tempMSClock = 0;
float windSpeed=0;
tempMSClock += millis() - clockTimer;
clockTimer = millis();
while (tempMSClock >= 1000)
{
secsClock++;
tempMSClock -= 1000;
}
// This is a once-per-second timer that calculates and prints off various
// values from the sensors attached to the system.
if (millis() - outLoopTimer >= 2000)
{
outLoopTimer = millis();
// Windspeed calculation, in mph. timeSinceLastTick gets updated by an
// interrupt when ticks come in from the wind speed sensor.
if (timeSinceLastTick != 0) windSpeed = 1000.0/timeSinceLastTick;
}
return(windSpeed);
}