hello friends
I made this project and its the hole attic fan for the house u can control it with blynk and also manual and also with voice command Alexa .
am going though some issue that any time I push On the fan turns on and the blynk goes Offline for couple of second then comes online again and any time I push Off the fan turns off and the same blynk goes offline and comes back online again
I don’t really know what is the issue is it the temperature code or some thins else .
here is my code and I would really appreciate if any one can help me with it or guide me .
thanks
// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"
// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"
// This #include statement was automatically added by the Particle IDE.
#include "SparkCorePolledTimer/SparkCorePolledTimer.h"
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social networks: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* Output any data on LCD widget!
*
* App project setup:
* LCD widget, switch to ADVANCED mode, select pin V1
*
**************************************************************/
#define BLYNK_PRINT Serial
const int Main_Relay= D7; //the main relay to energize fan relay and keep holding.
const int Remote_Stop=D3; //stop relay that can be controlled from the phone to stop the fan anytime.
const int Manually_Stop=D6; //manually stop button located in the house.
const int Fan_On=D1; //INPUT TOTURN THE fan on
const int Fan_Status=D2; // input to show the status of the fan
#define ON 255
#define OFF 0
//..............................
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D4 // Digital pin for communications
#define DHT_SAMPLE_INTERVAL 60000 // Sample every minute
#define READ_INTERVAL 60000
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
unsigned int DHTnextSampleTime; // Next time we want to start sample
bool bDHTstarted; // flag to indicate we started acquisition
int n; // counter
//this is coming from http://www.instructables.com/id/Datalogging-with-Spark-Core-Google-Drive/?ALLSTEPS
char resultstr[64]; //String to store the sensor data
char VERSION[64] = "0.04";
//..................................
SparkCorePolledTimer updateTimer(1000); //Create a timer object and set it's timeout in milliseconds
void OnTimer(void); //Prototype for timer callback method
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "c8a99822d99348eaa9ac80228d79464b";
WidgetLCD lcd(V1);
bool isFirstConnect = true;
//WidgetLCD lcd(V5);
int OldState;
int Status;
void setup()
{
Serial.begin(115200);
//updateTimer.SetCallback(OnTimer);
pinMode(Fan_On,INPUT);
pinMode(Fan_Status,INPUT);
pinMode(Remote_Stop,OUTPUT);
pinMode(Manually_Stop,OUTPUT);
pinMode(Main_Relay,OUTPUT);
Particle.function("Fan_On", Fan_On1);
Particle.function("Fan_Off", Fan_Off1);
Blynk.begin(auth);
Serial.print("Connecting");
while (Blynk.connect() == false) {
// Wait until connected
}
OldState=false;
Status=false;
//.......................................
DHTnextSampleTime = 0; // Start the first sample immediately
Particle.variable("result", resultstr, STRING);
Particle.publish("DHT22 - firmware version", VERSION, 60, PRIVATE);
//.........................................
}
//..........................................
// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
//..............................
BLYNK_WRITE(V10) //Function to test status from BLYNK widget to PHOTON
{
int state = param.asInt();
if (state ==1)
{
TurnOff();
}
else if (state ==0)
{
TurnOn();
}
}
void loop()
{
digitalWrite(Manually_Stop,LOW);
Blynk.run();
if( digitalRead(Fan_On) != OldState ) //input signal to turn the fan on
{
OldState=digitalRead(Fan_On);
if( digitalRead(Fan_On) == 0)
{
TurnOn();
}
else
{
TurnOff();
}
}
if(digitalRead(Fan_Status)!=Status) // LED indicator and LCD toshowthe status of the fan in the phone
{
Status=digitalRead(Fan_Status);
if (digitalRead(Fan_Status)==0)
{
Blynk.virtualWrite(V4, OFF); //Green LED
Blynk.virtualWrite(V5, ON); //Red LED
Blynk.virtualWrite(V6, "Fan Off");
// lcd.clear(); //Use it to clear the LCD Widget
// lcd.print(4, 0, "Fan Off"); //V1 LCD Status in the App
}
else
{
Blynk.virtualWrite(V5, OFF); //Red LED
Blynk.virtualWrite(V4, ON); // Green LED
Blynk.virtualWrite(V6, "Fan On");
// lcd.print(4, 0, "Fan On"); // V1 status in the App
}
}
//..............................................
// Check if we need to start the next sample
if (millis() > DHTnextSampleTime) {
if (!bDHTstarted) { // start the sample
DHT.acquire();
bDHTstarted = true;
}
if (!DHT.acquiring()) { // has sample completed?
float temp = (float)DHT.getFahrenheit();
int temp1 = (temp - (int)temp) * 100;
char tempInChar[32];
sprintf(tempInChar,"%0d.%d", (int)temp, temp1);
Particle.publish("The temperature from the dht22 is:", tempInChar, 60, PRIVATE);
//virtual pin 1 will be the temperature
Blynk.virtualWrite(V3, tempInChar);
Blynk.virtualWrite(V4, tempInChar);
//digitalWrite(Red,HIGH);
//google docs can get this variable
sprintf(resultstr, "{\"t\":%s}", tempInChar);
float humid = (float)DHT.getHumidity();
int humid1 = (humid - (int)humid) * 100;
sprintf(tempInChar,"%0d.%d", (int)humid, humid1);
Particle.publish("The humidity from the dht22 is:", tempInChar, 60, PRIVATE);
//virtual pin 2 will be the humidity
Blynk.virtualWrite(V2, tempInChar);
n++; // increment counter
bDHTstarted = false; // reset the sample flag so we can take another
DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample
}
}
//............................................
}
int Fan_On1(String args) { // Fan ON and it is controlled by Alexa voice command .
TurnOff();
delay(1000);
TurnOn();
return 1;
}
int Fan_Off1(String args) { // Fan Off and its controlled by Alexa voice command .
TurnOn();
delay(1000);
TurnOff();
return 1;
}
void TurnOn()
{
digitalWrite(Main_Relay, LOW);
}
void TurnOff()
{
digitalWrite(Main_Relay, HIGH);
}