I’m not sure why my project on the Blynk App intermittently doesn’t work or goes offline. I don’t have any error messages from Arduino IDE when I compile my sketch but the project does not always work. For example, when I click on the pushbuttons in my Blynk Project via the app, to turn the respective relays in my project ON/OFF, it doesn’t always work and sometimes when it does it takes a long time to activate the relay, like 10 to 15 seconds delay. I have 2 relays, both are controlled manually only via the Blynk App via 2 button widgets.
My project says “online” even though it initially said it had “disconnected”. Hence i am sometimes able to push the pushbuttons ON/OFF but nothing happens to the relays.
Can the Blynk community please look at my code and my attached picture and tell me what’s wrong.
Details :
• Arduino UNO with USB Communication (No Shields)
• Android + version 9
• Blynk server
• Blynk Library version 0.61
Hi Blynk Community! My program has 2 main functions. The first is called “void sensorDataSendTempControl()” which simply peforms temperature control by activating my Heating Element which is a" Heating Fan" (Heating Element + Fan on the back) which is 1 component made of 2 components in 1. This Heating Fan has 4 wires in total (2 for the Heating Element and 2 for the Fan of the Heater). This is manually via the Blynk app using 2 Button Widges (see attached screenshot) and is controlled by 2 Relays at Digital Pin 8 and 7 for the Heating Element and the Fan of the Heater respectively. I then have an LM35 Temperature Sensor which reads the Temperature and an Extractor Fan to remove the heat and maintain the setpoint temperature using Adaptive Cooling via PID Control.
The setpoint temperature needs to be inputted by the user via the Blynk App. Which I have created a numeric input. However, a problem only arose when I wrote the code for connecting the setpoint temperature (numeric input) to my Arduino IDE Code using the 2 fundamental commands for Virtual Pins:
“BLYNK_WRITE(V1)” and Blynk.virtualWrite(V1) <<<<< Most of my problems started when I incorporated these commands
I haven’t used the second command “Blynk.virtualWrite(V1)” as yet because I was getting the problems I mentioned above which mainly started after the incorporation of this code, using the first command.
The second main function of my code is called “void sensorDataSendBrightnessControl()” which simply peforms brigntess control using PWM. I’ve mapped the PWM Range (0 to 255) to the Operating Lumen Values Range (6 lumens to 74 lumens).
Please help me community, it’s urgent
This is my code:
#define BLYNK_PRINT DebugSerial
#define BLYNK_WRITE(pin)
// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX
#include <BlynkSimpleStream.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MUMRNK5o0oiBIJHNsIcjM6IaV8EyeUbu";
BlynkTimer timer1; // Announcing the timer1
BlynkTimer timer2; // Announcing the timer2
//Untuned PID Control for Fan Speed Control Circuit + Relay Module incorporated
int PwmPin = 3; //initialising the pin number of PWM signal
int analogPin = A0; //analogue pin that will be used (Pin A0)
int raw = 0; //store the raw value coming from ADC (0-1023)
int sensorValue = 0; //real ADC Value (Value from the 3 Point Averaging Filter)
float voltage = 0; //need to first calculate the voltage from this
float roomTemp = 0; //then can calculate the temperature
double roomTemperature; //To store Converted roomTemp to double to be compatibe type for PID Controller
double temp_PWM; //PWM Value of roomTemperature
int V1variable;
double setpointTemp; //initialising Setpoint Temperature
BLYNK_WRITE(V1){
V1variable = param.asInt();
setpointTemp = V1variable;
}
double setpoint_PWM; //PWM Value of Setpoint Temperature
double maxTemp = 30; //Max Temp room is goining to be heated to by Heating Fan
float DutyCyclePercent; //variable to store Duty Cycle. Type float used for calculations
// Relay pin for Heater (Heating Element) is controlled with D8. The active wire is connected to Normally Open and COM
int relayHeater = 8;
// Relay pin for Fan of Fan Heater is controlled with D7. The active wire is connected to Normally Open and COM
int relayFanH = 7;
#include <PID_v1.h>
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,0, REVERSE); //Gain values of Kp, Ki, Kd
//initilisation for second program "BrightnessControl"
int PwmPin2 = 6; //initialising the pin number of PWM signal
int PwmValue = 255; //variable to store the PWM value
float DutyCyclePercent2; //variable to store Duty Cycle. Type float for calculations
int V2variable;
double setpointLumens;
BLYNK_WRITE(V2){
V2variable = param.asInt();
setpointLumens = V2variable;
Serial.println(setpointLumens);
}
double setpointLumens_PWM;
double SetpointLumensPWM;
void setup() {
// Debug console
DebugSerial.begin(9600);
// Blynk will work through Serial
// Do not read or write this serial manually in your sketch
Serial.begin(9600); //setup of Serial Monitor
Blynk.begin(Serial, auth);
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
timer1.setInterval(2000L, sensorDataSendTempControl); //timer will run every 2 seconds
timer2.setInterval(1000L, sensorDataSendBrightnessControl); //timer will run every 2 seconds
//turn the PID ON
myPID.SetMode(AUTOMATIC);
}
void sensorDataSendTempControl() {
pinMode(PwmPin,OUTPUT); //initialising the PWM pin as an output
pinMode(analogPin,INPUT); //initialisin the Temperaure sensor pin as an input
//initialize the variables we're linked to
setpoint_PWM = map(setpointTemp, 26,30, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
Setpoint = setpoint_PWM;
raw = analogRead(analogPin); //1st time to read analogue Input
raw = 0; //and discard it
for(int x = 0; x <= 2; x++){ //3 Point Averaging Filter
raw = raw + analogRead(analogPin); //reading analog input 3 times
} //and adding it to raw
sensorValue = raw / 3; //real ADC value
voltage = (5*float(sensorValue)) / 1023; //Conversion to Temperature in Degrees Celcius
roomTemp = voltage * 100;
roomTemperature = double(roomTemp); //Convert roomTemp to double to be compatibe type for PID Controller
temp_PWM = map(roomTemperature, 26, 30, 0, 255); //Added for troubleshooting
Input = temp_PWM; //Convert roomTemperature from operating Temperature Range to PWM Range to be compatibe type for PID Controller
myPID.Compute(); //Use PID Controller library to compute Output PWM Signal
analogWrite(3,Output); //Write this Ouput PWM Signal to Analog Pin 3
Serial.print("Temperature = "); //Print text in quotation marks
Serial.print(roomTemp); //Print measured temperature
Serial.println(" Degrees Celcius"); //Print text in quotation marks
Serial.println("");
Serial.print("PWM Value = "); //print the text in quotation marks
Serial.println(Output); //print the PWM value next to this
}
void sensorDataSendBrightnessControl() {
pinMode(PwmPin2,OUTPUT); //initialising the PWM pin as an output
analogWrite(PwmPin2,PwmValue); //sending the PWM value to the PWM pin
DutyCyclePercent2 = ((PwmValue * 100) / 255); //duty cycle calculation
Serial.print("PWM Value = "); //to display the text in quotation marks
Serial.println(PwmValue); //print the PWM value next to this
Serial.print("Duty Cycle = "); //to display the text in quotation marks
Serial.print(DutyCyclePercent2); //print the DC value next to this
Serial.println("%"); //print the symbol in quotation marks
Serial.println("");
setpointLumens_PWM = map(setpointLumens, 6,74, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
SetpointLumensPWM = setpointLumens_PWM;
analogWrite(PwmPin2,SetpointLumensPWM); //sending the PWM value to the PWM pin
DutyCyclePercent2 = ((SetpointLumensPWM * 100) / 255); //duty cycle calculation
Serial.print("PWM Value = "); //to display the text in quotation marks
Serial.println(SetpointLumensPWM); //print the PWM value next to this
Serial.print("Duty Cycle = "); //to display the text in quotation marks
Serial.print(DutyCyclePercent2); //print the DC value next to this
Serial.println("%"); //print the symbol in quotation marks
Serial.println("");
}
void loop() {
Blynk.run(); // run Blynk magic
timer1.run(); // run timer every 2 seconds
timer2.run(); // run timer every 2 seconds
}
}