Here is my first code set. My c coding skills are a bit rusty…I wasn’t sure if something else I added was causing the problems so I loaded up the example code for idDHT22 and PietteTech_DHT, added some spark variable and ran the code. Both would start working then I would see the blue flashing light then back to breathing. Same thing this code was doing. I have IFTTT set to watch for the publish event in startup to email me when this event occurs, well my inbox is flooded with emails lol
// This #include statement was automatically added by the Spark IDE.
#include "elapsedMillis/elapsedMillis.h"
//#include "clickButton/clickButton.h"
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"
#include "PietteTech_DHT/PietteTech_DHT.h"
//#include "math.h"
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D3 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 3000 // Sample every 3 seconds
#define RELAY1 A6
#define TMPPIN A7
#define OLED_RESET D2
#define PUSHBUTTONPIN D6
#define _VERSION 0.1
float temperatureC, temperatureF = 0.0;
float relayTempC, relayTempF = 0.0;
float holdTempC = 0;
float awayTempC = 22; // save to EEPROM
float lowC, highC = 0;
float lowF, highF = 0;
float humidity = 0;
Adafruit_SSD1306 display(OLED_RESET);
int pushButtonStatus = 0;
int screenMode = 1;
//ClickButton button1(PUSHBUTTONPIN, LOW, CLICKBTN_PULLUP);
elapsedMillis tempReadTimer;
elapsedMillis displayTimer;
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
unsigned int DHTnextSampleTime;
bool bDHTstarted;
bool isHeatOn = false;
int heatState = -1;
bool displayModeCelsius = true;
void setup()
{
Serial.begin(9600);
//while (!Serial.available()) {
// Serial.println("Press any key to start.");
// delay (1000);
//}
// Register a Spark variable here
Spark.variable("CurrentTempC", &temperatureC, DOUBLE);
Spark.variable("CurrentTempF", &temperatureF, DOUBLE);
Spark.variable("Humidity", &humidity, DOUBLE);
Spark.variable("HoldTempC", &holdTempC, INT);
Spark.variable("IsHeaterOn", &isHeatOn, BOOLEAN);
// max name len 12
Spark.function("SetHeatMode", setHeatMode);
Spark.function("SetHoldTemp", setHoldTemp);
Spark.function("C_F_Display", tempDisplayMode);
DHTnextSampleTime = 0; // Start the first sample immediately
pinMode(PUSHBUTTONPIN, INPUT_PULLUP);
pinMode(RELAY1, OUTPUT);
pinMode(D7, OUTPUT);
digitalWrite(RELAY1, LOW);
digitalWrite(D7, LOW);
holdTempC = EEPROM.read(1);
if(holdTempC == 255) holdTempC = 0;
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
//button1.debounceTime = 20; // Debounce timer in ms
//button1.multiclickTime = 250; // Time limit for multi clicks
//button1.longClickTime = 1000; // time until "held-down clicks" register
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
//21 chars with txt size 1
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Loading...");
display.setCursor(0,7);
display.print("LIB version: ");
display.println(_VERSION);
display.setTextSize(2);
display.println("Initializing");
display.println("System");
display.display();
ReadDhtTemp1();
delay(2000);
display.clearDisplay();
Spark.publish("online", currentTimeJsonString(), 60, PRIVATE);
}
// This wrapper is in charge of calling
// mus be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
void loop()
{
/* button1.Update();
//pushButtonStatus = digitalRead(PUSHBUTTONPIN);
if (button1.clicks != 0) pushButtonStatus = button1.clicks;
if (button1.clicks != 0)
{
if (button1.clicks > 0)
{
screenMode += button1.clicks;
}
if (screenMode == 5)
screenMode = 1;
}
*/
// Check if we need to start the next sample
if (tempReadTimer > DHTnextSampleTime)
{
Serial.println("-- Start Temp Read --");
ReadDhtTemp1();
//ReadRelayTemp();
//printTempReadings();
tempReadTimer = 0;
if(DHTnextSampleTime == 0) DHTnextSampleTime = DHT_SAMPLE_INTERVAL;
UpdateDisplay();
Serial.println("-- End Temp Read --");
}
// Add ability to override for a set time?
if((int)temperatureC < holdTempC & heatState == -1 & !isHeatOn)
{
heatState = 1; //On
}
if((int)temperatureC >= holdTempC & heatState == -1 & isHeatOn)
{
heatState = 0; //Off
}
switch (heatState)
{
case 0: // Off
if(isHeatOn)
{
Serial.println("-- Start Turn Heat Off --");
isHeatOn = false;
digitalWrite(RELAY1, LOW);
digitalWrite(D7, LOW);
//Spark.publish(String eventName, String data, int ttl, PRIVATE)
Spark.publish("heater-off-txt", "Current Temp: xx, Low Temp: xx, High Temp: xx", 60, PRIVATE);
Spark.publish("heater-off-json", currentTimeJsonString(), 60, PRIVATE);
UpdateDisplay();
Serial.println("-- End Turn Heat Off --");
}
heatState = -1; // processed
break;
case 1: // On
if(!isHeatOn)
{
Serial.println("-- Start Turn Heat On --");
isHeatOn = true;
digitalWrite(RELAY1, HIGH);
digitalWrite(D7, HIGH);
//Spark.publish(String eventName, String data, int ttl, PRIVATE)
Spark.publish("heater-on-txt", "Current Temp: xx, Low Temp: xx, High Temp: xx", 60, PRIVATE);
Spark.publish("heater-on-json", currentTimeJsonString(), 60, PRIVATE);
UpdateDisplay();
Serial.println("-- End Turn Heat On --");
}
heatState = -1; // processed
break;
case 2: // Away
Serial.println("-- Start Set Heat to Away --");
if(isHeatOn)
{
isHeatOn = false;
digitalWrite(RELAY1, LOW);
digitalWrite(D7, LOW);
}
heatState = -1; // processed
Serial.println("-- End Set Heat to Away --");
break;
}
delay(2000);
}
void ReadDhtTemp1()
{
Serial.println("-- ReadDhtTemp1 --");
if (!bDHTstarted) { // start the sample
Serial.println("-- start the sample --");
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
Serial.println("-- get DHT status --");
int result = DHT.getStatus();
printDhtSensor1Results(result);
UpdateTempVars();
bDHTstarted = false; // reset the sample flag so we can take another
}
}
void UpdateTempVars()
{
//Serial.println("analogRead\ttemperatureC\ttemperatureF");
//sprintf(message,"%d\t\t %.0f\t\t %.0f : %.0f", reading, temperatureC, temperatureF, (temperatureC*1.8+32));
//Serial.println(message);
temperatureC = (double)DHT.getCelsius();
temperatureF = (double)DHT.getFahrenheit();
humidity = (double)DHT.getHumidity();
if(temperatureC < lowC)
lowC = temperatureC;
if(temperatureC > highC)
highC = temperatureC;
if(temperatureF < lowF)
lowF = temperatureF;
if(temperatureF > highF)
highF = temperatureF;
}
void printDhtSensor1Results(int result)
{
Serial.println("---------------------------------------");
Serial.print("Read DHT sensor1: ");
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;
}
Serial.print("Screen: ");
Serial.println(screenMode);
Serial.print("Humidity (%): ");
Serial.println(DHT.getHumidity(), 2);
Serial.print("Temperature (oC): ");
Serial.println(DHT.getCelsius(), 2);
Serial.print("Temperature (oF): ");
Serial.println(DHT.getFahrenheit(), 2);
Serial.print("Temperature (K): ");
Serial.println(DHT.getKelvin(), 2);
Serial.print("Dew Point (oC): ");
Serial.println(DHT.getDewPoint());
Serial.print("Dew Point Slow (oC): ");
Serial.println(DHT.getDewPointSlow());
Serial.println("---------------------------------------");
}
void UpdateDisplay()
{
//Serial.println("-- Start Update Display --");
display.setTextSize(2);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor(0,0);
display.print("Heat: ");
if(isHeatOn)
{
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.println("On");
}
else
{
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.println("Off");
}
display.setTextColor(WHITE);
display.print("Set:");
if(displayModeCelsius)
{
display.print(holdTempC, 1);
display.println((char)222); // "c");
display.setTextSize(4);
display.print(temperatureC, 1); //DHT.getCelsius()
//display.println("°");
display.println("c");
}
else
{
display.print(CtoF(holdTempC), 1);
display.println("f");
display.setTextSize(4);
display.print(temperatureF, 1); //DHT.getCelsius()
//display.println("°");
display.println("f");
}
display.display();
//Serial.println("-- End Update Display --");
}
void ReadRelayTemp()
{
//Serial.println("-- Start Reading Temp --");
float tempSamples[8];
//int reading = analogRead(TMPPIN);
//Serial.print("Temp:");
//float val = ( 5.0 * analogRead(TMPPIN) * 100.0) / 1024.0;
//Serial.println( roundf(val*10.0f)/10.0f );
for(int i = 0; i <= 7; i++){ // gets 8 samples of temperature
tempSamples[i] = (((analogRead(TMPPIN) * 3.3)/4095) - 0.5) * 100;
relayTempC += tempSamples[i];
delay(200);
}
relayTempC = relayTempC / 8.0;
//relayTempC = roundf(temperatureC*10.0f)/10.0f;
// The returned value from the Core is going to be in the range from 0 to 4095
// Calculate the voltage from the sensor reading
//double voltage = (reading * 3.3) / 4095.0;
// Calculate the temperature and update our static variable
//tempC = (((analogvalue * 3.3)/4095) - 0.5) * 100;
//relayTempC = (voltage - 0.5) * 100.0;
relayTempF = CtoF(temperatureC); // (relayTempC * 9.0 / 5.0) + 32.0;
//Serial.println("-- End Reading Temp --");
}
float CtoF(float c)
{
return ((c * 9.0 / 5.0) + 32.0);
}
int setHeatMode(String command)
{
if(command.equalsIgnoreCase("on"))
return heatState = 1;
else if(command.equalsIgnoreCase("off"))
return heatState = 0;
else if(command.equalsIgnoreCase("away"))
return heatState = 2;
else
return -1;
}
int setHoldTemp(String command)
{
if(command.toFloat())
{
if(command.toFloat() != holdTempC)
{
holdTempC = command.toFloat();
EEPROM.write(1, holdTempC);
}
}
else
return -1;
}
int tempDisplayMode(String command)
{
displayModeCelsius = !displayModeCelsius;
return 1;
}
String currentTimeJsonString()
{
char publishString[64];
// now is in milliseconds
unsigned long now = millis();
unsigned nowSec = now/1000UL;
unsigned sec = nowSec%60;
unsigned min = (nowSec%3600)/60;
unsigned hours = (nowSec%86400)/3600;
sprintf(publishString,"{\"Hours\": %u, \"Minutes\": %u, \"Seconds\": %u}",hours,min,sec);
return publishString;
}