-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoilSensorV4Code.cpp
209 lines (168 loc) · 5.52 KB
/
SoilSensorV4Code.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
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
//BME280 configuration
#include <SparkFunBME280.h>
BME280 airSensor;
//Thingspeak client configuration
#include <ThingSpeak.h>
TCPClient client;
unsigned int myChannelNumber = YOURCHANNELID; // replace with your ChannelID
const char * myWriteAPIKey = "YOURAPIKEY"; // replace with your WriteAPIKey
//FreedomPop Configuration -- if you use Particle SIM, you do not need this
STARTUP(cellular_credentials_set("fp.com.attz", "", "", NULL));
//This allows us to wait to connect to cellular until we want to
SYSTEM_MODE(SEMI_AUTOMATIC);
//Configure DS18B20 waterproof temp sensor
#include <DS18B20.h>
#include <math.h>
const int MAXRETRY = 4;
DS18B20 ds18b20(D2, true); //Sets Pin D2 for Water Temp Sensor and
char szInfo[64];
#define TMP36Sig A5 //Pin that TMP36 analog output is connected to
#define PowerPin A3 //Pin that drives NPN transistor to power sensors
#define soilSenseCapPin A4 //Pin that Capacitance sensor is connected to
PMIC pmic; //Charge Controller
FuelGauge fuel; //Battery Monitor
float soilSenseCap;
float airTemp;
float airPressure;
float airHumidity;
float airDewpoint;
float groundTemp;
float enclosureTemp;
float voltage;
float fuelLevel;
float chargeStatus;
void setup() {
//Turn off RGB Led to conserve power
RGB.control(true);
RGB.color(0,0,0);
//Initialize Serial Communication
Serial.begin(57600);
//switch NPN transistor to power sensors
pinMode(PowerPin, OUTPUT);
digitalWrite(PowerPin, HIGH);
//Configure Charge Controller for Solar Panel
//pmic.begin();
//pmic.setChargeCurrent(0,0,1,0,0,0);
//pmic.setInputVoltageLimit(4840);
//chargeStatus = pmic.getSystemStatus();
//Serial.print("Charge Status: ");
//Serial.println(chargeStatus);
//Configure BME280
airSensor.settings.commInterface = I2C_MODE;
airSensor.settings.I2CAddress = 0x76; //may be 0x77 depending on your sensor
airSensor.settings.runMode = 3; //Normal mode
airSensor.settings.tStandby = 0;
airSensor.settings.filter = 4;
airSensor.settings.tempOverSample = 5;
airSensor.settings.pressOverSample = 5;
airSensor.settings.humidOverSample = 1;
delay(10); // BME280 takes <5ms to power up
airSensor.begin();
//Initialize ThingSpeak client
ThingSpeak.begin(client);
}
void loop() {
if(fuel.getSoC() > 20){
Serial.println("");
delay(100);
getAirTemp();
delay(100);
getGroundTemp();
getEnclosureTemp();
getSoilData();
getBatteryLevel();
publishData();
}
System.sleep(SLEEP_MODE_DEEP, 300);
}
void getAirTemp(){
float airTempC = airSensor.readTempC();
airTemp = airSensor.readTempF();
airPressure = airSensor.readFloatPressure()*29.9212/101325;
airHumidity = airSensor.readFloatHumidity();
if(airTempC > 0)
{
float a = 6.1121;
float b = 17.368;
float c = 238.88;
float Gamma = log(airHumidity/100) + (b*airTempC)/(c + airTempC);
airDewpoint = (c*Gamma)/(b - Gamma);
}
if(airTempC < 0)
{
float a = 6.1121;
float b = 17.966;
float c = 247.15;
float Gamma = log(airHumidity/100) + (b*airTempC)/(c + airTempC);
airDewpoint = (c*Gamma)/(b - Gamma);
}
/*Serial.print("Temperature: ");
Serial.print(airTemp, 2);
Serial.println(" degrees F");
Serial.print("Temperature: ");
Serial.print(airSensor.readTempF(), 2);
Serial.println(" degrees F");
Serial.print("Pressure: ");
Serial.print(airPressure, 2);
Serial.println(" Pa");
Serial.print("%RH: ");
Serial.print(airHumidity, 2);
Serial.println(" %");*/
}
void getGroundTemp(){
float _temp;
float celsius;
int i = 0;
do {
_temp = ds18b20.getTemperature();
} while (!ds18b20.crcCheck() && MAXRETRY > i++);
if (i < MAXRETRY) {
celsius = _temp;
groundTemp = ds18b20.convertToFahrenheit(_temp);
Serial.print("Gnd Temp (F): ");
Serial.println(groundTemp);
}
else {
celsius = groundTemp = NAN;
Serial.println("Invalid reading");
}
}
void getEnclosureTemp() {
float enclosureTempC = (analogRead(TMP36Sig)*3300/4096-500);
float enclosureTempF = enclosureTempC*9/5+320;
enclosureTemp = enclosureTempF/10;
Serial.print("Enc Temp (F): ");
Serial.println(enclosureTemp);
}
void getSoilData(){
soilSenseCap = analogRead(soilSenseCapPin);
Serial.print("Soil Sense: ");
Serial.println(soilSenseCap);
}
void getBatteryLevel() {
fuelLevel = fuel.getSoC()*100/80;
voltage = fuel.getVCell();
Serial.print("Battery (V): ");
Serial.println(voltage);
Serial.print("Battery (%): ");
Serial.println(fuelLevel);
}
void publishData(){
Serial.println("Publishing now.");
Cellular.on();
Cellular.connect();
Particle.connect();
ThingSpeak.setField(1, airTemp);
ThingSpeak.setField(2, airHumidity);
ThingSpeak.setField(3, airDewpoint);
ThingSpeak.setField(4, airPressure);
ThingSpeak.setField(5, groundTemp);
ThingSpeak.setField(6, soilSenseCap);
ThingSpeak.setField(7, enclosureTemp);
ThingSpeak.setField(8, fuelLevel);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Particle.process();
delay(5000);
Particle.disconnect();
Cellular.off();
}