#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Set up the sensors
#define ONE_WIRE_BUS 2 // GPIO куда подключен DS18B20
#define PIN_POWER_DS 0 // GPIO куда подключено питание DS18B20
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with DS18B20
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature
DeviceAddress tempSensor; // Hard-coded address of Temperature Probe #1
//DeviceAddress tempSensor2 = { 0x28, 0x4D, 0xA3, 0xBC, 0x04, 0x00, 0x00, 0x41 }; // Hard-coded address of Temperature Probe #2
// Pass our oneWire reference to Dallas Temperature.
//DallasTemperature DS18B20(&oneWire);
int VCC=0.0; //Напряжение батареи
ADC_MODE (ADC_VCC) ; // Будем измерять напряжение на VCC внутри МК
float Vbat,V_min = 3.05; // напряжение батарейки, и минимальный порог напряжения для разрешения работы
// Set up the WiFi credentials
const char *ssid = "***";
const char *pass = "***";
// Set up the network details - Only needed if you want to have a static IP address. If not used then delete the 'WiFi.config(device_ip, dns, gateway, subnet);' line from void WiFi_Connect()
#define DHCP true
IPAddress device_ip (192,168,8,90); // Static IP Address for the device
IPAddress dns (192,168,8,1); // Normally the IP address of your router
IPAddress gateway (192,168,8,1); // The IP address of your router
IPAddress subnet (255,255,255,0); // The subnet used by your network
#define chanal 1 //канал wifi
byte macAP[6] = {0x94, 0x37, 0xF7, 0x03, 0x48, 0x7E}; //mac роутера 30:74:96:CD:FB:D7 - Telenor
// 0x94, 0x37, 0xF7, 0x03, 0x48, 0x7E - Huawei
// Set up the Blynk parameters
const char auth[] = "***"; // Your Blynk auth code
const char blynk_server [] = "blynk-cloud.com"; // new variable to hold the name of the Blynk server
const int blynk_port = 8080; // new variable to hold the port used by the Blynk server
// Declare variables
float temperature;
//float temperature2;
int wifi_connect_count = 0; // Переменная, чтобы отслеживать, сколько раз мы пытались подключиться к Wi-Fi.
int wifi_connect_max_retries = 20; // Попытки соеденения с WiFi
float sleep_time_minutes = 15; // Время сна
void sendSensors()
{
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
//temperature2 = sensors.getTempC(tempSensor2);
Blynk.virtualWrite(V4, temperature);
//Blynk.virtualWrite(V2, temperature2);
Serial.print("Temp: ");
Serial.println(temperature);
// Serial.print("Temp2: ");
// Serial.println(temperature2);
}
void WiFi_Connect() // Handle the connection to the Wi-Fi network
{
Serial.println(F("Connecting to Wi-Fi"));
WiFi.config(device_ip, dns, gateway, subnet); // Not needed if you just want to have a DHCP assigned IP address. If you don't use this then delete the device_ip, dns, gateway & subnet declarations
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass, chanal, macAP, true); // connect to the network
}
while (WiFi.status() != WL_CONNECTED && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attempts allowed
{
delay(500);
wifi_connect_count++;
Serial.print(F("Wi-Fi connection - attempt number "));
Serial.print(wifi_connect_count);
Serial.print(" of ");
Serial.println(wifi_connect_max_retries);
}
if (WiFi.status() == WL_CONNECTED)
{
WiFi.mode(WIFI_STA);
Serial.println(F("Wi-Fi CONNECTED"));
Serial.println();
}
} // End of void WiFi_Connect
void setup()
{
Serial.begin(115200);
Vbat = ESP.getVcc(); // читаем напряжение на ноге VCC модуля ESP8266
Vbat = Vbat / 1023;
if (Vbat < V_min ) // (засыпаю на...")
ESP.deepSleep(86400e6); //(86400e6) сутки, (3600*1000000)-60мин.
pinMode (PIN_POWER_DS, OUTPUT);
digitalWrite(PIN_POWER_DS, HIGH);
// 1) Attempt to connect to Wi-Fi a few times (how many times we try is specified by the 'wifi_connect_max_retries' variable)
// 2) If we successfully connected to Wi-Fi then attempt to connect to Blynk in a non-blocking way. If we aren't connected to Wi-Fi then go to sleep
// 3) If we connected to Blynk then run the rest of the code as normal. If we aren't connected to Blynk then go to sleep
Blynk.begin(auth, ssid, pass);//starts wifi and Blynk - Not used in the new code as it's a blocking function
WiFi_Connect(); // Attempt to connect to Wi-Fi
if (WiFi.status() == WL_CONNECTED) // If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
{
Blynk.config(auth, blynk_server, blynk_port); // Initialise the Blynk connection settings
Blynk.connect(); // Attempt to connect to Blynk
}
else
{
Serial.println ("Wi-Fi connection failed - going to sleep");
sleep_time_minutes = sleep_time_minutes * 1; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
//Deep_Sleep_Now();
}
if (Blynk.connected()) // If we managed to connect to Blynk then carry-on as normal, else go to sleep
{
Serial.println ("Connected to Blynk");
// If you wanted to retrieve data stored against a virtual pin from the Blynk server the you'd call Blynk.syncVirtual(VPin) here. This would trigger the corresponding BLYNK_WRIRE(VPin) callback
}
else
{
// If you enable the following line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal, you can adjust to suit your needs
//sleep_time_minutes = sleep_time_minutes * 2;
Serial.println("Blynk connection failed - going to sleep");
Deep_Sleep_Now();
}
sensors.begin();
sensors.setResolution(tempSensor, 10);
//sensors.setResolution(tempSensor2, 10);
Blynk.run(); // Give Blynk some processor time
sendSensors(); // Take the temperature readings and upload to Blynk
float wake_time = (float)millis()/float(1000); // Find out how long since the ESP rebooted
Blynk.virtualWrite(V3, wake_time);
Blynk.virtualWrite(V5, Vbat); // виртуальный порт напряжения
Serial.print("Wake Time = ");
Serial.print(wake_time);
Serial.println(" seconds");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
delay(100); // Give Blynk time to do its thing before going to sleep
Deep_Sleep_Now();
}
void Deep_Sleep_Now()
{
Serial.print(">>>>>>> Going to sleep for ");
Serial.print(sleep_time_minutes);
Serial.println(" minutes");
Serial.println();
digitalWrite(PIN_POWER_DS, LOW);
ESP.deepSleep(sleep_time_minutes * 60000000); // Deep Sleep time is specified in micros
delay(2000);
}
void loop()
{
// voud loop is empty, but can't be deleted
}