Good Peekay 123 days, Iāll be putting down a part of every code of 3 devices that are interconnected by Blynk.
Main Hardware if Photon and Blynk:
// This #include statement was automatically added by the Particle IDE.
#define BLYNK_PRINT Serial
#include "blynk/BlynkSimpleParticle.h"
#include "blynk/blynk.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
STARTUP(WiFi.selectAntenna(ANT_AUTO));
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
// VariƔveis utilizadas no equipamento.
.
.
.
.
.
.
.
Timer timer1(2000, Atualiza); // Tempo e Rotina do Temporizador
Timer timer2(60000, Alerta); // Tempo e Rotina do Temporizador
Timer timer3(100, AlertaLed); // Tempo e Rotina do Temporizador
ApplicationWatchdog wd(60000, System.reset);
//Canais Virtuais do Blynk
WidgetBridge bridge1(V3); // Canal Virtual da Ponte
WidgetBridge bridge2(V14); // Canal Virtual da Ponte
WidgetLCD lcd(V0); // Canal Virtual do LCD Virtual
WidgetLED LedAlarme(V5); //LED Virtual no APP
WidgetLED LedCerca(V6); //LED Virtual no APP
WidgetLED LedDisparo1(V7); //LED Virtual no APP
WidgetLED LedDisparo2(V8); //LED Virtual no APP
void setup() {
Serial.begin(115200); // Inicia a Serial
delay(50);
WiFi.on(); // Liga a MĆ³dulo WiFi com as credenciais prĆ© definidas
delay(50);
WiFi.connect(); // Conecta Ć Rede WiFi
pinMode(E_Sirene, INPUT_PULLUP); // Indica que Ć© entrada
pinMode(Bot, INPUT_PULLUP); // Indica que Ć© entrada
pinMode(S_Alarme, OUTPUT); // Indica que Ć© saĆda
pinMode(S_Cerca, OUTPUT); // Indica que Ć© saĆda
pinMode(St_Alarme, OUTPUT); // Indica que Ć© saĆda
pinMode(St_Cerca, OUTPUT); // Indica que Ć© saĆda
pinMode(LStatus, OUTPUT); // Indica que Ć© saĆda
pinMode(LPortao, OUTPUT); // Indica que Ć© saĆda
pinMode(LPortao, OUTPUT); // Indica que Ć© saĆda
pinMode(LLuz, OUTPUT); // Indica que Ć© saĆda
digitalWrite(S_Alarme, LOW); // Desliga SaĆda ao Iniciar
digitalWrite(S_Cerca, LOW); // Desliga SaĆda ao Iniciar
digitalWrite(St_Alarme, LOW); // Desliga SaĆda ao Iniciar
digitalWrite(St_Cerca, LOW); // Desliga SaĆda ao Iniciar
digitalWrite(LPortao, LOW); // Desliga SaĆda ao Iniciar
digitalWrite(LLuz, LOW); // Desliga SaĆda ao Iniciar
pinMode(A1, OUTPUT); // Indica que Ć© saĆda
pinMode(A2, OUTPUT); // Indica que Ć© saĆda
pinMode(A3, OUTPUT); // Indica que Ć© saĆda
pinMode(A4, OUTPUT); // Indica que Ć© saĆda
pinMode(DAC, OUTPUT); // Indica que Ć© saĆda
pinMode(WKP, OUTPUT); // Indica que Ć© saĆda
digitalWrite(A1, LOW); // Desliga SaĆda
digitalWrite(A2, LOW); // Desliga SaĆda
digitalWrite(A3, LOW); // Desliga SaĆda
digitalWrite(A4, LOW); // Desliga SaĆda
digitalWrite(DAC, LOW); // Desliga SaĆda
digitalWrite(WKP, LOW); // Desliga SaĆda
digitalWrite(LStatus, HIGH); // Liga SaĆda
waitUntil(WiFi.ready); // Agaurda conectar a WiFi
Blynk.begin(auth); // Faz AutenticaĆ§Ć£o no Servidor Blynk
digitalWrite(LStatus, LOW); // Desliga SaĆda
waitUntil(Blynk.connect); // Agaurda Conectar no Servidor Blink
timer1.start(); // Inicia Rotina Temporizador
timer2.start(); // Inicia Rotina Temporizador
timer3.start(); // Inicia Rotina Temporizador
PrimeiraBlynk = true;
delay(250); // Tempo
}
// Rotinas do Blynk
BLYNK_CONNECTED()
{
bridge1.setAuthToken("YYYYYY");
bridge2.setAuthToken("ZZZZZZZ");
if (PrimeiraBlynk)
{
Blynk.syncAll();
digitalWrite(LStatus, HIGH); // Desliga SaĆda
PrimeiraBlynk = false;
}
}
//Verifica BotĆ£o Virtual do Alarme
BLYNK_WRITE(V1)
{
if(param.asInt() != 0)
{
if( LeStatus == LOW )
{
ContadorE_Sirene = 0;
ControleAlarme = 1;
SinalStatus = LOW;
UltimoTempoEnvio = millis();
LeStatus = HIGH;
}
}
}
// Verifica BotĆ£o Virtual da Cerca
BLYNK_WRITE(V2)
{
//Enviado pelo Cliente Liga Cerca
if(param.asInt() != 0)
{
if( LeStatus == LOW )
{
ContadorE_Sirene = 0;
ControleCerca = 1;
SinalStatus = LOW;
UltimoTempoEnvio = millis();
LeStatus = HIGH;
}
}
}
// Verifica Cera
BLYNK_WRITE(V4)
{
if(param.asInt() > 0)
{
BtTrava = true;
bridge1.digitalWrite(14, HIGH);
digitalWrite(LPortao, HIGH);
}
else
{
bridge1.digitalWrite(14, LOW);
digitalWrite(LPortao, LOW);
BtTrava = false;
}
}
BLYNK_READ(V9)
{
Blynk.virtualWrite(V9, temp);
bridge1.virtualWrite(V8, 1);
}
BLYNK_READ(V10)
{
Blynk.virtualWrite(V10, humd);
bridge1.virtualWrite(V9, 1);
}
//Luz Externa
BLYNK_WRITE(V11)
{
if(param.asInt() > 0)
{
bridge1.digitalWrite(12, HIGH);
digitalWrite(LLuz, HIGH);
}
else
{
bridge1.digitalWrite(12, LOW);
digitalWrite(LLuz, LOW);
}
}
BLYNK_WRITE(V12)
{
if(param.asDouble() != 0)
{
temp = param.asDouble();
}
}
BLYNK_WRITE(V13)
{
if(param.asDouble() != 0)
{
humd = param.asDouble();
}
}
BLYNK_READ(V15)
{
Blynk.virtualWrite(V15, tempFundo);
bridge2.virtualWrite(V1, 1);
}
BLYNK_READ(V16)
{
Blynk.virtualWrite(V16, humdSolo);
bridge2.virtualWrite(V2, 1);
}
BLYNK_WRITE(V17)
{
if(param.asInt() != 0)
{
Particle.connect();
}
}
BLYNK_WRITE(V18)
{
if(param.asInt() != 0)
{
Particle.disconnect();
}
}
BLYNK_WRITE(V19)
{
if(param.asDouble() != 0)
{
tempFundo = param.asDouble();
}
}
BLYNK_WRITE(V20)
{
if(param.asDouble() != 0)
{
humdSolo = param.asDouble();
}
}
BLYNK_READ(V21)
{
tempC = ((analogRead(A0) * 2.5)/4095.0) * 100.0;
Blynk.virtualWrite(V21, tempC);
}
void Inicial()
{
.
.
.
.
.
.
.
. }
// Temporizadores de algumas funƧƵes
void Atualiza()
{
Particle.publish("Principal -> Ciclos/ms:", String (System.ticksPerMicrosecond()));
.
.
.
.
.
. }
void Alerta()
{
if(alarmeStatus == ALARME_DISPARADO)
{
Blynk.notify("Alarme Disparado!");
}
}
void AlertaLed()
{
.
.
.
.
.
}
// Loop Principal do Programa
void loop()
{
//if(Particle.connected)Particle.publish("Ciclos/ms:", String (System.ticksPerMicrosecond()), 30);
Blynk.run();
if(inicio == true)Inicial();
Geral();
wd.checkin();
if(!WiFi.ready())
{
digitalWrite(LStatus, LOW); // Desliga SaĆda
delay(250);
WiFi.connect();
}
if(WiFi.ready() && !Blynk.connected())
{
PrimeiraBlynk = true;
digitalWrite(LStatus, LOW); // Desliga SaĆda
delay(250);
Blynk.connect();
}
}
//Rotina Geral do Equipamento
void Geral()
{
.
.
.
.
.
.
}
Second Hardware if Photon and Blynk:
// This #include statement was automatically added by the Particle IDE.
#define BLYNK_PRINT Serial
#include "blynk/BlynkSimpleParticle.h"
#include "blynk/blynk.h"
#include "PietteTech_DHT/PietteTech_DHT.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
STARTUP(WiFi.selectAntenna(ANT_AUTO));
// system defines
#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN 1 // Digital pin for communications
char auth[] = "YYYYYYY";
int UmidadePin = A0;
long int UmidadeValor = 0;
long int h1 = 0;
float h2 = 0.00;
int LStatus = D7; // SaĆda Led Status do Equipamento
int result = 0;
bool PrimeiraBlynk = true;
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
Timer timer1(2000, Atualiza); // Tempo e Rotina do Temporizador
ApplicationWatchdog wd(60000, System.reset);
//Canais Virtuais do Blynk
WidgetBridge bridge1(V0); // Canal Virtual da Ponte
void setup()
{
Serial.begin(115200); // Inicia a Serial
delay(50);
WiFi.on(); // Liga a MĆ³dulo WiFi com as credenciais prĆ© definidas
delay(50);
WiFi.connect(); // Conecta Ć Rede WiFi
pinMode(LStatus, OUTPUT); // Indica que Ć© saĆda
pinMode(D0, OUTPUT); // Indica que Ć© saĆda
pinMode(D2, OUTPUT); // Indica que Ć© saĆda
pinMode(D3, OUTPUT); // Indica que Ć© saĆda
pinMode(D4, OUTPUT); // Indica que Ć© saĆda
pinMode(D5, OUTPUT); // Indica que Ć© saĆda
pinMode(D6, OUTPUT); // Indica que Ć© saĆda
pinMode(A1, OUTPUT); // Indica que Ć© saĆda
pinMode(A2, OUTPUT); // Indica que Ć© saĆda
pinMode(A3, OUTPUT); // Indica que Ć© saĆda
pinMode(A4, OUTPUT); // Indica que Ć© saĆda
pinMode(A5, OUTPUT); // Indica que Ć© saĆda
pinMode(DAC, OUTPUT); // Indica que Ć© saĆda
pinMode(WKP, OUTPUT); // Indica que Ć© saĆda
digitalWrite(D0, LOW); // Desliga SaĆda
digitalWrite(D2, LOW); // Desliga SaĆda
digitalWrite(D3, LOW); // Desliga SaĆda
digitalWrite(D4, LOW); // Desliga SaĆda
digitalWrite(D5, LOW); // Desliga SaĆda
digitalWrite(D6, LOW); // Desliga SaĆda
digitalWrite(A1, LOW); // Desliga SaĆda
digitalWrite(A2, LOW); // Desliga SaĆda
digitalWrite(A3, LOW); // Desliga SaĆda
digitalWrite(A4, LOW); // Desliga SaĆda
digitalWrite(A5, LOW); // Desliga SaĆda
digitalWrite(DAC, LOW); // Desliga SaĆda
digitalWrite(WKP, LOW); // Desliga SaĆda
digitalWrite(LStatus, HIGH); // Liga SaĆda
waitUntil(WiFi.ready); // Agaurda conectar a WiFi
digitalWrite(LStatus, LOW); // Liga SaĆda
Blynk.begin(auth); // Faz AutenticaĆ§Ć£o no Servidor Blynk
digitalWrite(LStatus, LOW); // Desliga SaĆda
waitUntil(Blynk.connect); // Agaurda Conectar no Servidor Blink
timer1.start(); // Inicia Rotina Temporizador
delay(50);
}
// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper()
{
DHT.isrCallback();
}
// Rotinas do Blynk
BLYNK_CONNECTED()
{
bridge1.setAuthToken("XXXXXXXXXXXXXX");
if (PrimeiraBlynk)
{
Blynk.syncAll();
digitalWrite(LStatus, HIGH); // Liga SaĆda
PrimeiraBlynk = false;
}
}
BLYNK_WRITE(V1)
{
if(param.asInt() > 0)
{
result = DHT.acquireAndWait();
bridge1.virtualWrite(V19, DHT.getCelsius());
Blynk.virtualWrite(V1, 0);
}
}
BLYNK_WRITE(V2)
{
if(param.asInt() > 0)
{
h1 = (map(analogRead(UmidadePin), 4095, 1900, 0, 10000));
h2 = h1;
h2 = h2 / 100;
bridge1.virtualWrite(V20, h2);
Blynk.virtualWrite(V2, 0);
}
}
BLYNK_READ(V3)
{
result = DHT.acquireAndWait();
Blynk.virtualWrite(V3, DHT.getCelsius());
}
BLYNK_READ(V4)
{
h1 = (map(analogRead(UmidadePin), 4095, 1900, 0, 10000));
h2 = h1;
h2 = h2 / 100;
Blynk.virtualWrite(V4, h2);
}
BLYNK_READ(V5)
{
Blynk.virtualWrite(V5, analogRead(UmidadePin));
}
BLYNK_WRITE(V6)
{
if(param.asInt() != 0)
{
Particle.connect();
}
}
BLYNK_WRITE(V7)
{
if(param.asInt() != 0)
{
Particle.disconnect();
digitalWrite(LStatus, LOW); // Desliga SaĆda
//System.reset();
Blynk.disconnect();
WiFi.disconnect();
}
}
// Temporizadores de algumas funƧƵes
void Atualiza()
{
Particle.publish("Edicula -> Ciclos/ms:", String (System.ticksPerMicrosecond()));
}
void loop()
{
// if(Particle.connected)Particle.publish("Ciclos/ms:", String (System.ticksPerMicrosecond()), 30);
Blynk.run();
wd.checkin();
if(!WiFi.ready())
{
digitalWrite(LStatus, LOW); // Desliga SaĆda
delay(250);
WiFi.connect();
delay(500);
}
if(WiFi.ready() && !Blynk.connected())
{
PrimeiraBlynk = true;
digitalWrite(LStatus, LOW); // Desliga SaĆda
delay(250);
Blynk.connect();
delay(1000);
}
}
Third Hardware if ESP8266 and Blynk (This hardware is very stable, more than three months running, and never had any problem):
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "DHT.h"
const char* ssid = "WIFI";
const char* password = "111111111111";
int Wifi = LOW;
double h = 0.0;
double t = 0.0;
bool Primeira = true;
char auth[] = "ZZZZZZZZZZZ";
WidgetBridge bridge1(V1);
#define DHTPIN D4 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Conectado Ć ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado.");
Serial.println("EndereƧo IP: ");
Serial.println(WiFi.localIP());
WiFi.printDiag(Serial);
Blynk.config(auth);
delay(250);
pinMode(D0, OUTPUT);
pinMode(D5, OUTPUT);
pinMode(D6, OUTPUT);
pinMode(D7, INPUT);
digitalWrite(D0, LOW);
digitalWrite(D5, LOW);
digitalWrite(D6, LOW);
// Inicializa o DHT22
dht.begin();
}
BLYNK_CONNECTED()
{
bridge1.setAuthToken("XXXXX");
if(Primeira && WiFi.status() == WL_CONNECTED)
{
Blynk.syncAll();
Primeira = false;
}
}
BLYNK_READ(V6)
{
h = dht.readHumidity();
Blynk.virtualWrite(V6, h);
}
BLYNK_READ(V7)
{
t = dht.readTemperature();
Blynk.virtualWrite(V7, t);
}
BLYNK_WRITE(V8)
{
if(param.asInt() > 0)
{
t = dht.readTemperature();
bridge1.virtualWrite(V12, t);
Blynk.virtualWrite(V8, 0);
}
}
BLYNK_WRITE(V9)
{
if(param.asInt() > 0)
{
h = dht.readHumidity();
bridge1.virtualWrite(V13, h);
Blynk.virtualWrite(V9, 0);
}
}
BLYNK_WRITE(V10)
{
if(param.asInt() > 0)
{
digitalWrite(D5, HIGH);
delay(500);
digitalWrite(D5, LOW);
Blynk.virtualWrite(V10, 0);
}
}
void loop()
{
while (WiFi.status() != WL_CONNECTED)
{
Wifi = HIGH;
digitalWrite(D0, LOW);
WiFi.begin(ssid, password);
delay(2500);
Serial.print(".");
digitalWrite(D0, HIGH);
delay(1000);
}
if(WiFi.status() == WL_CONNECTED && Wifi == HIGH)
{
Primeira = false;
digitalWrite(D0, LOW);
delay(500);
digitalWrite(D0, HIGH);
delay(500);
digitalWrite(D0, LOW);
delay(500);
digitalWrite(D0, HIGH);
delay(500);
digitalWrite(D0, LOW);
delay(500);
Serial.println("");
Serial.print("Connectando a ");
Serial.println(ssid);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
delay(250);
digitalWrite(D0, HIGH);
Wifi = LOW;
}
if(WiFi.status() == WL_CONNECTED && !Blynk.connected())
{
Primeira = true;
digitalWrite(D0, LOW); // Desliga SaĆda
delay(250);
Blynk.connect();
}
Blynk.run();
}
That is, if someone can give a help as best stability between Blynk and Particle I thank you very much.
Thank you, Hugo.