#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <TimeLib.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#define ONE_WIRE_BUS D5
#define RELE D9
#define BUTTON D3
Adafruit_INA219 ina219;
SimpleTimer timer;
bool rele;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "---";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "---";
char pass[] = "---";
bool Connected2Blynk = false;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Blynk.config(auth); // in place of Blynk.begin(auth, ssid, pass);
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
while (Blynk.connect() == false) {
// Wait until connected
}
Serial.println("Connected to Blynk server");
timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds
{
sensors.begin();
timer.setInterval(2000L, sendTemps);
pinMode (RELE, OUTPUT);
pinMode (BUTTON, INPUT);
}
}
BLYNK_WRITE(V2)
{
rele = param.asInt();
digitalWrite(RELE, rele);
timer.run();
if (digitalRead(BUTTON))
{
rele = !rele;
digitalWrite(RELE, rele);
Blynk.virtualWrite(V2, rele);
delay (1000);
}
}
BLYNK_WRITE(V3)
{
uint32_t currentFrequency;
ina219.begin();
}
void CheckConnection() {
Connected2Blynk = Blynk.connected();
if (!Connected2Blynk) {
Serial.println("Not connected to Blynk server");
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
}
else {
Serial.println("Connected to Blynk server");
}
}
void sendTemps()
{
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Serial.println(temp);
Blynk.virtualWrite(V1, temp);
}
void sendVoltage(){
float shuntvoltage = 0;
float busvoltage = 0;
float loadvoltage = 0;
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
loadvoltage = busvoltage + (shuntvoltage / 1000);
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");
Blynk.virtualWrite(V3, busvoltage);
Blynk.virtualWrite(V4, loadvoltage);
delay(2000);
}
void loop() {
if (Connected2Blynk) {
Blynk.run();
timer.run();
sendTemps();
sendVoltage();
}
}