Hello,
i am facing an issue with connecting to blynk or my wifi i am using this code to monitor power current and voltage using an INA219 sensor which transfers data to nodemcu esp8266 via i2c communication, when i run normal example it works but when i run in this particular code it shows “[5001] connecting to 0.0.0.0” and so on is this happening because i am using serial pins (scl and sda i.e. D1 and D2 pins)? any suggestions would be very helpful
Thank You
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_INA219.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
Adafruit_INA219 ina219;
unsigned long previousMillis = 0;
unsigned long interval = 2000;
float shuntvoltage = 0.00;
float busvoltage = 0.00;
float current = 0.00;
float loadvoltage = 0.00;
float energy = 0.00, energyCost, energyPrevious, energyDifference;
float energyPrice = 6.50;
float power = 0.00;
float capacity = 0.00;
void setup() {
Serial.begin(115200);
while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
uint32_t currentFrequency;
Serial.begin(115200);
if (!ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1) {
delay(10);
}
}
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
ina219.setCalibration_16V_400mA();
Serial.println("IoT Energy Meter with INA219 ...");
}
void loop() {
Blynk.run();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
ina219values();
displaydata();
}
}
void ina219values() {
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
power = loadvoltage * current;
energy = energy + power / 3600; //Wh
capacity = capacity + current / 1000;
energyDifference = energy - energyPrevious;
energyPrevious = energy;
energyCost = energyCost + (energyPrice / 1000 * energyDifference);
// Send data to blynk
Blynk.virtualWrite(V7, current);
Blynk.virtualWrite(V5, String("Rs.") + String(energyPrice));
// nothing connected? set all to 0, otherwise they float around 0.
if (loadvoltage < 1) loadvoltage = 0;
if (current < 1) {
current = 0;
power = 0;
energy = 0;
capacity = 0;
energyCost = 0;
}
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);
Serial.println(" mA");
Serial.print("Power: ");
Serial.print(power);
Serial.println(" mW");
Serial.print("Energy: ");
Serial.print(energy);
Serial.println(" Wh");
Serial.print("Capacity: ");
Serial.print(capacity);
Serial.println(" Ah");
Serial.print("Energy Cost: ");
Serial.print("Rs. ");
Serial.println(energyCost);
Serial.println("-------------------------");
}
void displaydata() {
// VOLTAGE
Blynk.virtualWrite(V0, String(loadvoltage, 2) + String(" V"));
// CURRENT
if (current > 1000) {
Blynk.virtualWrite(V1, String((current / 1000), 2) + String(" A"));
} else {
Blynk.virtualWrite(V1, String(current, 2) + String(" mA"));
}
// POWER
if (power > 1000) {
Blynk.virtualWrite(V2, String((power / 1000), 2) + String(" W"));
} else {
Blynk.virtualWrite(V2, String(power, 2) + String(" mW"));
}
//Energy Comsumption
if (energy > 1000) {
Blynk.virtualWrite(V3, String((energy / 1000), 2) + String(" kWh"));
} else {
Blynk.virtualWrite(V3, String(energy, 2) + String(" Wh"));
}
// CAPACITY
if (capacity > 1000) {
Blynk.virtualWrite(V4, String((capacity / 1000), 2) + String(" Ah"));
} else {
Blynk.virtualWrite(V4, String((capacity), 2) + String(" mAh"));
}
// ENERGY COST
Blynk.virtualWrite(V6, String("Rs.") + String(energyCost, 5));
}
i have made the suggested changes as shown in code below and my blynk device shows online but now my nodemcu esp8266 does not detect the INA219 sensor i am using pins D1 and D2 of the esp8266 is there a direct correlation or is my board defective since it cannot run both simultaneously
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_INA219.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "l";
char pass[] = "";
BlynkTimer timer;
unsigned long previousMillis = 0;
const unsigned long interval = 1000;
Adafruit_INA219 ina219;
float shuntvoltage = 0.00;
float busvoltage = 0.00;
float current = 0.00;
float loadvoltage = 0.00;
float energy = 0.00, energyCost, energyPrevious, energyDifference;
float energyPrice = 6.50;
float power = 0.00;
float capacity = 0.00;
void setup() {
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.begin(115200);
uint32_t currentFrequency;
if (!ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1) {
delay(10);
}
}
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
ina219.setCalibration_16V_400mA();
Serial.println("IoT Energy Meter with INA219 ...");
timer.setInterval(interval, ina219values);
timer.setInterval(interval, displaydata);
}
void loop() {
Blynk.run();
timer.run();
}
void ina219values() {
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current = ina219.getCurrent_mA();
loadvoltage = busvoltage + (shuntvoltage / 1000);
power = loadvoltage * current;
energy = energy + power / 3600; //Wh
capacity = capacity + current / 1000;
energyDifference = energy - energyPrevious;
energyPrevious = energy;
energyCost = energyCost + (energyPrice / 1000 * energyDifference);
// Send data to blynk
Blynk.virtualWrite(V7, current);
Blynk.virtualWrite(V5, String("Rs.") + String(energyPrice));
// nothing connected? set all to 0, otherwise they float around 0.
if (loadvoltage < 1) loadvoltage = 0;
if (current < 1) {
current = 0;
power = 0;
energy = 0;
capacity = 0;
energyCost = 0;
}
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);
Serial.println(" mA");
Serial.print("Power: ");
Serial.print(power);
Serial.println(" mW");
Serial.print("Energy: ");
Serial.print(energy);
Serial.println(" Wh");
Serial.print("Capacity: ");
Serial.print(capacity);
Serial.println(" Ah");
Serial.print("Energy Cost: ");
Serial.print("Rs. ");
Serial.println(energyCost);
Serial.println("-------------------------");
}
void displaydata() {
// VOLTAGE
Blynk.virtualWrite(V0, String(loadvoltage, 2) + String(" V"));
// CURRENT
if (current > 1000) {
Blynk.virtualWrite(V1, String((current / 1000), 2) + String(" A"));
} else {
Blynk.virtualWrite(V1, String(current, 2) + String(" mA"));
}
// POWER
if (power > 1000) {
Blynk.virtualWrite(V2, String((power / 1000), 2) + String(" W"));
} else {
Blynk.virtualWrite(V2, String(power, 2) + String(" mW"));
}
//Energy Comsumption
if (energy > 1000) {
Blynk.virtualWrite(V3, String((energy / 1000), 2) + String(" kWh"));
} else {
Blynk.virtualWrite(V3, String(energy, 2) + String(" Wh"));
}
// CAPACITY
if (capacity > 1000) {
Blynk.virtualWrite(V4, String((capacity / 1000), 2) + String(" Ah"));
} else {
Blynk.virtualWrite(V4, String((capacity), 2) + String(" mAh"));
}
// ENERGY COST
Blynk.virtualWrite(V6, String("Rs.") + String(energyCost, 5));
}
Included the library and yes they are correctly connected i checked multiple times when the sensor works the wifi does not connect now the wifi and blynk device is showing online but the INA is not working they both are not working simultaneously for some reason
Okay, the onboard 5v to 3.3v regulator in the NodeMCU may not be able to provide enough power to run the ESP8266 chip and the INA219 at the same time (the ESP8266 draws more power when its using WiFi than when it isn’t).
The INA219 can accept a Vin voltage of 3-5v, so try powering the INA219 from the 5v pin on the NodeMCU isnetad.