Hi,
I had build this code to work a experiment of mine to control my air conditioning unit with the Blynk APP. But its getting often disconnected then connected again and i dont know why. I’m not a professional and need help. What i’m doing wrong on my code?
I have a similar code that i used on a D1 Wemos and worked okay. I had just adapt this code to work on my new board. I tried a ethernet code build on the getting started of Blynk and worked fine. It’s only my code below that doens’t work…
Hardware:
01 Arduino Mega 2560
01 Wiznet Ethernet Shield W1500
03 DS18B20 Probes
01 GBK Voltage Sensor
01 YHDC SCT1013 Current Sensor
/*-----( Set Network Connection)-----*/
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";
#define W5100_CS 10
#define SDCARD_CS 4
/*-----( Define Configurations )-----*/
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#define SetPointHisterese 2
#define PAST_VAC_NUMBER 5
#define VOLTAGE_INCREMENT 100
#define VAC_SENSOR_DELAY 9000 //milliseconds
/*-----( Include External References )-----*/
#include "DallasTemperature.h"
#include "EmonLib.h"
/*-----( Timer )-----*/
BlynkTimer timer;
/*-----( Pins )-----*/
#define ONE_WIRE_BUS_PIN 8 //pin DATA ds18b20
#define VPinTerminal 0
#define VPinWaterInputGauge 1
#define VPinWaterOutputGauge 2
#define VPinLEDBlink 3
#define VPinLEDEstado 4
#define VPinSetPointStepper 5
#define VPinPowerButton 6
#define VPinVoltage 7
#define VPinTemperatureSetPoint 8
#define VPinSendSetPointButton 9
#define VPinAlarmButton 10
#define VPinLEDAlarm 11
#define VPinMaxVoltage 12
#define VPinCurrent 13
#define VPinMaxCurrent 14
#define CurrentSensorPin A0
#define VoltageSensorPin A1
#define PowerRelayPin 4
#define CompRelayPin 5
/*-----( Declare objects )-----*/
EnergyMonitor emon1;
WidgetTerminal terminal(VPinTerminal);
WidgetLED ledBlink (VPinLEDBlink);
WidgetLED ledEstado (VPinLEDEstado);
WidgetLED ledAlarm (VPinLEDAlarm);
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Probe01 = { 0x28, 0xFF, 0x11, 0x67, 0x30, 0x18, 0x01, 0xF5 };
DeviceAddress Probe02 = { 0x28, 0xFF, 0xB1, 0x59, 0x47, 0x16, 0x03, 0x6E };
DeviceAddress ProbeShort = { 0x28, 0xFF, 0xDC, 0x40, 0x45, 0x16, 0x03, 0x77 };
/*-----( Declare Variables )-----*/
float tempC, TemperatureSet = 22, TemperatureSetPreview = 22;
double BiggestIrms = 0;
int
PowerRelay = 1,
CompRelay = 0,
BlinkLedStatus = 0,
AlarmButton = 0,
timesescribed = 0, prevVoltage[PAST_VAC_NUMBER] = {0},
TensaoRaw, TensaoRede = 0, tensaoBornes, BiggestVAC = 0,
Half_Hour_Passed = 1, Called_Timer_Times = 0,
StartCurrent = 0, StartVoltage = 0;
/*-----( Declare User-written Functions )-----*/
void StartCurrentRead() {
StartCurrent = 1;
}
void StartVoltageRead() {
StartVoltage = 1;
BiggestVAC = tensaoBornes;
Blynk.virtualWrite(VPinMaxVoltage, BiggestVAC);
}
void GetCurrent()
{
//Calcula a corrente
double Irms = emon1.calcIrms(1480);
if(StartCurrent) {
//Mostra o valor da corrente no monitor serial
Serial.print("Corrente : ");
Serial.println(Irms);
Blynk.virtualWrite(VPinCurrent, Irms );
if (Irms > BiggestIrms)
{
BiggestIrms = Irms;
Blynk.virtualWrite(VPinMaxCurrent, BiggestIrms );
}
}
}
void terminalprintTemperature(DeviceAddress deviceAddress)
{
tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) { terminal.print("Error getting temperature"); }
else if (tempC == 85.00) { terminal.print("Initializing Sensor"); }
else { terminal.print("Temperetature Updated."); }
}
void GetVoltage()
{
double tensaoPinA0, tensaoPonteA0;
int i = 0, soma = 0, mediaRaw = 0;
for(;i<1000;i++)
{
TensaoRaw = analogRead(VoltageSensorPin);
soma += TensaoRaw;
//timer.run();
}
mediaRaw = soma/1000;
Serial.print("Voltage 0-1023: ");
Serial.println(mediaRaw);
tensaoPinA0 = mediaRaw *5. /1024;
tensaoPonteA0 = tensaoPinA0 *23 *1150 /22/680;
for(i=0; i<PAST_VAC_NUMBER; ) {
prevVoltage[i] = prevVoltage[++i];
}
prevVoltage[PAST_VAC_NUMBER] = tensaoBornes;
if ( mediaRaw <= 26 )
{
tensaoBornes = 1.9746*mediaRaw - 5.4237;
if (tensaoBornes < 0) {tensaoBornes = 0;}
}
else
{ tensaoBornes = (0.7551*mediaRaw) + 74; }
if (tensaoBornes > prevVoltage[0] +VOLTAGE_INCREMENT) {
StartVoltage = 0;
timer.setTimeout(VAC_SENSOR_DELAY, StartVoltageRead);
}
else {
if (StartVoltage) {
if ( mediaRaw <= 26 ) {
TensaoRede = 0; }
else if( mediaRaw >= 126 ) {
TensaoRede = 220; }
else { TensaoRede = 110; }
//terminal.print("Tensão Atual: "); terminal.print(TensaoRaw); terminal.println(" V");
//terminal.print("Última Tensão de alimentação: "); terminal.print(TensaoRede); terminal.println(" V");
//terminal.println("\n");
terminal.print("Tensão em A0: "); terminal.print(mediaRaw); terminal.print(" -> "); terminal.print(tensaoPinA0); terminal.println(" V\n");
terminal.print("Tensão de alimentação dos bornes 6 e 7: "); terminal.print(TensaoRede); terminal.println(" V\n");
if (tensaoBornes > BiggestVAC) {
BiggestVAC = tensaoBornes;
Blynk.virtualWrite(VPinMaxVoltage, BiggestVAC);
}
Blynk.virtualWrite(VPinVoltage, tensaoBornes);
}
}
}
void myTimerEvent()
{
terminal.print("----------------------------------------------------");
terminal.println("\n\n");
sensors.requestTemperatures(); // Command all devices on bus to read temperature
tempC = sensors.getTempC(Probe01);
if (tempC == -127.00) { terminal.println("Error: Could not get temperature 1"); }
else
{
if (tempC == 85.00) { terminal.println("Initializing Sensor 1"); }
else
{
if (++Called_Timer_Times == 300)
{
Half_Hour_Passed = 1;
Called_Timer_Times = 0;
}
if (tempC > 27 || tempC < 17)
{
if (Half_Hour_Passed == 1) {
Half_Hour_Passed = 0;
Blynk.notify("Aviso! A temperatura no CPD chegou a níveis de risco!");
//Blynk.email("supervisao1@gruposaocarlos.com.br", "Automação - CPD", "Aviso! A temperatura no CPD chegou a níveis de risco!");
Blynk.virtualWrite(VPinAlarmButton, 1); ledAlarm.on();
PowerRelay = 0;
Blynk.virtualWrite(VPinPowerButton, PowerRelay);
CompRelay = 0; digitalWrite(CompRelayPin, CompRelay);
Blynk.setProperty(VPinLEDEstado, "label", "RLY OFF");
ledEstado.off();
}
}
else{
//Blynk.virtualWrite(VPinAlarmButton, 0); ledAlarm.off();
}
if ((tempC > TemperatureSet +SetPointHisterese) && PowerRelay)
{
CompRelay=1;
Blynk.setProperty(VPinLEDEstado, "label", "RLY ON");
ledEstado.on();
}
else if (tempC < TemperatureSet -SetPointHisterese)
{
CompRelay=0;
Blynk.setProperty(VPinLEDEstado, "label", "RLY OFF");
ledEstado.off();
}
digitalWrite (CompRelayPin, CompRelay);
Blynk.virtualWrite(VPinWaterInputGauge, tempC);
}
}
//clearterminal();
terminal.print("Getting Room Temperature. ");
tempC = sensors.getTempC(ProbeShort);
if (tempC == -127.00) { terminal.print("Error getting temperature"); }
else if (tempC == 85.00) { terminal.print("Initializing Sensor"); }
else {
terminal.print("\nTemperatura Ambiente: ");
terminal.print(tempC);
terminal.println(" °C");
}
terminal.println("\n");
terminal.print("Getting Probe02 Temperature. ");
terminalprintTemperature(Probe02);
Blynk.virtualWrite(VPinWaterOutputGauge, tempC);
terminal.println("\n\n");
GetVoltage();
GetCurrent();
}
void clearterminal()
{
for (timesescribed = 0; timesescribed < 25; timesescribed++) { terminal.print(" \n"); }
}
void BlinkLEDs()
{
if (BlinkLedStatus == 0) {
ledBlink.on();
digitalWrite(LED_BUILTIN, 1);
BlinkLedStatus = 1;
}
else {
ledBlink.off();
digitalWrite(LED_BUILTIN, 0);
BlinkLedStatus = 0;
}
}
/*-----( Declare Blynk Protocols )-----*/
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPinTemperatureSetPoint); //get app's setpoint temperature
Blynk.syncVirtual(VPinPowerButton); //get app's split state
// Alternatively, you could override server state using:
Blynk.virtualWrite(VPinVoltage, tensaoBornes); // send aprox voltage on bornes
Blynk.virtualWrite(VPinAlarmButton, AlarmButton); // send alarm state
Blynk.virtualWrite(VPinLEDAlarm, AlarmButton);
Blynk.virtualWrite(VPinMaxVoltage, BiggestVAC);
Blynk.virtualWrite(VPinMaxCurrent, BiggestIrms);
Blynk.virtualWrite(VPinLEDEstado, CompRelay); //send actual relay state
if(CompRelay) {
Blynk.setProperty(VPinLEDEstado, "label", "RLY ON");
}
else{
Blynk.setProperty(VPinLEDEstado, "label", "RLY OFF");
}
}
BLYNK_WRITE(VPinSetPointStepper) {
TemperatureSetPreview = param.asFloat();
}
BLYNK_WRITE(VPinPowerButton) {
PowerRelay = param.asInt();
if (PowerRelay == 0) {
CompRelay = 0; digitalWrite(CompRelayPin, CompRelay);
Blynk.setProperty(VPinLEDEstado, "label", "RLY OFF");
ledEstado.off();
}
digitalWrite (PowerRelayPin, PowerRelay);
}
BLYNK_WRITE(VPinSendSetPointButton) {
if(param.asInt()) {
TemperatureSet = TemperatureSetPreview;
Blynk.virtualWrite(VPinTemperatureSetPoint, TemperatureSet);
}
}
BLYNK_WRITE(VPinAlarmButton) {
AlarmButton = param.asInt();
if(AlarmButton) {
ledAlarm.on();
PowerRelay = 0; digitalWrite(PowerRelayPin, PowerRelay);
Blynk.virtualWrite(VPinPowerButton, PowerRelay);
Half_Hour_Passed = 1;
CompRelay = 0; digitalWrite(CompRelayPin, CompRelay);
Blynk.setProperty(VPinLEDEstado, "label", "RLY OFF");
ledEstado.off();
}
else ledAlarm.off();
}
/*-----( Declare Default Funcions )-----*/
void setup() {
// Debug console
Serial.begin(9600);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
delay(10);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(PowerRelayPin, OUTPUT);
pinMode(CompRelayPin, OUTPUT);
digitalWrite(PowerRelayPin, 0);
digitalWrite(CompRelayPin, 0);
Blynk.begin(auth ); //, "blynk-cloud.com" , 8442);
clearterminal();
Blynk.virtualWrite(VPinSetPointStepper, TemperatureSet);
Blynk.virtualWrite(VPinTemperatureSetPoint, TemperatureSet);
timer.setInterval(500L, BlinkLEDs);
timer.setInterval(3000L, myTimerEvent);
timer.setTimeout(15000, StartCurrentRead);
}
void loop () {
if(!Blynk.connect())
Blynk.begin(auth );
Blynk.run();
timer.run();
}