Created
April 21, 2020 19:07
-
-
Save edalquist/50998aa15bc2186ba340c214add16195 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include <ArduinoJson.h> | |
// This #include statement was automatically added by the Particle IDE. | |
#include <HttpClient.h> | |
// This #include statement was automatically added by the Particle IDE. | |
#include <PietteTech_DHT.h> | |
// This #include statement was automatically added by the Particle IDE. | |
#include <DS18B20.h> | |
#include "math.h" | |
const int MAXRETRY = 20; | |
const float MIN_REPORT_TEMP_DIFF = 0.1; | |
#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302 | |
#define DHTPIN D5 // Digital pin for communications | |
// Soil Temp Sensor | |
DS18B20 TempSensor_0(D0, TRUE); | |
// Ambient Temp Sensor | |
PietteTech_DHT DHT(DHTPIN, DHTTYPE); | |
HttpClient http; | |
http_request_t request; | |
http_response_t response; | |
http_header_t headers[] = { | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
float prevSoilTemp; | |
void setup() { | |
pinMode(D0, INPUT); | |
// Start DHT sensor | |
DHT.begin(); | |
request.hostname = "192.168.0.70"; | |
request.port = 80; | |
Particle.function("heaterSwitch", heaterSwitch); | |
delay(1000); | |
} | |
float getSoilTemp() { | |
int tries = 0; | |
float soilTemp = 0; | |
do { | |
delay(100); | |
soilTemp = TempSensor_0.getTemperature(); | |
soilTemp = ToFahrenheit(soilTemp); | |
} while (!TempSensor_0.crcCheck() || MAXRETRY > tries++) ; | |
Particle.publish("soil/temp", String(soilTemp)); | |
float tempDiff = prevSoilTemp - soilTemp; | |
if (tempDiff < 0) { | |
tempDiff *= -1.0; | |
} | |
if (tempDiff > MIN_REPORT_TEMP_DIFF) { | |
request.path = String::format("/apps/api/251/devices/836/setTemperature/%f?access_token=something", soilTemp); | |
http.get(request, response, headers); | |
Particle.publish("soil/temp/http_status", String(response.status)); | |
if (response.status != 200) { | |
Particle.publish("soil/temp/http_error", response.body); | |
} | |
prevSoilTemp = soilTemp; | |
} | |
return soilTemp; | |
} | |
void logAmbient() { | |
int result = DHT.acquireAndWait(1000); // wait up to 1 sec (default indefinitely) | |
switch (result) { | |
case DHTLIB_OK: | |
// Serial.println("OK"); | |
break; | |
case DHTLIB_ERROR_CHECKSUM: | |
Particle.publish("ambient/error", "Checksum error"); | |
break; | |
case DHTLIB_ERROR_ISR_TIMEOUT: | |
Particle.publish("ambient/error", "ISR time out error"); | |
break; | |
case DHTLIB_ERROR_RESPONSE_TIMEOUT: | |
Particle.publish("ambient/error", "Response time out error"); | |
break; | |
case DHTLIB_ERROR_DATA_TIMEOUT: | |
Particle.publish("ambient/error", "Data time out error"); | |
break; | |
case DHTLIB_ERROR_ACQUIRING: | |
Particle.publish("ambient/error", "Acquiring"); | |
break; | |
case DHTLIB_ERROR_DELTA: | |
Particle.publish("ambient/error", "Delta time to small"); | |
break; | |
case DHTLIB_ERROR_NOTSTARTED: | |
Particle.publish("ambient/error", "Not Started"); | |
break; | |
default: | |
Particle.publish("ambient/error", "Unknown error"); | |
break; | |
} | |
Particle.publish("ambient/humidity", String(DHT.getHumidity())); | |
Particle.publish("ambient/temp", String(DHT.getFahrenheit())); | |
} | |
void loop() { | |
logAmbient(); | |
getSoilTemp(); | |
delay(11007); | |
} | |
float ToFahrenheit(float tempIn) { | |
float retVal; | |
retVal = ((tempIn * 1.8) + 32); | |
return retVal; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment