BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

HELP! trying to upload senor values

Before creating the topic

wrote a code to monitor solar and battery power using voltage & current sensors. the power levels would automatically control 3 relays basen on set thresholds. I want to monitor the solar and battery power on blynk but I dont know how to go about it.

define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerialh>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 38400

ESP8266 wifi(&EspSerial);

int value = 0;
int adcValue = 0;
int sensitivity = 66;
int offsetVoltage = 2500;
double adcVoltage = 0;
double currentValue = 0;
double power_solar = 0;
double power_battery = 0;
int analogV = A4;
int analogI = A3;
int analogV_1 = A5;
int analogI_1 = A2;
float iout = 0.0;
float iin = 0.0;
float vout = 0.0;
float vin = 0.0;
float R1 = 10000.0; // resistance of R1 (100K) -see text!
float R2 = 3300.0; // resistance of R2 (10K) - see text!
float R3 = 10000.0; // resistance of R1 (100K) -see text!
float R4 = 4700.0; // resistance of R2 (10K) - see text!

int relay_pump1 = 3; // relay 1 connected to Arduino pin 3 and working for pumps 1 and 4 connected in series.
int relay_pump2 = 5; // relay 2 connected to Arduino pin 5
int relay_pump3 = 6; // relay 3 connected to Arduino pin 6

const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;


void setup() {

// Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  


  
  Serial.begin(9600); // Starts the serial communication  
  pinMode(analogV, INPUT );
  pinMode(analogI, INPUT );
  pinMode(analogV_1, INPUT );
  pinMode(analogI_1, INPUT );



  // relay/pump setup (declaring them as output devices) 
  pinMode(relay_pump1, OUTPUT); 
  pinMode(relay_pump2, OUTPUT); 
  pinMode(relay_pump3, OUTPUT); 
  

  
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
}

void sensor(){
  
//This sedtions deals of voltage sensor of solar
   value = analogRead(A0);
   vout = (value * 4.96) / 1023.0; // see text
   vin = vout / (R2/(R1+R2)); // Applying voltage divider rule
   
   // This section deals with the current sensor of solar
   adcValue=analogRead(A1);
   adcVoltage = (adcValue / 1023.0) * 5000;// read the value at analog input
   currentValue = ((adcVoltage - offsetVoltage) / sensitivity); 
   power_solar = ( vin * currentValue); // For the power coming from the solar panel
   if (vin<0.09) { vin=0.0;  } //statement to quash undesired reading !

  // This section deals with battery voltage sensor
   value = analogRead(A2);
   vout = (value * 4.96) / 1023.0; // see text
   vin = vout / (R3/(R3+R4)); // Applying voltage divider rule
   
 //This section deals with battery current voltage 
   adcValue=analogRead(A3);
   adcVoltage = (adcValue / 1023.0) * 5000;// read the value at analog input
   currentValue = ((adcVoltage - offsetVoltage) / sensitivity); 
   power_battery = ( vin * currentValue); // For the power coming from the solar panel
   if (vin<0.09) { vin=0.0;  }         //statement to quash undesired reading !
    Serial.print("Output V = ");
    Serial.println(vin);
    
    Serial.print("Current A = ");
    Serial.println(currentValue);
    
    
    Serial.print("solar power W = ");
    Serial.println(power_solar);
    
    Serial.print("Output V = ");
    Serial.println(vin);
    
    Serial.print("Current A = ");
    Serial.println(currentValue);   
    Serial.print("Battery power W = ");
    Serial.println(power_battery);
    Serial.print(" ");
    Serial.println();
    Blynk.virtualWrite(V4, power_solar); 
    Blynk.virtualWrite(V5, power_battery);
    // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
// Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
// Calculating the distance
  distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
   Serial.println(distance);
   Blynk.virtualWrite(V6, distance);
  delay(5000);

  if(power_solar >= 70 && power_battery >= 14.0 && distance <= 5 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, HIGH);
  digitalWrite (5, HIGH);
  }
   
  else if(power_solar >= 60 && power_solar < 70 && power_battery >= 5.8 && power_battery < 14 && distance <= 75 && distance > 5 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, HIGH);
  digitalWrite (5, LOW);
  }

  else if(power_solar >= 32 && power_solar < 60  && power_battery >= 1.4 && power_battery < 5 && distance <= 150 && distance > 75 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, LOW);
  digitalWrite (5, LOW);
}
  else{
    digitalWrite (3, LOW);
    digitalWrite (5, LOW);
    digitalWrite (5, LOW);
  }
  
}

void loop() {
Blynk.run();
}

@MIKEVICE please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

@PeteKnight Done

Your void sensor() function never executes, as it’s not being called.
You need to declare a BlynkTimer object, add a timer to your void setup() to call void sensor() on a regular basis (maybe every 10,000 milliseconds), and add timer.run(); to your void loop()

Pete.

@PeteKnight Noted. please can you help confirm if I did the right thing?

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "YourAuthToken";.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

// or Software Serial on Uno, Nano...
//#include <SoftwareSerialh>
//SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 38400

ESP8266 wifi(&EspSerial);
BlynkTimer timer;

int value = 0;
int adcValue = 0;
int sensitivity = 66;
int offsetVoltage = 2500;
double adcVoltage = 0;
double currentValue = 0;
double power_solar = 0;
double power_battery = 0;
int analogV = A4;
int analogI = A3;
int analogV_1 = A5;
int analogI_1 = A2;
float iout = 0.0;
float iin = 0.0;
float vout = 0.0;
float vin = 0.0;
float R1 = 10000.0; // resistance of R1 (100K) -see text!
float R2 = 3300.0; // resistance of R2 (10K) - see text!
float R3 = 10000.0; // resistance of R1 (100K) -see text!
float R4 = 4700.0; // resistance of R2 (10K) - see text!

int relay_pump1 = 3; // relay 1 connected to Arduino pin 3 and working for pumps 1 and 4 connected in series.
int relay_pump2 = 5; // relay 2 connected to Arduino pin 5
int relay_pump3 = 6; // relay 3 connected to Arduino pin 6

const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;


void setup() {

// Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:  
  Serial.begin(9600); // Starts the serial communication  
  pinMode(analogV, INPUT );
  pinMode(analogI, INPUT );
  pinMode(analogV_1, INPUT );
  pinMode(analogI_1, INPUT );
  // relay/pump setup (declaring them as output devices) 
  pinMode(relay_pump1, OUTPUT); 
  pinMode(relay_pump2, OUTPUT); 
  pinMode(relay_pump3, OUTPUT); 
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  timer.setInterval(1000L, sensor);
  
}

void sensor(){
  
//This sedtions deals of voltage sensor of solar
   value = analogRead(A0);
   vout = (value * 4.96) / 1023.0; // see text
   vin = vout / (R2/(R1+R2)); // Applying voltage divider rule
   
   // This section deals with the current sensor of solar
   adcValue=analogRead(A1);
   adcVoltage = (adcValue / 1023.0) * 5000;// read the value at analog input
   currentValue = ((adcVoltage - offsetVoltage) / sensitivity); 
   power_solar = ( vin * currentValue); // For the power coming from the solar panel
   if (vin<0.09) { vin=0.0;  } //statement to quash undesired reading !

  // This section deals with battery voltage sensor
   value = analogRead(A2);
   vout = (value * 4.96) / 1023.0; // see text
   vin = vout / (R3/(R3+R4)); // Applying voltage divider rule
   
 //This section deals with battery current voltage 
   adcValue=analogRead(A3);
   adcVoltage = (adcValue / 1023.0) * 5000;// read the value at analog input
   currentValue = ((adcVoltage - offsetVoltage) / sensitivity); 
   power_battery = ( vin * currentValue); // For the power coming from the solar panel
   if (vin<0.09) { vin=0.0;  }         //statement to quash undesired reading !
    Serial.print("Output V = ");
    Serial.println(vin);
    
    Serial.print("Current A = ");
    Serial.println(currentValue);
    
    
    Serial.print("solar power W = ");
    Serial.println(power_solar);
    
    Serial.print("Output V = ");
    Serial.println(vin);
    
    Serial.print("Current A = ");
    Serial.println(currentValue);   
    Serial.print("Battery power W = ");
    Serial.println(power_battery);
    Serial.print(" ");
    Serial.println();
    Blynk.virtualWrite(V4, power_solar); 
    Blynk.virtualWrite(V5, power_battery);
    // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
// Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
// Calculating the distance
  distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
   Serial.println(distance);
   Blynk.virtualWrite(V6, distance);
  delay(5000);

  if(power_solar >= 70 && power_battery >= 14.0 && distance <= 5 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, HIGH);
  digitalWrite (5, HIGH);
  }
   
  else if(power_solar >= 60 && power_solar < 70 && power_battery >= 5.8 && power_battery < 14 && distance <= 75 && distance > 5 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, HIGH);
  digitalWrite (5, LOW);
  }

  else if(power_solar >= 32 && power_solar < 60  && power_battery >= 1.4 && power_battery < 5 && distance <= 150 && distance > 75 ) {
  digitalWrite (3, HIGH);
  digitalWrite (5, LOW);
  digitalWrite (5, LOW);
}
  else{
    digitalWrite (3, LOW);
    digitalWrite (5, LOW);
    digitalWrite (5, LOW);
  }
  
}

void loop() {
Blynk.run();
timer.run();
}

I’d increase that number to 10000 for testing, as I don’t know how long it takes for your sensor function to execute. Once it’s working then make it run more often if you wish.

Pete.

Alright, I’ll do that. thanks a lot for the help, I’ll update you on the progress

1 Like