hi, for your knowledge I would like to paste the code so it can help someone.
My project control energy created by my solar panel and control my garden illumination. My hardware is divided:
Solar panel > INA219 current sensor > NodeMCU > 2 Relay > Illumination.
The whole is controlled by Blynk Application that can I update with OTA system.
Before paste my code I would like to have confirmation from you…in my code I use Blynk.config and not Blynk.begin because, I read somewhere that when Blynk goes offline and if set Blynk.begin, NodeMCU stop running the code unlike Blynk.config, right?
So in this case, if Blynk goes offline NodeMCU goes on to read power from solar panel right?in my code read power of WIFI trasmission (RSSI), that was important to work OTA. Thank you
#include <Blynk.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = "********";
Adafruit_INA219 ina219;
char ssid[] = "++++";
char pass[] = "+++";
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
float energy = 0;
float current_h = 0;
float current_day = 0;
long rssi = 0;
#define relayFaretto10WD4 D4
#define relayMadonnaD5 D5
#define greenLEDD7 D7
#define redLEDD8 D8
#define buttonFaretto10W V0 // >> su D4
#define buttonMadonna V1 // >> su D5
#define timerFaretto V2
#define timerMadonna V3
#define correnteIstantanea V4
#define potenzaIstantanea V5
#define energiaTotale V6
#define correnteTotale V7
#define correnteTotaleDay V8
#define correnteTotaleDayGraph V9
#define virtualRSSI V10
boolean isFirstConnect = true;
boolean isConnesso;
boolean canSendCurrentDay = false;
BlynkTimer timer;
void setup(void) {
Serial.begin(9600);
ina219.begin();
Blynk.config(auth);
Blynk.connectWiFi(ssid, pass);
pinMode(greenLEDD7, OUTPUT);
pinMode(redLEDD8, OUTPUT);
pinMode(relayFaretto10WD4, OUTPUT);
pinMode(relayMadonnaD5, OUTPUT);
digitalWrite(relayFaretto10WD4, LOW);
digitalWrite(relayMadonnaD5, LOW);
ArduinoOTA.begin(); // For OTA
// Setup a function to be called
timer.setInterval(1000L, letturaPannello);
timer.setInterval(1500L, sendInaValues);
timer.setInterval(3000L, controllaConnessione);
timer.setInterval(3000L, sendRSSI);
}
void loop(void){
timer.run(); // Initiates BlynkTimer
ArduinoOTA.handle();
Blynk.run();
}
void letturaPannello() {
if(!isFirstConnect){
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = (ina219.getCurrent_mA()/1000);
if(current_mA<0){
current_mA = current_mA*(-1);
}
power_mW = (ina219.getPower_mW()/1000);
if(power_mW<0){
power_mW = power_mW*(-1);
}
loadvoltage = busvoltage + (shuntvoltage / 1000);
current_h = (current_h + current_mA / 3600); // conteggio corrente totale
current_day = (current_day + current_mA / 3600); // conteggio corrente giornaliero
energy = (energy + loadvoltage * current_mA / 3600); // conteggio corrente energia
}
}
void controllaConnessione(){
if(Blynk.connected()){
digitalWrite(greenLEDD7, HIGH);
digitalWrite(redLEDD8, LOW);
isConnesso = true;
rssi= WiFi.RSSI();
}else{
digitalWrite(greenLEDD7, LOW);
digitalWrite(redLEDD8, HIGH);
isConnesso = false;
Blynk.connect();
//Blynk.begin(auth, ssid, pass);
}
}
void sendInaValues(){
Blynk.virtualWrite(correnteIstantanea, current_mA); // V4
Blynk.virtualWrite(potenzaIstantanea, power_mW); // V5
Blynk.virtualWrite(energiaTotale, energy); // V6
Blynk.virtualWrite(correnteTotale, current_h); // V7
Blynk.virtualWrite(correnteTotaleDay, current_day); // V8
if(canSendCurrentDay){ // ALLO START DEL TIMER FARETTO
Blynk.virtualWrite(correnteTotaleDayGraph, current_day); // MANDO IL VALORE TOTALE DELLA CORRENTE PRODOTTA
current_day = 0;
canSendCurrentDay = false;
}
}
void sendRSSI(){
Blynk.virtualWrite(virtualRSSI, rssi); // V10 RSSI in dB
}
BLYNK_WRITE(buttonFaretto10W){ //controllo VIRTUAL Button Faretto V0 >> D4
if(param.asInt()==1){
digitalWrite(relayFaretto10WD4, HIGH); // START RELAY FARETTO
}else{
digitalWrite(relayFaretto10WD4, LOW); // STOP RELAY FARETTO
}
}
BLYNK_WRITE(buttonMadonna){ //controllo VIRTUAL Button Madonna V1 >> D5
if(param.asInt()==1){
digitalWrite(relayMadonnaD5, HIGH); // START RELAY MADONNA
}else{
digitalWrite(relayMadonnaD5, LOW); // STOP RELAY MADONNA
}
}
BLYNK_WRITE(timerFaretto){ // timer V2 >>> buttonFaretto V0
if (param.asInt() == 1) {
Blynk.virtualWrite(buttonFaretto10W, 1); // button set to ON from timer
digitalWrite(relayFaretto10WD4, HIGH);
canSendCurrentDay = true;
}
else{
Blynk.virtualWrite(buttonFaretto10W, 0); // button set to OFF from timer
digitalWrite(relayFaretto10WD4, LOW);
}
}
BLYNK_WRITE(timerMadonna){ // timer V3 >>> buttonFaretto V1
if (param.asInt() == 1) {
Blynk.virtualWrite(buttonMadonna, 1); // button set to ON from timer
digitalWrite(relayMadonnaD5, HIGH);
}
else{
Blynk.virtualWrite(buttonMadonna, 0); // button set to OFF from timer
digitalWrite(relayMadonnaD5, LOW);
}
}
////////////////// S Y N C /////////////////////////
BLYNK_CONNECTED() {
if (isFirstConnect) {
Blynk.syncVirtual(energiaTotale); // V6
Blynk.syncVirtual(correnteTotale); // V7
Blynk.syncVirtual(correnteTotaleDay); // V8
isFirstConnect = false;
}
isConnesso = true;
}
BLYNK_WRITE(energiaTotale){
energy = param[0].asFloat();
}
BLYNK_WRITE(correnteTotale){
current_h = param[0].asFloat();
}
BLYNK_WRITE(correnteTotaleDay){
current_day = param[0].asFloat();
}
void stampaValori(){
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
Serial.print("Energia: "); Serial.print(energy); Serial.println(" mWh");
Serial.println("");
}