Okay, here’s the code that I’ve been experimenting with recently, modified to use your sensors and virtual pins.
The code expects a third display widget attached to pin V3, which will show how long (in seconds) your ESP device was awake for.
Previously, the shortest wake time I was getting was around the the 5 second mark, when using Blynk.begin and an IP address assigned by DHCP.
With this code I’m consistently getting wake times around the 1.2 second mark. This is without the two Dallas temperature sensors connected (I don’t have any to try it with), but the code is attempting to take a reading and returning a value of -127 for each nonexistent sensor, so I don’t imagine that the times would be too much longer with sensors connected.
This is using the Blynk cloud server, with ping times averaging around 80ms.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Set up the sensors
#define ONE_WIRE_BUS 5 // This is the ESP8266 pin to which the sensors are connected
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with DS18B20
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature
DeviceAddress tempSensor1 = { 0x28, 0xF4, 0x11, 0xBD, 0x04, 0x00, 0x00, 0x93 }; // 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);
// 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()
IPAddress device_ip (192,168,1,199); // Static IP Address for the device
IPAddress dns (192,168,1,1); // Normally the IP address of your router
IPAddress gateway (192,168,1,1); // The IP address of your router
IPAddress subnet (255,255,255,0); // The subnet used by your network
// 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 temperature1;
float temperature2;
int wifi_connect_count = 0; // Variable to keep track of how manty times we've tried to connect to the Wi-Fi
int wifi_connect_max_retries = 10; // Variable to specify how many attempts we will have at connecting to the Wi-Fi
float sleep_time_minutes = 5; // Variable - how long (in minutes) we sleep for. As this is a float variable then it can be say 0.5 for a 30 second test
void sendSensors()
{
sensors.requestTemperatures();
temperature1 = sensors.getTempC(tempSensor1);
temperature2 = sensors.getTempC(tempSensor2);
Blynk.virtualWrite(V1, temperature1);
Blynk.virtualWrite(V2, temperature2);
Serial.print("Temp1: ");
Serial.println(temperature1);
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); // 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()
{
// Choose the serial baud rate that allows you to see the ESP boot messages correctly...
//Serial.begin(115200);
Serial.begin(74880);
// 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 * 2; // 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(tempSensor1, 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);
Serial.print("Wake Time = ");
Serial.print(wake_time);
Serial.println(" seconds");
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();
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
}
Read the comments about the network settings, as these will need to be changed to meet your own network settings, or commented-out if you want to use DHCP assigned network settings.
This is the sort of serial output I’m getting:
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
vbb28d4a3
~ld
Connecting to Wi-Fi
Wi-Fi connection - attempt number 1 of 10
Wi-Fi CONNECTED
[701]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on ESP8266
[712] Connecting to blynk-cloud.com:8080
[934] Ready (ping: 91ms).
Connected to Blynk
Temp1: -127.00
Temp2: -127.00
Wake Time = 1.17 seconds
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Going to sleep for 5.00 minutes
One other thing to note, I’ve been having some issues with version 2.5.0 of the Arduino core, so these tests are done using version 2.4.2 of the core. If you’re having Wi-Fi connection issues or long wake times then maybe try downgrading your Core version (in Tools/Board/Boards Manager then scroll down to esp8266 by ESP8266 Community).
Pete.