/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials:
Home
* Sketch generator:
Blynk Example Browser
* Blynk community:
Blynk
* Social networks:
Blynk
*
blynk (@blynk_app) | Твиттер
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example runs directly on ESP8266 chip.
*
* Note: This requires ESP8266 support package:
*
GitHub - esp8266/Arduino: ESP8266 core for Arduino
*
* Please be sure to select the right ESP8266 module
* in the Tools -> Board menu!
*
* Change WiFi ssid, pass, and Blynk auth token to run
*
**************************************************************/
// CheckServer.ino by Costas 17/7/2016
#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(6000L, 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, millis() / 6000);
}
}
BLYNK_WRITE(V3)
{
uint32_t currentFrequency;
ina219.begin();
timer.setInterval(6000L, sendVoltage);
}
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 / 6000);
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, millis() / 6000);
Blynk.virtualWrite(V4, loadvoltage, millis() / 6000);
}
void loop() {
if (Connected2Blynk) {
Blynk.run();
timer.run();
sendTemps();
sendVoltage();
}
}