summaryrefslogtreecommitdiff
path: root/esp/estacao_principal/estacao_principal.ino
blob: cfc54707a57ede277383bb61347fff417f67ffa0 (plain)
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
#include <Wire.h>
#include <Adafruit_BMP280.h>
#include <BH1750.h>
#include <DHT.h>

// LM393
#define LM393_INTERVAL 1000
#define LM393_PIN 35  // ou 35 se preferir

volatile unsigned long contagem_lm393 = 0;

volatile bool prevLm393State = LOW;
void isr_lm393() {
  bool state = digitalRead(LM393_PIN);

  if (prevLm393State != state) {
    contagem_lm393++;
  }

  prevLm393State = state;
}

unsigned long lastTime_lm393 = 0;
///////////////////////////////////////////////////////////////////////////

// BH1750
#define BH1750_INTERVAL 2000

BH1750 lightMeter;

unsigned long lastTime_bh1750 = 0;
///////////////////////////////////////////////////////////////////////////

// DHT11
#define DHT_PIN 32
#define DHTTYPE DHT11
DHT dht(DHT_PIN, DHTTYPE);

#define DHT11_HUM_INTERVAL  4000

unsigned long lastTime_dht11_hum = 0;

///////////////////////////////////

// BMP280
Adafruit_BMP280 bmp;
#define BMP280_PRESSURE_INTERVAL 5000
#define BMP280_TEMPERATURE_INTERVAL 3000

unsigned long lastTime_bmp280_press = 0;
unsigned long lastTime_bmp280_temp = 0;
/////////////////////////////////////

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);

  // LM393
  pinMode(LM393_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(LM393_PIN), isr_lm393, CHANGE);

  // BH1750
  lightMeter.begin();

  // DHT11
  dht.begin();

  // BMP280
  bmp.begin(0x76);

  Serial.println("Estacao iniciada");
}


void loop() {

  unsigned long now = millis();

  // LM393
  if (now - lastTime_lm393 >= LM393_INTERVAL) {
    int pulsos;

    const float FATOR_KHM = 2.4;
    
    noInterrupts();
    pulsos = contagem_lm393;
    contagem_lm393 = 0;
    interrupts();
    
    int giros = pulsos / 10;

    float rps = ((float)pulsos/10.0f) / (LM393_INTERVAL / 1000.0f);
    float velocidade = rps * FATOR_KHM;

    Serial.print("Velocidade do tempo: ");
    Serial.print(velocidade);
    Serial.println(" km/h");
    lastTime_lm393 = now;
  }

  // BH1750
  if (now - lastTime_bh1750 >= BH1750_INTERVAL) {
    Serial.print("Luz: ");
    Serial.print(lightMeter.readLightLevel());
    Serial.println(" lux");

    lastTime_bh1750 = now;
  }

  // DHT11 - Umidade
  if (now - lastTime_dht11_hum >= DHT11_HUM_INTERVAL) {
    Serial.print("Umidade: ");
    Serial.print(dht.readHumidity());
    Serial.println(" %");
    lastTime_dht11_hum = now;
  }

  // BMP280 - AIR PRESSURE
  if (now - lastTime_bmp280_press >= BMP280_PRESSURE_INTERVAL) {
    Serial.print("Pressao: ");
    Serial.print(bmp.readPressure() / 100.0f);
    Serial.println(" hPa");
    
    lastTime_bmp280_press = now;
  }

  // BMP280 - TEMPERATURE
  if (now - lastTime_bmp280_temp >= BMP280_TEMPERATURE_INTERVAL) {
    Serial.print("Temperatura: ");
    Serial.print(bmp.readTemperature());
    Serial.println(" C");
    
    lastTime_bmp280_temp = now;
  }
}