-
Notifications
You must be signed in to change notification settings - Fork 0
/
higrow_sensors.cpp
65 lines (55 loc) · 1.67 KB
/
higrow_sensors.cpp
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
#include <Arduino.h>
#include <DHT12.h>
#include <BH1750.h>
#include <Wire.h>
#include <sys/time.h>
#include "higrow_sensors.h"
void HiGrowSensors::toggleSensors(){
if(TOGGLE_SENSORS == false){
TOGGLE_SENSORS = true;
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 1);
} else {
TOGGLE_SENSORS = false;
pinMode(POWER_CTRL, OUTPUT);
digitalWrite(POWER_CTRL, 0);
}
}
HiGrowSensors::HiGrowSensors(){
d1 = new DHT12(DHT12_PIN, true);
}
void HiGrowSensors::begin(){
HiGrowSensors::toggleSensors();
delay(1000);
Wire.begin(I2C_SDA, I2C_SCL);
lightMeter.begin();
d1->begin();
delay(500);
}
SensorData HiGrowSensors::measure_sensors(){
//this part of the code implements the dht12 stuff
float temp = d1->readTemperature();
float hum = d1->readHumidity();
//this part implemets the humidity of the soil using a linear function which technically it isn't but it serves for this purpose
float soil_moisture = map(analogRead(SOIL_PIN), 0, 4095, 100, 0); //this needs more calibration?
float soil_salt = map(analogRead(SALT_PIN), 0, 4095, 0, 100);
float lux = lightMeter.readLightLevel();
lux = lightMeter.readLightLevel();
delay(500);
lux = lightMeter.readLightLevel();
Serial.printf("temperatura: %.f ºC\n", temp);
Serial.printf("humidade: %.f %\n", hum);
Serial.printf("Humidade do Solo %.f\n", soil_moisture);
Serial.printf("Sal do Solo %.f\n", soil_salt);
Serial.printf("Luminosidade %.f\n", lux);
SensorData s;
s.temp = temp;
s.hum = hum;
s.soil_moisture = soil_moisture;
s.soil_salt = soil_salt;
s.lux = lux;
struct timeval tempo;
gettimeofday(&tempo, NULL);
s.timestamp = tempo.tv_sec;
return s;
}