hi,
im trying to make a simle sketch where im putting the esp board in sleep mode after sending some data from a senzor.
It seems to get stuck in the while loop and i don’t know how to fix it.
here’s the code:
#define BLYNK_TEMPLATE_ID "TMPLlrSk5bl7"
#define BLYNK_DEVICE_NAME "SICD"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define USE_NODE_MCU_BOARD
#include "BlynkEdgent.h"
#define trigPin 4
#define echoPin 5
int Distance()
{
long duration;
int distanceMeasured;
digitalWrite(trigPin, LOW);
delay(1000);
//send a signal for 10 seconds from trigPin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//measuring the distance from the senzor to the object
duration = pulseIn(echoPin, HIGH);
distanceMeasured = duration*0.034/2;
return distanceMeasured;
}
void setup()
{
delay(500);
pinMode(13, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(500);
Serial.begin(9600);
delay(100);
BlynkEdgent.begin();
BlynkEdgent.run();
while(Blynk.connected() != true){
Serial.println("x");
}
Blynk.virtualWrite(V0, Distance());
//ESP.deepSleep(10* 1000000);
}
void loop() {
}
I’m want to make a senzor that detects how full a container is, but right know I just want to send the data from the senzor(HC-SR04) over to blynk. I need to put it on deep sleep because it’ll be powered by batteries.
After esp.deepsleep() is called and the timer reaches the end it should restart from the beginning of the setup function. So I don’t think I need anything in the loop() function from what I understand
It’s written for Blynk Legacy, so the blynk_server would need to be changed to blynk.cloud and it would be a good idea to put the Template ID and Device Name at the very beginning of of the sketch.
#define BLYNK_TEMPLATE_ID "TMPLlrSk5bl7"
#define BLYNK_DEVICE_NAME "SICD"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define USE_NODE_MCU_BOARD
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 10
//#include <WiFi.h>
//#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#define trigPin 4
#define echoPin 5
int Distance()
{
long duration;
int distanceMeasured;
digitalWrite(trigPin, LOW);
delay(1000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceMeasured = duration*0.034/2;
return distanceMeasured;
}
//RTC_DATA_ATTR int bootCount = 0;
const char auth[] = "***";
const char blynk_server [] = "blynk.cloud"; // new variable to hold the name of the Blynk server
const int blynk_port = 443; // new variable to hold the port used by the Blynk server
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****";
char pass[] = "***";
//int wifi_connect_count = 0; // New variable to keep track of how manty times we've tried to connect to the Wi-Fi
//int wifi_connect_max_retries = 50; // New variable to specify how many attempts we will have at connecting to the Wi-Fi
//
//float sleep_time_minutes = 1; // New variable - how long (in minutes) we sleep for. As this is a float variable then it can be 0.5 for a 30 second test
void WiFi_Connect() // New functon to handle the connectuon 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 diont 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){
//wait to connect
Serial.println("connnecting to wifi");
}
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(9600);
pinMode(13, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// New section of code - stop using Blynk.begin, which is a blocking function, and instead do the following:
//
// 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 manages to connect to Blynk then carry-on as normal, else go to sleep
{
Serial.println ("Connected to Blynk");
}
else
{
//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
Serial.println("Blynk connection failed - going to sleep");
Deep_Sleep_Now();
}
delay(2000);
}
void loop()
{
delay(2000);
Blynk.virtualWrite(V0, Distance());
Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
delay(100);
Deep_Sleep_Now();
}
void Deep_Sleep_Now() // New function - moded code out of void loop so that the sleep function can be called if we fail to connect to Wi-Fi or Blynk
{
// esp_sleep_enable_timer_wakeup((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
// esp_deep_sleep_start();
ESP.deepSleep((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
delay(2000);
}