Yup, Particle has made sure that all global variables are initialised with zero valuse, so for boolean the default is false, but sure enough explicit initialisation is good practice
But there is another problem with this bDHTstarted. It should only be reset to falseinside the if (!DHT.acquiring()) { ... } block, but it's done outside of it.
The same is true for DHTnextSampleTime.
It may well do, but it definetly makes your code not work as expected
That doesn't change a thing. I'd guess you sprinkled your curly braces not exactly the way they were in the original code and that does make or break your code.
@csh13, You can always test the examples on their own. However, when you combine examples together and add your own code, then it all has to be put together carefully. With experience you will learn why the libraries and examples are done the way they are. Sometimes the example fits your needs; sometimes your needs are different. Keep learning and working on coding; you will soon be doing it on your own.
Thinking of learning, thanks @ScruffR for the tip on default values. I just learned something.
The examples have the example files, but there is more than just that. It is necessary to fully setup your project.
The example depends on two libraries; one of these libraries is mentioned in your error message: Blynk.
The error message clearly indicates that the Blynk.h file could not be found. Therefore, you have to do something so the file can be found.
What I would do is ensure you are using the Blynk library correctly.
First, remove any blynk.cpp and blynk.h files from your project.
Second, go over the instructions on using libraries in the documentation here.
Third, add the Blynk library to your project following the instructions.
Fourth, once you are done, you can double-check your project.properties file. It should look something like this (I named my project Test; yours might be different)
Even though it says #include <blynk>
at the top, apparently doesnât mean you have it in your project. you have to manually press the include in project button to make it work. the automatic version from the example needs it.
in this post, that does - not apparently but definetly - not mean that library is now included in this post.
The same is the case with your source file - it's only text.
And for that exact reason @cyclin_al gave you a link to the docs
If you use a bundled example via the USE THIS EXAMPLE button, the library already gets imported automatically, hence that code is OK in the sample, but that's not the case for private projects hence different rules apply.
Now its flipping off at random times (kinda acts like it automatically hits the reset button but takes longer to get back online). Would you like me to post my code?
Loops that are to large and external power problems.
Am I using the ifs and bools correctly?
My relay will only click on when I have it connected to some sort of ground.
Is that regular?
/*
Attach D4 to Heater relay,
Attach sensor inputs Top to D3 and bottom to D2,
Attach V1 to the minimum slider,
Attach V2 to the maximum slider,
Temperture top comes out of V5,
Temperture bottom comes out of V10.
*/
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
char auth[] = "23e459b94bfe4ebc8b3669ef795c121e";
#define BLYNK_PRINT Serial
// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
/*
* FILE: DHT_2sensor.cpp
* VERSION: 0.4
* PURPOSE: Example that uses DHT library with two sensors
* LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
*
* Calls acquire on two sensors and monitors the results for long term
* analysis. It uses DHT.acquire and DHT.acquiring
* Also keeps track of the time to complete the acquire and tracks errors
*
* Scott Piette (Piette Technologies) scott.piette@gmail.com
* January 2014 Original Spark Port
* October 2014 Added support for DHT21/22 sensors
* Improved timing, moved FP math out of ISR
* September 2016 Updated for Particle and removed dependency
* on callback_wrapper. Use of callback_wrapper
* is still for backward compatibility but not used
* ScruffR
* February 2017 Migrated for Libraries 2.0
* Fixed blocking acquireAndWait()
* and previously ignored timeout setting
* Added timeout when waiting for Serial input
* Fixed possible cloud drop with faulty sensor
*
* With this library connect the DHT sensor to the following pins
* Spark Core: D0, D1, D2, D3, D4, A0, A1, A3, A5, A6, A7
* Particle : any Pin but D0 & A5
* See docs for more background
* https://docs.particle.io/reference/firmware/photon/#attachinterrupt-
*/
// NOTE DHT_REPORT_TIMING requires DHT_DEBUG_TIMING in PietteTech_DHT.h for debugging edge->edge timings
//#define DHT_REPORT_TIMING
#include "PietteTech_DHT.h"
#define DHTTYPEA DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINA D3 // Digital pin for comunications
#define DHTTYPEB DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINB D2 // Digital pin for comunications
// Instantiate two class objects
PietteTech_DHT DHTA(DHTPINA, DHTTYPEA);
PietteTech_DHT DHTB(DHTPINB, DHTTYPEB);
int n; // counter
#define LOOP_DELAY 5000 // 5s intervals
int _sensorA_error_count;
int _sensorB_error_count;
int _spark_error_count;
unsigned long _lastTimeInLoop;
bool heatOn;
WidgetLCD lcd(V3);
void setup()
{
Serial.begin(9600);
while (!Serial.available() && millis() < 30000) {
Serial.println("Press any key to start.");
Particle.process();
delay(1000);
}
Serial.println("DHT 2 Sensor program using DHT.acquire and DHT.aquiring");
Serial.print("LIB version: ");
Serial.println(DHTLIB_VERSION);
Serial.println("---------------");
delay(1000); // Delay 1s to let the sensors settle
_lastTimeInLoop = millis();
Serial.begin(9600);
delay(5000); // Allow board to settle
Blynk.begin(auth);
}
#if defined(DHT_REPORT_TIMING)
// This function will report the timings collected
void printEdgeTiming(class PietteTech_DHT *_d) {
byte n;
volatile uint8_t *_e = &_d->_edges[0];
Serial.print("Edge timing = ");
for (n = 0; n < 41; n++) {
Serial.print(*_e++);
if (n < 40)
Serial.print(".");
}
Serial.print("\n\r");
}
#endif
void printSensorData(class PietteTech_DHT *_d) {
int result = _d->getStatus();
if (result != DHTLIB_OK)
if (_d == &DHTA)
_sensorA_error_count++;
else
_sensorB_error_count++;
switch (result) {
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Error\n\r\tChecksum error");
break;
case DHTLIB_ERROR_ISR_TIMEOUT:
Serial.println("Error\n\r\tISR time out error");
break;
case DHTLIB_ERROR_RESPONSE_TIMEOUT:
Serial.println("Error\n\r\tResponse time out error");
break;
case DHTLIB_ERROR_DATA_TIMEOUT:
Serial.println("Error\n\r\tData time out error");
break;
case DHTLIB_ERROR_ACQUIRING:
Serial.println("Error\n\r\tAcquiring");
break;
case DHTLIB_ERROR_DELTA:
Serial.println("Error\n\r\tDelta time to small");
break;
case DHTLIB_ERROR_NOTSTARTED:
Serial.println("Error\n\r\tNot started");
break;
default:
Serial.println("Unknown error");
break;
}
#if defined(DHT_REPORT_TIMING)
// print debug timing information
printEdgeTiming(_d);
#endif
Serial.print("Humidity (%): ");
Serial.println(_d->getHumidity(), 2);
Serial.print("Temperature (oC): ");
Serial.println(_d->getCelsius(), 2);
Serial.print("Temperature (oF): ");
Serial.println(_d->getFahrenheit(), 2);
Blynk.virtualWrite(V5, DHTA.getFahrenheit());
Blynk.virtualWrite(V10, DHTB.getFahrenheit());
Serial.print("Temperature (K): ");
Serial.println(_d->getKelvin(), 2);
Serial.print("Dew Point (oC): ");
Serial.println(_d->getDewPoint());
Serial.print("Dew Point Slow (oC): ");
Serial.println(_d->getDewPointSlow());
}
void loop()
{
Blynk.run();
float minTemp = V1;
float maxTemp = V2;
int temp = (int)DHTB.getFahrenheit();
unsigned long _us = millis();
unsigned long _delta = (_us - _lastTimeInLoop);
if (_delta > (1.05 * LOOP_DELAY))
_spark_error_count++;
// Launch the acquisition on the two sensors
DHTA.acquire();
DHTB.acquire();
// Print information for Sensor A
Serial.print("\n");
Serial.print(n);
Serial.print(" : ");
Serial.print((float)(_delta / 1000.0));
Serial.print("s");
if (_sensorA_error_count > 0 || _spark_error_count > 0) {
Serial.print(" : E=");
Serial.print(_sensorA_error_count);
Serial.print("/");
Serial.print(_spark_error_count);
}
Serial.print(", Retrieving information from sensor: ");
Serial.print("Read sensor Top: ");
while (DHTA.acquiring()) Particle.process();
printSensorData(&DHTA);
// Print information for Sensor B
Serial.print("\n");
Serial.print(n);
Serial.print(" : ");
Serial.print((float)(_delta / 1000.0));
Serial.print("s");
if (_sensorB_error_count > 0 || _spark_error_count > 0) {
Serial.print(" : E=");
Serial.print(_sensorB_error_count);
Serial.print("/");
Serial.print(_spark_error_count);
}
Serial.print(", Retrieving information from sensor: ");
Serial.print("Read sensor Bottom: ");
while (DHTB.acquiring()) Particle.process();
printSensorData(&DHTB);
n++;
_lastTimeInLoop = _us;
delay(LOOP_DELAY);
if (temp < minTemp)
{
if (D4 == LOW)
{
digitalWrite(D4,HIGH);
lcd.clear();
lcd.print(0,0,"Turning on");
}
}
else
{
}
if (temp > maxTemp)
{
if (D4 == HIGH)
{
digitalWrite(D4,LOW);
lcd.clear();
lcd.print(0,0,"Turning off");
}
}
else
{
}
if (DHTA.getFahrenheit() > 199)
{
if (D4 == HIGH)
{
digitalWrite(D4,LOW);
lcd.clear();
lcd.print(0,0, "OverHeat! Emer-");
lcd.print(0,1, "gency Shutoff!!");
delay(600000);
}
}
else
{
}
if (D4 == HIGH)
{
Blynk.virtualWrite(V0,HIGH);
}
else
{
Blynk.virtualWrite(V0,LOW);
}
}